| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestSubagentStoreCleanupStaleRunningParentProbe(t *testing.T) { |
| 9 | live, dead := true, false |
| 10 | tests := []struct { |
| 11 | name string |
| 12 | probeResult *bool |
| 13 | wantCleaned int |
| 14 | wantStatus SubagentStatus |
| 15 | }{ |
| 16 | {name: "live parent", probeResult: &live, wantStatus: SubagentRunning}, |
| 17 | {name: "dead parent", probeResult: &dead, wantCleaned: 1, wantStatus: SubagentInterrupted}, |
| 18 | {name: "nil probe", wantCleaned: 1, wantStatus: SubagentInterrupted}, |
| 19 | } |
| 20 | for _, tt := range tests { |
| 21 | t.Run(tt.name, func(t *testing.T) { |
| 22 | sessionDir := t.TempDir() |
| 23 | store := NewSubagentStore(filepath.Join(sessionDir, "subagents")) |
| 24 | spec := testSubagentSpec(t, "review") |
| 25 | spec.ParentSession = "probe-parent" |
| 26 | run, err := store.PrepareFresh(spec) |
| 27 | if err != nil { |
| 28 | t.Fatalf("PrepareFresh: %v", err) |
| 29 | } |
| 30 | if err := store.MarkRunning(run); err != nil { |
| 31 | t.Fatalf("MarkRunning: %v", err) |
| 32 | } |
| 33 | ref := run.Ref |
| 34 | run.Release() |
| 35 | |
| 36 | probeCalls := 0 |
| 37 | if tt.probeResult != nil { |
| 38 | store.WithParentSessionProbe(func(path string) bool { |
| 39 | probeCalls++ |
| 40 | wantPath := filepath.Join(sessionDir, spec.ParentSession+".jsonl") |
| 41 | if filepath.Clean(path) != filepath.Clean(wantPath) { |
| 42 | t.Fatalf("probe path = %q, want %q", path, wantPath) |
| 43 | } |
| 44 | return *tt.probeResult |
| 45 | }) |
| 46 | } |
| 47 | cleaned, err := store.CleanupStaleRunning() |
| 48 | if err != nil { |
| 49 | t.Fatalf("CleanupStaleRunning: %v", err) |
| 50 | } |
| 51 | if cleaned != tt.wantCleaned { |
| 52 | t.Fatalf("cleaned = %d, want %d", cleaned, tt.wantCleaned) |
| 53 | } |
| 54 | if tt.probeResult != nil && probeCalls != 1 { |
| 55 | t.Fatalf("probe calls = %d, want 1", probeCalls) |
| 56 | } |
| 57 | meta, err := store.LoadMeta(ref) |
| 58 | if err != nil { |
| 59 | t.Fatalf("LoadMeta: %v", err) |
| 60 | } |
| 61 | if meta.Status != tt.wantStatus { |
| 62 | t.Fatalf("status = %q, want %q", meta.Status, tt.wantStatus) |
| 63 | } |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 |