gunk-dev/the-valley

tmux_fake_test.go

sourced9c8073fd613main's published tip history
5.0 KiB
1package cmd
2
3import (
4 "context"
5 "fmt"
6 "sync"
7 "testing"
8
9 "the-valley/flow/internal/tmux"
10)
11
12// fakeTmux is a complete in-memory tmux.Client for unit tests. It deliberately
13// implements every method rather than embedding the exec-backed client: an
14// embedded real client would make any method the fake forgets to override talk
15// to the live tmux server, which is how `go test` inside an agent worktree once
16// killed the coordinator's agents session (issue #295). Every method here is
17// pure bookkeeping.
18type fakeTmux struct {
19 mu sync.Mutex
20
21 // panes maps a session name to the pane IDs it contains. A session that
22 // is absent from the map does not exist.
23 panes map[string][]string
24 // runningPanes holds panes whose command is still running; every other
25 // known pane reports as idle.
26 runningPanes map[string]bool
27 // existingPanes holds panes that exist on the server. Panes listed in a
28 // session or marked running count as existing too.
29 existingPanes map[string]bool
30
31 killedPanes []string
32 killedSessions []string
33 splitCommands []string
34 newWindows []string
35}
36
37// Compile-time check that the fake really covers the whole interface.
38var _ tmux.Client = (*fakeTmux)(nil)
39
40func (f *fakeTmux) paneKnown(id string) bool {
41 if f.existingPanes[id] || f.runningPanes[id] {
42 return true
43 }
44 for _, ps := range f.panes {
45 for _, p := range ps {
46 if p == id {
47 return true
48 }
49 }
50 }
51 return false
52}
53
54func (f *fakeTmux) InSession() bool { return true }
55
56func (f *fakeTmux) SplitWindow(_ context.Context, _, _, command string) (string, error) {
57 f.mu.Lock()
58 defer f.mu.Unlock()
59 f.splitCommands = append(f.splitCommands, command)
60 return fmt.Sprintf("%%split-%d", len(f.splitCommands)), nil
61}
62
63func (f *fakeTmux) SplitWindowSized(ctx context.Context, target, dir, command, _, _ string) (string, error) {
64 return f.SplitWindow(ctx, target, dir, command)
65}
66
67func (f *fakeTmux) SessionExists(_ context.Context, session string) bool {
68 f.mu.Lock()
69 defer f.mu.Unlock()
70 _, ok := f.panes[session]
71 return ok
72}
73
74func (f *fakeTmux) NewDetachedWindow(_ context.Context, session, _, _, command string) (string, error) {
75 f.mu.Lock()
76 defer f.mu.Unlock()
77 f.newWindows = append(f.newWindows, command)
78 id := fmt.Sprintf("%%win-%d", len(f.newWindows))
79 if f.panes == nil {
80 f.panes = map[string][]string{}
81 }
82 f.panes[session] = append(f.panes[session], id)
83 return id, nil
84}
85
86func (f *fakeTmux) SessionPanes(_ context.Context, session string) ([]string, error) {
87 f.mu.Lock()
88 defer f.mu.Unlock()
89 panes, ok := f.panes[session]
90 if !ok {
91 return nil, fmt.Errorf("session not found: %s", session)
92 }
93 return append([]string(nil), panes...), nil
94}
95
96func (f *fakeTmux) KillSession(_ context.Context, session string) error {
97 f.mu.Lock()
98 defer f.mu.Unlock()
99 f.killedSessions = append(f.killedSessions, session)
100 delete(f.panes, session)
101 return nil
102}
103
104func (f *fakeTmux) SetPaneTitle(context.Context, string, string) error { return nil }
105func (f *fakeTmux) LockPaneTitle(context.Context, string) error { return nil }
106func (f *fakeTmux) RebalanceLayout(context.Context, string) error { return nil }
107func (f *fakeTmux) SwapPane(context.Context, string, string) error { return nil }
108func (f *fakeTmux) SetWindowOption(context.Context, string, string, string) error { return nil }
109func (f *fakeTmux) RenameWindow(context.Context, string, string) error { return nil }
110func (f *fakeTmux) SendKeys(context.Context, string, string) error { return nil }
111func (f *fakeTmux) SelectPane(context.Context, string) error { return nil }
112
113func (f *fakeTmux) ListWindowPanes(_ context.Context, target string) ([]string, error) {
114 f.mu.Lock()
115 defer f.mu.Unlock()
116 for _, panes := range f.panes {
117 for _, p := range panes {
118 if p == target {
119 return append([]string(nil), panes...), nil
120 }
121 }
122 }
123 return nil, fmt.Errorf("pane not found: %s", target)
124}
125
126func (f *fakeTmux) PaneExists(_ context.Context, id string) bool {
127 f.mu.Lock()
128 defer f.mu.Unlock()
129 return f.paneKnown(id)
130}
131
132func (f *fakeTmux) PaneIsDead(_ context.Context, id string) bool {
133 f.mu.Lock()
134 defer f.mu.Unlock()
135 return f.paneKnown(id) && !f.runningPanes[id]
136}
137
138func (f *fakeTmux) PaneIsIdle(ctx context.Context, id string) bool {
139 return f.PaneIsDead(ctx, id)
140}
141
142func (f *fakeTmux) CapturePane(context.Context, string, int) (string, error) { return "", nil }
143
144func (f *fakeTmux) KillPane(_ context.Context, id string) error {
145 f.mu.Lock()
146 defer f.mu.Unlock()
147 f.killedPanes = append(f.killedPanes, id)
148 return nil
149}
150
151// testSessionID is the KLAUS_SESSION_ID unit tests pin themselves to. Agent
152// panes export the coordinator's real KLAUS_SESSION_ID, so a test that leaves
153// it alone would compute the live agents session name (issue #295).
154const testSessionID = "test-session-xyz"
155
156// isolatedTmux returns a fake tmux client and pins KLAUS_SESSION_ID so
157// agentsSessionName() can never resolve to a session on the real server.
158func isolatedTmux(t *testing.T) *fakeTmux {
159 t.Helper()
160 t.Setenv(sessionIDEnv, testSessionID)
161 return &fakeTmux{}
162}