|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import type { GatewayBrowserClient } from "../api/gateway.ts"; |
| 3 | +import { waitForGatewayClient } from "./gateway-readiness.ts"; |
| 4 | + |
| 5 | +type Gateway = Parameters<typeof waitForGatewayClient>[0]; |
| 6 | +type GatewaySnapshot = Gateway["snapshot"]; |
| 7 | + |
| 8 | +function createGateway(connected = false) { |
| 9 | + const client = {} as GatewayBrowserClient; |
| 10 | + let snapshot = { |
| 11 | + phase: connected ? "connected" : "connecting", |
| 12 | + client: connected ? client : null, |
| 13 | + } as GatewaySnapshot; |
| 14 | + const listeners = new Set<(next: GatewaySnapshot) => void>(); |
| 15 | + const unsubscribe = vi.fn((listener: (next: GatewaySnapshot) => void) => { |
| 16 | + listeners.delete(listener); |
| 17 | + }); |
| 18 | + const subscribe = vi.fn((listener: (next: GatewaySnapshot) => void) => { |
| 19 | + listeners.add(listener); |
| 20 | + return () => unsubscribe(listener); |
| 21 | + }); |
| 22 | + const gateway: Gateway = { |
| 23 | + get snapshot() { |
| 24 | + return snapshot; |
| 25 | + }, |
| 26 | + subscribe, |
| 27 | + }; |
| 28 | + return { |
| 29 | + gateway, |
| 30 | + client, |
| 31 | + subscribe, |
| 32 | + unsubscribe, |
| 33 | + connect: () => { |
| 34 | + snapshot = { ...snapshot, phase: "connected", client }; |
| 35 | + for (const listener of Array.from(listeners)) { |
| 36 | + listener(snapshot); |
| 37 | + } |
| 38 | + }, |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +describe("waitForGatewayClient", () => { |
| 43 | + it("rejects an already-aborted signal before returning a connected client", async () => { |
| 44 | + const { gateway, subscribe } = createGateway(true); |
| 45 | + const cancellation = new Error("navigation canceled"); |
| 46 | + const controller = new AbortController(); |
| 47 | + controller.abort(cancellation); |
| 48 | + |
| 49 | + await expect(waitForGatewayClient(gateway, controller.signal)).rejects.toBe(cancellation); |
| 50 | + expect(subscribe).not.toHaveBeenCalled(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("rejects an already-aborted signal before subscribing to a disconnected gateway", async () => { |
| 54 | + const { gateway, subscribe } = createGateway(); |
| 55 | + const controller = new AbortController(); |
| 56 | + controller.abort(); |
| 57 | + |
| 58 | + await expect(waitForGatewayClient(gateway, controller.signal)).rejects.toBe( |
| 59 | + controller.signal.reason, |
| 60 | + ); |
| 61 | + expect(subscribe).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("keeps the existing AbortError contract for a non-error cancellation reason", async () => { |
| 65 | + const { gateway } = createGateway(true); |
| 66 | + const controller = new AbortController(); |
| 67 | + controller.abort("navigation canceled"); |
| 68 | + |
| 69 | + await expect(waitForGatewayClient(gateway, controller.signal)).rejects.toMatchObject({ |
| 70 | + name: "AbortError", |
| 71 | + message: "Gateway wait aborted", |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it("returns a connected client without subscribing while its signal remains active", async () => { |
| 76 | + const { gateway, client, subscribe } = createGateway(true); |
| 77 | + |
| 78 | + await expect(waitForGatewayClient(gateway, new AbortController().signal)).resolves.toBe(client); |
| 79 | + expect(subscribe).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("removes a pending gateway subscription when its signal is aborted", async () => { |
| 83 | + const { gateway, unsubscribe } = createGateway(); |
| 84 | + const cancellation = new Error("navigation canceled"); |
| 85 | + const controller = new AbortController(); |
| 86 | + const pending = waitForGatewayClient(gateway, controller.signal); |
| 87 | + |
| 88 | + controller.abort(cancellation); |
| 89 | + |
| 90 | + await expect(pending).rejects.toBe(cancellation); |
| 91 | + expect(unsubscribe).toHaveBeenCalledOnce(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("releases a pending subscription once its gateway connects", async () => { |
| 95 | + const { gateway, client, connect, unsubscribe } = createGateway(); |
| 96 | + const controller = new AbortController(); |
| 97 | + const pending = waitForGatewayClient(gateway, controller.signal); |
| 98 | + |
| 99 | + connect(); |
| 100 | + |
| 101 | + await expect(pending).resolves.toBe(client); |
| 102 | + expect(unsubscribe).toHaveBeenCalledOnce(); |
| 103 | + controller.abort(); |
| 104 | + expect(unsubscribe).toHaveBeenCalledOnce(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("does not retain an abort listener after synchronous connection replay", async () => { |
| 108 | + const client = {} as GatewayBrowserClient; |
| 109 | + const snapshot = { phase: "connected", client } as GatewaySnapshot; |
| 110 | + const unsubscribe = vi.fn(); |
| 111 | + const gateway: Gateway = { |
| 112 | + snapshot: { phase: "connecting", client: null } as GatewaySnapshot, |
| 113 | + subscribe(listener) { |
| 114 | + listener(snapshot); |
| 115 | + return unsubscribe; |
| 116 | + }, |
| 117 | + }; |
| 118 | + const controller = new AbortController(); |
| 119 | + const addAbortListener = vi.spyOn(controller.signal, "addEventListener"); |
| 120 | + const removeAbortListener = vi.spyOn(controller.signal, "removeEventListener"); |
| 121 | + |
| 122 | + await expect(waitForGatewayClient(gateway, controller.signal)).resolves.toBe(client); |
| 123 | + |
| 124 | + const abortListener = addAbortListener.mock.calls[0]?.[1]; |
| 125 | + expect(abortListener).toBeTypeOf("function"); |
| 126 | + expect(removeAbortListener).toHaveBeenCalledWith("abort", abortListener); |
| 127 | + expect(unsubscribe).toHaveBeenCalledOnce(); |
| 128 | + |
| 129 | + controller.abort(new Error("late navigation cancellation")); |
| 130 | + |
| 131 | + expect(unsubscribe).toHaveBeenCalledOnce(); |
| 132 | + }); |
| 133 | + |
| 134 | + it("releases a subscription when it synchronously aborts during registration", async () => { |
| 135 | + const cancellation = new Error("navigation canceled during registration"); |
| 136 | + const controller = new AbortController(); |
| 137 | + const unsubscribe = vi.fn(); |
| 138 | + const gateway: Gateway = { |
| 139 | + snapshot: { phase: "connecting", client: null } as GatewaySnapshot, |
| 140 | + subscribe() { |
| 141 | + controller.abort(cancellation); |
| 142 | + return unsubscribe; |
| 143 | + }, |
| 144 | + }; |
| 145 | + |
| 146 | + await expect(waitForGatewayClient(gateway, controller.signal)).rejects.toBe(cancellation); |
| 147 | + expect(unsubscribe).toHaveBeenCalledOnce(); |
| 148 | + }); |
| 149 | +}); |
0 commit comments