| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "the-valley/flow/internal/git" |
| 11 | "the-valley/flow/internal/run" |
| 12 | ) |
| 13 | |
| 14 | // TestUnitTestsNeverUseRealTmux fails if a test in this package builds a tmux |
| 15 | // client that talks to the real server. Agent panes export KLAUS_SESSION_ID, |
| 16 | // so `go test` run by a klaus agent inherits the coordinator's session id; a |
| 17 | // real client here can reach — and kill — the live agents session (issue #295). |
| 18 | func TestUnitTestsNeverUseRealTmux(t *testing.T) { |
| 19 | files, err := filepath.Glob("*_test.go") |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | if len(files) == 0 { |
| 24 | t.Fatal("found no test files to scan") |
| 25 | } |
| 26 | |
| 27 | banned := []struct{ frag, why string }{ |
| 28 | {"tmux.NewExecClient(", "constructs a client against the real tmux server"}, |
| 29 | {"tmux.ExecClient", "embedding ExecClient forwards un-overridden methods to the real tmux server"}, |
| 30 | } |
| 31 | for _, f := range files { |
| 32 | if f == "tmux_isolation_test.go" { |
| 33 | continue |
| 34 | } |
| 35 | src, err := os.ReadFile(f) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | for _, b := range banned { |
| 40 | if strings.Contains(string(src), b.frag) { |
| 41 | t.Errorf("%s: %s — %s; use isolatedTmux(t) instead", f, b.frag, b.why) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // TestCleanupAllNeverKillsAmbientAgentsSession is the regression test for |
| 48 | // issue #295: with a real-looking KLAUS_SESSION_ID in the environment and a |
| 49 | // store that knows nothing about the session's panes, `cleanup --all` must |
| 50 | // leave the live agents session alone. |
| 51 | func TestCleanupAllNeverKillsAmbientAgentsSession(t *testing.T) { |
| 52 | t.Setenv(sessionIDEnv, "session-20260907-1241-c0da2e6d") |
| 53 | ambient := agentsSessionName() |
| 54 | |
| 55 | tc := &fakeTmux{ |
| 56 | panes: map[string][]string{ambient: {"%1", "%2"}}, |
| 57 | runningPanes: map[string]bool{"%1": true, "%2": true}, |
| 58 | } |
| 59 | // A store holding runs that have nothing to do with those panes — the |
| 60 | // shape every unit test's fake store has. |
| 61 | store := newFakeStore( |
| 62 | &run.State{ID: "run-1", Branch: "b1", CreatedAt: "2026-01-01T00:00:00Z"}, |
| 63 | &run.State{ID: "run-2", Branch: "b2", CreatedAt: "2026-01-01T00:01:00Z"}, |
| 64 | ) |
| 65 | deps := CleanupDeps{IsRunActive: func(*run.State) bool { return false }} |
| 66 | |
| 67 | captureStdout(t, func() { |
| 68 | if err := cleanupAll(context.Background(), "", store, git.NewExecClient(), true, deps, tc); err != nil { |
| 69 | t.Fatalf("cleanupAll() error: %v", err) |
| 70 | } |
| 71 | }) |
| 72 | |
| 73 | for _, killed := range tc.killedSessions { |
| 74 | if killed == ambient { |
| 75 | t.Fatalf("cleanupAll killed the ambient agents session %q", ambient) |
| 76 | } |
| 77 | } |
| 78 | if _, err := tc.SessionPanes(context.Background(), ambient); err != nil { |
| 79 | t.Fatalf("ambient agents session %q no longer exists: %v", ambient, err) |
| 80 | } |
| 81 | } |