-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathstdin_test.go
More file actions
454 lines (376 loc) · 11.1 KB
/
stdin_test.go
File metadata and controls
454 lines (376 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package main
import (
"bytes"
"encoding/json"
"math"
"strings"
"testing"
)
// resetGlobals resets global state before each test
func resetGlobals() {
pausedMu.Lock()
paused = false
pausedMu.Unlock()
minAmplitude = 0.05
cooldownMs = 750
stdioMode = true
volumeScaling = false
}
func TestPauseCommand(t *testing.T) {
resetGlobals()
input := `{"cmd":"pause"}` + "\n"
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
// Check state changed
pausedMu.RLock()
if !paused {
t.Error("expected paused to be true after pause command")
}
pausedMu.RUnlock()
// Check output
var resp map[string]string
if err := json.Unmarshal(output.Bytes(), &resp); err != nil {
t.Fatalf("failed to parse response: %v", err)
}
if resp["status"] != "paused" {
t.Errorf("expected status 'paused', got %q", resp["status"])
}
}
func TestResumeCommand(t *testing.T) {
resetGlobals()
// First pause
pausedMu.Lock()
paused = true
pausedMu.Unlock()
input := `{"cmd":"resume"}` + "\n"
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
// Check state changed
pausedMu.RLock()
if paused {
t.Error("expected paused to be false after resume command")
}
pausedMu.RUnlock()
// Check output
var resp map[string]string
if err := json.Unmarshal(output.Bytes(), &resp); err != nil {
t.Fatalf("failed to parse response: %v", err)
}
if resp["status"] != "resumed" {
t.Errorf("expected status 'resumed', got %q", resp["status"])
}
}
func TestSetAmplitudeCommand(t *testing.T) {
resetGlobals()
input := `{"cmd":"set","amplitude":0.15}` + "\n"
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
// Check state changed
if minAmplitude != 0.15 {
t.Errorf("expected minAmplitude 0.15, got %f", minAmplitude)
}
// Check output
var resp struct {
Status string `json:"status"`
Amplitude float64 `json:"amplitude"`
Cooldown int `json:"cooldown"`
}
if err := json.Unmarshal(output.Bytes(), &resp); err != nil {
t.Fatalf("failed to parse response: %v", err)
}
if resp.Status != "settings_updated" {
t.Errorf("expected status 'settings_updated', got %q", resp.Status)
}
if resp.Amplitude != 0.15 {
t.Errorf("expected amplitude 0.15 in response, got %f", resp.Amplitude)
}
}
func TestSetCooldownCommand(t *testing.T) {
resetGlobals()
input := `{"cmd":"set","cooldown":500}` + "\n"
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
// Check state changed
if cooldownMs != 500 {
t.Errorf("expected cooldownMs 500, got %d", cooldownMs)
}
// Check output
var resp struct {
Status string `json:"status"`
Cooldown int `json:"cooldown"`
}
if err := json.Unmarshal(output.Bytes(), &resp); err != nil {
t.Fatalf("failed to parse response: %v", err)
}
if resp.Cooldown != 500 {
t.Errorf("expected cooldown 500 in response, got %d", resp.Cooldown)
}
}
func TestSetBothCommand(t *testing.T) {
resetGlobals()
input := `{"cmd":"set","amplitude":0.2,"cooldown":1000}` + "\n"
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
if minAmplitude != 0.2 {
t.Errorf("expected minAmplitude 0.2, got %f", minAmplitude)
}
if cooldownMs != 1000 {
t.Errorf("expected cooldownMs 1000, got %d", cooldownMs)
}
}
func TestSetAmplitudeOutOfRange(t *testing.T) {
resetGlobals()
originalAmplitude := minAmplitude
// Test amplitude > 1 (should be ignored)
input := `{"cmd":"set","amplitude":1.5}` + "\n"
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
if minAmplitude != originalAmplitude {
t.Errorf("amplitude should not change for value > 1, got %f", minAmplitude)
}
// Test amplitude <= 0 (should be ignored)
resetGlobals()
input = `{"cmd":"set","amplitude":0}` + "\n"
output.Reset()
processCommands(strings.NewReader(input), &output)
if minAmplitude != originalAmplitude {
t.Errorf("amplitude should not change for value <= 0, got %f", minAmplitude)
}
// Test negative amplitude
resetGlobals()
input = `{"cmd":"set","amplitude":-0.5}` + "\n"
output.Reset()
processCommands(strings.NewReader(input), &output)
if minAmplitude != originalAmplitude {
t.Errorf("amplitude should not change for negative value, got %f", minAmplitude)
}
}
func TestVolumeScalingCommand(t *testing.T) {
resetGlobals()
// Toggle on
input := `{"cmd":"volume-scaling"}` + "\n"
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
if !volumeScaling {
t.Error("expected volumeScaling to be true after toggle")
}
var resp struct {
Status string `json:"status"`
VolumeScaling bool `json:"volume_scaling"`
}
if err := json.Unmarshal(output.Bytes(), &resp); err != nil {
t.Fatalf("failed to parse response: %v", err)
}
if resp.Status != "volume_scaling_toggled" {
t.Errorf("expected status 'volume_scaling_toggled', got %q", resp.Status)
}
if !resp.VolumeScaling {
t.Error("expected volume_scaling true in response")
}
// Toggle off
output.Reset()
processCommands(strings.NewReader(input), &output)
if volumeScaling {
t.Error("expected volumeScaling to be false after second toggle")
}
}
func TestStatusCommand(t *testing.T) {
resetGlobals()
minAmplitude = 0.1
cooldownMs = 600
input := `{"cmd":"status"}` + "\n"
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
var resp struct {
Status string `json:"status"`
Paused bool `json:"paused"`
Amplitude float64 `json:"amplitude"`
Cooldown int `json:"cooldown"`
VolumeScaling bool `json:"volume_scaling"`
}
if err := json.Unmarshal(output.Bytes(), &resp); err != nil {
t.Fatalf("failed to parse response: %v", err)
}
if resp.Status != "ok" {
t.Errorf("expected status 'ok', got %q", resp.Status)
}
if resp.Paused != false {
t.Errorf("expected paused false, got %t", resp.Paused)
}
if resp.Amplitude != 0.1 {
t.Errorf("expected amplitude 0.1, got %f", resp.Amplitude)
}
if resp.Cooldown != 600 {
t.Errorf("expected cooldown 600, got %d", resp.Cooldown)
}
if resp.VolumeScaling != false {
t.Errorf("expected volume_scaling false, got %t", resp.VolumeScaling)
}
}
func TestStatusCommandWhenPaused(t *testing.T) {
resetGlobals()
pausedMu.Lock()
paused = true
pausedMu.Unlock()
input := `{"cmd":"status"}` + "\n"
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
var resp struct {
Paused bool `json:"paused"`
}
if err := json.Unmarshal(output.Bytes(), &resp); err != nil {
t.Fatalf("failed to parse response: %v", err)
}
if resp.Paused != true {
t.Errorf("expected paused true, got %t", resp.Paused)
}
}
func TestUnknownCommand(t *testing.T) {
resetGlobals()
input := `{"cmd":"invalid"}` + "\n"
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
var resp map[string]string
if err := json.Unmarshal(output.Bytes(), &resp); err != nil {
t.Fatalf("failed to parse response: %v", err)
}
if _, hasError := resp["error"]; !hasError {
t.Error("expected error field in response for unknown command")
}
if !strings.Contains(resp["error"], "unknown command") {
t.Errorf("expected 'unknown command' error, got %q", resp["error"])
}
}
func TestInvalidJSON(t *testing.T) {
resetGlobals()
input := `{not valid json}` + "\n"
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
var resp map[string]string
if err := json.Unmarshal(output.Bytes(), &resp); err != nil {
t.Fatalf("failed to parse response: %v", err)
}
if _, hasError := resp["error"]; !hasError {
t.Error("expected error field in response for invalid JSON")
}
if !strings.Contains(resp["error"], "invalid command") {
t.Errorf("expected 'invalid command' error, got %q", resp["error"])
}
}
func TestEmptyLines(t *testing.T) {
resetGlobals()
// Empty lines should be skipped, only the status command should produce output
input := "\n\n" + `{"cmd":"status"}` + "\n\n"
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
lines := strings.Split(strings.TrimSpace(output.String()), "\n")
if len(lines) != 1 {
t.Errorf("expected 1 output line, got %d: %v", len(lines), lines)
}
}
func TestMultipleCommands(t *testing.T) {
resetGlobals()
input := `{"cmd":"pause"}
{"cmd":"status"}
{"cmd":"resume"}
{"cmd":"status"}
`
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
lines := strings.Split(strings.TrimSpace(output.String()), "\n")
if len(lines) != 4 {
t.Fatalf("expected 4 output lines, got %d", len(lines))
}
// First: paused
var resp1 map[string]string
json.Unmarshal([]byte(lines[0]), &resp1)
if resp1["status"] != "paused" {
t.Errorf("line 1: expected 'paused', got %q", resp1["status"])
}
// Second: status shows paused=true
var resp2 struct {
Paused bool `json:"paused"`
}
json.Unmarshal([]byte(lines[1]), &resp2)
if !resp2.Paused {
t.Error("line 2: expected paused=true")
}
// Third: resumed
var resp3 map[string]string
json.Unmarshal([]byte(lines[2]), &resp3)
if resp3["status"] != "resumed" {
t.Errorf("line 3: expected 'resumed', got %q", resp3["status"])
}
// Fourth: status shows paused=false
var resp4 struct {
Paused bool `json:"paused"`
}
json.Unmarshal([]byte(lines[3]), &resp4)
if resp4.Paused {
t.Error("line 4: expected paused=false")
}
}
func TestAmplitudeToVolume(t *testing.T) {
tests := []struct {
name string
amplitude float64
wantMin float64
wantMax float64
}{
{"below minimum returns min volume", 0.01, -3.0, -3.0},
{"at minimum returns min volume", 0.05, -3.0, -3.0},
{"above maximum returns max volume", 1.0, 0.0, 0.0},
{"at maximum returns max volume", 0.80, 0.0, 0.0},
{"mid amplitude returns mid-range", 0.40, -2.0, -0.5},
{"low amplitude is quieter than high", 0.10, -3.0, -1.5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := amplitudeToVolume(tt.amplitude)
if got < tt.wantMin || got > tt.wantMax {
t.Errorf("amplitudeToVolume(%f) = %f, want in [%f, %f]",
tt.amplitude, got, tt.wantMin, tt.wantMax)
}
})
}
// Monotonicity: higher amplitude should yield higher (or equal) volume
prev := amplitudeToVolume(0.05)
for amp := 0.10; amp <= 0.80; amp += 0.05 {
cur := amplitudeToVolume(amp)
if cur < prev-1e-9 {
t.Errorf("non-monotonic: amplitudeToVolume(%f)=%f < amplitudeToVolume(prev)=%f",
amp, cur, prev)
}
prev = cur
}
// Verify no NaN or Inf
for _, amp := range []float64{0, 0.05, 0.1, 0.5, 0.8, 1.0, 10.0} {
v := amplitudeToVolume(amp)
if math.IsNaN(v) || math.IsInf(v, 0) {
t.Errorf("amplitudeToVolume(%f) returned %f", amp, v)
}
}
}
func TestNoOutputWhenStdioModeDisabled(t *testing.T) {
resetGlobals()
stdioMode = false
input := `{"cmd":"pause"}
{"cmd":"status"}
{"cmd":"set","amplitude":0.5}
`
var output bytes.Buffer
processCommands(strings.NewReader(input), &output)
// No output should be produced when stdioMode is false
if output.Len() != 0 {
t.Errorf("expected no output when stdioMode=false, got %q", output.String())
}
// But state should still change
pausedMu.RLock()
if !paused {
t.Error("expected paused to be true even with stdioMode=false")
}
pausedMu.RUnlock()
if minAmplitude != 0.5 {
t.Errorf("expected minAmplitude 0.5, got %f", minAmplitude)
}
}