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