| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // TestLaunchDetachedAgentPane covers the default agent_display ("detached"): |
| 12 | // the agent gets a real tmux pane for lifecycle management, but that pane lives |
| 13 | // in a separate detached session rather than the coordinator's window, and |
| 14 | // `klaus cleanup --all` takes the session down with the last agent. |
| 15 | func TestLaunchDetachedAgentPane(t *testing.T) { |
| 16 | t.Parallel() |
| 17 | h := NewHarness(t) |
| 18 | |
| 19 | before := h.WindowPanes(h.InitialPane) |
| 20 | |
| 21 | res := h.RunKlaus("launch", "add a health check endpoint") |
| 22 | if res.ExitCode != 0 { |
| 23 | t.Fatalf("launch exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 24 | } |
| 25 | |
| 26 | ids := h.RunIDs() |
| 27 | if len(ids) != 1 { |
| 28 | t.Fatalf("expected 1 run, got %d: %v", len(ids), ids) |
| 29 | } |
| 30 | runID := ids[0] |
| 31 | |
| 32 | st, err := h.ReadState(runID) |
| 33 | if err != nil { |
| 34 | t.Fatalf("reading state: %v", err) |
| 35 | } |
| 36 | if st.TmuxPane == nil || *st.TmuxPane == "" { |
| 37 | t.Fatal("state has no TmuxPane") |
| 38 | } |
| 39 | pane := *st.TmuxPane |
| 40 | |
| 41 | h.WaitForClaudeStart(30 * time.Second) |
| 42 | |
| 43 | // The pane is real and alive. |
| 44 | if !h.PaneExists(pane) { |
| 45 | t.Fatalf("expected pane %s to exist; panes: %v", pane, h.ListPanes()) |
| 46 | } |
| 47 | |
| 48 | // It lives in the agents session... |
| 49 | agents := h.AgentsSession() |
| 50 | if !h.SessionExists(agents) { |
| 51 | t.Fatalf("expected detached session %s to exist", agents) |
| 52 | } |
| 53 | if !contains(h.SessionPanes(agents), pane) { |
| 54 | t.Errorf("pane %s not in session %s (panes: %v)", pane, agents, h.SessionPanes(agents)) |
| 55 | } |
| 56 | |
| 57 | // ...and nowhere near the coordinator's window, which is untouched. |
| 58 | after := h.WindowPanes(h.InitialPane) |
| 59 | if contains(after, pane) { |
| 60 | t.Errorf("agent pane %s leaked into the coordinator window: %v", pane, after) |
| 61 | } |
| 62 | if len(after) != len(before) { |
| 63 | t.Errorf("coordinator window pane count changed: %v -> %v", before, after) |
| 64 | } |
| 65 | |
| 66 | // cleanup --all kills the pane and empties out the agents session. |
| 67 | res = h.RunKlaus("cleanup", "--all", "--force") |
| 68 | if res.ExitCode != 0 { |
| 69 | t.Fatalf("cleanup exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 70 | } |
| 71 | waitPaneGone(t, h, pane, 30*time.Second) |
| 72 | |
| 73 | deadline := time.Now().Add(10 * time.Second) |
| 74 | for h.SessionExists(agents) && time.Now().Before(deadline) { |
| 75 | time.Sleep(100 * time.Millisecond) |
| 76 | } |
| 77 | if h.SessionExists(agents) { |
| 78 | t.Errorf("agents session %s should be gone after cleanup --all (panes: %v)", agents, h.SessionPanes(agents)) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // TestLaunchPaneDisplayModeSplitsWindow pins the escape hatch: with |
| 83 | // agent_display "pane", the agent still splits the coordinator's window and no |
| 84 | // detached agents session is created. |
| 85 | func TestLaunchPaneDisplayModeSplitsWindow(t *testing.T) { |
| 86 | t.Parallel() |
| 87 | h := NewHarness(t) |
| 88 | h.AmendRepoConfig(map[string]any{"agent_display": "pane"}) |
| 89 | |
| 90 | res := h.RunKlaus("launch", "add a health check endpoint") |
| 91 | if res.ExitCode != 0 { |
| 92 | t.Fatalf("launch exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 93 | } |
| 94 | |
| 95 | ids := h.RunIDs() |
| 96 | if len(ids) != 1 { |
| 97 | t.Fatalf("expected 1 run, got %d: %v", len(ids), ids) |
| 98 | } |
| 99 | st, err := h.ReadState(ids[0]) |
| 100 | if err != nil { |
| 101 | t.Fatalf("reading state: %v", err) |
| 102 | } |
| 103 | if st.TmuxPane == nil { |
| 104 | t.Fatal("state has no TmuxPane") |
| 105 | } |
| 106 | pane := *st.TmuxPane |
| 107 | |
| 108 | h.WaitForClaudeStart(30 * time.Second) |
| 109 | |
| 110 | if panes := h.WindowPanes(h.InitialPane); !contains(panes, pane) { |
| 111 | t.Errorf("agent pane %s should be in the coordinator window, got %v", pane, panes) |
| 112 | } |
| 113 | if h.SessionExists(h.AgentsSession()) { |
| 114 | t.Errorf("no detached session should exist in pane mode, but %s does", h.AgentsSession()) |
| 115 | } |
| 116 | |
| 117 | h.ReleaseClaude() |
| 118 | } |
| 119 | |
| 120 | // TestLaunchInvalidAgentDisplay asserts a typo in agent_display fails the |
| 121 | // launch up front instead of silently picking a mode. |
| 122 | func TestLaunchInvalidAgentDisplay(t *testing.T) { |
| 123 | t.Parallel() |
| 124 | h := NewHarness(t) |
| 125 | h.AmendRepoConfig(map[string]any{"agent_display": "windowed"}) |
| 126 | |
| 127 | res := h.RunKlaus("launch", "do the thing") |
| 128 | if res.ExitCode == 0 { |
| 129 | t.Fatalf("launch with invalid agent_display exited 0\nstdout:\n%s", res.Stdout) |
| 130 | } |
| 131 | for _, want := range []string{"windowed", "detached", "pane"} { |
| 132 | if !strings.Contains(res.Stderr, want) { |
| 133 | t.Errorf("stderr should mention %q, got:\n%s", want, res.Stderr) |
| 134 | } |
| 135 | } |
| 136 | if ids := h.RunIDs(); len(ids) != 0 { |
| 137 | t.Errorf("no run should have been recorded, got %v", ids) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func contains(haystack []string, needle string) bool { |
| 142 | for _, s := range haystack { |
| 143 | if s == needle { |
| 144 | return true |
| 145 | } |
| 146 | } |
| 147 | return false |
| 148 | } |