1package orchestration
2
3import (
4 "testing"
5
6 "github.com/bnema/zut/packages/agent/subagents"
7)
8
9func TestCoordinatorSealsWorkerWaveAndWakesOnce(t *testing.T) {
10 c := New()
11 c.Apply(Event{Kind: EventManagerStarted})
12 c.Apply(Event{Kind: EventWorkerRegistered, WorkerID: "a"})
13 c.Apply(Event{Kind: EventWorkerRegistered, WorkerID: "b"})
14 assertActions(t, c.Apply(Event{Kind: EventManagerFinished}), []Action{{Kind: ActionWait}})
15 assertActions(t, c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "b", Completion: subagents.Completion{AgentID: "b"}}), nil)
16 assertActions(t, c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "a", Completion: subagents.Completion{AgentID: "a"}}), []Action{{
17 Kind: ActionRunManager, Reason: WakeWorkers,
18 Completions: []subagents.Completion{{AgentID: "a"}, {AgentID: "b"}},
19 }})
20}
21
22func TestCoordinatorGivesQueuedUserInputPriority(t *testing.T) {
23 c := New()
24 c.Apply(Event{Kind: EventManagerStarted})
25 c.Apply(Event{Kind: EventWorkerRegistered, WorkerID: "worker"})
26 c.Apply(Event{Kind: EventManagerFinished})
27 c.Apply(Event{Kind: EventUserInput, Text: "stop and explain"})
28 assertActions(t, c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "worker", Completion: subagents.Completion{AgentID: "worker", Status: "failed"}}), []Action{{
29 Kind: ActionRunManager, Reason: WakeUser, Text: "stop and explain",
30 Completions: []subagents.Completion{{AgentID: "worker", Status: "failed"}},
31 }})
32}
33
34func TestCoordinatorUsesGoalOnlyWhenIdle(t *testing.T) {
35 c := New()
36 c.Apply(Event{Kind: EventGoalChanged, GoalActive: true})
37 c.Apply(Event{Kind: EventManagerStarted})
38 assertActions(t, c.Apply(Event{Kind: EventManagerFinished}), []Action{{Kind: ActionRunManager, Reason: WakeGoal}})
39}
40
41func TestCoordinatorStopsAndIgnoresLateWorkerEvents(t *testing.T) {
42 c := New()
43 c.Apply(Event{Kind: EventManagerStarted})
44 c.Apply(Event{Kind: EventWorkerRegistered, WorkerID: "worker"})
45 assertActions(t, c.Apply(Event{Kind: EventCancelled}), []Action{{Kind: ActionStop}})
46 assertActions(t, c.Apply(Event{Kind: EventWorkerFinished, WorkerID: "worker"}), nil)
47}
48
49func assertActions(t *testing.T, got Result, want []Action) {
50 t.Helper()
51 if len(got.Actions) != len(want) {
52 t.Fatalf("actions = %#v, want %#v", got.Actions, want)
53 }
54 for i := range want {
55 if got.Actions[i].Kind != want[i].Kind || got.Actions[i].Reason != want[i].Reason || got.Actions[i].Text != want[i].Text {
56 t.Fatalf("action %d = %#v, want %#v", i, got.Actions[i], want[i])
57 }
58 if len(got.Actions[i].Completions) != len(want[i].Completions) {
59 t.Fatalf("action %d completions = %#v, want %#v", i, got.Actions[i].Completions, want[i].Completions)
60 }
61 for j := range want[i].Completions {
62 if got.Actions[i].Completions[j].AgentID != want[i].Completions[j].AgentID || got.Actions[i].Completions[j].Status != want[i].Completions[j].Status {
63 t.Fatalf("action %d completion %d = %#v, want %#v", i, j, got.Actions[i].Completions[j], want[i].Completions[j])
64 }
65 }
66 }
67}