| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "os" |
| 9 | "testing" |
| 10 | |
| 11 | "the-valley/flow/internal/git" |
| 12 | "the-valley/flow/internal/run" |
| 13 | ) |
| 14 | |
| 15 | func TestCleanupAllSkipsActiveRuns(t *testing.T) { |
| 16 | store := newFakeStore( |
| 17 | &run.State{ID: "run-1", Prompt: "a", Branch: "b1", CreatedAt: "2026-01-01T00:00:00Z"}, |
| 18 | &run.State{ID: "run-2", Prompt: "b", Branch: "b2", CreatedAt: "2026-01-01T00:01:00Z"}, |
| 19 | &run.State{ID: "run-3", Prompt: "c", Branch: "b3", CreatedAt: "2026-01-01T00:02:00Z"}, |
| 20 | ) |
| 21 | |
| 22 | ctx := context.Background() |
| 23 | tc := isolatedTmux(t) |
| 24 | |
| 25 | // Make run-2 active |
| 26 | deps := CleanupDeps{IsRunActive: func(s *run.State) bool { return s.ID == "run-2" }} |
| 27 | |
| 28 | output := captureStdout(t, func() { |
| 29 | if err := cleanupAll(ctx, "", store, git.NewExecClient(), false, deps, tc); err != nil { |
| 30 | t.Fatalf("cleanupAll() error: %v", err) |
| 31 | } |
| 32 | }) |
| 33 | |
| 34 | // run-2 should be skipped |
| 35 | if !contains(output, "skipping run-2 (still running)") { |
| 36 | t.Errorf("expected skip message for run-2, got: %s", output) |
| 37 | } |
| 38 | |
| 39 | // run-1 and run-3 should be cleaned up |
| 40 | if _, err := store.Load("run-1"); err == nil { |
| 41 | t.Error("run-1 should have been deleted") |
| 42 | } |
| 43 | if _, err := store.Load("run-2"); err != nil { |
| 44 | t.Error("run-2 should still exist (was skipped)") |
| 45 | } |
| 46 | if _, err := store.Load("run-3"); err == nil { |
| 47 | t.Error("run-3 should have been deleted") |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestCleanupAllForceRemovesActiveRuns(t *testing.T) { |
| 52 | store := newFakeStore( |
| 53 | &run.State{ID: "run-1", Prompt: "a", Branch: "b1", CreatedAt: "2026-01-01T00:00:00Z"}, |
| 54 | &run.State{ID: "run-2", Prompt: "b", Branch: "b2", CreatedAt: "2026-01-01T00:01:00Z"}, |
| 55 | ) |
| 56 | |
| 57 | ctx := context.Background() |
| 58 | tc := isolatedTmux(t) |
| 59 | deps := CleanupDeps{IsRunActive: func(s *run.State) bool { return s.ID == "run-2" }} |
| 60 | |
| 61 | output := captureStdout(t, func() { |
| 62 | if err := cleanupAll(ctx, "", store, git.NewExecClient(), true, deps, tc); err != nil { |
| 63 | t.Fatalf("cleanupAll() error: %v", err) |
| 64 | } |
| 65 | }) |
| 66 | |
| 67 | // No skip messages |
| 68 | if contains(output, "skipping") { |
| 69 | t.Errorf("expected no skip messages with --force, got: %s", output) |
| 70 | } |
| 71 | |
| 72 | // Both should be cleaned up |
| 73 | if _, err := store.Load("run-1"); err == nil { |
| 74 | t.Error("run-1 should have been deleted") |
| 75 | } |
| 76 | if _, err := store.Load("run-2"); err == nil { |
| 77 | t.Error("run-2 should have been deleted") |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestCleanupOneSkipsActiveRun(t *testing.T) { |
| 82 | store := newFakeStore( |
| 83 | &run.State{ID: "run-1", Prompt: "a", Branch: "b1", CreatedAt: "2026-01-01T00:00:00Z"}, |
| 84 | ) |
| 85 | |
| 86 | ctx := context.Background() |
| 87 | tc := isolatedTmux(t) |
| 88 | deps := CleanupDeps{IsRunActive: func(s *run.State) bool { return true }} |
| 89 | |
| 90 | output := captureStdout(t, func() { |
| 91 | if err := cleanupOne(ctx, "", store, git.NewExecClient(), "run-1", false, deps, tc); err != nil { |
| 92 | t.Fatalf("cleanupOne() error: %v", err) |
| 93 | } |
| 94 | }) |
| 95 | |
| 96 | if !contains(output, "skipping run-1 (still running)") { |
| 97 | t.Errorf("expected skip message, got: %s", output) |
| 98 | } |
| 99 | if _, err := store.Load("run-1"); err != nil { |
| 100 | t.Error("run-1 should still exist") |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func TestCleanupOneForceRemovesActiveRun(t *testing.T) { |
| 105 | store := newFakeStore( |
| 106 | &run.State{ID: "run-1", Prompt: "a", Branch: "b1", CreatedAt: "2026-01-01T00:00:00Z"}, |
| 107 | ) |
| 108 | |
| 109 | ctx := context.Background() |
| 110 | tc := isolatedTmux(t) |
| 111 | deps := CleanupDeps{IsRunActive: func(s *run.State) bool { return true }} |
| 112 | |
| 113 | captureStdout(t, func() { |
| 114 | if err := cleanupOne(ctx, "", store, git.NewExecClient(), "run-1", true, deps, tc); err != nil { |
| 115 | t.Fatalf("cleanupOne() error: %v", err) |
| 116 | } |
| 117 | }) |
| 118 | |
| 119 | if _, err := store.Load("run-1"); err == nil { |
| 120 | t.Error("run-1 should have been deleted") |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func TestIsRunActiveWithSessionEnv(t *testing.T) { |
| 125 | tc := isolatedTmux(t) |
| 126 | t.Setenv(sessionIDEnv, "sess-123") |
| 127 | |
| 128 | // Session run matching current session ID should be active |
| 129 | s := &run.State{ID: "sess-123", Type: "session"} |
| 130 | if !defaultIsRunActive(s, tc) { |
| 131 | t.Error("expected session run matching KLAUS_SESSION_ID to be active") |
| 132 | } |
| 133 | |
| 134 | // Different session ID should not be active (no tmux pane) |
| 135 | s2 := &run.State{ID: "sess-456", Type: "session"} |
| 136 | if defaultIsRunActive(s2, tc) { |
| 137 | t.Error("expected different session ID to not be active") |
| 138 | } |
| 139 | |
| 140 | // Non-session run without tmux pane should not be active |
| 141 | s3 := &run.State{ID: "sess-123", Type: "launch"} |
| 142 | if defaultIsRunActive(s3, tc) { |
| 143 | t.Error("expected non-session run without tmux pane to not be active") |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func TestIsRunActiveWithDashboardPane(t *testing.T) { |
| 148 | tc := isolatedTmux(t) |
| 149 | |
| 150 | // A run with no panes should not be active |
| 151 | s := &run.State{ID: "run-1"} |
| 152 | if defaultIsRunActive(s, tc) { |
| 153 | t.Error("expected run without panes to not be active") |
| 154 | } |
| 155 | |
| 156 | // A run with a DashboardPane that doesn't exist should not be active |
| 157 | fakePaneID := "%999999" |
| 158 | s2 := &run.State{ID: "run-2", DashboardPane: &fakePaneID} |
| 159 | if defaultIsRunActive(s2, tc) { |
| 160 | t.Error("expected run with dead dashboard pane to not be active") |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // fakeStore is an in-memory StateStore for testing. |
| 165 | type fakeStore struct { |
| 166 | states map[string]*run.State |
| 167 | } |
| 168 | |
| 169 | func newFakeStore(states ...*run.State) *fakeStore { |
| 170 | s := &fakeStore{states: make(map[string]*run.State)} |
| 171 | for _, st := range states { |
| 172 | s.states[st.ID] = st |
| 173 | } |
| 174 | return s |
| 175 | } |
| 176 | |
| 177 | func (f *fakeStore) Save(s *run.State) error { f.states[s.ID] = s; return nil } |
| 178 | func (f *fakeStore) Load(id string) (*run.State, error) { |
| 179 | s, ok := f.states[id] |
| 180 | if !ok { |
| 181 | return nil, fmt.Errorf("not found: %s", id) |
| 182 | } |
| 183 | return s, nil |
| 184 | } |
| 185 | func (f *fakeStore) List() ([]*run.State, error) { |
| 186 | var out []*run.State |
| 187 | for _, s := range f.states { |
| 188 | out = append(out, s) |
| 189 | } |
| 190 | return out, nil |
| 191 | } |
| 192 | func (f *fakeStore) Delete(id string) error { delete(f.states, id); return nil } |
| 193 | func (f *fakeStore) LogDir() string { return "" } |
| 194 | func (f *fakeStore) StateDir() string { return "" } |
| 195 | func (f *fakeStore) EnsureDirs() error { return nil } |
| 196 | |
| 197 | func captureStdout(t *testing.T, fn func()) string { |
| 198 | t.Helper() |
| 199 | old := os.Stdout |
| 200 | r, w, _ := os.Pipe() |
| 201 | os.Stdout = w |
| 202 | |
| 203 | fn() |
| 204 | |
| 205 | w.Close() |
| 206 | os.Stdout = old |
| 207 | |
| 208 | var buf bytes.Buffer |
| 209 | io.Copy(&buf, r) |
| 210 | return buf.String() |
| 211 | } |
| 212 | |
| 213 | func contains(s, substr string) bool { |
| 214 | return len(s) >= len(substr) && bytes.Contains([]byte(s), []byte(substr)) |
| 215 | } |