package ai import ( "strings" "testing" ) func TestShouldUseSurgicalReplayPartialFailureWithTrace(t *testing.T) { trace := SurgicalTraceContext{SessionID: "sess-abc", HopIndex: 1, HopCount: 3} snap := AgentSnapshot{ LOTLAttempts: []TierAttempt{ {Tier: "vuln_recon", OK: true}, {Tier: "docker", OK: false, Error: "daemon missing"}, }, } if !ShouldUseSurgicalReplay(trace, snap) { t.Fatal("expected surgical replay for partial spread failure with trace") } } func TestShouldUseSurgicalReplaySkipsFullCourtStuck(t *testing.T) { trace := SurgicalTraceContext{SessionID: "sess-abc"} snap := AgentSnapshot{Stuck: true, LOTLAttempts: stuckSpreadAttempts()} if ShouldUseSurgicalReplay(trace, snap) { t.Fatal("expected court path, not surgical replay, when fully stuck") } } func TestShouldUseSurgicalReplayRequiresTrace(t *testing.T) { snap := AgentSnapshot{ LOTLAttempts: []TierAttempt{{Tier: "docker", OK: false}}, } if ShouldUseSurgicalReplay(SurgicalTraceContext{}, snap) { t.Fatal("expected false without pathtrace session") } } func TestBuildSurgicalReplayPromptMentionsFailedTier(t *testing.T) { bundle := BuildSurgicalDiagnosticBundle( AgentSnapshot{AgentID: "a1", Name: "host", JoinLane: "do_peer"}, SurgicalTraceContext{SessionID: "trace-123", HopIndex: 0, HopCount: 2, DiscoverError: "no smb"}, "wsl 3/3 failed", PersonaBalanced, false, ) bundle.FailedTier = "docker" bundle.FailedError = "not installed" bundle.FailedAttempts = []TierAttempt{{Tier: "docker", OK: false, Error: "not installed"}} _, user := BuildSurgicalReplayPrompt(bundle) if !strings.Contains(user, "docker") { t.Fatalf("missing failed tier in prompt: %s", user) } if !strings.Contains(user, "trace-123") { t.Fatalf("missing session id in prompt: %s", user) } if !strings.Contains(user, "no smb") { t.Fatalf("missing discover error in prompt: %s", user) } } func TestSelectSurgicalCommandPicksFirstActionable(t *testing.T) { cmd, ok := SelectSurgicalCommand([]Command{ {Type: CmdNoop}, {Type: CmdSkipTier, Args: map[string]interface{}{"tier": "docker"}}, {Type: CmdRestartMining}, }) if !ok || cmd.Type != CmdSkipTier { t.Fatalf("got %+v ok=%v", cmd, ok) } } func TestExpandSurgicalCommandSkipTier(t *testing.T) { out := ExpandSurgicalCommand(Command{Type: CmdSkipTier, Args: map[string]interface{}{"tier": "wsl"}}) if len(out) != 1 || out[0].Type != CmdReorderTiers { t.Fatalf("got %+v", out) } }