-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore_test.go
More file actions
702 lines (576 loc) · 15.2 KB
/
core_test.go
File metadata and controls
702 lines (576 loc) · 15.2 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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
package core_test
import (
. "dappco.re/go"
)
// --- New ---
func TestCore_New_Good(t *T) {
c := New()
AssertNotNil(t, c)
}
func TestCore_New_WithOptions_Good(t *T) {
c := New(WithOptions(NewOptions(Option{Key: "name", Value: "myapp"})))
AssertNotNil(t, c)
AssertEqual(t, "myapp", c.App().Name)
}
func TestCore_New_WithOptions_Bad(t *T) {
// Empty options — should still create a valid Core
c := New(WithOptions(NewOptions()))
AssertNotNil(t, c)
}
func TestCore_New_WithService_Good(t *T) {
started := false
c := New(
WithOptions(NewOptions(Option{Key: "name", Value: "myapp"})),
WithService(func(c *Core) Result {
c.Service("test", Service{
OnStart: func() Result { started = true; return Result{OK: true} },
})
return Result{OK: true}
}),
)
svc := c.Service("test")
AssertTrue(t, svc.OK)
c.ServiceStartup(Background(), nil)
AssertTrue(t, started)
}
func TestCore_New_WithServiceLock_Good(t *T) {
c := New(
WithService(func(c *Core) Result {
c.Service("allowed", Service{})
return Result{OK: true}
}),
WithServiceLock(),
)
// Registration after lock should fail
reg := c.Service("blocked", Service{})
AssertFalse(t, reg.OK)
}
func TestCore_New_WithService_Bad_FailingOption(t *T) {
secondCalled := false
_ = New(
WithService(func(c *Core) Result {
return Result{Value: E("test", "intentional failure", nil), OK: false}
}),
WithService(func(c *Core) Result {
secondCalled = true
return Result{OK: true}
}),
)
AssertFalse(t, secondCalled, "second option should not run after first fails")
}
// --- Accessors ---
func TestCore_Accessors_Good(t *T) {
c := New()
AssertNotNil(t, c.App())
AssertNotNil(t, c.Data())
AssertNotNil(t, c.Drive())
AssertNotNil(t, c.Fs())
AssertNotNil(t, c.Config())
AssertNotNil(t, c.Error())
AssertNotNil(t, c.Log())
AssertNotNil(t, c.Cli())
AssertNotNil(t, c.IPC())
AssertNotNil(t, c.I18n())
AssertEqual(t, c, c.Core())
}
func TestOptions_Accessor_Good(t *T) {
c := New(WithOptions(NewOptions(
Option{Key: "name", Value: "testapp"},
Option{Key: "port", Value: 8080},
Option{Key: "debug", Value: true},
)))
opts := c.Options()
AssertNotNil(t, opts)
AssertEqual(t, "testapp", opts.String("name"))
AssertEqual(t, 8080, opts.Int("port"))
AssertTrue(t, opts.Bool("debug"))
}
func TestOptions_Accessor_Nil(t *T) {
c := New()
// No options passed — Options() returns nil
AssertNil(t, c.Options())
}
// --- Core Error/Log Helpers ---
func TestCore_LogError_Good(t *T) {
c := New()
cause := AnError
r := c.LogError(cause, "test.Operation", "something broke")
err, ok := r.Value.(error)
AssertTrue(t, ok)
AssertErrorIs(t, err, cause)
}
func TestCore_LogWarn_Good(t *T) {
c := New()
r := c.LogWarn(AnError, "test.Operation", "heads up")
_, ok := r.Value.(error)
AssertTrue(t, ok)
}
func TestCore_Must_Ugly(t *T) {
c := New()
AssertPanics(t, func() {
c.Must(AnError, "test.Operation", "fatal")
})
}
func TestCore_Must_Nil_Good(t *T) {
c := New()
AssertNotPanics(t, func() {
c.Must(nil, "test.Operation", "no error")
})
}
// --- RegistryOf ---
func TestCore_RegistryOf_Good_Services(t *T) {
c := New(
WithService(func(c *Core) Result {
return c.Service("alpha", Service{})
}),
WithService(func(c *Core) Result {
return c.Service("bravo", Service{})
}),
)
reg := c.RegistryOf("services")
// cli is auto-registered + our 2
AssertTrue(t, reg.Has("alpha"))
AssertTrue(t, reg.Has("bravo"))
AssertTrue(t, reg.Has("cli"))
}
func TestCore_RegistryOf_Good_Commands(t *T) {
c := New()
c.Command("deploy", Command{Action: func(_ Options) Result { return Result{OK: true} }})
c.Command("test", Command{Action: func(_ Options) Result { return Result{OK: true} }})
reg := c.RegistryOf("commands")
AssertTrue(t, reg.Has("deploy"))
AssertTrue(t, reg.Has("test"))
}
func TestCore_RegistryOf_Good_Actions(t *T) {
c := New()
c.Action("process.run", func(_ Context, _ Options) Result { return Result{OK: true} })
c.Action("brain.recall", func(_ Context, _ Options) Result { return Result{OK: true} })
reg := c.RegistryOf("actions")
AssertTrue(t, reg.Has("process.run"))
AssertTrue(t, reg.Has("brain.recall"))
AssertEqual(t, 2, reg.Len())
}
func TestCore_RegistryOf_Bad_Unknown(t *T) {
c := New()
reg := c.RegistryOf("nonexistent")
AssertEqual(t, 0, reg.Len(), "unknown registry returns empty")
}
// --- RunResult ---
func TestCore_RunResult_Good(t *T) {
c := New(
WithService(func(c *Core) Result {
return c.Service("healthy", Service{
OnStart: func() Result { return Result{OK: true} },
OnStop: func() Result { return Result{OK: true} },
})
}),
)
r := c.RunResult()
AssertTrue(t, r.OK)
}
func TestCore_RunResult_Bad(t *T) {
c := New(
WithService(func(c *Core) Result {
return c.Service("broken", Service{
OnStart: func() Result {
return Result{Value: NewError("startup failed"), OK: false}
},
})
}),
)
r := c.RunResult()
AssertFalse(t, r.OK)
AssertContains(t, r.Error(), "startup failed")
}
func TestCore_RunResult_Ugly(t *T) {
shutdownCalled := false
c := New(
WithService(func(c *Core) Result {
return c.Service("cleanup", Service{
OnStart: func() Result { return Result{OK: true} },
OnStop: func() Result { shutdownCalled = true; return Result{OK: true} },
})
}),
WithService(func(c *Core) Result {
return c.Service("broken", Service{
OnStart: func() Result {
return Result{Value: NewError("boom"), OK: false}
},
})
}),
)
r := c.RunResult()
AssertFalse(t, r.OK)
AssertTrue(t, shutdownCalled, "ServiceShutdown must be called even when startup fails — cleanup service must get OnStop")
}
// Run() delegates to RunResult() — tested via RunResult tests above.
// c.Exit(1) behaviour is verified by RunResult returning OK=false correctly.
func TestCore_Core_ACTION_Good(t *T) {
c := New()
var got Message
c.RegisterAction(func(_ *Core, msg Message) Result {
got = msg
return Result{OK: true}
})
r := c.ACTION("agent.dispatch")
AssertTrue(t, r.OK)
AssertEqual(t, Message("agent.dispatch"), got)
}
func TestCore_Core_ACTION_Bad(t *T) {
c := New()
c.RegisterAction(func(_ *Core, _ Message) Result {
return Result{Value: NewError("handler refused"), OK: false}
})
r := c.ACTION("agent.dispatch")
AssertTrue(t, r.OK)
}
func TestCore_Core_ACTION_Ugly(t *T) {
c := New()
c.RegisterAction(func(_ *Core, _ Message) Result {
panic("handler panic")
})
r := c.ACTION(nil)
AssertTrue(t, r.OK)
}
func TestCore_Core_App_Good(t *T) {
c := New(WithOption("name", "homelab"))
AssertEqual(t, "homelab", c.App().Name)
}
func TestCore_Core_App_Bad(t *T) {
c := New()
AssertEqual(t, "", c.App().Name)
}
func TestCore_Core_App_Ugly(t *T) {
c := New()
AssertSame(t, c.App(), c.App())
}
func TestCore_Core_Cli_Good(t *T) {
c := New()
AssertNotNil(t, c.Cli())
}
func TestCore_Core_Cli_Bad(t *T) {
c := New(WithServiceLock())
AssertNotNil(t, c.Cli())
}
func TestCore_Core_Cli_Ugly(t *T) {
c := New()
c.Cli().SetOutput(NewBuffer())
AssertNotPanics(t, func() {
_ = c.Cli().Run()
})
}
func TestCore_Core_Config_Good(t *T) {
c := New()
c.Config().Set("agent.region", "lab")
AssertEqual(t, "lab", c.Config().String("agent.region"))
}
func TestCore_Core_Config_Bad(t *T) {
c := New()
AssertEqual(t, "", c.Config().String("missing"))
}
func TestCore_Core_Config_Ugly(t *T) {
c := New()
AssertSame(t, c.Config(), c.Config())
}
func TestCore_Core_Context_Good(t *T) {
c := New()
AssertNotNil(t, c.Context())
}
func TestCore_Core_Context_Bad(t *T) {
c := New()
c.ServiceShutdown(Background())
AssertError(t, c.Context().Err())
}
func TestCore_Core_Context_Ugly(t *T) {
c := New()
before := c.Context()
c.ServiceStartup(Background(), nil)
AssertNotNil(t, before)
AssertNotNil(t, c.Context())
}
func TestCore_Core_Core_Good(t *T) {
c := New()
AssertSame(t, c, c.Core())
}
func TestCore_Core_Core_Bad(t *T) {
c := New(WithOption("name", "agent"))
AssertSame(t, c, c.Core())
}
func TestCore_Core_Core_Ugly(t *T) {
var c *Core
AssertNil(t, c.Core())
}
func TestCore_Core_Data_Good(t *T) {
c := New()
AssertNotNil(t, c.Data())
}
func TestCore_Core_Data_Bad(t *T) {
c := New()
AssertSame(t, c.Data(), c.Data())
}
func TestCore_Core_Data_Ugly(t *T) {
c := New()
AssertEqual(t, 0, c.Data().Len())
}
func TestCore_Core_Drive_Good(t *T) {
c := New()
AssertNotNil(t, c.Drive())
}
func TestCore_Core_Drive_Bad(t *T) {
c := New()
AssertSame(t, c.Drive(), c.Drive())
}
func TestCore_Core_Drive_Ugly(t *T) {
c := New()
AssertEqual(t, 0, c.Drive().Len())
}
func TestCore_Core_Env_Good(t *T) {
c := New()
AssertEqual(t, OS(), c.Env("OS"))
}
func TestCore_Core_Env_Bad(t *T) {
c := New()
AssertEqual(t, "", c.Env("CORE_TEST_MISSING"))
}
func TestCore_Core_Env_Ugly(t *T) {
t.Setenv("CORE_TEST_SESSION", "token")
c := New()
AssertEqual(t, "token", c.Env("CORE_TEST_SESSION"))
}
func TestCore_Core_Error_Good(t *T) {
c := New()
AssertNotNil(t, c.Error())
}
func TestCore_Core_Error_Bad(t *T) {
c := New()
AssertNotPanics(t, func() {
c.Error().Recover()
})
}
func TestCore_Core_Error_Ugly(t *T) {
c := New()
AssertSame(t, c.Error(), c.Error())
}
func TestCore_Core_Fs_Good(t *T) {
c := New()
AssertNotNil(t, c.Fs())
}
func TestCore_Core_Fs_Bad(t *T) {
c := New()
AssertFalse(t, c.Fs().Read(Path(t.TempDir(), "missing")).OK)
}
func TestCore_Core_Fs_Ugly(t *T) {
c := New()
AssertSame(t, c.Fs(), c.Fs())
}
func TestCore_Core_I18n_Good(t *T) {
c := New()
AssertNotNil(t, c.I18n())
}
func TestCore_Core_I18n_Bad(t *T) {
c := New()
AssertSame(t, c.I18n(), c.I18n())
}
func TestCore_Core_I18n_Ugly(t *T) {
c := New()
r := c.I18n().Translate("missing.key")
AssertTrue(t, r.OK)
AssertEqual(t, "missing.key", r.Value)
}
func TestCore_Core_IPC_Good(t *T) {
c := New()
AssertNotNil(t, c.IPC())
}
func TestCore_Core_IPC_Bad(t *T) {
c := New()
AssertSame(t, c.IPC(), c.IPC())
}
func TestCore_Core_IPC_Ugly(t *T) {
c := New()
AssertTrue(t, c.ACTION(nil).OK)
}
func TestCore_Core_Log_Good(t *T) {
c := New()
AssertNotNil(t, c.Log())
}
func TestCore_Core_Log_Bad(t *T) {
c := New()
AssertTrue(t, c.Log().Error(nil, "agent.Dispatch", "ok").OK)
}
func TestCore_Core_Log_Ugly(t *T) {
c := New()
AssertSame(t, c.Log(), c.Log())
}
func TestCore_Core_LogError_Good(t *T) {
r := New().LogError(AnError, "agent.Dispatch", "failed")
AssertFalse(t, r.OK)
AssertErrorIs(t, r.Value.(error), AnError)
}
func TestCore_Core_LogError_Bad(t *T) {
r := New().LogError(nil, "agent.Dispatch", "ok")
AssertTrue(t, r.OK)
}
func TestCore_Core_LogError_Ugly(t *T) {
r := New().LogError(NewCode("agent.refused", "dispatch refused"), "agent.Dispatch", "failed")
AssertFalse(t, r.OK)
AssertEqual(t, "agent.refused", ErrorCode(r.Value.(error)))
}
func TestCore_Core_LogWarn_Good(t *T) {
r := New().LogWarn(AnError, "agent.Dispatch", "degraded")
AssertFalse(t, r.OK)
AssertErrorIs(t, r.Value.(error), AnError)
}
func TestCore_Core_LogWarn_Bad(t *T) {
r := New().LogWarn(nil, "agent.Dispatch", "ok")
AssertTrue(t, r.OK)
}
func TestCore_Core_LogWarn_Ugly(t *T) {
r := New().LogWarn(NewCode("agent.degraded", "dispatch degraded"), "agent.Dispatch", "degraded")
AssertFalse(t, r.OK)
AssertEqual(t, "agent.degraded", ErrorCode(r.Value.(error)))
}
func TestCore_Core_Must_Good(t *T) {
AssertNotPanics(t, func() {
New().Must(nil, "agent.Dispatch", "ok")
})
}
func TestCore_Core_Must_Bad(t *T) {
AssertPanicsWithError(t, "dispatch failed", func() {
New().Must(AnError, "agent.Dispatch", "dispatch failed")
})
}
func TestCore_Core_Must_Ugly(t *T) {
AssertPanicsWithError(t, "agent.refused", func() {
New().Must(NewCode("agent.refused", "dispatch refused"), "agent.Dispatch", "failed")
})
}
func TestCore_Core_Options_Good(t *T) {
c := New(WithOption("name", "agent"))
AssertEqual(t, "agent", c.Options().String("name"))
}
func TestCore_Core_Options_Bad(t *T) {
c := New()
AssertNil(t, c.Options())
}
func TestCore_Core_Options_Ugly(t *T) {
c := New(WithOptions(NewOptions()))
AssertEqual(t, 0, c.Options().Len())
}
func TestCore_Core_QUERY_Good(t *T) {
c := New()
c.RegisterQuery(func(_ *Core, q Query) Result {
return Result{Value: Sprintf("ack:%v", q), OK: true}
})
r := c.QUERY("agent.status")
AssertTrue(t, r.OK)
AssertEqual(t, "ack:agent.status", r.Value)
}
func TestCore_Core_QUERY_Bad(t *T) {
r := New().QUERY("missing")
AssertFalse(t, r.OK)
}
func TestCore_Core_QUERY_Ugly(t *T) {
c := New()
c.RegisterQuery(func(_ *Core, _ Query) Result {
return Result{Value: nil, OK: true}
})
r := c.QUERY(nil)
AssertTrue(t, r.OK)
AssertNil(t, r.Value)
}
func TestCore_Core_QUERYALL_Good(t *T) {
c := New()
c.RegisterQuery(func(_ *Core, _ Query) Result { return Result{Value: "agent", OK: true} })
c.RegisterQuery(func(_ *Core, _ Query) Result { return Result{Value: "health", OK: true} })
r := c.QUERYALL("status")
AssertTrue(t, r.OK)
AssertElementsMatch(t, []any{"agent", "health"}, r.Value.([]any))
}
func TestCore_Core_QUERYALL_Bad(t *T) {
c := New()
c.RegisterQuery(func(_ *Core, _ Query) Result { return Result{Value: "ignored", OK: false} })
r := c.QUERYALL("status")
AssertTrue(t, r.OK)
AssertEmpty(t, r.Value.([]any))
}
func TestCore_Core_QUERYALL_Ugly(t *T) {
c := New()
c.RegisterQuery(func(_ *Core, _ Query) Result { return Result{Value: nil, OK: true} })
r := c.QUERYALL(nil)
AssertTrue(t, r.OK)
AssertEmpty(t, r.Value.([]any))
}
func TestCore_Core_RegistryOf_Good(t *T) {
c := New()
c.Action("agent.dispatch", func(_ Context, _ Options) Result { return Result{OK: true} })
reg := c.RegistryOf("actions")
AssertTrue(t, reg.Has("agent.dispatch"))
}
func TestCore_Core_RegistryOf_Bad(t *T) {
reg := New().RegistryOf("missing")
AssertEqual(t, 0, reg.Len())
}
func TestCore_Core_RegistryOf_Ugly(t *T) {
c := New()
reg := c.RegistryOf("actions")
c.Action("agent.dispatch", func(_ Context, _ Options) Result { return Result{OK: true} })
AssertFalse(t, reg.Has("agent.dispatch"))
}
func TestCore_Core_Run_Good(t *T) {
c := New()
c.Cli().SetOutput(NewBuffer())
AssertNotPanics(t, func() {
c.Run()
})
}
func TestCore_Core_Run_Bad(t *T) {
c := New(WithService(func(c *Core) Result {
return c.Service("agent", Service{OnStart: func() Result { return Result{OK: true} }})
}))
c.Cli().SetOutput(NewBuffer())
AssertNotPanics(t, func() {
c.Run()
})
}
func TestCore_Core_Run_Ugly(t *T) {
c := New()
c.Cli().SetOutput(NewBuffer())
AssertNotPanics(t, func() {
c.Run()
})
}
func TestCore_Core_RunResult_Good(t *T) {
c := New()
c.Cli().SetOutput(NewBuffer())
AssertTrue(t, c.RunResult().OK)
}
func TestCore_Core_RunResult_Bad(t *T) {
c := New(WithService(func(c *Core) Result {
return c.Service("agent", Service{
OnStart: func() Result { return Result{Value: NewError("startup refused"), OK: false} },
})
}))
r := c.RunResult()
AssertFalse(t, r.OK)
AssertContains(t, r.Error(), "startup refused")
}
func TestCore_Core_RunResult_Ugly(t *T) {
stopped := false
c := New(
WithService(func(c *Core) Result {
return c.Service("cleanup", Service{
OnStart: func() Result { return Result{OK: true} },
OnStop: func() Result { stopped = true; return Result{OK: true} },
})
}),
WithService(func(c *Core) Result {
return c.Service("broken", Service{
OnStart: func() Result { return Result{Value: NewError("boom"), OK: false} },
})
}),
)
r := c.RunResult()
AssertFalse(t, r.OK)
AssertContains(t, r.Error(), "boom")
AssertTrue(t, stopped)
}