Skip to content

Commit 8dd2e1e

Browse files
committed
test(ui): split swarm activity integration coverage
1 parent 2587bc4 commit 8dd2e1e

2 files changed

Lines changed: 173 additions & 122 deletions

File tree

‎ui/src/lib/sessions/index.test.ts‎

Lines changed: 1 addition & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
type GatewayEventFrame,
66
type GatewayHelloOk,
77
} from "../../api/gateway.ts";
8-
import type { GatewaySessionRow, SessionsListResult } from "../../api/types.ts";
8+
import type { SessionsListResult } from "../../api/types.ts";
99
import { waitForFast } from "../../test-helpers/wait-for.ts";
1010
import { createSessionCapability, reconcileSessionRunTerminal } from "./index.ts";
1111

@@ -1079,125 +1079,4 @@ describe("createSessionCapability", () => {
10791079
expect(request).toHaveBeenCalledTimes(2);
10801080
sessions.dispose();
10811081
});
1082-
1083-
it("keeps chronological swarm phase and log annotations across canonical refreshes", async () => {
1084-
const parentKey = "agent:main:main";
1085-
const groupId = "swarm:agent:main:main:turn-42";
1086-
let rows: SessionsListResult["sessions"] = [
1087-
{ key: parentKey, kind: "direct", updatedAt: 1 },
1088-
{
1089-
key: "agent:main:subagent:older",
1090-
kind: "direct",
1091-
parentSessionKey: parentKey,
1092-
swarmGroupId: groupId,
1093-
status: "running",
1094-
updatedAt: 2,
1095-
},
1096-
];
1097-
let ts = 1;
1098-
const request = vi.fn(async (method: string) => {
1099-
if (method !== "sessions.list") {
1100-
throw new Error(`Unexpected request: ${method}`);
1101-
}
1102-
return sessionsResult(rows, ts++);
1103-
});
1104-
const client = { request } as unknown as GatewayBrowserClient;
1105-
const { gateway, emitEvent } = createGatewayHarness(client);
1106-
const sessions = createSessionCapability(gateway);
1107-
const note = (kind: "phase" | "log", text: string) => ({
1108-
sessionKey: parentKey,
1109-
reason: "swarm-note",
1110-
swarmGroupId: groupId,
1111-
kind,
1112-
text,
1113-
key: parentKey,
1114-
updatedAt: 1,
1115-
});
1116-
const child = (key: string, updatedAt: number) => ({
1117-
sessionKey: key,
1118-
reason: "create",
1119-
key,
1120-
kind: "direct",
1121-
parentSessionKey: parentKey,
1122-
swarmGroupId: groupId,
1123-
status: "running",
1124-
updatedAt,
1125-
});
1126-
const emitChanged = (payload: Record<string, unknown>) =>
1127-
emitEvent({ type: "event", event: "sessions.changed", payload });
1128-
1129-
await sessions.refresh({ force: true });
1130-
emitChanged(note("phase", "Plan"));
1131-
await waitForFast(() => expect(request).toHaveBeenCalledTimes(2));
1132-
rows = [
1133-
...rows,
1134-
{
1135-
key: "agent:main:subagent:planner",
1136-
kind: "direct",
1137-
parentSessionKey: parentKey,
1138-
swarmGroupId: groupId,
1139-
status: "running",
1140-
updatedAt: 3,
1141-
},
1142-
];
1143-
emitChanged(child("agent:main:subagent:planner", 3));
1144-
await waitForFast(() =>
1145-
expect(sessions.state.result?.sessions.some((row) => row.key.endsWith(":planner"))).toBe(
1146-
true,
1147-
),
1148-
);
1149-
1150-
type SwarmDisplayRow = GatewaySessionRow & { swarmLog?: string; swarmPhase?: string };
1151-
const displayRows = () => sessions.state.result?.sessions as SwarmDisplayRow[] | undefined;
1152-
expect(displayRows()?.find((row) => row.key.endsWith(":older"))?.swarmPhase).toBeUndefined();
1153-
expect(displayRows()?.find((row) => row.key.endsWith(":planner"))?.swarmPhase).toBe("Plan");
1154-
1155-
emitChanged(note("log", "Planning is complete."));
1156-
await waitForFast(() =>
1157-
expect(
1158-
displayRows()
1159-
?.filter((row) => row.swarmGroupId === groupId)
1160-
.map((row) => row.swarmLog),
1161-
).toEqual(["Planning is complete.", "Planning is complete."]),
1162-
);
1163-
1164-
emitChanged(note("phase", "Build"));
1165-
rows = [
1166-
...rows,
1167-
{
1168-
key: "agent:main:subagent:builder",
1169-
kind: "direct",
1170-
parentSessionKey: parentKey,
1171-
swarmGroupId: groupId,
1172-
status: "running",
1173-
updatedAt: 4,
1174-
},
1175-
];
1176-
emitChanged(child("agent:main:subagent:builder", 4));
1177-
await waitForFast(() =>
1178-
expect(sessions.state.result?.sessions.some((row) => row.key.endsWith(":builder"))).toBe(
1179-
true,
1180-
),
1181-
);
1182-
1183-
expect(displayRows()?.find((row) => row.key.endsWith(":planner"))?.swarmPhase).toBe("Plan");
1184-
expect(displayRows()?.find((row) => row.key.endsWith(":builder"))?.swarmPhase).toBe("Build");
1185-
1186-
// A pre-phase child receiving a plain status update after phases were
1187-
// announced must stay Unphased; only creation events assign implicitly.
1188-
emitChanged({
1189-
sessionKey: "agent:main:subagent:older",
1190-
reason: "status",
1191-
key: "agent:main:subagent:older",
1192-
kind: "direct",
1193-
parentSessionKey: parentKey,
1194-
swarmGroupId: groupId,
1195-
status: "done",
1196-
updatedAt: 5,
1197-
});
1198-
await waitForFast(() =>
1199-
expect(displayRows()?.find((row) => row.key.endsWith(":older"))?.swarmPhase).toBeUndefined(),
1200-
);
1201-
sessions.dispose();
1202-
});
12031082
});
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import type { GatewayBrowserClient, GatewayEventFrame, GatewayHelloOk } from "../../api/gateway.ts";
3+
import type { GatewaySessionRow, SessionsListResult } from "../../api/types.ts";
4+
import { waitForFast } from "../../test-helpers/wait-for.ts";
5+
import { createSessionCapability } from "./index.ts";
6+
7+
function sessionsResult(sessions: SessionsListResult["sessions"], ts: number): SessionsListResult {
8+
return {
9+
ts,
10+
path: "(multiple)",
11+
count: sessions.length,
12+
defaults: { modelProvider: null, model: null, contextTokens: null },
13+
sessions,
14+
};
15+
}
16+
17+
function createGatewayHarness(client: GatewayBrowserClient) {
18+
const snapshot: {
19+
client: GatewayBrowserClient | null;
20+
connected: boolean;
21+
sessionKey: string;
22+
assistantAgentId: string | null;
23+
hello: GatewayHelloOk | null;
24+
} = {
25+
client,
26+
connected: true,
27+
sessionKey: "agent:main:main",
28+
assistantAgentId: "main",
29+
hello: null,
30+
};
31+
const eventListeners = new Set<(event: GatewayEventFrame) => void>();
32+
return {
33+
gateway: {
34+
get snapshot() {
35+
return snapshot;
36+
},
37+
subscribe: () => () => undefined,
38+
subscribeEvents(listener: (event: GatewayEventFrame) => void) {
39+
eventListeners.add(listener);
40+
return () => eventListeners.delete(listener);
41+
},
42+
},
43+
emitEvent: (event: GatewayEventFrame) => {
44+
for (const listener of eventListeners) {
45+
listener(event);
46+
}
47+
},
48+
};
49+
}
50+
51+
describe("session swarm activity", () => {
52+
it("keeps chronological phase and log annotations across canonical refreshes", async () => {
53+
const parentKey = "agent:main:main";
54+
const groupId = "swarm:agent:main:main:turn-42";
55+
let rows: SessionsListResult["sessions"] = [
56+
{ key: parentKey, kind: "direct", updatedAt: 1 },
57+
{
58+
key: "agent:main:subagent:older",
59+
kind: "direct",
60+
parentSessionKey: parentKey,
61+
swarmGroupId: groupId,
62+
status: "running",
63+
updatedAt: 2,
64+
},
65+
];
66+
let ts = 1;
67+
const request = vi.fn(async (method: string) => {
68+
if (method !== "sessions.list") {
69+
throw new Error(`Unexpected request: ${method}`);
70+
}
71+
return sessionsResult(rows, ts++);
72+
});
73+
const client = { request } as unknown as GatewayBrowserClient;
74+
const { gateway, emitEvent } = createGatewayHarness(client);
75+
const sessions = createSessionCapability(gateway);
76+
const note = (kind: "phase" | "log", text: string) => ({
77+
sessionKey: parentKey,
78+
reason: "swarm-note",
79+
swarmGroupId: groupId,
80+
kind,
81+
text,
82+
key: parentKey,
83+
updatedAt: 1,
84+
});
85+
const child = (key: string, updatedAt: number) => ({
86+
sessionKey: key,
87+
reason: "create",
88+
key,
89+
kind: "direct",
90+
parentSessionKey: parentKey,
91+
swarmGroupId: groupId,
92+
status: "running",
93+
updatedAt,
94+
});
95+
const emitChanged = (payload: Record<string, unknown>) =>
96+
emitEvent({ type: "event", event: "sessions.changed", payload });
97+
98+
await sessions.refresh({ force: true });
99+
emitChanged(note("phase", "Plan"));
100+
await waitForFast(() => expect(request).toHaveBeenCalledTimes(2));
101+
rows = [
102+
...rows,
103+
{
104+
key: "agent:main:subagent:planner",
105+
kind: "direct",
106+
parentSessionKey: parentKey,
107+
swarmGroupId: groupId,
108+
status: "running",
109+
updatedAt: 3,
110+
},
111+
];
112+
emitChanged(child("agent:main:subagent:planner", 3));
113+
await waitForFast(() =>
114+
expect(sessions.state.result?.sessions.some((row) => row.key.endsWith(":planner"))).toBe(
115+
true,
116+
),
117+
);
118+
119+
type SwarmDisplayRow = GatewaySessionRow & { swarmLog?: string; swarmPhase?: string };
120+
const displayRows = () => sessions.state.result?.sessions as SwarmDisplayRow[] | undefined;
121+
expect(displayRows()?.find((row) => row.key.endsWith(":older"))?.swarmPhase).toBeUndefined();
122+
expect(displayRows()?.find((row) => row.key.endsWith(":planner"))?.swarmPhase).toBe("Plan");
123+
124+
emitChanged(note("log", "Planning is complete."));
125+
await waitForFast(() =>
126+
expect(
127+
displayRows()
128+
?.filter((row) => row.swarmGroupId === groupId)
129+
.map((row) => row.swarmLog),
130+
).toEqual(["Planning is complete.", "Planning is complete."]),
131+
);
132+
133+
emitChanged(note("phase", "Build"));
134+
rows = [
135+
...rows,
136+
{
137+
key: "agent:main:subagent:builder",
138+
kind: "direct",
139+
parentSessionKey: parentKey,
140+
swarmGroupId: groupId,
141+
status: "running",
142+
updatedAt: 4,
143+
},
144+
];
145+
emitChanged(child("agent:main:subagent:builder", 4));
146+
await waitForFast(() =>
147+
expect(sessions.state.result?.sessions.some((row) => row.key.endsWith(":builder"))).toBe(
148+
true,
149+
),
150+
);
151+
152+
expect(displayRows()?.find((row) => row.key.endsWith(":planner"))?.swarmPhase).toBe("Plan");
153+
expect(displayRows()?.find((row) => row.key.endsWith(":builder"))?.swarmPhase).toBe("Build");
154+
155+
// Only creation events assign an implicit phase; a later status update
156+
// must leave a child that predates all phase notes unphased.
157+
emitChanged({
158+
sessionKey: "agent:main:subagent:older",
159+
reason: "status",
160+
key: "agent:main:subagent:older",
161+
kind: "direct",
162+
parentSessionKey: parentKey,
163+
swarmGroupId: groupId,
164+
status: "done",
165+
updatedAt: 5,
166+
});
167+
await waitForFast(() =>
168+
expect(displayRows()?.find((row) => row.key.endsWith(":older"))?.swarmPhase).toBeUndefined(),
169+
);
170+
sessions.dispose();
171+
});
172+
});

0 commit comments

Comments
 (0)