-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
339 lines (316 loc) · 8.91 KB
/
Copy pathmain_test.go
File metadata and controls
339 lines (316 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"bytes"
"context"
"io"
"os"
"path/filepath"
"strings"
"testing"
tea "github.com/charmbracelet/bubbletea"
"github.com/RandomCodeSpace/unified-agent-manager/internal/cli"
"github.com/RandomCodeSpace/unified-agent-manager/internal/session"
"github.com/RandomCodeSpace/unified-agent-manager/internal/store"
"github.com/RandomCodeSpace/unified-agent-manager/internal/version"
)
// TestMain doubles as the session host/attach entry point: the native backend
// spawns os.Executable() with the internal __host/__attach subcommands, and
// under `go test` that executable is this test binary. Routing those argv
// shapes into the real CLI makes the dispatch/peek/stop tests below exercise
// the actual session backend end to end.
func TestMain(m *testing.M) {
if len(os.Args) > 1 && (os.Args[1] == "__host" || os.Args[1] == "__attach") {
cli.Main()
os.Exit(0)
}
os.Exit(m.Run())
}
func TestRunHelpAndList(t *testing.T) {
dir := setupFakeCLIConfig(t, "cfg")
if out := runStderr(t, []string{"help"}); !strings.Contains(out, "uam") {
t.Fatalf("help=%q", out)
}
if out := runStdout(t, []string{"ls", "--json"}); !strings.Contains(out, "[") {
t.Fatalf("ls=%q", out)
}
_ = dir
}
func TestRunDispatchVariants(t *testing.T) {
setupFakeCLIConfig(t, "cfg-dispatch")
assertNonEmptyRunOutput(t, []string{"dispatch", "claude", "hello world"}, "empty dispatch id")
assertNonEmptyRunOutput(t, []string{"dispatch", "claude"}, "empty no-prompt dispatch id")
assertNonEmptyRunOutput(t, []string{"dispatch", "claude", "#bugfix", "fix", "thing"}, "empty named dispatch id")
if text := runStdout(t, []string{"ls"}); !strings.Contains(text, "bugfix") {
t.Fatalf("named session missing from list: %q", text)
}
}
func TestRunArgumentErrors(t *testing.T) {
setupFakeCLIConfig(t, "cfg-errors")
cases := []struct {
args []string
msg string
}{
{[]string{"unknown"}, "want unknown error"},
{[]string{"peek"}, "want peek arg error"},
{[]string{"stop"}, "want stop arg error"},
{[]string{"rm"}, "want rm arg error"},
{[]string{"attach"}, "want attach arg error"},
}
for _, tc := range cases {
if err := run(context.Background(), tc.args); err == nil {
t.Fatal(tc.msg)
}
}
}
func setupFakeCLIConfig(t *testing.T, name string) string {
t.Helper()
dir := setupFakeCLIEnv(t)
t.Setenv("UAM_CONFIG_DIR", filepath.Join(dir, name))
return dir
}
func runStdout(t *testing.T, args []string) string {
t.Helper()
return captureStdout(t, func() {
if err := run(context.Background(), args); err != nil {
t.Fatal(err)
}
})
}
func runStderr(t *testing.T, args []string) string {
t.Helper()
return captureStderr(t, func() {
if err := run(context.Background(), args); err != nil {
t.Fatal(err)
}
})
}
func assertNonEmptyRunOutput(t *testing.T, args []string, message string) {
t.Helper()
if strings.TrimSpace(runStdout(t, args)) == "" {
t.Fatal(message)
}
}
func TestRunNew(t *testing.T) {
dir := setupFakeCLIEnv(t)
t.Setenv("UAM_CONFIG_DIR", filepath.Join(dir, "cfg2"))
oldRunTUI := runTUIFn
defer func() { runTUIFn = oldRunTUI }()
returnedToTUI := false
runTUIFn = func(ctx context.Context, model tea.Model) error {
returnedToTUI = true
return nil
}
old := os.Stdin
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
_, _ = w.WriteString("claude\n\n/tmp\nfrom wizard\n")
_ = w.Close()
os.Stdin = r
defer func() { os.Stdin = old }()
captureStdout(t, func() {
if err := run(context.Background(), []string{"new"}); err != nil {
t.Fatal(err)
}
})
if !returnedToTUI {
t.Fatal("new should attach and then return to UAM TUI")
}
}
// F54 — `uam new` keeps the prompt optional: a blank final prompt dispatches a
// prompt-less session and exits 0.
func TestRunNewAllowsEmptyPrompt(t *testing.T) {
dir := setupFakeCLIEnv(t)
cfgDir := filepath.Join(dir, "cfg-empty-prompt")
t.Setenv("UAM_CONFIG_DIR", cfgDir)
oldRunTUI := runTUIFn
defer func() { runTUIFn = oldRunTUI }()
returnedToTUI := false
runTUIFn = func(ctx context.Context, model tea.Model) error {
returnedToTUI = true
return nil
}
old := os.Stdin
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
_, _ = w.WriteString("claude\n\n/tmp\n\n")
_ = w.Close()
os.Stdin = r
defer func() { os.Stdin = old }()
captureStdout(t, func() {
if err := run(context.Background(), []string{"new"}); err != nil {
t.Fatal(err)
}
})
if !returnedToTUI {
t.Fatal("new should attach and then return to UAM TUI")
}
st, err := store.Open(filepath.Join(cfgDir, "sessions.json"))
if err != nil {
t.Fatal(err)
}
cfg, err := st.Load()
if err != nil {
t.Fatal(err)
}
if len(cfg.Sessions) != 1 {
t.Fatalf("sessions = %d, want 1", len(cfg.Sessions))
}
for _, rec := range cfg.Sessions {
if rec.Prompt != "" {
t.Fatalf("prompt = %q, want empty", rec.Prompt)
}
}
}
func TestRunMoreCLIPaths(t *testing.T) {
dir := setupFakeCLIEnv(t)
t.Setenv("UAM_CONFIG_DIR", filepath.Join(dir, "cfgmore"))
out := captureStdout(t, func() {
if err := run(context.Background(), []string{"dispatch", "--safe", "--cwd", "/tmp", "claude", "safe prompt"}); err != nil {
t.Fatal(err)
}
})
id := strings.TrimSpace(out)
if id == "" {
t.Fatal("empty id")
}
if text := captureStdout(t, func() {
if err := run(context.Background(), []string{"ls"}); err != nil {
t.Fatal(err)
}
}); !strings.Contains(text, "claude") {
t.Fatalf("ls text=%q", text)
}
if text := captureStdout(t, func() {
if err := run(context.Background(), []string{"peek", id}); err != nil {
t.Fatal(err)
}
}); !strings.Contains(text, "pane") {
t.Fatalf("peek=%q", text)
}
if err := run(context.Background(), []string{"stop", id}); err != nil {
t.Fatal(err)
}
if err := run(context.Background(), []string{"rm", id}); err != nil {
t.Fatal(err)
}
if err := run(context.Background(), []string{"last"}); err == nil {
t.Fatal("want no sessions after rm")
}
if err := runDispatch(context.Background(), newService(mustTestStore(t, dir)), []string{"--bad"}); err == nil {
t.Fatal("want bad flag error")
}
}
func TestAttachReturnsToTUI(t *testing.T) {
dir := setupFakeCLIEnv(t)
t.Setenv("UAM_CONFIG_DIR", filepath.Join(dir, "cfg-attach"))
out := captureStdout(t, func() {
if err := run(context.Background(), []string{"dispatch", "claude", "attach me"}); err != nil {
t.Fatal(err)
}
})
id := strings.TrimSpace(out)
if id == "" {
t.Fatal("empty id")
}
oldRunTUI := runTUIFn
defer func() { runTUIFn = oldRunTUI }()
returnedToTUI := false
runTUIFn = func(ctx context.Context, model tea.Model) error {
returnedToTUI = true
return nil
}
if err := run(context.Background(), []string{"attach", id}); err != nil {
t.Fatal(err)
}
if !returnedToTUI {
t.Fatal("attach should return to UAM TUI after the session exits")
}
}
func mustTestStore(t *testing.T, dir string) *store.Store {
t.Helper()
st, err := store.Open(filepath.Join(dir, "explicit", "sessions.json"))
if err != nil {
t.Fatal(err)
}
return st
}
func TestUsageAndNewService(t *testing.T) {
oldVersion := version.Override
version.Override = "v9.9.9"
t.Cleanup(func() { version.Override = oldVersion })
dir := setupFakeCLIEnv(t)
t.Setenv("UAM_CONFIG_DIR", filepath.Join(dir, "cfg3"))
out := captureStderr(t, usage)
if !strings.Contains(out, "dispatch") {
t.Fatalf("usage=%q", out)
}
if out := captureStdout(t, func() {
if err := run(context.Background(), []string{"version"}); err != nil {
t.Fatal(err)
}
}); !strings.Contains(out, "v9.9.9") {
t.Fatalf("version=%q", out)
}
st, err := store.Open(filepath.Join(dir, "direct", "sessions.json"))
if err != nil {
t.Fatal(err)
}
svc := newService(st)
if svc == nil || svc.Registry == nil {
t.Fatal("nil svc")
}
}
func setupFakeCLIEnv(t *testing.T) string {
t.Helper()
dir := t.TempDir()
// A long-lived fake agent: prints a recognizable line for peek asserts and
// then idles so the session stays alive for list/stop/attach paths.
writeFileMode(t, filepath.Join(dir, "claude"), "#!/bin/sh\necho pane\nexec sleep 60\n", 0o755)
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
t.Setenv("UAM_CACHE_DIR", filepath.Join(dir, "cache"))
sessDir := filepath.Join(dir, "runtime")
t.Setenv("UAM_SESSION_DIR", sessDir)
t.Cleanup(func() {
c := session.NewClient()
c.Dir = sessDir
_ = c.KillAll(context.Background())
})
return dir
}
func writeFileMode(t *testing.T, path, content string, mode os.FileMode) {
t.Helper()
if err := os.WriteFile(path, []byte(content), mode); err != nil {
t.Fatal(err)
}
}
func captureStdout(t *testing.T, fn func()) string {
t.Helper()
return captureFile(t, &os.Stdout, fn)
}
func captureStderr(t *testing.T, fn func()) string {
t.Helper()
return captureFile(t, &os.Stderr, fn)
}
func captureFile(t *testing.T, target **os.File, fn func()) string {
t.Helper()
old := *target
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
*target = w
defer func() {
_ = w.Close()
*target = old
}()
fn()
_ = w.Close()
*target = old
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
return buf.String()
}