forked from mudrii/openclaw-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_cmd_test.go
More file actions
224 lines (216 loc) · 5.64 KB
/
Copy pathservice_cmd_test.go
File metadata and controls
224 lines (216 loc) · 5.64 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
package dashboard
import (
"errors"
"slices"
"testing"
"github.com/mudrii/openclaw-dashboard/internal/appservice"
)
// fakeBackend records which methods were called and with what args.
type fakeBackend struct {
installedWith *appservice.InstallConfig
uninstalled bool
started bool
stopped bool
restarted bool
statusCalled bool
statusResult appservice.ServiceStatus
errInstall error
errUninstall error
errStart error
errStop error
errRestart error
errStatus error
}
func (f *fakeBackend) Install(cfg appservice.InstallConfig) error {
f.installedWith = &cfg
return f.errInstall
}
func (f *fakeBackend) Uninstall() error { f.uninstalled = true; return f.errUninstall }
func (f *fakeBackend) Start() error { f.started = true; return f.errStart }
func (f *fakeBackend) Stop() error { f.stopped = true; return f.errStop }
func (f *fakeBackend) Restart() error { f.restarted = true; return f.errRestart }
func (f *fakeBackend) Status() (appservice.ServiceStatus, error) {
f.statusCalled = true
return f.statusResult, f.errStatus
}
func TestNormaliseCmd(t *testing.T) {
tests := []struct {
name string
args []string
wantCmd string
wantRest []string
}{
{"direct start", []string{"start"}, "start", nil},
{"direct stop", []string{"stop"}, "stop", nil},
{"service start", []string{"service", "start"}, "start", nil},
{"service install with flags", []string{"service", "install", "--port", "9090"}, "install", []string{"--port", "9090"}},
{"bare service", []string{"service"}, "", nil},
{"empty args", []string{}, "", nil},
{"direct status", []string{"status"}, "status", nil},
{"service status", []string{"service", "status"}, "status", nil},
{"flag arg intercepted", []string{"--version"}, "", nil},
{"unknown cmd passes through", []string{"bogus"}, "bogus", nil},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cmd, rest := normaliseCmd(tc.args)
if cmd != tc.wantCmd {
t.Errorf("cmd = %q, want %q", cmd, tc.wantCmd)
}
if !slices.Equal(rest, tc.wantRest) {
t.Errorf("rest = %v, want %v", rest, tc.wantRest)
}
})
}
}
func baseOpts(fb *fakeBackend, args []string) serviceCmdOpts {
return serviceCmdOpts{
dir: "/tmp/dir",
binPath: "/tmp/bin",
version: "v1.0",
backend: fb,
args: args,
defaultBind: "127.0.0.1",
defaultPort: 8080,
}
}
func TestRunServiceCmd(t *testing.T) {
tests := []struct {
name string
cmd string
args []string
setupFb func(*fakeBackend)
wantCode int
checkFb func(*testing.T, *fakeBackend)
}{
{
name: "install forwards port and paths",
cmd: "install",
args: []string{"--port", "9090"},
wantCode: 0,
checkFb: func(t *testing.T, fb *fakeBackend) {
t.Helper()
if fb.installedWith == nil {
t.Fatal("Install not called")
}
if fb.installedWith.Port != 9090 {
t.Errorf("port = %d, want 9090", fb.installedWith.Port)
}
if fb.installedWith.WorkDir != "/tmp/dir" {
t.Errorf("WorkDir = %q, want /tmp/dir", fb.installedWith.WorkDir)
}
if fb.installedWith.BinPath != "/tmp/bin" {
t.Errorf("BinPath = %q, want /tmp/bin", fb.installedWith.BinPath)
}
},
},
{
name: "start calls Start",
cmd: "start",
wantCode: 0,
checkFb: func(t *testing.T, fb *fakeBackend) {
t.Helper()
if !fb.started {
t.Error("Start not called")
}
},
},
{
name: "stop calls Stop",
cmd: "stop",
wantCode: 0,
checkFb: func(t *testing.T, fb *fakeBackend) {
t.Helper()
if !fb.stopped {
t.Error("Stop not called")
}
},
},
{
name: "restart calls Restart",
cmd: "restart",
wantCode: 0,
checkFb: func(t *testing.T, fb *fakeBackend) {
t.Helper()
if !fb.restarted {
t.Error("Restart not called")
}
},
},
{
name: "uninstall calls Uninstall",
cmd: "uninstall",
wantCode: 0,
checkFb: func(t *testing.T, fb *fakeBackend) {
t.Helper()
if !fb.uninstalled {
t.Error("Uninstall not called")
}
},
},
{
name: "status calls Status",
cmd: "status",
setupFb: func(fb *fakeBackend) {
fb.statusResult = appservice.ServiceStatus{Running: true, PID: 999, Port: 8080}
},
wantCode: 0,
checkFb: func(t *testing.T, fb *fakeBackend) {
t.Helper()
if !fb.statusCalled {
t.Error("Status not called")
}
},
},
{
name: "error propagates as exit 1",
cmd: "start",
setupFb: func(fb *fakeBackend) { fb.errStart = errors.New("launchctl failed") },
wantCode: 1,
},
{
name: "invalid flag returns exit 1 without executing command",
cmd: "status",
args: []string{"--port", "nope"},
wantCode: 1,
checkFb: func(t *testing.T, fb *fakeBackend) {
t.Helper()
if fb.statusCalled {
t.Fatal("Status should not be called when flag parsing fails")
}
},
},
{
name: "unexpected positional args return exit 1 without executing command",
cmd: "status",
args: []string{"extra"},
wantCode: 1,
checkFb: func(t *testing.T, fb *fakeBackend) {
t.Helper()
if fb.statusCalled {
t.Fatal("Status should not be called when unexpected args are present")
}
},
},
{
name: "unknown cmd returns exit 1",
cmd: "bogus",
wantCode: 1,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
fb := &fakeBackend{}
if tc.setupFb != nil {
tc.setupFb(fb)
}
code := runServiceCmd(tc.cmd, baseOpts(fb, tc.args))
if code != tc.wantCode {
t.Fatalf("exit code = %d, want %d", code, tc.wantCode)
}
if tc.checkFb != nil {
tc.checkFb(t, fb)
}
})
}
}