using System.Text.Json; using System.Text.Json.Nodes; using ChaoXingAutoClass.Core; using XFEExtension.NetCore.XUnit.Assertions; using XFEExtension.NetCore.XUnit.Attributes; namespace ChaoXingAutoClass.Test; [TestFixture] public sealed class MultipleChoiceReviewTests { private static QuestionSnapshot Question => new("q", QuestionKind.Multiple, "哪些选项符合材料?", [new("A", "含义一"), new("B", "含义二"), new("C", "含义三"), new("D", "含义四")], "f", 0); private static JsonObject Reviewed() { return JsonSerializer.SerializeToNode(new { summary = "逐项检查完成", answers = new[] { new { questionId = "q", choices = new[] { "A", "B", "C", "D" }, text = "", confidence = .96, grounded = true, explanation = "材料覆盖四项", evidence = "课程定义及其概念包含关系" } }, reviews = new[] { new { questionId = "q", options = new[] { "A", "B", "C", "D" }.Select(key => new { key, selected = true, confidence = .96, reason = "符合课程定义", evidence = "00:20 的定义与语义推论" }).ToArray() } } })!.AsObject(); } [Test] public void EveryOptionHasAnAuditableDecision() { var json = Reviewed().ToJsonString(); var answers = DeepSeekAgent.ParseAnswers(json); DeepSeekAgent.ValidateOptionReviews(json, [Question], answers.Answers, .85); Assert.Equal(4, Assert.Single(answers.Reviews!).Options.Length); } [TestCase("missing")] [TestCase("duplicate")] [TestCase("low-confidence")] [TestCase("contradiction")] [TestCase("no-evidence")] [TestCase("unknown-key")] public void IncompleteOrUncertainReviewStopsSubmission(string issue) { var root = Reviewed(); var options = root["reviews"]![0]!["options"]!.AsArray(); switch (issue) { case "missing": options.RemoveAt(1); break; case "duplicate": options[1]!["key"] = "A"; break; case "low-confidence": options[1]!["confidence"] = .5; break; case "contradiction": options[1]!["selected"] = false; break; case "no-evidence": options[1]!["evidence"] = ""; break; case "unknown-key": options[1]!["key"] = "X"; break; } var json = root.ToJsonString(); var answers = DeepSeekAgent.ParseAnswers(json); Assert.Throws(() => DeepSeekAgent.ValidateOptionReviews(json, [Question], answers.Answers, .85)); } [Test] public async Task IndependentReviewCanRestoreAnOmittedChoice() { var calls = 0; using var http = new HttpClient(new AgentTests.StubHandler(async request => { calls++; var body = await request.Content!.ReadAsStringAsync(); var answer = Reviewed(); if (calls == 1) answer["answers"]![0]!["choices"] = new JsonArray("A", "C", "D"); else Assert.Contains("独立复核", JsonNode.Parse(body)!["messages"]![1]!["content"]!.ToJsonString(new JsonSerializerOptions { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping })); return AgentTests.Ok(JsonSerializer.Serialize(new { choices = new[] { new { finish_reason = "stop", message = new { content = answer.ToJsonString() } } } })); })); var result = await new DeepSeekAgent(http, new AppSettings { ApiKey = "test", VisionEnabled = false, DeepReviewEnabled = true }).AnswerAsync("概念", "课程定义", [Question], CancellationToken.None); Assert.Equal(2, calls); Assert.Equal(new[] { "A", "B", "C", "D" }, result.Answers[0].Choices); } [TestCase(.96)] [TestCase(.6)] public async Task ReviewAndAdjudicationDoNotSeeEarlierPredictionsOrConfidence(double finalConfidence) { var calls = 0; using var http = new HttpClient(new AgentTests.StubHandler(async request => { calls++; var body = await request.Content!.ReadAsStringAsync(); Assert.False(body.Contains("INITIAL_PREDICTION_MARKER")); Assert.False(body.Contains("REVIEW_PREDICTION_MARKER")); var answer = Reviewed(); if (calls < 3) answer["summary"] = calls == 1 ? "INITIAL_PREDICTION_MARKER" : "REVIEW_PREDICTION_MARKER"; if (calls >= 2) { var confidence = calls == 2 ? .6 : finalConfidence; answer["answers"]![0]!["confidence"] = confidence; foreach (var option in answer["reviews"]![0]!["options"]!.AsArray()) option!["confidence"] = confidence; } return AgentTests.Ok(JsonSerializer.Serialize(new { choices = new[] { new { message = new { content = answer.ToJsonString() } } } })); })); var agent = new DeepSeekAgent(http, new AppSettings { ApiKey = "test", VisionEnabled = false, DeepReviewEnabled = true, StopOnUncertainAnswers = true }, new BlindReferences()); AgentAnswerSet? result = null; Exception? error = null; try { result = await agent.AnswerAsync("概念", "课程定义", [Question], CancellationToken.None); } catch (Exception ex) { error = ex; } Assert.Equal(3, calls); if (finalConfidence >= .85) { Assert.Null(error); Assert.Equal(finalConfidence, result!.Answers[0].Confidence); } else Assert.True(error is AnswerEvidenceException); } private sealed class BlindReferences : ILessonReferenceProvider { public Task> GatherAsync(string chapter, string knowledge, IReadOnlyList questions, CancellationToken token) => Task.FromResult>([new("合成规范", "https://example.test/spec", "四个选项均符合定义。", DateTimeOffset.Now)]); } }