-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction_test.go
More file actions
833 lines (710 loc) · 22.8 KB
/
Copy pathaction_test.go
File metadata and controls
833 lines (710 loc) · 22.8 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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
package core_test
import . "dappco.re/go"
// --- NamedAction Register ---
func TestAction_NamedAction_Good_Register(t *T) {
c := New()
def := c.Action("process.run", func(_ Context, opts Options) Result {
return Result{Value: "output", OK: true}
})
AssertNotNil(t, def)
AssertEqual(t, "process.run", def.Name)
AssertTrue(t, def.Exists())
}
func TestAction_NamedAction_Good_Invoke(t *T) {
c := New()
c.Action("git.log", func(_ Context, opts Options) Result {
dir := opts.String("dir")
return Result{Value: Concat("log from ", dir), OK: true}
})
r := c.Action("git.log").Run(Background(), NewOptions(
Option{Key: "dir", Value: "/repo"},
))
AssertTrue(t, r.OK)
AssertEqual(t, "log from /repo", r.Value)
}
func TestAction_NamedAction_Bad_NotRegistered(t *T) {
c := New()
r := c.Action("missing.action").Run(Background(), NewOptions())
AssertFalse(t, r.OK, "invoking unregistered action must fail")
}
func TestAction_NamedAction_Good_Exists(t *T) {
c := New()
c.Action("brain.recall", func(_ Context, _ Options) Result {
return Result{OK: true}
})
AssertTrue(t, c.Action("brain.recall").Exists())
AssertFalse(t, c.Action("brain.forget").Exists())
}
func TestAction_NamedAction_Ugly_PanicRecovery(t *T) {
c := New()
c.Action("agent.explode", func(_ Context, _ Options) Result {
panic("boom")
})
r := c.Action("agent.explode").Run(Background(), NewOptions())
AssertFalse(t, r.OK, "panicking action must return !OK, not crash")
err, ok := r.Value.(error)
AssertTrue(t, ok)
AssertContains(t, err.Error(), "panic")
}
func TestAction_NamedAction_Ugly_NilAction(t *T) {
var def *Action
r := def.Run(Background(), NewOptions())
AssertFalse(t, r.OK)
AssertFalse(t, def.Exists())
}
// --- Actions listing ---
func TestAction_Actions_Good(t *T) {
c := New()
c.Action("process.run", func(_ Context, _ Options) Result { return Result{OK: true} })
c.Action("process.kill", func(_ Context, _ Options) Result { return Result{OK: true} })
c.Action("agentic.dispatch", func(_ Context, _ Options) Result { return Result{OK: true} })
names := c.Actions()
// The three core.* discovery built-ins ride every Core (W4-1).
AssertLen(t, names, 6)
AssertEqual(t, []string{"core.actions", "core.info", "core.health", "process.run", "process.kill", "agentic.dispatch"}, names)
}
func TestAction_Actions_Bad_Empty(t *T) {
// A bare Core is never action-empty: the discovery built-ins are
// always answerable. Nothing ELSE is registered.
c := New()
AssertEqual(t, []string{"core.actions", "core.info", "core.health"}, c.Actions())
}
// --- Action fields ---
func TestAction_NamedAction_Good_DescriptionAndSchema(t *T) {
c := New()
def := c.Action("process.run", func(_ Context, _ Options) Result { return Result{OK: true} })
def.Description = "Execute a command synchronously"
def.Schema = NewOptions(
Option{Key: "command", Value: "string"},
Option{Key: "args", Value: "[]string"},
)
retrieved := c.Action("process.run")
AssertEqual(t, "Execute a command synchronously", retrieved.Description)
AssertTrue(t, retrieved.Schema.Has("command"))
}
// --- Permission by registration ---
func TestAction_NamedAction_Good_PermissionModel(t *T) {
// Full Core — process registered
full := New()
full.Action("process.run", func(_ Context, _ Options) Result {
return Result{Value: "executed", OK: true}
})
// Sandboxed Core — no process
sandboxed := New()
// Full can execute
r := full.Action("process.run").Run(Background(), NewOptions())
AssertTrue(t, r.OK)
// Sandboxed returns not-registered
r = sandboxed.Action("process.run").Run(Background(), NewOptions())
AssertFalse(t, r.OK, "sandboxed Core must not have process capability")
}
// --- Action overwrite ---
func TestAction_NamedAction_Good_Overwrite(t *T) {
c := New()
c.Action("hot.reload", func(_ Context, _ Options) Result {
return Result{Value: "v1", OK: true}
})
c.Action("hot.reload", func(_ Context, _ Options) Result {
return Result{Value: "v2", OK: true}
})
r := c.Action("hot.reload").Run(Background(), NewOptions())
AssertTrue(t, r.OK)
AssertEqual(t, "v2", r.Value, "latest handler wins")
}
// --- Task Composition ---
func TestAction_Task_Good_Sequential(t *T) {
c := New()
var order []string
c.Action("step.a", func(_ Context, _ Options) Result {
order = append(order, "a")
return Result{Value: "output-a", OK: true}
})
c.Action("step.b", func(_ Context, _ Options) Result {
order = append(order, "b")
return Result{Value: "output-b", OK: true}
})
c.Task("pipeline", Task{
Steps: []Step{
{Action: "step.a"},
{Action: "step.b"},
},
})
r := c.Task("pipeline").Run(Background(), c, NewOptions())
AssertTrue(t, r.OK)
AssertEqual(t, []string{"a", "b"}, order, "steps must run in order")
AssertEqual(t, "output-b", r.Value, "last step's result is returned")
}
func TestAction_Task_Bad_StepFails(t *T) {
c := New()
var order []string
c.Action("step.ok", func(_ Context, _ Options) Result {
order = append(order, "ok")
return Result{OK: true}
})
c.Action("step.fail", func(_ Context, _ Options) Result {
order = append(order, "fail")
return Result{Value: NewError("broke"), OK: false}
})
c.Action("step.never", func(_ Context, _ Options) Result {
order = append(order, "never")
return Result{OK: true}
})
c.Task("broken", Task{
Steps: []Step{
{Action: "step.ok"},
{Action: "step.fail"},
{Action: "step.never"},
},
})
r := c.Task("broken").Run(Background(), c, NewOptions())
AssertFalse(t, r.OK)
AssertEqual(t, []string{"ok", "fail"}, order, "chain stops on failure, step.never skipped")
}
func TestAction_Task_Bad_MissingAction(t *T) {
c := New()
c.Task("missing", Task{
Steps: []Step{
{Action: "nonexistent"},
},
})
r := c.Task("missing").Run(Background(), c, NewOptions())
AssertFalse(t, r.OK)
}
func TestAction_Task_Good_PreviousInput(t *T) {
c := New()
c.Action("pipeline.produce", func(_ Context, _ Options) Result {
return Result{Value: "data-from-step-1", OK: true}
})
c.Action("pipeline.consume", func(_ Context, opts Options) Result {
input := opts.Get("_input")
if !input.OK {
return Result{Value: "no input", OK: true}
}
return Result{Value: "got: " + input.Value.(string), OK: true}
})
c.Task("pipe", Task{
Steps: []Step{
{Action: "pipeline.produce"},
{Action: "pipeline.consume", Input: "previous"},
},
})
r := c.Task("pipe").Run(Background(), c, NewOptions())
AssertTrue(t, r.OK)
AssertEqual(t, "got: data-from-step-1", r.Value)
}
func TestAction_Task_Ugly_EmptySteps(t *T) {
c := New()
c.Task("empty", Task{})
r := c.Task("empty").Run(Background(), c, NewOptions())
AssertFalse(t, r.OK)
}
func TestAction_Tasks_Good(t *T) {
c := New()
c.Task("deploy", Task{Steps: []Step{{Action: "x"}}})
c.Task("review", Task{Steps: []Step{{Action: "y"}}})
AssertEqual(t, []string{"deploy", "review"}, c.Tasks())
}
// --- AX-7 canonical triplets ---
func TestAction_Action_Run_Good(t *T) {
c := New()
a := c.Action("agent.dispatch", func(_ Context, opts Options) Result {
return Result{Value: opts.String("agent"), OK: true}
})
r := a.Run(Background(), NewOptions(Option{Key: "agent", Value: "codex"}))
AssertTrue(t, r.OK)
AssertEqual(t, "codex", r.Value)
}
func TestAction_Action_Run_Bad(t *T) {
a := &Action{Name: "agent.missing"}
r := a.Run(Background(), NewOptions())
AssertFalse(t, r.OK)
AssertContains(t, r.Error(), "not registered")
}
func TestAction_Action_Run_Ugly(t *T) {
c := New()
a := c.Action("agent.panic", func(_ Context, _ Options) Result {
panic("dispatch crashed")
})
r := a.Run(Background(), NewOptions())
AssertFalse(t, r.OK)
AssertContains(t, r.Error(), "panic")
}
func TestAction_Action_Exists_Good(t *T) {
a := &Action{Name: "agent.dispatch", Handler: func(_ Context, _ Options) Result { return Result{OK: true} }}
AssertTrue(t, a.Exists())
}
func TestAction_Action_Exists_Bad(t *T) {
a := &Action{Name: "agent.dispatch"}
AssertFalse(t, a.Exists())
}
func TestAction_Action_Exists_Ugly(t *T) {
var a *Action
AssertFalse(t, a.Exists())
}
func TestAction_Core_Action_Good(t *T) {
c := New()
a := c.Action("agent.dispatch", func(_ Context, _ Options) Result {
return Result{Value: "queued", OK: true}
})
AssertTrue(t, a.Exists())
AssertEqual(t, "queued", c.Action("agent.dispatch").Run(Background(), NewOptions()).Value)
}
func TestAction_Core_Action_Bad(t *T) {
c := New()
a := c.Action("agent.missing")
AssertFalse(t, a.Exists())
AssertFalse(t, a.Run(Background(), NewOptions()).OK)
}
func TestAction_Core_Action_Ugly(t *T) {
c := New()
c.Action("agent.dispatch", func(_ Context, _ Options) Result { return Result{Value: "v1", OK: true} })
c.Action("agent.dispatch", func(_ Context, _ Options) Result { return Result{Value: "v2", OK: true} })
r := c.Action("agent.dispatch").Run(Background(), NewOptions())
AssertTrue(t, r.OK)
AssertEqual(t, "v2", r.Value)
}
func TestAction_Core_Actions_Good(t *T) {
c := New()
c.Action("agent.prepare", func(_ Context, _ Options) Result { return Result{OK: true} })
c.Action("agent.dispatch", func(_ Context, _ Options) Result { return Result{OK: true} })
AssertEqual(t, []string{"core.actions", "core.info", "core.health", "agent.prepare", "agent.dispatch"}, c.Actions())
}
func TestAction_Core_Actions_Bad(t *T) {
c := New()
AssertLen(t, c.Actions(), 3) // only the built-ins
}
func TestAction_Core_Actions_Ugly(t *T) {
c := New()
c.Action("agent.prepare", func(_ Context, _ Options) Result { return Result{OK: true} })
names := c.Actions()
names[0] = "mutated"
AssertEqual(t, []string{"core.actions", "core.info", "core.health", "agent.prepare"}, c.Actions())
}
func TestAction_Task_Run_Good(t *T) {
c := New()
c.Action("agent.prepare", func(_ Context, _ Options) Result {
return Result{Value: "prepared", OK: true}
})
task := &Task{Name: "agent.dispatch", Steps: []Step{{Action: "agent.prepare"}}}
r := task.Run(Background(), c, NewOptions())
AssertTrue(t, r.OK)
AssertEqual(t, "prepared", r.Value)
}
func TestAction_Task_Run_Bad(t *T) {
c := New()
task := &Task{Name: "agent.dispatch", Steps: []Step{{Action: "agent.missing"}}}
r := task.Run(Background(), c, NewOptions())
AssertFalse(t, r.OK)
}
func TestAction_Task_Run_Ugly(t *T) {
c := New()
c.Action("agent.prepare", func(_ Context, _ Options) Result {
return Result{Value: "session-token", OK: true}
})
c.Action("agent.dispatch", func(_ Context, opts Options) Result {
return Result{Value: opts.String("_input"), OK: true}
})
task := &Task{Name: "agent.pipeline", Steps: []Step{
{Action: "agent.prepare"},
{Action: "agent.dispatch", Input: "previous"},
}}
r := task.Run(Background(), c, NewOptions())
AssertTrue(t, r.OK)
AssertEqual(t, "session-token", r.Value)
}
func TestAction_Core_Task_Good(t *T) {
c := New()
task := c.Task("agent.pipeline", Task{Steps: []Step{{Action: "agent.prepare"}}})
AssertEqual(t, "agent.pipeline", task.Name)
AssertLen(t, task.Steps, 1)
}
func TestAction_Core_Task_Bad(t *T) {
c := New()
task := c.Task("agent.missing")
r := task.Run(Background(), c, NewOptions())
AssertFalse(t, r.OK)
}
func TestAction_Core_Task_Ugly(t *T) {
c := New()
c.Task("agent.pipeline", Task{Steps: []Step{{Action: "agent.prepare"}}})
c.Task("agent.pipeline", Task{Steps: []Step{{Action: "agent.dispatch"}}})
task := c.Task("agent.pipeline")
AssertEqual(t, "agent.dispatch", task.Steps[0].Action)
}
func TestAction_Core_Tasks_Good(t *T) {
c := New()
c.Task("agent.prepare", Task{Steps: []Step{{Action: "agent.prepare"}}})
c.Task("agent.dispatch", Task{Steps: []Step{{Action: "agent.dispatch"}}})
AssertEqual(t, []string{"agent.prepare", "agent.dispatch"}, c.Tasks())
}
func TestAction_Core_Tasks_Bad(t *T) {
c := New()
AssertEmpty(t, c.Tasks())
}
func TestAction_Core_Tasks_Ugly(t *T) {
c := New()
c.Task("agent.prepare", Task{Steps: []Step{{Action: "agent.prepare"}}})
tasks := c.Tasks()
tasks[0] = "mutated"
AssertEqual(t, []string{"agent.prepare"}, c.Tasks())
}
// --- PerformAsync ---
func TestAction_PerformAsync_Good(t *T) {
c := New()
var mu Mutex
var result string
c.Action("async.work", func(_ Context, _ Options) Result {
mu.Lock()
result = "done"
mu.Unlock()
return Result{Value: "done", OK: true}
})
r := c.PerformAsync("async.work", NewOptions())
AssertTrue(t, r.OK)
AssertTrue(t, HasPrefix(r.Value.(string), "id-"), "should return task ID")
Sleep(100 * Millisecond)
mu.Lock()
AssertEqual(t, "done", result)
mu.Unlock()
}
func TestAction_PerformAsync_Good_Progress(t *T) {
c := New()
c.Action("async.tracked", func(_ Context, _ Options) Result {
return Result{OK: true}
})
progress := make(chan ActionTaskProgress, 1)
c.RegisterAction(func(_ *Core, msg Message) Result {
if evt, ok := msg.(ActionTaskProgress); ok {
progress <- evt
}
return Result{OK: true}
})
r := c.PerformAsync("async.tracked", NewOptions())
taskID := r.Value.(string)
c.Progress(taskID, 0.5, "halfway", "async.tracked")
timeout, cancel := WithTimeout(Background(), 2*Second)
defer cancel()
select {
case evt := <-progress:
AssertEqual(t, taskID, evt.TaskIdentifier)
AssertEqual(t, 0.5, evt.Progress)
AssertEqual(t, "halfway", evt.Message)
AssertEqual(t, "async.tracked", evt.Action)
case <-timeout.Done():
t.Fatal("timed out waiting for progress event")
}
}
func TestAction_PerformAsync_Good_Completion(t *T) {
c := New()
completed := make(chan ActionTaskCompleted, 1)
c.Action("async.completable", func(_ Context, _ Options) Result {
return Result{Value: "output", OK: true}
})
c.RegisterAction(func(_ *Core, msg Message) Result {
if evt, ok := msg.(ActionTaskCompleted); ok {
completed <- evt
}
return Result{OK: true}
})
c.PerformAsync("async.completable", NewOptions())
timeout, cancel := WithTimeout(Background(), 2*Second)
defer cancel()
select {
case evt := <-completed:
AssertTrue(t, evt.Result.OK)
AssertEqual(t, "output", evt.Result.Value)
case <-timeout.Done():
t.Fatal("timed out waiting for completion")
}
}
func TestAction_PerformAsync_Bad_ActionNotRegistered(t *T) {
c := New()
completed := make(chan ActionTaskCompleted, 1)
c.RegisterAction(func(_ *Core, msg Message) Result {
if evt, ok := msg.(ActionTaskCompleted); ok {
completed <- evt
}
return Result{OK: true}
})
c.PerformAsync("nonexistent", NewOptions())
timeout, cancel := WithTimeout(Background(), 2*Second)
defer cancel()
select {
case evt := <-completed:
AssertFalse(t, evt.Result.OK, "unregistered action should fail")
case <-timeout.Done():
t.Fatal("timed out")
}
}
func TestAction_PerformAsync_Bad_AfterShutdown(t *T) {
c := New()
c.Action("async.work", func(_ Context, _ Options) Result { return Result{OK: true} })
c.ServiceStartup(Background(), nil)
c.ServiceShutdown(Background())
r := c.PerformAsync("async.work", NewOptions())
AssertFalse(t, r.OK)
}
// --- RegisterAction + RegisterActions (broadcast handlers) ---
func TestAction_RegisterAction_Good(t *T) {
c := New()
called := false
c.RegisterAction(func(_ *Core, _ Message) Result {
called = true
return Result{OK: true}
})
c.ACTION(nil)
AssertTrue(t, called)
}
func TestAction_RegisterActions_Good(t *T) {
c := New()
count := 0
h := func(_ *Core, _ Message) Result { count++; return Result{OK: true} }
c.RegisterActions(h, h)
c.ACTION(nil)
AssertEqual(t, 2, count)
}
func TestAction_Core_PerformAsync_Good(t *T) {
c := New()
completed := make(chan ActionTaskCompleted, 1)
c.Action("agent.dispatch", func(_ Context, _ Options) Result {
return Result{Value: "dispatched", OK: true}
})
c.RegisterAction(func(_ *Core, msg Message) Result {
if evt, ok := msg.(ActionTaskCompleted); ok {
completed <- evt
}
return Result{OK: true}
})
r := c.PerformAsync("agent.dispatch", NewOptions())
AssertTrue(t, r.OK)
AssertTrue(t, HasPrefix(r.Value.(string), "id-"))
timeout, cancel := WithTimeout(Background(), 2*Second)
defer cancel()
select {
case evt := <-completed:
AssertTrue(t, evt.Result.OK)
AssertEqual(t, "agent.dispatch", evt.Action)
AssertEqual(t, "dispatched", evt.Result.Value)
case <-timeout.Done():
t.Fatal("timed out waiting for agent.dispatch completion")
}
}
func TestAction_Core_PerformAsync_Bad(t *T) {
c := New()
completed := make(chan ActionTaskCompleted, 1)
c.RegisterAction(func(_ *Core, msg Message) Result {
if evt, ok := msg.(ActionTaskCompleted); ok {
completed <- evt
}
return Result{OK: true}
})
r := c.PerformAsync("agent.missing", NewOptions())
AssertTrue(t, r.OK)
timeout, cancel := WithTimeout(Background(), 2*Second)
defer cancel()
select {
case evt := <-completed:
AssertFalse(t, evt.Result.OK)
AssertEqual(t, "agent.missing", evt.Action)
case <-timeout.Done():
t.Fatal("timed out waiting for missing action completion")
}
}
func TestAction_Core_PerformAsync_Ugly(t *T) {
c := New()
completed := make(chan ActionTaskCompleted, 1)
c.Action("agent.panics", func(_ Context, _ Options) Result {
panic("dispatch blew up")
})
c.RegisterAction(func(_ *Core, msg Message) Result {
if evt, ok := msg.(ActionTaskCompleted); ok {
completed <- evt
}
return Result{OK: true}
})
r := c.PerformAsync("agent.panics", NewOptions())
AssertTrue(t, r.OK)
timeout, cancel := WithTimeout(Background(), 2*Second)
defer cancel()
select {
case evt := <-completed:
AssertFalse(t, evt.Result.OK)
AssertContains(t, evt.Result.Error(), "panic")
case <-timeout.Done():
t.Fatal("timed out waiting for panic completion")
}
}
func TestAction_Core_Progress_Good(t *T) {
c := New()
progress := make(chan ActionTaskProgress, 1)
c.RegisterAction(func(_ *Core, msg Message) Result {
if evt, ok := msg.(ActionTaskProgress); ok {
progress <- evt
}
return Result{OK: true}
})
c.Progress("task-1", 0.5, "halfway", "agent.dispatch")
timeout, cancel := WithTimeout(Background(), 2*Second)
defer cancel()
select {
case evt := <-progress:
AssertEqual(t, "task-1", evt.TaskIdentifier)
AssertEqual(t, 0.5, evt.Progress)
AssertEqual(t, "halfway", evt.Message)
AssertEqual(t, "agent.dispatch", evt.Action)
case <-timeout.Done():
t.Fatal("timed out waiting for progress event")
}
}
func TestAction_Core_Progress_Bad(t *T) {
c := New()
// Reporting progress for an unknown task is a safe no-op, not a panic.
AssertNotPanics(t, func() {
c.Progress("task-absent", -1, "refused", "agent.dispatch")
})
}
func TestAction_Core_Progress_Ugly(t *T) {
c := New()
progress := make(chan ActionTaskProgress, 1)
c.RegisterAction(func(_ *Core, msg Message) Result {
if evt, ok := msg.(ActionTaskProgress); ok {
progress <- evt
}
return Result{OK: true}
})
c.Progress("", 1, "", "")
timeout, cancel := WithTimeout(Background(), 2*Second)
defer cancel()
select {
case evt := <-progress:
AssertEqual(t, "", evt.TaskIdentifier)
AssertEqual(t, 1.0, evt.Progress)
AssertEqual(t, "", evt.Message)
AssertEqual(t, "", evt.Action)
case <-timeout.Done():
t.Fatal("timed out waiting for empty progress event")
}
}
// --- Action.Enable / Disable / Enabled ---
func TestAction_Action_Enable_Good(t *T) {
c := New()
c.Action("agent.dispatch", func(_ Context, _ Options) Result { return Result{OK: true} })
a := c.Action("agent.dispatch")
a.Disable()
AssertFalse(t, a.Enabled())
a.Enable()
AssertTrue(t, a.Enabled())
}
func TestAction_Action_Enable_Bad(t *T) {
// Enable on nil action is a no-op (no panic).
var a *Action
a.Enable()
AssertFalse(t, a.Enabled())
}
func TestAction_Action_Enable_Ugly(t *T) {
// Enable on action with no handler stays disabled.
a := &Action{Name: "nohandler"}
a.Enable()
AssertFalse(t, a.Enabled())
}
func TestAction_Action_Disable_Good(t *T) {
c := New()
c.Action("agent.dispatch", func(_ Context, _ Options) Result { return Result{OK: true} })
a := c.Action("agent.dispatch")
a.Disable()
r := a.Run(Background(), NewOptions())
AssertFalse(t, r.OK)
AssertContains(t, r.Error(), "disabled")
}
func TestAction_Action_Disable_Bad(t *T) {
var a *Action
a.Disable()
AssertFalse(t, a.Enabled())
}
func TestAction_Action_Disable_Ugly(t *T) {
// Disable then Enable round-trips.
c := New()
c.Action("agent.dispatch", func(_ Context, _ Options) Result { return Result{OK: true} })
a := c.Action("agent.dispatch")
a.Disable()
a.Enable()
r := a.Run(Background(), NewOptions())
AssertTrue(t, r.OK)
}
func TestAction_Action_Enabled_Good(t *T) {
c := New()
c.Action("agent.dispatch", func(_ Context, _ Options) Result { return Result{OK: true} })
AssertTrue(t, c.Action("agent.dispatch").Enabled())
}
func TestAction_Action_Enabled_Bad(t *T) {
var a *Action
AssertFalse(t, a.Enabled())
}
func TestAction_Action_Enabled_Ugly(t *T) {
// Action with no handler is not enabled.
a := &Action{Name: "x"}
AssertFalse(t, a.Enabled())
}
// --- W3: schema validation, remote colon dispatch, task ID injection ---
func TestAction_Run_Good_SchemaSatisfied(t *T) {
c := New()
a := c.Action("schema.check", func(_ Context, opts Options) Result { return Ok(opts.String("dir")) })
a.Schema = NewOptions(Option{Key: "dir", Value: "required"})
r := c.Action("schema.check").Run(Background(), NewOptions(Option{Key: "dir", Value: "/tmp"}))
AssertTrue(t, r.OK)
AssertEqual(t, "/tmp", r.Value.(string))
}
func TestAction_Run_Bad_SchemaMissingKey(t *T) {
c := New()
a := c.Action("schema.check", func(Context, Options) Result { return Ok(nil) })
a.Schema = NewOptions(Option{Key: "dir", Value: "required"})
r := c.Action("schema.check").Run(Background(), NewOptions())
AssertFalse(t, r.OK)
AssertEqual(t, "action.schema", r.Code())
}
func TestAction_Action_Good_RemoteColon(t *T) {
c := New()
c.API().RegisterProtocol("http", mockFactory(`{"ok":true}`))
c.Drive().New(NewOptions(
Option{Key: "name", Value: "charon"},
Option{Key: "transport", Value: "http://10.69.69.165:9101/mcp"},
))
a := c.Action("charon:agentic.status")
AssertTrue(t, a.Exists())
r := a.Run(Background(), NewOptions())
AssertTrue(t, r.OK)
}
func TestAction_Action_Bad_RemoteColonNoHandle(t *T) {
// No Drive handle for the host — the colon name stays a zombie.
AssertFalse(t, New().Action("ghost:x.y").Exists())
}
func TestAction_Action_Ugly_LocalBeatsColon(t *T) {
c := New()
c.Drive().New(NewOptions(
Option{Key: "name", Value: "charon"},
Option{Key: "transport", Value: "http://10.69.69.165:9101/mcp"},
))
// A local registration under a colon name wins over remote synthesis.
c.Action("charon:agentic.status", func(Context, Options) Result { return Ok("local") })
r := c.Action("charon:agentic.status").Run(Background(), NewOptions())
AssertEqual(t, "local", r.Value.(string))
}
func TestAction_PerformAsync_Good_TaskIDInjected(t *T) {
c := New()
got := make(chan string, 1)
c.Action("task.echo", func(_ Context, opts Options) Result {
got <- opts.String("_task")
return Ok(nil)
})
r := c.PerformAsync("task.echo", NewOptions())
AssertEqual(t, r.Value.(string), <-got)
}
func TestAction_PerformAsync_Ugly_CallerOptionsUntouched(t *T) {
c := New()
done := make(chan struct{})
c.Action("task.noop", func(Context, Options) Result { close(done); return Ok(nil) })
opts := NewOptions(Option{Key: "k", Value: "v"})
c.PerformAsync("task.noop", opts)
<-done
AssertFalse(t, opts.Has("_task"))
}