using System.Net; using System.Text; using System.Text.Json; using SopHelper.Engine.Sop; internal static class SequenceProtocolTests { private static void Check(bool condition, string message) { if (!condition) throw new Exception(message); } private sealed class Handler(string mode) : HttpMessageHandler { protected override async Task SendAsync(HttpRequestMessage request, CancellationToken token) { using var body = JsonDocument.Parse(await request.Content!.ReadAsStringAsync(token)); Check(body.RootElement.GetProperty("max_tokens").GetInt32() == 768, "时序接口应限制解释输出预算"); Check(mode == "backdated" ? !body.RootElement.GetProperty("chat_template_kwargs").GetProperty("enable_thinking").GetBoolean() : !body.RootElement.TryGetProperty("chat_template_kwargs", out _), "思考开关必须按显式选择发送,默认沿用服务设置"); var parts = body.RootElement.GetProperty("messages")[1].GetProperty("content"); Check(parts.EnumerateArray().Count(p => p.GetProperty("type").GetString() == "image_url") == 3, "必须发送三张真实证据,不能把时序请求退化为尾图"); var answer = JsonSerializer.Serialize(new { status = "completed", confidence = .99, reason = "关键变化与结果可见", evidence_frame_ids = mode == "fabricated" ? new[] { 1, 999 } : new[] { 1, 3 }, completion_frame_id = mode == "fabricated" ? 999 : mode == "backdated" ? 1 : 3 }); return new(HttpStatusCode.OK) { Content = new StringContent(JsonSerializer.Serialize(new { choices = new[] { new { finish_reason = mode == "truncated" ? "length" : "stop", message = new { content = answer } } } }), Encoding.UTF8, "application/json") }; } } public static async Task RunAsync() { var frames = Enumerable.Range(1, 3).Select(i => new VisionEvidenceFrame(i, DateTime.UnixEpoch.AddSeconds(i), [(byte)i], i)).ToArray(); var request = new VisionDecisionRequest("通用流程", 1, "操作", "主体状态变化", "主体状态变化", 3, frames[^1].CapturedAt, frames[^1].Jpeg, VisionEvidenceMode.Sequence, frames, 3); foreach (var mode in new[] { "valid", "backdated", "fabricated", "truncated" }) { using var http = new HttpClient(new Handler(mode)); var client = new SequenceVisionDecisionClient(http, new("https://probe.invalid/v1", "test-model", EnableThinking: mode == "backdated" ? false : null)); var result = await client.DecideAsync(request, default); Check(result.Completed == (mode is "valid" or "backdated") && (mode is "valid" or "backdated" || result.EffectiveStatus == VisionDecisionStatus.Insufficient), "缩短输出预算后仍须拒绝虚构帧和被截断的判定"); if (mode == "backdated") Check(VisionDecisionEvidence.SupportEnd(request, result)?.FrameId == 3 && result.CompletionFrameId == 1, "必须分开保留模型指示帧与后续支持证据的确认边界,不能用早帧倒填确认时间"); } Console.WriteLine("PASS: 时序多图协议、输出预算、真实帧引用和截断响应保护"); } }