|
| 1 | +// @vitest-environment node |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import type { |
| 4 | + GatewayBrowserClient, |
| 5 | + GatewayBrowserClientOptions, |
| 6 | + GatewayEventFrame, |
| 7 | + GatewayHelloOk, |
| 8 | +} from "../api/gateway.ts"; |
| 9 | +import { createStorageMock } from "../test-helpers/storage.ts"; |
| 10 | +import { createApplicationGateway } from "./gateway-store.ts"; |
| 11 | +import { loadSettings } from "./settings.ts"; |
| 12 | + |
| 13 | +vi.mock("../build-info.ts", () => ({ |
| 14 | + CONTROL_UI_BUILD_INFO: { version: "2026.7.2" }, |
| 15 | +})); |
| 16 | + |
| 17 | +const HELLO: GatewayHelloOk = { |
| 18 | + type: "hello-ok", |
| 19 | + protocol: 1, |
| 20 | + auth: { role: "operator", scopes: [] }, |
| 21 | +}; |
| 22 | + |
| 23 | +function createGatewayEvent(seq: number): GatewayEventFrame { |
| 24 | + return { |
| 25 | + type: "event", |
| 26 | + event: "chat", |
| 27 | + payload: { text: `event-${seq}` }, |
| 28 | + seq, |
| 29 | + stateVersion: { presence: seq, health: seq }, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function createGatewayStore() { |
| 34 | + const clients: Array<{ |
| 35 | + opts: GatewayBrowserClientOptions; |
| 36 | + start: ReturnType<typeof vi.fn>; |
| 37 | + stop: ReturnType<typeof vi.fn>; |
| 38 | + }> = []; |
| 39 | + const gateway = createApplicationGateway(loadSettings(), "", "", (opts) => { |
| 40 | + const client = { |
| 41 | + opts, |
| 42 | + instanceId: opts.instanceId ?? "", |
| 43 | + request: vi.fn().mockRejectedValue(new Error("unexpected gateway request")), |
| 44 | + start: vi.fn(), |
| 45 | + stop: vi.fn(), |
| 46 | + }; |
| 47 | + clients.push(client); |
| 48 | + return client as unknown as GatewayBrowserClient; |
| 49 | + }); |
| 50 | + return { |
| 51 | + gateway, |
| 52 | + clients, |
| 53 | + current: () => { |
| 54 | + const client = clients.at(-1); |
| 55 | + if (!client) { |
| 56 | + throw new Error("expected a gateway client"); |
| 57 | + } |
| 58 | + return client; |
| 59 | + }, |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +describe("application gateway observer ownership", () => { |
| 64 | + beforeEach(() => { |
| 65 | + vi.stubGlobal("localStorage", createStorageMock()); |
| 66 | + vi.stubGlobal("sessionStorage", createStorageMock()); |
| 67 | + vi.stubGlobal("navigator", { language: "en-US" } as Navigator); |
| 68 | + vi.stubGlobal("location", { |
| 69 | + protocol: "http:", |
| 70 | + host: "127.0.0.1:18789", |
| 71 | + hostname: "127.0.0.1", |
| 72 | + pathname: "/", |
| 73 | + } as Location); |
| 74 | + }); |
| 75 | + |
| 76 | + afterEach(() => { |
| 77 | + vi.useRealTimers(); |
| 78 | + vi.unstubAllGlobals(); |
| 79 | + vi.restoreAllMocks(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("isolates a failing snapshot observer during the actual hello callback", () => { |
| 83 | + const { gateway, current } = createGatewayStore(); |
| 84 | + const failure = new Error("snapshot observer failed"); |
| 85 | + const reportError = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 86 | + const healthy = vi.fn(); |
| 87 | + gateway.subscribe((snapshot) => { |
| 88 | + if (snapshot.phase === "connected") { |
| 89 | + throw failure; |
| 90 | + } |
| 91 | + }); |
| 92 | + gateway.subscribe(healthy); |
| 93 | + gateway.start(); |
| 94 | + |
| 95 | + expect(() => current().opts.onHello?.(HELLO)).not.toThrow(); |
| 96 | + |
| 97 | + expect(gateway.snapshot.phase).toBe("connected"); |
| 98 | + expect(healthy.mock.calls.map(([snapshot]) => snapshot.phase)).toEqual([ |
| 99 | + "connecting", |
| 100 | + "connected", |
| 101 | + ]); |
| 102 | + expect(reportError).toHaveBeenCalledExactlyOnceWith( |
| 103 | + "[gateway] snapshot handler error:", |
| 104 | + failure, |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + it("snapshots connection observers before subscription membership changes", () => { |
| 109 | + const { gateway, current } = createGatewayStore(); |
| 110 | + const second = vi.fn(); |
| 111 | + const third = vi.fn(); |
| 112 | + let unsubscribeSecond = () => {}; |
| 113 | + gateway.subscribe((snapshot) => { |
| 114 | + if (snapshot.phase === "connecting") { |
| 115 | + unsubscribeSecond(); |
| 116 | + gateway.subscribe(third); |
| 117 | + } |
| 118 | + }); |
| 119 | + unsubscribeSecond = gateway.subscribe(second); |
| 120 | + |
| 121 | + gateway.start(); |
| 122 | + |
| 123 | + expect(second).toHaveBeenCalledOnce(); |
| 124 | + expect(second.mock.calls[0]?.[0].phase).toBe("connecting"); |
| 125 | + expect(third).not.toHaveBeenCalled(); |
| 126 | + |
| 127 | + current().opts.onHello?.(HELLO); |
| 128 | + |
| 129 | + expect(second).toHaveBeenCalledOnce(); |
| 130 | + expect(third).toHaveBeenCalledOnce(); |
| 131 | + expect(third.mock.calls[0]?.[0].phase).toBe("connected"); |
| 132 | + }); |
| 133 | + |
| 134 | + it("retires a snapshot immediately when an observer replaces its gateway client", () => { |
| 135 | + const { gateway, current } = createGatewayStore(); |
| 136 | + const healthy = vi.fn(); |
| 137 | + let replaced = false; |
| 138 | + gateway.subscribe((snapshot) => { |
| 139 | + if (snapshot.phase === "connected" && !replaced) { |
| 140 | + replaced = true; |
| 141 | + gateway.connect(); |
| 142 | + } |
| 143 | + }); |
| 144 | + gateway.subscribe(healthy); |
| 145 | + gateway.start(); |
| 146 | + |
| 147 | + current().opts.onHello?.(HELLO); |
| 148 | + |
| 149 | + expect(gateway.snapshot.phase).toBe("reconnecting"); |
| 150 | + expect(healthy.mock.calls.map(([snapshot]) => snapshot.phase)).toEqual([ |
| 151 | + "connecting", |
| 152 | + "reconnecting", |
| 153 | + ]); |
| 154 | + }); |
| 155 | + |
| 156 | + it("isolates a failing event-log observer from the remaining log observers", () => { |
| 157 | + const { gateway, current } = createGatewayStore(); |
| 158 | + const failure = new Error("event log observer failed"); |
| 159 | + const reportError = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 160 | + const healthy = vi.fn(); |
| 161 | + gateway.subscribeEventLog(() => { |
| 162 | + throw failure; |
| 163 | + }); |
| 164 | + gateway.subscribeEventLog(healthy); |
| 165 | + gateway.start(); |
| 166 | + |
| 167 | + current().opts.onEvent?.(createGatewayEvent(1)); |
| 168 | + |
| 169 | + expect(healthy).toHaveBeenCalledExactlyOnceWith(gateway.eventLog); |
| 170 | + expect(reportError).toHaveBeenCalledExactlyOnceWith("[gateway] event handler error:", failure); |
| 171 | + }); |
| 172 | + |
| 173 | + it("snapshots event-log observers before subscription membership changes", () => { |
| 174 | + const { gateway, current } = createGatewayStore(); |
| 175 | + const second = vi.fn(); |
| 176 | + const third = vi.fn(); |
| 177 | + let unsubscribeSecond = () => {}; |
| 178 | + gateway.subscribeEventLog(() => { |
| 179 | + unsubscribeSecond(); |
| 180 | + gateway.subscribeEventLog(third); |
| 181 | + }); |
| 182 | + unsubscribeSecond = gateway.subscribeEventLog(second); |
| 183 | + gateway.start(); |
| 184 | + |
| 185 | + current().opts.onEvent?.(createGatewayEvent(1)); |
| 186 | + |
| 187 | + expect(second).toHaveBeenCalledOnce(); |
| 188 | + expect(third).not.toHaveBeenCalled(); |
| 189 | + |
| 190 | + current().opts.onEvent?.(createGatewayEvent(2)); |
| 191 | + |
| 192 | + expect(second).toHaveBeenCalledOnce(); |
| 193 | + expect(third).toHaveBeenCalledExactlyOnceWith(gateway.eventLog); |
| 194 | + }); |
| 195 | + |
| 196 | + it("never logs a presence event after its snapshot observer replaces the client", () => { |
| 197 | + const { gateway, current } = createGatewayStore(); |
| 198 | + const logged = vi.fn(); |
| 199 | + const delivered = vi.fn(); |
| 200 | + let replaced = false; |
| 201 | + gateway.subscribe((snapshot) => { |
| 202 | + if (snapshot.selfUser?.id === "retired-owner" && !replaced) { |
| 203 | + replaced = true; |
| 204 | + gateway.connect(); |
| 205 | + } |
| 206 | + }); |
| 207 | + gateway.subscribeEventLog(logged); |
| 208 | + gateway.subscribeEvents(delivered); |
| 209 | + gateway.start(); |
| 210 | + const retired = current(); |
| 211 | + retired.opts.onHello?.(HELLO); |
| 212 | + |
| 213 | + retired.opts.onEvent?.({ |
| 214 | + ...createGatewayEvent(1), |
| 215 | + event: "presence", |
| 216 | + payload: { |
| 217 | + presence: [{ instanceId: retired.opts.instanceId, user: { id: "retired-owner" } }], |
| 218 | + }, |
| 219 | + }); |
| 220 | + |
| 221 | + expect(replaced).toBe(true); |
| 222 | + expect(gateway.snapshot.selfUser).toBeNull(); |
| 223 | + expect(gateway.eventLog).toEqual([]); |
| 224 | + expect(logged).not.toHaveBeenCalled(); |
| 225 | + expect(delivered).not.toHaveBeenCalled(); |
| 226 | + }); |
| 227 | + |
| 228 | + it("does not start a client replaced by a connecting snapshot observer", () => { |
| 229 | + const { gateway, clients } = createGatewayStore(); |
| 230 | + let replaced = false; |
| 231 | + gateway.subscribe((snapshot) => { |
| 232 | + if (snapshot.phase === "connecting" && !replaced) { |
| 233 | + replaced = true; |
| 234 | + gateway.connect(); |
| 235 | + } |
| 236 | + }); |
| 237 | + |
| 238 | + gateway.start(); |
| 239 | + |
| 240 | + expect(clients).toHaveLength(2); |
| 241 | + expect(clients[0]?.stop).toHaveBeenCalledOnce(); |
| 242 | + expect(clients[0]?.start).not.toHaveBeenCalled(); |
| 243 | + expect(clients[1]?.start).toHaveBeenCalledOnce(); |
| 244 | + }); |
| 245 | + |
| 246 | + it("does not start a client stopped by a connecting snapshot observer", () => { |
| 247 | + const { gateway, clients } = createGatewayStore(); |
| 248 | + let halted = false; |
| 249 | + gateway.subscribe((snapshot) => { |
| 250 | + if (snapshot.phase === "connecting" && !halted) { |
| 251 | + halted = true; |
| 252 | + gateway.stop(); |
| 253 | + } |
| 254 | + }); |
| 255 | + |
| 256 | + gateway.start(); |
| 257 | + |
| 258 | + expect(clients).toHaveLength(1); |
| 259 | + expect(clients[0]?.stop).toHaveBeenCalledOnce(); |
| 260 | + expect(clients[0]?.start).not.toHaveBeenCalled(); |
| 261 | + expect(gateway.snapshot.phase).toBe("stopped"); |
| 262 | + }); |
| 263 | + |
| 264 | + it("does not reconnect again after a gap observer replaces its client", () => { |
| 265 | + const { gateway, clients } = createGatewayStore(); |
| 266 | + gateway.start(); |
| 267 | + const retired = clients[0]; |
| 268 | + retired?.opts.onHello?.(HELLO); |
| 269 | + let replaced = false; |
| 270 | + gateway.subscribe((snapshot) => { |
| 271 | + if (snapshot.lastError?.startsWith("event gap detected") && !replaced) { |
| 272 | + replaced = true; |
| 273 | + gateway.connect(); |
| 274 | + } |
| 275 | + }); |
| 276 | + |
| 277 | + retired?.opts.onGap?.({ expected: 2, received: 5 }); |
| 278 | + |
| 279 | + expect(replaced).toBe(true); |
| 280 | + expect(clients).toHaveLength(2); |
| 281 | + expect(clients[1]?.start).toHaveBeenCalledOnce(); |
| 282 | + expect(gateway.snapshot.client).toBe(clients[1]); |
| 283 | + }); |
| 284 | + |
| 285 | + it("does not reconnect after a gap observer stops its client", () => { |
| 286 | + const { gateway, clients } = createGatewayStore(); |
| 287 | + gateway.start(); |
| 288 | + const retired = clients[0]; |
| 289 | + retired?.opts.onHello?.(HELLO); |
| 290 | + let halted = false; |
| 291 | + gateway.subscribe((snapshot) => { |
| 292 | + if (snapshot.lastError?.startsWith("event gap detected") && !halted) { |
| 293 | + halted = true; |
| 294 | + gateway.stop(); |
| 295 | + } |
| 296 | + }); |
| 297 | + |
| 298 | + retired?.opts.onGap?.({ expected: 2, received: 5 }); |
| 299 | + |
| 300 | + expect(halted).toBe(true); |
| 301 | + expect(clients).toHaveLength(1); |
| 302 | + expect(retired?.start).toHaveBeenCalledOnce(); |
| 303 | + expect(gateway.snapshot.phase).toBe("stopped"); |
| 304 | + }); |
| 305 | +}); |
0 commit comments