-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_test.go
More file actions
292 lines (251 loc) · 7.05 KB
/
cli_test.go
File metadata and controls
292 lines (251 loc) · 7.05 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
package core_test
import (
. "dappco.re/go"
)
// fakeProcess registers an in-test process.run handler that echoes a
// composed string so AssertCLI scenarios stay self-contained without a
// real process service. Returns the registered Core.
func fakeProcess(t *T, response string, ok bool) *Core {
t.Helper()
c := New()
c.Action("process.run", func(ctx Context, opts Options) Result {
if !ok {
return Result{Value: NewCode("test.process.bad", response), OK: false}
}
return Result{Value: response, OK: true}
})
return c
}
// --- AssertCLI ---
func TestCLI_AssertCLI_Good(t *T) {
c := fakeProcess(t, "go version go1.26.0\n", true)
AssertCLI(t, c, CLITest{
Cmd: "go",
Args: []string{"version"},
WantOK: true,
Output: "go version go1.26.0\n",
})
}
func TestCLI_AssertCLI_Contains_Good(t *T) {
c := fakeProcess(t, "go version go1.26.0 darwin/arm64\n", true)
AssertCLI(t, c, CLITest{
Cmd: "go",
Args: []string{"version"},
WantOK: true,
Contains: "go1.26",
})
}
func TestCLI_AssertCLI_WantOK_False_Good(t *T) {
c := fakeProcess(t, "process not registered", false)
AssertCLI(t, c, CLITest{
Cmd: "missing-binary",
WantOK: false,
})
}
func TestCLI_AssertCLI_Dir_Good(t *T) {
c := fakeProcess(t, "ok\n", true)
AssertCLI(t, c, CLITest{
Name: "run-in-tempdir",
Cmd: "true",
Dir: t.TempDir(),
WantOK: true,
Output: "ok\n",
})
}
func TestCLI_AssertCLI_Env_Good(t *T) {
c := fakeProcess(t, "ok\n", true)
AssertCLI(t, c, CLITest{
Cmd: "true",
Dir: t.TempDir(),
Env: []string{"GOWORK=off"},
WantOK: true,
Output: "ok\n",
})
}
// --- AssertCLIs ---
func TestCLI_AssertCLIs_Good(t *T) {
c := fakeProcess(t, "fixture-output\n", true)
AssertCLIs(t, c, []CLITest{
{Name: "first", Cmd: "go", Args: []string{"version"}, WantOK: true, Contains: "fixture"},
{Name: "second", Cmd: "go", Args: []string{"env"}, WantOK: true, Output: "fixture-output\n"},
})
}
// --- AX-7 canonical triplets ---
func TestCli_CliRegister_Good(t *T) {
c := New()
r := c.Service("cli")
AssertTrue(t, r.OK)
AssertNotNil(t, c.Cli())
}
func TestCli_CliRegister_Bad(t *T) {
c := New()
r := CliRegister(c)
AssertFalse(t, r.OK)
}
func TestCli_CliRegister_Ugly(t *T) {
AssertPanics(t, func() {
CliRegister(nil)
})
}
func TestCli_Cli_Print_Good(t *T) {
c := New()
buf := NewBuffer()
c.Cli().SetOutput(buf)
c.Cli().Print("agent %s ready", "codex")
AssertEqual(t, "agent codex ready\n", buf.String())
}
func TestCli_Cli_Print_Bad(t *T) {
c := New()
buf := NewBuffer()
c.Cli().SetOutput(buf)
c.Cli().Print("agent %s ready")
AssertContains(t, buf.String(), "%!s(MISSING)")
}
func TestCli_Cli_Print_Ugly(t *T) {
c := New()
buf := NewBuffer()
c.Cli().SetOutput(buf)
c.Cli().Print("")
AssertEqual(t, "\n", buf.String())
}
func TestCli_Cli_SetOutput_Good(t *T) {
c := New()
buf := NewBuffer()
c.Cli().SetOutput(buf)
c.Cli().Print("homelab")
AssertEqual(t, "homelab\n", buf.String())
}
func TestCli_Cli_SetOutput_Bad(t *T) {
c := New()
buf := NewBuffer()
c.Cli().SetOutput(buf)
c.Cli().SetOutput(buf)
c.Cli().Print("agent")
AssertEqual(t, "agent\n", buf.String())
}
func TestCli_Cli_SetOutput_Ugly(t *T) {
c := New()
first := NewBuffer()
second := NewBuffer()
c.Cli().SetOutput(first)
c.Cli().Print("first")
c.Cli().SetOutput(second)
c.Cli().Print("second")
AssertEqual(t, "first\n", first.String())
AssertEqual(t, "second\n", second.String())
}
func TestCli_Cli_Run_Good(t *T) {
c := New()
var target string
c.Command("deploy/to/homelab", Command{Action: func(opts Options) Result {
target = opts.String("target")
return Result{Value: "deployed", OK: true}
}})
r := c.Cli().Run("deploy", "to", "homelab", "--target=lethean")
AssertTrue(t, r.OK)
AssertEqual(t, "lethean", target)
}
func TestCli_Cli_Run_Bad(t *T) {
c := New()
c.Command("agent/status", Command{})
r := c.Cli().Run("agent", "status")
AssertFalse(t, r.OK)
}
func TestCli_Cli_Run_Ugly(t *T) {
c := New(WithOption("name", "homelab"))
buf := NewBuffer()
c.Cli().SetOutput(buf)
c.Cli().SetBanner(func(_ *Cli) string { return "homelab ops" })
r := c.Cli().Run("missing")
AssertFalse(t, r.OK)
AssertContains(t, buf.String(), "homelab ops")
}
func TestCli_Cli_RunMissingCommand_Bad(t *T) {
c := New(WithOption("name", "homelab"))
buf := NewBuffer()
c.Cli().SetOutput(buf)
c.Cli().SetBanner(func(_ *Cli) string { return "homelab ops" })
c.Command("system/status", Command{Action: func(_ Options) Result { return Result{OK: true} }})
r := c.Cli().Run("agent", "missing")
AssertFalse(t, r.OK)
AssertContains(t, buf.String(), "homelab ops")
AssertContains(t, buf.String(), "system/status")
}
func TestCli_Cli_RunFlagForms_Ugly(t *T) {
c := New()
var dryRun bool
var name string
c.Command("agent/run", Command{Action: func(opts Options) Result {
dryRun = opts.Bool("dry-run")
name = opts.String("_arg")
return Result{OK: true}
}})
r := c.Cli().Run("agent", "run", "--dry-run", "codex")
AssertTrue(t, r.OK)
AssertTrue(t, dryRun)
AssertEqual(t, "codex", name)
}
func TestCli_Cli_PrintHelp_Good(t *T) {
c := New(WithOption("name", "homelab"))
buf := NewBuffer()
c.Cli().SetOutput(buf)
c.Command("agent/status", Command{Action: func(_ Options) Result { return Result{OK: true} }})
c.Cli().PrintHelp()
AssertContains(t, buf.String(), "homelab commands:")
AssertContains(t, buf.String(), "agent/status")
}
func TestCli_Cli_PrintHelp_Bad(t *T) {
c := New()
buf := NewBuffer()
c.Cli().SetOutput(buf)
c.Command("agent/hidden", Command{Hidden: true, Action: func(_ Options) Result { return Result{OK: true} }})
c.Cli().PrintHelp()
AssertNotContains(t, buf.String(), "agent/hidden")
}
func TestCli_Cli_PrintHelp_Ugly(t *T) {
c := New()
buf := NewBuffer()
c.Cli().SetOutput(buf)
c.Cli().PrintHelp()
AssertEqual(t, "Commands:\n", buf.String())
}
func TestCli_Cli_PrintHelpTranslated_Good(t *T) {
c := New()
buf := NewBuffer()
c.Cli().SetOutput(buf)
c.I18n().SetTranslator(&mockTranslator{})
c.Command("agent/status", Command{Action: func(_ Options) Result { return Result{OK: true} }})
c.Cli().PrintHelp()
AssertContains(t, buf.String(), "agent/status")
AssertContains(t, buf.String(), "translated:cmd.agent.status.description")
}
func TestCli_Cli_SetBanner_Good(t *T) {
c := New()
c.Cli().SetBanner(func(_ *Cli) string { return "dAppCore agent" })
AssertEqual(t, "dAppCore agent", c.Cli().Banner())
}
func TestCli_Cli_SetBanner_Bad(t *T) {
c := New(WithOption("name", "homelab"))
c.Cli().SetBanner(nil)
AssertEqual(t, "homelab", c.Cli().Banner())
}
func TestCli_Cli_SetBanner_Ugly(t *T) {
c := New(WithOption("name", "homelab"))
c.Cli().SetBanner(func(cl *Cli) string {
return Concat(cl.Core().App().Name, " banner")
})
AssertEqual(t, "homelab banner", c.Cli().Banner())
}
func TestCli_Cli_Banner_Good(t *T) {
c := New()
c.Cli().SetBanner(func(_ *Cli) string { return "agent dispatch" })
AssertEqual(t, "agent dispatch", c.Cli().Banner())
}
func TestCli_Cli_Banner_Bad(t *T) {
c := New(WithOption("name", "homelab"))
AssertEqual(t, "homelab", c.Cli().Banner())
}
func TestCli_Cli_Banner_Ugly(t *T) {
c := New()
AssertEqual(t, "", c.Cli().Banner())
}