-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl_test.go
More file actions
314 lines (285 loc) · 11.1 KB
/
Copy pathrepl_test.go
File metadata and controls
314 lines (285 loc) · 11.1 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
package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
"controlpanel/owlcms"
"controlpanel/shared"
"controlpanel/tracker"
)
func TestParseREPLInvocation(t *testing.T) {
invocation, found, err := parseREPLInvocation([]string{"--instance", "records", "--repl"})
if err != nil {
t.Fatalf("parseREPLInvocation returned error: %v", err)
}
if !found || invocation.batchFile != "" || !invocation.interactive {
t.Fatalf("unexpected invocation: %#v, found=%v", invocation, found)
}
}
func TestParseREPLInvocationRejectsOldSubcommand(t *testing.T) {
_, found, err := parseREPLInvocation([]string{"repl"})
if !found || err == nil || !strings.Contains(err.Error(), "--repl") {
t.Fatalf("expected --repl guidance, found=%v err=%v", found, err)
}
}
func TestSplitREPLCommandSupportsQuotedPaths(t *testing.T) {
args, err := splitREPLCommand(`owlcms install-zip "~/Downloads/competition build.zip" 66.0.0`)
if err != nil {
t.Fatalf("splitREPLCommand returned error: %v", err)
}
want := []string{"owlcms", "install-zip", "~/Downloads/competition build.zip", "66.0.0"}
if strings.Join(args, "|") != strings.Join(want, "|") {
t.Fatalf("expected %#v, got %#v", want, args)
}
}
func TestBuildREPLModuleCommandStartUsesDefaultSelector(t *testing.T) {
cmd, err := buildREPLModuleCommand("owlcms", "start", nil)
if err != nil {
t.Fatalf("buildREPLModuleCommand returned error: %v", err)
}
if cmd.Action != "launch" || !cmd.DaemonMode || cmd.Version != "A" || cmd.Port != "" {
t.Fatalf("unexpected command: %#v", cmd)
}
}
func TestOwlcmsVersionSettingsPersist(t *testing.T) {
installDir := t.TempDir()
versionDir := filepath.Join(installDir, "66.0.0")
if err := os.MkdirAll(versionDir, 0o755); err != nil {
t.Fatalf("create version dir: %v", err)
}
if err := os.WriteFile(filepath.Join(versionDir, "env.properties"), []byte("OWLCMS_PORT=8080\n"), 0o644); err != nil {
t.Fatalf("write version env: %v", err)
}
owlcms.SetInstallDir(installDir)
t.Cleanup(resetInstallDirsForTest)
var output bytes.Buffer
commands := [][]string{
{"A", "port", "18080"},
{"A", "tracker", "on", "wss://tracker.example.org/ws", "18443"},
{"A", "mqtt", "on", "1884"},
}
for _, command := range commands {
if handled, err := executeREPLOwlcmsSetting(command, &output); !handled || err != nil {
t.Fatalf("execute %#v: handled=%v err=%v", command, handled, err)
}
}
content, err := os.ReadFile(filepath.Join(versionDir, "env.properties"))
if err != nil {
t.Fatalf("read version env: %v", err)
}
for _, expected := range []string{
"OWLCMS_PORT = 18080",
"OWLCMS_VIDEODATA = wss://tracker.example.org:443/ws",
"OWLCMS_ENABLEEMBEDDEDMQTT = true",
"OWLCMS_MQTTPORT = 1884",
} {
if !strings.Contains(string(content), expected) {
t.Errorf("expected %q in version env, got %q", expected, string(content))
}
}
for _, command := range [][]string{{"A", "tracker", "off"}, {"A", "mqtt", "off"}} {
if handled, err := executeREPLOwlcmsSetting(command, &output); !handled || err != nil {
t.Fatalf("execute %#v: handled=%v err=%v", command, handled, err)
}
}
content, err = os.ReadFile(filepath.Join(versionDir, "env.properties"))
if err != nil {
t.Fatalf("read disabled version env: %v", err)
}
if !strings.Contains(string(content), "OWLCMS_VIDEODATA = ") || !strings.Contains(string(content), "OWLCMS_ENABLEEMBEDDEDMQTT = false") {
t.Fatalf("expected disabled settings in version env, got %q", string(content))
}
}
func TestOwlcmsTrackerKeyThreeStates(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
installDir := t.TempDir()
if err := os.MkdirAll(filepath.Join(installDir, "66.0.0"), 0o755); err != nil {
t.Fatalf("create version dir: %v", err)
}
owlcms.SetInstallDir(installDir)
t.Cleanup(resetInstallDirsForTest)
var output bytes.Buffer
run := func(value string) {
command := []string{"A", "tracker", "key", value}
if handled, err := executeREPLOwlcmsSetting(command, &output); !handled || err != nil {
t.Fatalf("execute %#v: handled=%v err=%v", command, handled, err)
}
}
run("s3cret")
if key, hasOwn, err := owlcms.GetOwnTrackerConnectionKeyForRelease("66.0.0"); err != nil || !hasOwn || key != "s3cret" {
t.Fatalf("own key: key=%q hasOwn=%v err=%v", key, hasOwn, err)
}
run("off")
if key, hasOwn, err := owlcms.GetOwnTrackerConnectionKeyForRelease("66.0.0"); err != nil || !hasOwn || key != "" {
t.Fatalf("off: key=%q hasOwn=%v err=%v", key, hasOwn, err)
}
run("default")
if _, hasOwn, err := owlcms.GetOwnTrackerConnectionKeyForRelease("66.0.0"); err != nil || hasOwn {
t.Fatalf("default: hasOwn=%v err=%v", hasOwn, err)
}
}
func TestREPLDefaultSettings(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
owlcms.SetInstallDir(t.TempDir())
tracker.SetInstallDir(t.TempDir())
t.Cleanup(resetInstallDirsForTest)
var output bytes.Buffer
owlcmsCommands := [][]string{
{"default", "tracker", "on", "wss://tracker.example.org/ws", "18443"},
{"default", "tracker", "key", "s3cret"},
}
for _, command := range owlcmsCommands {
if handled, err := executeREPLOwlcmsSetting(command, &output); !handled || err != nil {
t.Fatalf("execute %#v: handled=%v err=%v", command, handled, err)
}
}
if url, port, enabled := owlcms.GetTrackerConnectionSettings(); url != "wss://tracker.example.org/ws" || port != "443" || !enabled {
t.Fatalf("default connection = %q %q %v", url, port, enabled)
}
if key, _ := owlcms.GetDefaultTrackerConnectionKey(); key != "s3cret" {
t.Fatalf("default owlcms key = %q", key)
}
if handled, err := executeREPLOwlcmsSetting([]string{"default", "tracker", "off"}, &output); !handled || err != nil {
t.Fatalf("default off: handled=%v err=%v", handled, err)
}
if url, _, enabled := owlcms.GetTrackerConnectionSettings(); url != "wss://tracker.example.org/ws" || enabled {
t.Fatalf("off must keep URL and disable: %q %v", url, enabled)
}
for _, command := range [][]string{{"default", "key", "fallback"}, {"default", "new-versions", "off"}} {
if handled, err := executeREPLTrackerSetting(command, &output); !handled || err != nil {
t.Fatalf("execute %#v: handled=%v err=%v", command, handled, err)
}
}
if key, _ := tracker.GetDefaultSharedKey(); key != "fallback" {
t.Fatalf("default tracker key = %q", key)
}
if tracker.GetDefaultSharedKeyEnabled() {
t.Fatalf("new-versions off must disable copying the default key")
}
}
func TestBuildREPLModuleCommandDuplicateUsesSelector(t *testing.T) {
cmd, err := buildREPLModuleCommand("tracker", "duplicate", []string{"B", "practice"})
if err != nil {
t.Fatalf("buildREPLModuleCommand returned error: %v", err)
}
if cmd.Action != "duplicate" || cmd.FromVersion != "B" || cmd.DuplicateName != "practice" {
t.Fatalf("unexpected command: %#v", cmd)
}
}
func TestBuildREPLModuleCommandUpdateSelectorDefaultsReleaseToLatest(t *testing.T) {
cmd, err := buildREPLModuleCommand("owlcms", "update", []string{"A"})
if err != nil {
t.Fatalf("buildREPLModuleCommand returned error: %v", err)
}
if cmd.Action != "update" || cmd.Version != "A" || cmd.UpdateTo != "latest" {
t.Fatalf("unexpected command: %#v", cmd)
}
}
func TestREPLPromptUsesStartupSelectedInstance(t *testing.T) {
t.Setenv("CONTROLPANEL_INSTANCE", "records")
if got := (replSession{}).prompt(); got != "controlpanel[records]> " {
t.Fatalf("expected records prompt, got %q", got)
}
}
func TestREPLCommandChangesVersions(t *testing.T) {
for _, action := range []string{"install", "install-zip", "update", "rename", "duplicate", "remove"} {
if !replCommandChangesVersions(action) {
t.Errorf("expected %q to refresh the version list", action)
}
}
for _, action := range []string{"list", "launch", "stop", "status", "create-zip", "import"} {
if replCommandChangesVersions(action) {
t.Errorf("expected %q not to refresh the version list", action)
}
}
}
func TestREPLSelectorLabelsUseSpreadsheetOrder(t *testing.T) {
for index, want := range []string{"A", "Z", "AA", "AB"} {
actualIndex := []int{0, 25, 26, 27}[index]
if got := replVersionSelector(actualIndex); got != want {
t.Errorf("selector %d: expected %q, got %q", actualIndex, want, got)
}
if got, ok := replSelectorIndex(want); !ok || got != actualIndex {
t.Errorf("index %q: expected %d, got %d (ok=%v)", want, actualIndex, got, ok)
}
}
}
func TestBatchStopsAtFirstCommandError(t *testing.T) {
var output bytes.Buffer
err := runREPL(replInvocation{batchFile: "-"}, replSession{}, strings.NewReader("# setup\nunknown\ncontext\n"), &output, &output)
if err == nil || !strings.Contains(err.Error(), "batch line 2") {
t.Fatalf("expected line 2 batch error, got %v", err)
}
}
func TestInteractiveREPLPrintsPromptBeforeCommands(t *testing.T) {
var output bytes.Buffer
err := runREPL(replInvocation{interactive: true}, replSession{}, strings.NewReader("help\nexit\n"), &output, &output)
if err != nil {
t.Fatalf("runREPL returned error: %v", err)
}
if !strings.HasPrefix(output.String(), "controlpanel> ") {
t.Fatalf("expected output to start with prompt, got %q", output.String())
}
}
func TestREPLHelpPointsToModuleHelp(t *testing.T) {
var output bytes.Buffer
_, err := executeREPLLine("help", &replSession{}, &output)
if err != nil {
t.Fatalf("help returned error: %v", err)
}
if !strings.Contains(output.String(), "owlcms help") || !strings.Contains(output.String(), "tracker help") {
t.Fatalf("expected module help directions, got %q", output.String())
}
}
func TestREPLSecretEncrypt(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
var output bytes.Buffer
if _, err := executeREPLLine("secret encrypt s3cret", &replSession{}, &output); err != nil {
t.Fatalf("secret encrypt returned error: %v", err)
}
encrypted := strings.TrimSpace(output.String())
if !strings.HasPrefix(encrypted, "enc:v1:") || strings.Contains(encrypted, "s3cret") {
t.Fatalf("unexpected encrypted value %q", encrypted)
}
if plain, err := shared.DecryptSecret(encrypted); err != nil || plain != "s3cret" {
t.Fatalf("decrypt = %q, %v", plain, err)
}
for _, command := range []string{"secret encrypt", "secret encrypt ''", "secret decrypt x"} {
if _, err := executeREPLLine(command, &replSession{}, &output); err == nil {
t.Fatalf("%q must be rejected", command)
}
}
}
func TestREPLHelpRejectsCommandArgument(t *testing.T) {
var output bytes.Buffer
_, err := executeREPLLine("help tracker", &replSession{}, &output)
if err == nil || !strings.Contains(err.Error(), "tracker help") {
t.Fatalf("expected tracker help guidance, got %v", err)
}
}
func TestREPLModuleHelpIsSeparated(t *testing.T) {
for _, test := range []struct {
command string
want string
doNotWant string
}{
{command: "owlcms help", want: "OWLCMS commands:", doNotWant: "Tracker commands:"},
{command: "tracker help", want: "Tracker commands:", doNotWant: "OWLCMS commands:"},
} {
var output bytes.Buffer
if _, err := executeREPLLine(test.command, &replSession{}, &output); err != nil {
t.Fatalf("%s returned error: %v", test.command, err)
}
if !strings.Contains(output.String(), test.want) || strings.Contains(output.String(), test.doNotWant) {
t.Fatalf("unexpected %s output: %q", test.command, output.String())
}
}
}