|
| 1 | +// Node result/close ordering tests keep admitted terminal frames authoritative. |
| 2 | +import { randomUUID } from "node:crypto"; |
| 3 | +import { afterEach, expect, test, vi } from "vitest"; |
| 4 | +import { writeConfigFile } from "../config/config.js"; |
| 5 | +import { approveNodePairing, requestNodePairing } from "../infra/node-pairing.js"; |
| 6 | +import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js"; |
| 7 | +import { pairDeviceIdentity } from "./device-authz.test-helpers.js"; |
| 8 | +import { GatewayNodeLifecycleDispatchTracker } from "./server/ws-connection/node-lifecycle-dispatch.js"; |
| 9 | +import { connectGatewayClient } from "./test-helpers.e2e.js"; |
| 10 | +import { installGatewayTestHooks, startServer } from "./test-helpers.js"; |
| 11 | + |
| 12 | +const pairingRead = vi.hoisted(() => ({ |
| 13 | + blocked: null as Promise<void> | null, |
| 14 | + onBlocked: null as (() => void) | null, |
| 15 | + release: null as (() => void) | null, |
| 16 | +})); |
| 17 | + |
| 18 | +vi.mock("../infra/node-pairing-state.js", async (importOriginal) => { |
| 19 | + const actual = await importOriginal<typeof import("../infra/node-pairing-state.js")>(); |
| 20 | + return { |
| 21 | + ...actual, |
| 22 | + resolveCurrentNodePairingBinding: async (nodeId: string) => { |
| 23 | + const current = await actual.resolveCurrentNodePairingBinding(nodeId); |
| 24 | + if (pairingRead.blocked) { |
| 25 | + pairingRead.onBlocked?.(); |
| 26 | + await pairingRead.blocked; |
| 27 | + } |
| 28 | + return current; |
| 29 | + }, |
| 30 | + }; |
| 31 | +}); |
| 32 | + |
| 33 | +installGatewayTestHooks({ scope: "suite" }); |
| 34 | + |
| 35 | +afterEach(() => { |
| 36 | + vi.restoreAllMocks(); |
| 37 | + pairingRead.blocked = null; |
| 38 | + pairingRead.onBlocked = null; |
| 39 | + pairingRead.release = null; |
| 40 | +}); |
| 41 | + |
| 42 | +test.each([ |
| 43 | + ["a terminal node result admitted before close wins over disconnect cleanup", false], |
| 44 | + ["pairing removal still fences a terminal node result while close drains", true], |
| 45 | +] as const)("%s", async (_name, removePairingDuringDrain) => { |
| 46 | + const pairedNode = await pairDeviceIdentity({ |
| 47 | + name: "node-result-before-close", |
| 48 | + role: "node", |
| 49 | + scopes: [], |
| 50 | + clientId: GATEWAY_CLIENT_NAMES.NODE_HOST, |
| 51 | + clientMode: GATEWAY_CLIENT_MODES.NODE, |
| 52 | + }); |
| 53 | + const pairing = await requestNodePairing({ |
| 54 | + nodeId: pairedNode.identity.deviceId, |
| 55 | + platform: "linux", |
| 56 | + deviceFamily: "Linux", |
| 57 | + commands: ["camera.list"], |
| 58 | + }); |
| 59 | + await approveNodePairing(pairing.request.requestId, { |
| 60 | + callerScopes: ["operator.pairing", "operator.write"], |
| 61 | + }); |
| 62 | + await writeConfigFile({ |
| 63 | + gateway: { nodes: { commands: { allow: ["camera.list"] } } }, |
| 64 | + }); |
| 65 | + |
| 66 | + const { port, server } = await startServer("secret"); |
| 67 | + const url = `ws://127.0.0.1:${port}`; |
| 68 | + let operator: Awaited<ReturnType<typeof connectGatewayClient>> | undefined; |
| 69 | + let node: Awaited<ReturnType<typeof connectGatewayClient>> | undefined; |
| 70 | + let resolveInvokeFrame: |
| 71 | + | ((frame: { id: string; nodeId: string; command: string }) => void) |
| 72 | + | undefined; |
| 73 | + const invokeFrame = new Promise<{ id: string; nodeId: string; command: string }>((resolve) => { |
| 74 | + resolveInvokeFrame = resolve; |
| 75 | + }); |
| 76 | + |
| 77 | + try { |
| 78 | + operator = await connectGatewayClient({ |
| 79 | + url, |
| 80 | + token: "secret", |
| 81 | + role: "operator", |
| 82 | + clientName: GATEWAY_CLIENT_NAMES.GATEWAY_CLIENT, |
| 83 | + clientDisplayName: "node result close operator", |
| 84 | + mode: GATEWAY_CLIENT_MODES.BACKEND, |
| 85 | + scopes: ["operator.admin", "operator.read", "operator.write"], |
| 86 | + }); |
| 87 | + node = await connectGatewayClient({ |
| 88 | + url, |
| 89 | + token: "secret", |
| 90 | + role: "node", |
| 91 | + clientName: GATEWAY_CLIENT_NAMES.NODE_HOST, |
| 92 | + clientDisplayName: "node result close host", |
| 93 | + mode: GATEWAY_CLIENT_MODES.NODE, |
| 94 | + platform: "linux", |
| 95 | + deviceFamily: "Linux", |
| 96 | + scopes: [], |
| 97 | + commands: ["camera.list"], |
| 98 | + deviceIdentity: pairedNode.identity, |
| 99 | + onEvent: (event) => { |
| 100 | + if (event.event !== "node.invoke.request" || !event.payload) { |
| 101 | + return; |
| 102 | + } |
| 103 | + resolveInvokeFrame?.(event.payload as { id: string; nodeId: string; command: string }); |
| 104 | + }, |
| 105 | + }); |
| 106 | + await vi.waitFor(async () => { |
| 107 | + const listed = await operator?.request<{ |
| 108 | + nodes?: Array<{ nodeId?: string; connected?: boolean; commands?: string[] }>; |
| 109 | + }>("node.list", {}, { timeoutMs: 10_000 }); |
| 110 | + expect(listed?.nodes?.find((entry) => entry.nodeId === pairedNode.identity.deviceId)).toEqual( |
| 111 | + expect.objectContaining({ |
| 112 | + connected: true, |
| 113 | + commands: ["camera.list"], |
| 114 | + }), |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + const invoked = operator.request<{ |
| 119 | + ok: boolean; |
| 120 | + nodeId: string; |
| 121 | + command: string; |
| 122 | + payload: unknown; |
| 123 | + }>( |
| 124 | + "node.invoke", |
| 125 | + { |
| 126 | + nodeId: pairedNode.identity.deviceId, |
| 127 | + command: "camera.list", |
| 128 | + timeoutMs: 10_000, |
| 129 | + idempotencyKey: randomUUID(), |
| 130 | + }, |
| 131 | + { timeoutMs: 10_000 }, |
| 132 | + ); |
| 133 | + const frame = await Promise.race([ |
| 134 | + invokeFrame, |
| 135 | + invoked.then( |
| 136 | + () => { |
| 137 | + throw new Error("node.invoke settled without sending a node request"); |
| 138 | + }, |
| 139 | + (error: unknown) => { |
| 140 | + throw error instanceof Error ? error : new Error(String(error)); |
| 141 | + }, |
| 142 | + ), |
| 143 | + ]); |
| 144 | + |
| 145 | + pairingRead.blocked = new Promise<void>((resolve) => { |
| 146 | + pairingRead.release = resolve; |
| 147 | + }); |
| 148 | + const pairingReadStarted = new Promise<void>((resolve) => { |
| 149 | + pairingRead.onBlocked = resolve; |
| 150 | + }); |
| 151 | + const drainSpy = vi.spyOn(GatewayNodeLifecycleDispatchTracker.prototype, "drain"); |
| 152 | + const resultAck = node |
| 153 | + .request( |
| 154 | + "node.invoke.result", |
| 155 | + { |
| 156 | + id: frame.id, |
| 157 | + nodeId: frame.nodeId, |
| 158 | + ok: true, |
| 159 | + payloadJSON: JSON.stringify({ completed: "before-close" }), |
| 160 | + }, |
| 161 | + { timeoutMs: 10_000 }, |
| 162 | + ) |
| 163 | + .catch((error: unknown) => error); |
| 164 | + await pairingReadStarted; |
| 165 | + const rawNodeSocket = Reflect.get(node, "ws") as { terminate?: () => void } | null; |
| 166 | + const stopped = node.stopAndWait({ timeoutMs: 1_000 }); |
| 167 | + rawNodeSocket?.terminate?.(); |
| 168 | + await stopped; |
| 169 | + node = undefined; |
| 170 | + await vi.waitFor(() => expect(drainSpy).toHaveBeenCalledOnce()); |
| 171 | + if (removePairingDuringDrain) { |
| 172 | + await operator.request( |
| 173 | + "node.pair.remove", |
| 174 | + { nodeId: pairedNode.identity.deviceId }, |
| 175 | + { timeoutMs: 10_000 }, |
| 176 | + ); |
| 177 | + } |
| 178 | + pairingRead.release?.(); |
| 179 | + |
| 180 | + if (removePairingDuringDrain) { |
| 181 | + await expect(invoked).rejects.toThrow("node pairing changed while invocation was active"); |
| 182 | + } else { |
| 183 | + await expect(invoked).resolves.toMatchObject({ |
| 184 | + ok: true, |
| 185 | + nodeId: pairedNode.identity.deviceId, |
| 186 | + command: "camera.list", |
| 187 | + payload: { completed: "before-close" }, |
| 188 | + }); |
| 189 | + } |
| 190 | + await resultAck; |
| 191 | + await vi.waitFor(async () => { |
| 192 | + const listed = await operator?.request<{ |
| 193 | + nodes?: Array<{ nodeId?: string; connected?: boolean }>; |
| 194 | + }>("node.list", {}, { timeoutMs: 10_000 }); |
| 195 | + const listedNode = listed?.nodes?.find( |
| 196 | + (entry) => entry.nodeId === pairedNode.identity.deviceId, |
| 197 | + ); |
| 198 | + if (removePairingDuringDrain) { |
| 199 | + expect(listedNode).toBeUndefined(); |
| 200 | + } else { |
| 201 | + expect(listedNode?.connected).toBe(false); |
| 202 | + } |
| 203 | + }); |
| 204 | + } finally { |
| 205 | + releaseBlockedPairingRead(); |
| 206 | + await Promise.allSettled([ |
| 207 | + ...(node ? [node.stopAndWait({ timeoutMs: 1_000 })] : []), |
| 208 | + ...(operator ? [operator.stopAndWait({ timeoutMs: 1_000 })] : []), |
| 209 | + ]); |
| 210 | + await server.close(); |
| 211 | + } |
| 212 | +}); |
| 213 | + |
| 214 | +function releaseBlockedPairingRead(): void { |
| 215 | + pairingRead.release?.(); |
| 216 | + pairingRead.onBlocked = null; |
| 217 | + pairingRead.blocked = null; |
| 218 | + pairingRead.release = null; |
| 219 | +} |
0 commit comments