|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | + |
| 3 | +import { installMockBridge } from "../helpers/bridge"; |
| 4 | + |
| 5 | +test("mock socket REQs preserve exact readiness semantics", async ({ |
| 6 | + page, |
| 7 | +}) => { |
| 8 | + // Keep app-owned channel subscriptions out of the readiness result. The |
| 9 | + // bridge is installed, but locked boot never mounts the channel consumers. |
| 10 | + await installMockBridge(page, { identityLocked: true }); |
| 11 | + await page.goto("/"); |
| 12 | + await expect(page.getByTestId("keyring-locked")).toBeVisible(); |
| 13 | + |
| 14 | + const observed = await page.evaluate(async () => { |
| 15 | + const invoke = window.__BUZZ_E2E_INVOKE_MOCK_COMMAND__; |
| 16 | + const ready = window.__BUZZ_E2E_HAS_MOCK_LIVE_SUBSCRIPTION__; |
| 17 | + if (!invoke || !ready) throw new Error("Mock socket bridge is unavailable"); |
| 18 | + const channel = "9a1657ac-f7aa-5db0-b632-d8bbeb6dfb50"; |
| 19 | + const owner = "deadbeef".repeat(8); |
| 20 | + const frames: unknown[][] = []; |
| 21 | + const id = await invoke("plugin:websocket|connect", { |
| 22 | + onMessage: (batch: Array<{ type: string; data?: string }>) => { |
| 23 | + for (const frame of batch) { |
| 24 | + if (frame.type === "Text" && frame.data) { |
| 25 | + frames.push(JSON.parse(frame.data)); |
| 26 | + } |
| 27 | + } |
| 28 | + }, |
| 29 | + }); |
| 30 | + const subId = "live-readiness-regression"; |
| 31 | + const check = (kind: number | undefined = 9, exactChannel = true) => |
| 32 | + ready({ channelName: "general", kind, exactChannel }); |
| 33 | + const send = (message: unknown[]) => |
| 34 | + invoke("plugin:websocket|send", { |
| 35 | + id, |
| 36 | + message: { type: "Text", data: JSON.stringify(message) }, |
| 37 | + }); |
| 38 | + const req = async (filters: Array<Record<string, unknown>>) => { |
| 39 | + frames.length = 0; |
| 40 | + await send(["REQ", subId, ...filters]); |
| 41 | + if (!frames.some((frame) => frame[0] === "EOSE" && frame[1] === subId)) { |
| 42 | + throw new Error(`REQ was not accepted: ${JSON.stringify(frames)}`); |
| 43 | + } |
| 44 | + return check(); |
| 45 | + }; |
| 46 | + |
| 47 | + try { |
| 48 | + const before = check(); |
| 49 | + const splitGlobal = await req([ |
| 50 | + { "#h": [channel], kinds: [30078] }, |
| 51 | + { kinds: [9] }, |
| 52 | + ]); |
| 53 | + const splitOtherChannel = await req([ |
| 54 | + { "#h": [channel], kinds: [30078] }, |
| 55 | + { "#h": ["other"], kinds: [9] }, |
| 56 | + ]); |
| 57 | + const sameFilter = await req([{ "#h": [channel], kinds: [9] }]); |
| 58 | + const emptyKinds = await req([ |
| 59 | + { "#h": [channel], "#p": [owner], kinds: [] }, |
| 60 | + ]); |
| 61 | + const omittedKinds = await req([{ "#h": [channel], "#p": [owner] }]); |
| 62 | + const globalOnly = await req([{ kinds: [9] }]); |
| 63 | + const legacyGlobal = check(9, false); |
| 64 | + await send(["CLOSE", subId]); |
| 65 | + const afterClose = check(); |
| 66 | + return { |
| 67 | + before, |
| 68 | + splitGlobal, |
| 69 | + splitOtherChannel, |
| 70 | + sameFilter, |
| 71 | + emptyKinds, |
| 72 | + omittedKinds, |
| 73 | + globalOnly, |
| 74 | + legacyGlobal, |
| 75 | + afterClose, |
| 76 | + }; |
| 77 | + } finally { |
| 78 | + await invoke("plugin:websocket|disconnect", { id }); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + expect(observed).toEqual({ |
| 83 | + before: false, |
| 84 | + splitGlobal: false, |
| 85 | + splitOtherChannel: false, |
| 86 | + sameFilter: true, |
| 87 | + emptyKinds: false, |
| 88 | + omittedKinds: true, |
| 89 | + globalOnly: false, |
| 90 | + legacyGlobal: true, |
| 91 | + afterClose: false, |
| 92 | + }); |
| 93 | +}); |
0 commit comments