Preserve live stream subscriptions across explicit reconnects (#1972)

This commit is contained in:
Julius Marminge committed 2026-04-13 14:41:46 -07:00
1 parent 6e88d40e60
commit a4ca7cecdc
2 files changed
+114 -1

No files matched your search

+109
View File
@@ -639,6 +639,115 @@ describe("WsTransport", () => {
await transport.dispose();
});
it("re-subscribes live stream listeners after an explicit transport reconnect", async () => {
const transport = createTransport("ws://localhost:3020");
const listener = vi.fn();
const onResubscribe = vi.fn();
const unsubscribe = transport.subscribe(
(client) => client[WS_METHODS.subscribeServerLifecycle]({}),
listener,
{ onResubscribe },
);
await waitFor(() => {
expect(sockets).toHaveLength(1);
});
const firstSocket = getSocket();
firstSocket.open();
await waitFor(() => {
expect(firstSocket.sent).toHaveLength(1);
});
const firstRequest = JSON.parse(firstSocket.sent[0] ?? "{}") as { id: string };
const firstEvent = {
version: 1,
sequence: 1,
type: "welcome",
payload: {
environment: {
environmentId: "environment-local",
label: "Local environment",
platform: { os: "darwin", arch: "arm64" },
serverVersion: "0.0.0-test",
capabilities: { repositoryIdentity: true },
},
cwd: "/tmp/one",
projectName: "one",
},
};
firstSocket.serverMessage(
JSON.stringify({
_tag: "Chunk",
requestId: firstRequest.id,
values: [firstEvent],
}),
);
await waitFor(() => {
expect(listener).toHaveBeenLastCalledWith(firstEvent);
});
await transport.reconnect();
await waitFor(() => {
expect(sockets).toHaveLength(2);
});
const secondSocket = getSocket();
expect(secondSocket).not.toBe(firstSocket);
expect(firstSocket.readyState).toBe(MockWebSocket.CLOSED);
secondSocket.open();
await waitFor(() => {
expect(secondSocket.sent).toHaveLength(1);
});
const secondRequest = JSON.parse(secondSocket.sent[0] ?? "{}") as {
id: string;
tag: string;
};
expect(secondRequest.tag).toBe(WS_METHODS.subscribeServerLifecycle);
expect(secondRequest.id).not.toBe(firstRequest.id);
expect(onResubscribe).toHaveBeenCalledOnce();
const secondEvent = {
version: 1,
sequence: 2,
type: "welcome",
payload: {
environment: {
environmentId: "environment-local",
label: "Local environment",
platform: { os: "darwin", arch: "arm64" },
serverVersion: "0.0.0-test",
capabilities: { repositoryIdentity: true },
},
cwd: "/tmp/two",
projectName: "two",
},
};
secondSocket.serverMessage(
JSON.stringify({
_tag: "Chunk",
requestId: secondRequest.id,
values: [secondEvent],
}),
);
await waitFor(() => {
expect(listener).toHaveBeenLastCalledWith(secondEvent);
});
unsubscribe();
await transport.dispose();
});
it("does not fire onResubscribe when the first stream attempt exits before any value", async () => {
const transport = createTransport("ws://localhost:3020");
const listener = vi.fn();
+5 -1
View File
@@ -121,6 +121,7 @@ export class WsTransport {
return;
}
const session = this.session;
try {
if (hasReceivedValue) {
try {
@@ -130,7 +131,6 @@ export class WsTransport {
}
}
const session = this.session;
const runningStream = this.runStreamOnSession(
session,
connect,
@@ -150,6 +150,10 @@ export class WsTransport {
return;
}
if (session !== this.session) {
continue;
}
const formattedError = formatErrorMessage(error);
if (!isTransportConnectionErrorMessage(formattedError)) {
console.warn("WebSocket RPC subscription failed", {