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 TestCoordinatorGivesQueuedUserInputPriority(t *testing.T) {
24 c := New()
25 c.Apply(Event{Kind: EventManagerStarted})
26 c.Apply(Event{Kind: EventWorkerRegistered, WorkerID: "worker"})
27 c.Apply(Event{Kind: EventManagerFinished})
28 image := provider.ImageBlock{MimeType: "image/png", Data: []byte("png-1")}
29 c.Apply(Event{Kind: EventUserInput, Text: "stop and explain", Images: []provider.ImageBlock{image}})
30 assertActions(t, c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "worker", Completion: subagents.Completion{AgentID: "worker", Status: "failed"}}), []Action{{
31 Kind: ActionRunManager, Reason: WakeUser, Text: "stop and explain", Images: []provider.ImageBlock{image},
32 Completions: []subagents.Completion{{AgentID: "worker", Status: "failed"}},
33 }})
34}
35
36func TestCoordinatorUsesGoalOnlyWhenIdle(t *testing.T) {
37 c := New()
38 c.Apply(Event{Kind: EventGoalChanged, GoalActive: true})
39 c.Apply(Event{Kind: EventManagerStarted})
40 assertActions(t, c.Apply(Event{Kind: EventManagerFinished}), []Action{{Kind: ActionRunManager, Reason: WakeGoal}})
41}
42
43func TestCoordinatorStopsAndIgnoresLateWorkerEvents(t *testing.T) {
44 c := New()
45 c.Apply(Event{Kind: EventManagerStarted})
46 c.Apply(Event{Kind: EventWorkerRegistered, WorkerID: "worker"})
47 assertActions(t, c.Apply(Event{Kind: EventCancelled}), []Action{{Kind: ActionStop}})
48 assertActions(t, c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "worker"}), nil)
49}
50
51func assertActions(t *testing.T, got Result, want []Action) {
52 t.Helper()
53 if len(got.Actions) != len(want) {
54 t.Fatalf("actions = %#v, want %#v", got.Actions, want)
55 }
56 for i := range want {
57 if got.Actions[i].Kind != want[i].Kind || got.Actions[i].Reason != want[i].Reason || got.Actions[i].Text != want[i].Text {
58 t.Fatalf("action %d = %#v, want %#v", i, got.Actions[i], want[i])
59 }
60 if len(got.Actions[i].Images) != len(want[i].Images) {
61 t.Fatalf("action %d images = %#v, want %#v", i, got.Actions[i].Images, want[i].Images)
62 }
63 for j := range want[i].Images {
64 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) {
65 t.Fatalf("action %d image %d = %#v, want %#v", i, j, got.Actions[i].Images[j], want[i].Images[j])
66 }
67 }
68 if len(got.Actions[i].Completions) != len(want[i].Completions) {
69 t.Fatalf("action %d completions = %#v, want %#v", i, got.Actions[i].Completions, want[i].Completions)
70 }
71 for j := range want[i].Completions {
72 if got.Actions[i].Completions[j].AgentID != want[i].Completions[j].AgentID || got.Actions[i].Completions[j].Status != want[i].Completions[j].Status {
73 t.Fatalf("action %d completion %d = %#v, want %#v", i, j, got.Actions[i].Completions[j], want[i].Completions[j])
74 }
75 }
76 }
77}