-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_test.go
More file actions
210 lines (180 loc) · 5.93 KB
/
Copy pathcommand_test.go
File metadata and controls
210 lines (180 loc) · 5.93 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
package core_test
import (
. "dappco.re/go"
)
// --- Command DTO ---
func TestCommand_Run_Good(t *T) {
c := New()
c.Command("greet", Command{Action: func(opts Options) Result {
return Result{Value: Concat("hello ", opts.String("name")), OK: true}
}})
cmd := c.Command("greet").Value.(*Command)
r := cmd.Run(NewOptions(Option{Key: "name", Value: "world"}))
AssertTrue(t, r.OK)
AssertEqual(t, "hello world", r.Value)
}
func TestCommand_Run_NoAction_Good(t *T) {
c := New()
c.Command("empty", Command{Description: "no action"})
cmd := c.Command("empty").Value.(*Command)
r := cmd.Run(NewOptions())
AssertFalse(t, r.OK)
}
// --- I18n Key Derivation ---
func TestCommand_I18nKey_Good(t *T) {
c := New()
c.Command("deploy/to/homelab", Command{})
cmd := c.Command("deploy/to/homelab").Value.(*Command)
AssertEqual(t, "cmd.deploy.to.homelab.description", cmd.I18nKey())
}
func TestCommand_I18nKey_Custom_Good(t *T) {
c := New()
c.Command("deploy", Command{Description: "custom.deploy.key"})
cmd := c.Command("deploy").Value.(*Command)
AssertEqual(t, "custom.deploy.key", cmd.I18nKey())
}
func TestCommand_I18nKey_Simple_Good(t *T) {
c := New()
c.Command("serve", Command{})
cmd := c.Command("serve").Value.(*Command)
AssertEqual(t, "cmd.serve.description", cmd.I18nKey())
}
// --- Managed ---
func TestCommand_IsManaged_Good(t *T) {
c := New()
c.Command("serve", Command{
Action: func(_ Options) Result { return Result{Value: "running", OK: true} },
Managed: "process.daemon",
})
cmd := c.Command("serve").Value.(*Command)
AssertTrue(t, cmd.IsManaged())
}
func TestCommand_IsManaged_Bad_NotManaged(t *T) {
c := New()
c.Command("deploy", Command{
Action: func(_ Options) Result { return Result{OK: true} },
})
cmd := c.Command("deploy").Value.(*Command)
AssertFalse(t, cmd.IsManaged())
}
// --- Cli Run with Managed ---
func TestCli_Run_Managed_Good(t *T) {
c := New(WithCli())
ran := false
c.Command("serve", Command{
Action: func(_ Options) Result { ran = true; return Result{OK: true} },
Managed: "process.daemon",
})
r := c.Cli().Run("serve")
AssertTrue(t, r.OK)
AssertTrue(t, ran)
}
func TestCli_Run_NoAction_Bad(t *T) {
c := New(WithCli())
c.Command("empty", Command{})
r := c.Cli().Run("empty")
AssertFalse(t, r.OK)
}
// --- AX-7 canonical triplets ---
func TestCommand_Command_I18nKey_Good(t *T) {
cmd := &Command{Path: "deploy/to/homelab"}
AssertEqual(t, "cmd.deploy.to.homelab.description", cmd.I18nKey())
}
func TestCommand_Command_I18nKey_Bad(t *T) {
cmd := &Command{Description: "cmd.custom.description", Path: "deploy"}
AssertEqual(t, "cmd.custom.description", cmd.I18nKey())
}
func TestCommand_Command_I18nKey_Ugly(t *T) {
cmd := &Command{}
AssertEqual(t, "cmd..description", cmd.I18nKey())
}
func TestCommand_Command_Run_Good(t *T) {
cmd := &Command{Path: "agent/run", Action: func(opts Options) Result {
return Result{Value: opts.String("agent"), OK: true}
}}
r := cmd.Run(NewOptions(Option{Key: "agent", Value: "codex"}))
AssertTrue(t, r.OK)
AssertEqual(t, "codex", r.Value)
}
func TestCommand_Command_Run_Bad(t *T) {
cmd := &Command{Path: "agent/run"}
r := cmd.Run(NewOptions())
AssertFalse(t, r.OK)
AssertContains(t, r.Error(), "not executable")
}
func TestCommand_Command_Run_Ugly(t *T) {
cmd := &Command{Path: "agent/run", Action: func(opts Options) Result {
return Result{Value: opts.Len(), OK: true}
}}
r := cmd.Run(NewOptions())
AssertTrue(t, r.OK)
AssertEqual(t, 0, r.Value)
}
func TestCommand_Command_IsManaged_Good(t *T) {
cmd := &Command{Managed: "process.daemon"}
AssertTrue(t, cmd.IsManaged())
}
func TestCommand_Command_IsManaged_Bad(t *T) {
cmd := &Command{}
AssertFalse(t, cmd.IsManaged())
}
func TestCommand_Command_IsManaged_Ugly(t *T) {
cmd := &Command{Managed: " "}
AssertTrue(t, cmd.IsManaged())
}
func TestCommand_Core_Command_Good(t *T) {
c := New()
r := c.Command("deploy/to/homelab", Command{Action: func(_ Options) Result {
return Result{Value: "deployed", OK: true}
}})
AssertTrue(t, r.OK)
AssertTrue(t, c.Command("deploy/to/homelab").OK)
// Parent paths are auto-created and retrievable.
AssertTrue(t, c.Command("deploy").OK)
AssertTrue(t, c.Command("deploy/to").OK)
}
func TestCommand_Core_Command_Bad(t *T) {
c := New()
// Malformed paths are rejected.
AssertFalse(t, c.Command("/deploy", Command{}).OK) // leading slash
AssertFalse(t, c.Command("trailing/", Command{}).OK) // trailing slash
AssertFalse(t, c.Command("deploy//homelab", Command{}).OK) // double slash
AssertFalse(t, c.Command("", Command{}).OK) // empty path
// Duplicate registration of the same real (action-bearing) path is rejected.
dup := Command{Action: func(_ Options) Result { return Result{OK: true} }}
AssertTrue(t, c.Command("once", dup).OK)
AssertFalse(t, c.Command("once", dup).OK)
// Retrieving an unregistered path misses.
AssertFalse(t, c.Command("never/registered").OK)
}
func TestCommand_Core_Command_Ugly(t *T) {
c := New()
c.Command("deploy/to/homelab", Command{})
r := c.Command("deploy", Command{Action: func(_ Options) Result {
return Result{Value: "parent", OK: true}
}})
AssertTrue(t, r.OK)
parent := c.Command("deploy").Value.(*Command)
AssertNotNil(t, parent)
AssertNotEmpty(t, parent.I18nKey())
}
func TestCommand_Core_Commands_Good(t *T) {
c := New()
c.Command("agent/prepare", Command{Action: func(_ Options) Result { return Result{OK: true} }})
c.Command("agent/dispatch", Command{Action: func(_ Options) Result { return Result{OK: true} }})
commands := c.Commands()
AssertContains(t, commands, "agent/prepare")
AssertContains(t, commands, "agent/dispatch")
}
func TestCommand_Core_Commands_Bad(t *T) {
c := New()
AssertEmpty(t, c.Commands())
}
func TestCommand_Core_Commands_Ugly(t *T) {
c := New()
c.Command("agent/prepare", Command{Action: func(_ Options) Result { return Result{OK: true} }})
commands := c.Commands()
commands[0] = "mutated"
AssertContains(t, c.Commands(), "agent/prepare")
AssertNotContains(t, c.Commands(), "mutated")
}