|
| 1 | +import { expect, test, type Page } from "@playwright/test"; |
| 2 | + |
| 3 | +import { installBridge } from "../helpers/bridge"; |
| 4 | +import { TwoRelayHarness, type RelaySpec } from "./helpers/twoRelayHarness"; |
| 5 | + |
| 6 | +// Live gate: boots a REAL buzz-relay process, points the app at it, SIGTERMs |
| 7 | +// the relay mid-session, restarts it on the same port, and asserts the client |
| 8 | +// converges back to "connected". This proves the full restart story end to |
| 9 | +// end: the relay's graceful-drain 1012 close broadcast (server side) and the |
| 10 | +// client's dial-failure retry + 1012 fast-reconnect (desktop side) — the two |
| 11 | +// halves that synthetic mock-websocket specs cannot compose. |
| 12 | +// |
| 13 | +// Requires: BUZZ_E2E_RELAY_RESTART=1, BUZZ_E2E_RELAY_BIN, and |
| 14 | +// BUZZ_E2E_DATABASE_URL (plus reachable Redis and media object store, same |
| 15 | +// infra as the agents-everywhere live gate). |
| 16 | +const enabled = process.env.BUZZ_E2E_RELAY_RESTART === "1"; |
| 17 | + |
| 18 | +function required(name: string, value: string | undefined): string { |
| 19 | + if (!value) throw new Error(`${name} is required for the live gate`); |
| 20 | + return value; |
| 21 | +} |
| 22 | + |
| 23 | +async function connectionState(page: Page): Promise<string> { |
| 24 | + return page.evaluate(() => { |
| 25 | + const win = window as Window & { |
| 26 | + __BUZZ_E2E_GET_RELAY_CONNECTION_STATE__?: () => string; |
| 27 | + }; |
| 28 | + return win.__BUZZ_E2E_GET_RELAY_CONNECTION_STATE__?.() ?? "uninstalled"; |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +test.describe("relay restart live gate", () => { |
| 33 | + test.skip(!enabled, "set BUZZ_E2E_RELAY_RESTART=1 to run live gate"); |
| 34 | + |
| 35 | + test("client reconnects after the relay is SIGTERMed and restarted", async ({ |
| 36 | + page, |
| 37 | + }) => { |
| 38 | + test.setTimeout(180_000); |
| 39 | + const portBase = 26_000 + (process.pid % 3_000); |
| 40 | + const spec: RelaySpec = { |
| 41 | + name: "relay-restart", |
| 42 | + ports: { |
| 43 | + main: portBase, |
| 44 | + health: portBase + 3_000, |
| 45 | + metrics: portBase + 6_000, |
| 46 | + }, |
| 47 | + databaseUrl: required( |
| 48 | + "BUZZ_E2E_DATABASE_URL", |
| 49 | + process.env.BUZZ_E2E_DATABASE_URL, |
| 50 | + ), |
| 51 | + redisUrl: |
| 52 | + process.env.BUZZ_E2E_REDIS_RESTART ?? "redis://127.0.0.1:6379/13", |
| 53 | + }; |
| 54 | + const harness = await TwoRelayHarness.create([spec]); |
| 55 | + try { |
| 56 | + await harness.startRelays(); |
| 57 | + |
| 58 | + const relayHttpUrl = `http://127.0.0.1:${spec.ports.main}`; |
| 59 | + await installBridge(page, { |
| 60 | + mode: "relay", |
| 61 | + user: "tyler", |
| 62 | + relayHttpUrl, |
| 63 | + relayWsUrl: `ws://127.0.0.1:${spec.ports.main}`, |
| 64 | + }); |
| 65 | + await page.goto("/"); |
| 66 | + |
| 67 | + // Baseline: the app converges to a live authenticated session. |
| 68 | + await expect |
| 69 | + .poll(() => connectionState(page), { timeout: 60_000 }) |
| 70 | + .toBe("connected"); |
| 71 | + |
| 72 | + // Roll the pod. Graceful drain: readiness 503 → 5s grace → 1012 close |
| 73 | + // broadcast → process exit. The client must observe the close (not a |
| 74 | + // silent stall) and start retrying. |
| 75 | + await harness.terminateRelayGracefully(spec.name); |
| 76 | + await expect |
| 77 | + .poll(() => connectionState(page), { timeout: 30_000 }) |
| 78 | + .not.toBe("connected"); |
| 79 | + |
| 80 | + // Bring the "new pod" up on the same address, exactly like a k8s |
| 81 | + // restart behind a stable service endpoint. |
| 82 | + await harness.restartRelay(spec.name); |
| 83 | + |
| 84 | + // The client's retry loop must find the fresh relay and converge back |
| 85 | + // to connected without any user interaction. |
| 86 | + await expect |
| 87 | + .poll(() => connectionState(page), { timeout: 60_000 }) |
| 88 | + .toBe("connected"); |
| 89 | + } catch (error) { |
| 90 | + console.error(await harness.logs()); |
| 91 | + throw error; |
| 92 | + } finally { |
| 93 | + await harness.stop(); |
| 94 | + } |
| 95 | + }); |
| 96 | +}); |
0 commit comments