using SopHelper.Engine.Sop; internal static class SettingsMigrationChecks { public static void Run() { var config = SopConfigLoader.Parse(""" sop: name: 旧设置迁移 settings: { stable_frames: 9, position_tolerance: 35, enable_timeout_detection: false, frame_stride: 3, auto_reset_on_complete: false, reset_delay_sec: 4, confidence_threshold: 0.8 } steps: - id: 1 timeout: 10 detection: { method: hand_in_region, stable_frames: 0, tolerance: 0 } - id: 2 timeout: 20 detection: { method: hand_in_region, stable_frames: 2, tolerance: 3 } """); Check(config.Sop.HasLegacySettings && config.Sop.Steps[0].Detection is { StableFrames: 9, Tolerance: 35 }, "旧配方继承的稳定帧和容差迁移到步骤"); Check(config.Sop.Steps[1].Detection is { StableFrames: 2, Tolerance: 3 } && config.Sop.Steps.All(s => s.Timeout == 0), "显式步骤参数优先,关闭的超时告警迁移为步骤超时0"); var yaml = SopConfigLoader.ToYaml(config); var restored = SopConfigLoader.Parse(yaml); Check(!yaml.Contains("settings:") && !yaml.Contains("has_legacy_settings") && !restored.Sop.HasLegacySettings && restored.Sop.Steps[0].Detection is { StableFrames: 9, Tolerance: 35 }, "保存后的配方不含全局或运行设置且保留步骤行为"); Check(config.Sop.Settings.FrameStride == 3 && !config.Sop.Settings.AutoResetOnComplete && config.Sop.Settings.ResetDelaySec == 4, "配方导出不修改内存中待迁移的旧运行参数"); restored.Sop.Steps[0].Timeout = 2; var detector = new ViolationDetector(restored.Sop.Settings); Check(detector.CheckTimeout(restored.Sop.Steps[0], DateTime.Now.AddSeconds(-3)) is not null, "步骤正数超时直接启用违规告警"); restored.Sop.Steps[0].Timeout = 0; Check(new ViolationDetector(restored.Sop.Settings).CheckTimeout(restored.Sop.Steps[0], DateTime.Now.AddHours(-1)) is null, "步骤超时0关闭违规告警"); var zero = SopConfigLoader.Parse("sop: {steps: [{id: 1, detection: {method: hand_in_region, tolerance: 0, stable_frames: 1}}]}"); Check(SopConfigLoader.Parse(SopConfigLoader.ToYaml(zero)).Sop.Steps[0].Detection!.Tolerance == 0, "新配方的零像素容差不再继承隐藏的全局容差"); } private static void Check(bool condition, string message) { if (!condition) throw new Exception(message); Console.WriteLine("PASS: " + message); } }