1package orchestration
2
3import (
4 "testing"
5
6 "github.com/bnema/zut/packages/agent/subagents"
7 "github.com/bnema/zut/packages/provider"
8)
9
10func TestCoordinatorSealsWorkerWaveAndWakesOnce(t *testing.T) {
11 c := New()
12 c.Apply(Event{Kind: EventManagerStarted})
13 c.Apply(Event{Kind: EventWorkerRegistered, WorkerID: "a"})
14 c.Apply(Event{Kind: EventWorkerRegistered, WorkerID: "b"})
15 assertActions(t, c.Apply(Event{Kind: EventManagerFinished}), []Action{{Kind: ActionWait}})
16 assertActions(t, c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "b", Completion: subagents.Completion{AgentID: "b"}}), nil)
17 assertActions(t, c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "a", Completion: subagents.Completion{AgentID: "a"}}), []Action{{
18 Kind: ActionRunManager, Reason: WakeWorkers,
19 Completions: []subagents.Completion{{AgentID: "a"}, {AgentID: "b"}},
20 }})
21}
22
23func TestCoordinatorSlidesCompletionIntoActiveManagerOnce(t *testing.T) {
24 c := New()
25 c.Apply(Event{Kind: EventManagerStarted})
26 c.Apply(Event{Kind: EventWorkerRegistered, WorkerID: "a"})
27 c.Apply(Event{Kind: EventWorkerRegistered, WorkerID: "b"})
28 completion := subagents.Completion{AgentID: "a", Status: "completed"}
29 assertActions(t, c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "a", Completion: completion}), []Action{{
30 Kind: ActionQueueManager, Reason: WakeWorkers, Completions: []subagents.Completion{completion},
31 }})
32 assertActions(t, c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "a", Completion: completion}), nil)
33 assertActions(t, c.Apply(Event{Kind: EventManagerFinished}), []Action{{Kind: ActionWait}})
34 assertActions(t, c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "b", Completion: subagents.Completion{AgentID: "b"}}), []Action{{
35 Kind: ActionRunManager, Reason: WakeWorkers, Completions: []subagents.Completion{{AgentID: "b"}},
36 }})
37}
38
39func TestCoordinatorDoesNotWakeForAlreadyQueuedCompletion(t *testing.T) {
40 c := New()
41 c.Apply(Event{Kind: EventManagerStarted})
42 c.Apply(Event{Kind: EventWorkerRegistered, WorkerID: "a"})
43 c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "a"})
44 assertActions(t, c.Apply(Event{Kind: EventManagerFinished}), nil)
45}
46
47func TestCoordinatorGivesQueuedUserInputPriority(t *testing.T) {
48 c := New()
49 c.Apply(Event{Kind: EventManagerStarted})
50 c.Apply(Event{Kind: EventWorkerRegistered, WorkerID: "worker"})
51 c.Apply(Event{Kind: EventManagerFinished})
52 image := provider.ImageBlock{MimeType: "image/png", Data: []byte("png-1")}
53 c.Apply(Event{Kind: EventUserInput, Text: "stop and explain", Images: []provider.ImageBlock{image}})
54 assertActions(t, c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "worker", Completion: subagents.Completion{AgentID: "worker", Status: "failed"}}), []Action{{
55 Kind: ActionRunManager, Reason: WakeUser, Text: "stop and explain", Images: []provider.ImageBlock{image},
56 Completions: []subagents.Completion{{AgentID: "worker", Status: "failed"}},
57 }})
58}
59
60func TestCoordinatorUsesGoalOnlyWhenIdle(t *testing.T) {
61 c := New()
62 c.Apply(Event{Kind: EventGoalChanged, GoalActive: true})
63 c.Apply(Event{Kind: EventManagerStarted})
64 assertActions(t, c.Apply(Event{Kind: EventManagerFinished}), []Action{{Kind: ActionRunManager, Reason: WakeGoal}})
65}
66
67func TestCoordinatorStopsAndIgnoresLateWorkerEvents(t *testing.T) {
68 c := New()
69 c.Apply(Event{Kind: EventManagerStarted})
70 c.Apply(Event{Kind: EventWorkerRegistered, WorkerID: "worker"})
71 assertActions(t, c.Apply(Event{Kind: EventCancelled}), []Action{{Kind: ActionStop}})
72 assertActions(t, c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "worker"}), nil)
73}
74
75func assertActions(t *testing.T, got Result, want []Action) {
76 t.Helper()
77 if len(got.Actions) != len(want) {
78 t.Fatalf("actions = %#v, want %#v", got.Actions, want)
79 }
80 for i := range want {
81 if got.Actions[i].Kind != want[i].Kind || got.Actions[i].Reason != want[i].Reason || got.Actions[i].Text != want[i].Text {
82 t.Fatalf("action %d = %#v, want %#v", i, got.Actions[i], want[i])
83 }
84 if len(got.Actions[i].Images) != len(want[i].Images) {
85 t.Fatalf("action %d images = %#v, want %#v", i, got.Actions[i].Images, want[i].Images)
86 }
87 for j := range want[i].Images {
88 if got.Actions[i].Images[j].MimeType != want[i].Images[j].MimeType || string(got.Actions[i].Images[j].Data) != string(want[i].Images[j].Data) {
89 t.Fatalf("action %d image %d = %#v, want %#v", i, j, got.Actions[i].Images[j], want[i].Images[j])
90 }
91 }
92 if len(got.Actions[i].Completions) != len(want[i].Completions) {
93 t.Fatalf("action %d completions = %#v, want %#v", i, got.Actions[i].Completions, want[i].Completions)
94 }
95 for j := range want[i].Completions {
96 if got.Actions[i].Completions[j].AgentID != want[i].Completions[j].AgentID || got.Actions[i].Completions[j].Status != want[i].Completions[j].Status {
97 t.Fatalf("action %d completion %d = %#v, want %#v", i, j, got.Actions[i].Completions[j], want[i].Completions[j])
98 }
99 }
100 }
101}