Skip to content

Commit 08dbd18

Browse files
committed
test(ui): prove session hydration reconnect ownership
1 parent 952c903 commit 08dbd18

1 file changed

Lines changed: 63 additions & 2 deletions

File tree

‎ui/src/lib/sessions/connection-hydration.test.ts‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ describe("session connection hydration", () => {
8787
sessionKey: "agent:main:main",
8888
assistantAgentId: "main" as string | null,
8989
hello: null as GatewayHelloOk | null,
90+
canvasPluginSurfaceUrl: null as string | null,
91+
selfUser: null as { id: string; name?: string } | null,
9092
};
9193
let gatewayListener: ((next: typeof snapshot) => void) | undefined;
9294
const sessions = createSessionCapability({
@@ -104,8 +106,10 @@ describe("session connection hydration", () => {
104106
gatewayListener?.(snapshot);
105107
await waitForFast(() => expect(listCalls).toBe(1));
106108

107-
gatewayListener?.({ ...snapshot });
108-
gatewayListener?.({ ...snapshot });
109+
snapshot = { ...snapshot, canvasPluginSurfaceUrl: "https://gateway.example.test/canvas" };
110+
gatewayListener?.(snapshot);
111+
snapshot = { ...snapshot, selfUser: { id: "operator", name: "Operator" } };
112+
gatewayListener?.(snapshot);
109113
resolveList(result);
110114
await waitForFast(() => expect(sessions.state.result).toBe(result));
111115
await Promise.resolve();
@@ -114,4 +118,61 @@ describe("session connection hydration", () => {
114118
expect(listCalls).toBe(1);
115119
sessions.dispose();
116120
});
121+
122+
it("hydrates again after the current client reconnects", async () => {
123+
let listCalls = 0;
124+
const result: SessionsListResult = {
125+
ts: 1,
126+
path: "(multiple)",
127+
count: 0,
128+
defaults: { modelProvider: null, model: null, contextTokens: null },
129+
sessions: [],
130+
};
131+
const request = vi.fn(async (method: string) => {
132+
if (method === "sessions.subscribe") {
133+
return {};
134+
}
135+
if (method === "sessions.list") {
136+
listCalls += 1;
137+
return result;
138+
}
139+
throw new Error(`Unexpected request: ${method}`);
140+
});
141+
const client = { request } as unknown as GatewayBrowserClient;
142+
let snapshot = {
143+
client,
144+
phase: "connected" as "connected" | "reconnecting",
145+
sessionKey: "agent:main:main",
146+
assistantAgentId: "main" as string | null,
147+
hello: null as GatewayHelloOk | null,
148+
canvasPluginSurfaceUrl: null as string | null,
149+
selfUser: null as { id: string; name?: string } | null,
150+
};
151+
let gatewayListener: ((next: typeof snapshot) => void) | undefined;
152+
const sessions = createSessionCapability({
153+
get snapshot() {
154+
return snapshot;
155+
},
156+
subscribe(listener) {
157+
gatewayListener = listener;
158+
return () => undefined;
159+
},
160+
subscribeEvents: () => () => undefined,
161+
});
162+
163+
gatewayListener?.(snapshot);
164+
await waitForFast(() => expect(listCalls).toBe(1));
165+
await waitForFast(() => expect(sessions.state.result).toBe(result));
166+
167+
snapshot = { ...snapshot, phase: "reconnecting" };
168+
gatewayListener?.(snapshot);
169+
expect(sessions.state.result).toBeNull();
170+
171+
snapshot = { ...snapshot, phase: "connected" };
172+
gatewayListener?.(snapshot);
173+
await waitForFast(() => expect(listCalls).toBe(2));
174+
await waitForFast(() => expect(sessions.state.result).toBe(result));
175+
176+
sessions.dispose();
177+
});
117178
});

0 commit comments

Comments
 (0)