aisop/tests/VideoFlowTest.Smoke/StepTestDecisionChecks.cs
2026-12-05 14:39:11 +08:00

152 lines
9.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Avalonia.Threading;
using OpenCvSharp;
using SopHelper.Engine.Common;
using SopHelper.Engine.Sop;
using SopHelper.VVMs.RecipeEditor;
internal static class StepTestDecisionChecks
{
public static void Run()
{
var model = new RecipeEditorViewModel();
var probe = new Probe();
model.StepTestDecisionClient = probe;
model.TestStepDecisionCommand.Execute();
Check(model.StepTestStatus.Contains("选中"), "无步骤时测试提示先选择");
model.Recipe.AddStepCommand.Execute();
var step = model.Recipe.SelectedStep!;
step.Id = 7; step.Name = "装配"; step.Description = "将红色零件放入装配区"; step.CompletionCriteria = "红色零件安装到位";
model.TestStepDecisionCommand.Execute();
Check(model.StepTestStatus.Contains("关键帧") && probe.Calls == 0, "无关键帧时不调用AI");
using var image = new Mat(48, 64, MatType.CV_8UC3, new Scalar(10, 20, 230));
step.KeyFrameBase64 = Convert.ToBase64String(image.ToBytes(".png"));
step.KeyFrameSeconds = 2.5;
step.CompletionCriteria = "";
model.TestStepDecisionCommand.Execute();
Check(model.StepTestStatus.Contains("完成条件") && probe.Calls == 0, "空判据不发送图片");
step.CompletionCriteria = "红色零件安装到位";
var store = SopHelper.Models.RuntimeSettingsStore.Current;
var runtime = store.Read() ?? new();
var originalTimeout = runtime.DjevTimeoutMs;
runtime.DjevTimeoutMs = 900; store.Save(runtime);
step.AiTimeoutSeconds = 300; // 旧步骤字段不能覆盖全局设置。
model.TestStepDecisionCommand.Execute();
Check(model.IsTestingStep && !model.TestStepDecisionCommand.CanExecute(), "请求期间禁用重复测试");
Until(() => probe.Request is not null);
var request = probe.Request!;
Check(model.StepDecisionLog.Contains(D3VisionDecisionClient.StatePrompt)
&& model.StepDecisionLog.Contains(D3VisionDecisionClient.BuildInstructions(request.Criteria)),
"判定日志显示与dlite请求共用的完整state和instructions提示词");
using var sent = Cv2.ImDecode(request.Jpeg, ImreadModes.Color);
Check(request.StepId == 7 && request.Criteria == step.CompletionCriteria && request.SourceSeconds == 2.5
&& request.ImageCount == 1 && request.EvidenceMode == VisionEvidenceMode.SingleFrame && request.TimeoutMs == 900
&& sent.Width == 64 && sent.Height == 48 && Cv2.Mean(sent).Val2 > 220,
"AI收到独立完成条件及已保存关键帧的真实像素和时间");
step.CompletionCriteria = "修改后的条件";
runtime.DjevTimeoutMs = 500; store.Save(runtime);
model.Recipe.AddStepCommand.Execute();
Thread.Sleep(650);
probe.Result.SetResult(new(true, .99, "安装清晰可见"));
Until(() => !model.IsTestingStep);
Check(model.StepTestStatus.Contains("步骤 7「装配」:通过") && model.StepTestStatus.Contains("安装清晰可见")
&& model.Recipe.Steps.Count == 2 && step.CompletionCriteria == "修改后的条件",
"切换步骤后结果保留送检步骤身份,测试不修改配方");
Check(model.StepDecisionLog.Contains("安装清晰可见") && model.StepDecisionLog.Contains("红色零件安装到位")
&& !model.StepDecisionLog.Contains("修改后的条件"), "日志追加结果并保留送检时的原始提示词");
model.Recipe.SelectedStep = step;
probe = new Probe(); model.StepTestDecisionClient = probe;
model.TestStepDecisionCommand.Execute();
Until(() => probe.Request is not null);
probe.Result.SetResult(new(true, .5, "低置信度"));
Until(() => !model.IsTestingStep);
Check(model.StepTestStatus.Contains("未通过"), "测试按当前步骤置信度阈值判断通过");
probe = new Probe(); model.StepTestDecisionClient = probe;
model.TestStepDecisionCommand.Execute();
Until(() => !model.IsTestingStep);
Check(model.StepTestStatus.Contains("超时") && model.TestStepDecisionCommand.CanExecute(), "AI未返回时按截止时间结束并可重试");
var region = model.Recipe.AddRegion("inspect");
region.X1 = 16; region.Y1 = 12; region.X2 = 48; region.Y2 = 36;
using (var patch = new Mat(image, new Rect(16, 12, 32, 24))) patch.SetTo(new Scalar(20, 230, 10));
step.KeyFrameBase64 = Convert.ToBase64String(image.ToBytes(".png"));
step.AiImageMode = VisionImageMode.FixedRegion; step.AiImageRegion = "INSPECT";
probe = new Probe(); model.StepTestDecisionClient = probe;
model.TestStepDecisionCommand.Execute(); Until(() => probe.Request is not null);
using (var cropped = Cv2.ImDecode(probe.Request!.Jpeg, ImreadModes.Color))
Check(cropped.Width == 32 && cropped.Height == 24 && Cv2.Mean(cropped).Val1 > 220,
"固定区域测试实际只发送指定区域像素,区域名忽略大小写");
Check(model.StepDecisionLog.Contains("INSPECT [16,12,48,36]"), "日志公开实际裁剪坐标");
region.X2 = 60; // 进行中的请求已固定原裁剪。
probe.Result.SetResult(new(true, .99, "区域测试")); Until(() => !model.IsTestingStep);
var regionCalls = probe.Calls;
step.AiImageRegion = "missing";
model.TestStepDecisionCommand.Execute(); Until(() => !model.IsTestingStep);
Check(probe.Calls == regionCalls && model.StepTestStatus.Contains("不存在"), "送检区缺失不回退整帧");
step.AiImageRegion = "inspect"; region.X2 = 100;
model.TestStepDecisionCommand.Execute(); Until(() => !model.IsTestingStep);
Check(probe.Calls == regionCalls && model.StepTestStatus.Contains("超出"), "越界送检区域不回退整帧");
step.AiImageMode = VisionImageMode.Hand;
step.AiImageHand = "left";
step.AiHandMinSize = 32;
step.AiHandPadding = 0;
step.Detection!.Method = ConditionMethod.TimeElapsed;
step.KeyFrameSourceSize = new() { Width = 128, Height = 96 };
// 先提供错误手别,再提供缩图上的左手位置,验证手别过滤和原视频坐标换算。
var hand = new Hand { HandType = HandType.Right, Rect = new(20, 16, 12, 8) };
model.DetectStepTestHands = (_, _) => [hand];
model.TestStepDecisionCommand.Execute(); Until(() => !model.IsTestingStep);
Check(probe.Calls == regionCalls && model.StepTestStatus.Contains("测试流程"), "无匹配送检手时提示使用测试流程且不发送");
model.DetectStepTestHands = (_, _) => null;
model.TestStepDecisionCommand.Execute(); Until(() => !model.IsTestingStep);
Check(probe.Calls == regionCalls && model.StepTestStatus.Contains("手部模型未就绪"), "手部模型不可用时给出专门提示");
hand.HandType = HandType.Left;
model.DetectStepTestHands = (_, _) => [hand];
probe = new Probe(); model.StepTestDecisionClient = probe;
model.TestStepDecisionCommand.Execute(); Until(() => probe.Request is not null);
using (var cropped = Cv2.ImDecode(probe.Request!.Jpeg, ImreadModes.Color))
Check(cropped.Width == 16 && cropped.Height == 16 && Cv2.Mean(cropped).Val1 > 220,
"按关键帧实际手部裁剪,并按原视频像素应用最小宽高");
Check(model.StepDecisionLog.Contains("手部附近 [36,24,68,56] · 关键帧裁剪 [18,12,34,28]"),
"跟手测试日志记录实际送检范围");
probe.Result.SetResult(new(true, .99, "跟手测试")); Until(() => !model.IsTestingStep);
step.AiImageMode = VisionImageMode.FullFrame;
step.KeyFrameBase64 = "broken";
var calls = probe.Calls;
model.TestStepDecisionCommand.Execute(); Until(() => !model.IsTestingStep);
Check(model.StepTestStatus.Contains("失败") && probe.Calls == calls, "损坏关键帧不发送并显示失败");
step.KeyFrameBase64 = Convert.ToBase64String(image.ToBytes(".png"));
model.TestStepDecisionCommand.Execute();
var shutdown = model.ShutdownAsync(); Until(() => shutdown.IsCompleted); shutdown.GetAwaiter().GetResult();
Check(!model.IsTestingStep && !model.TestStepDecisionCommand.CanExecute(), "关闭编辑器取消测试并等待清理");
runtime.DjevTimeoutMs = originalTimeout; store.Save(runtime);
Console.WriteLine("PASS: 步骤关键帧AI测试判定、请求快照、阈值、超时与关闭清理");
}
private static void Until(Func<bool> ready)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
while (!ready() && watch.Elapsed < TimeSpan.FromSeconds(10))
{
using var cancellation = new CancellationTokenSource(5);
Dispatcher.UIThread.MainLoop(cancellation.Token);
}
if (!ready()) throw new TimeoutException("步骤测试判定检查超时");
Dispatcher.UIThread.RunJobs();
}
private static void Check(bool value, string message) { if (!value) throw new Exception(message); }
private sealed class Probe : IVisionDecisionClient
{
public int Calls;
public VisionDecisionRequest? Request;
public TaskCompletionSource<VisionDecisionResult> Result { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
public Task<VisionDecisionResult> DecideAsync(VisionDecisionRequest request, CancellationToken token)
{
Interlocked.Increment(ref Calls); Request = request;
return Result.Task.WaitAsync(token);
}
}
}