test(browser): verify out-of-order response correlation

This commit is contained in:
morluto
2026-10-27 08:04:29 +08:00
parent 8c8a2752c0
commit 69b328eec8
+23 -1
View File
@@ -107,7 +107,7 @@ describe("CDP connection admission cancellation", () => {
describe("CDP connection response correlation", () => {
it("correlates concurrent command responses over a real WebSocket", async () => {
const browser = await startFakeCdpBrowser();
const browser = await startFakeCdpBrowser({ hangOnMethod: "Page.enable" });
browsers.push(browser);
const connection = await CdpConnection.connect(
browser.browserWebSocketUrl,
@@ -124,6 +124,28 @@ describe("CDP connection response correlation", () => {
});
browser.emitRawMessage(JSON.stringify({ id: 999 }));
await expect(connection.send("Runtime.enable")).resolves.toEqual({});
const first = connection.send("Page.enable", { selected: "first" });
const second = connection.send("Page.enable", { selected: "second" });
const replies = Promise.all([first, second]);
await vi.waitFor(() => expect(browser.commands).toHaveLength(5));
const firstCommand = browser.commands[3];
const secondCommand = browser.commands[4];
if (firstCommand === undefined || secondCommand === undefined)
throw new Error("Fake browser did not receive both pending commands");
browser.emitRawMessage(
JSON.stringify({
id: secondCommand.id,
result: { selected: "second" },
}),
);
browser.emitRawMessage(
JSON.stringify({ id: firstCommand.id, result: { selected: "first" } }),
);
await expect(replies).resolves.toEqual([
{ selected: "first" },
{ selected: "second" },
]);
await expect(connection.send("Runtime.enable")).resolves.toEqual({});
} finally {
await connection.close();
}