|
| 1 | +import { EventEmitter } from "node:events"; |
| 2 | +import type { IncomingMessage } from "node:http"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import type { WebSocket } from "ws"; |
| 5 | +import type { ResolvedGatewayAuth } from "../../auth.js"; |
| 6 | +import { PROTOCOL_VERSION } from "../../protocol/index.js"; |
| 7 | +import type { GatewayRequestContext } from "../../server-methods/types.js"; |
| 8 | + |
| 9 | +const { |
| 10 | + buildGatewaySnapshotMock, |
| 11 | + getHealthCacheMock, |
| 12 | + getHealthVersionMock, |
| 13 | + incrementPresenceVersionMock, |
| 14 | + loadConfigMock, |
| 15 | + upsertPresenceMock, |
| 16 | +} = vi.hoisted(() => ({ |
| 17 | + buildGatewaySnapshotMock: vi.fn(() => ({ |
| 18 | + presence: [], |
| 19 | + health: {}, |
| 20 | + stateVersion: { presence: 1, health: 1 }, |
| 21 | + uptimeMs: 1, |
| 22 | + sessionDefaults: { |
| 23 | + defaultAgentId: "main", |
| 24 | + mainKey: "main", |
| 25 | + mainSessionKey: "main", |
| 26 | + scope: "per-sender", |
| 27 | + }, |
| 28 | + })), |
| 29 | + getHealthCacheMock: vi.fn(() => null), |
| 30 | + getHealthVersionMock: vi.fn(() => 1), |
| 31 | + incrementPresenceVersionMock: vi.fn(() => 2), |
| 32 | + loadConfigMock: vi.fn(() => ({ |
| 33 | + gateway: { |
| 34 | + auth: { mode: "none" }, |
| 35 | + controlUi: {}, |
| 36 | + }, |
| 37 | + })), |
| 38 | + upsertPresenceMock: vi.fn(), |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock("../../../config/config.js", () => ({ |
| 42 | + loadConfig: loadConfigMock, |
| 43 | +})); |
| 44 | + |
| 45 | +vi.mock("../../../infra/system-presence.js", () => ({ |
| 46 | + upsertPresence: upsertPresenceMock, |
| 47 | +})); |
| 48 | + |
| 49 | +vi.mock("../../server-methods.js", () => ({ |
| 50 | + handleGatewayRequest: vi.fn(), |
| 51 | +})); |
| 52 | + |
| 53 | +vi.mock("../health-state.js", () => ({ |
| 54 | + buildGatewaySnapshot: buildGatewaySnapshotMock, |
| 55 | + getHealthCache: getHealthCacheMock, |
| 56 | + getHealthVersion: getHealthVersionMock, |
| 57 | + incrementPresenceVersion: incrementPresenceVersionMock, |
| 58 | +})); |
| 59 | + |
| 60 | +import { attachGatewayWsMessageHandler } from "./message-handler.js"; |
| 61 | + |
| 62 | +function createLogger() { |
| 63 | + return { |
| 64 | + debug: vi.fn(), |
| 65 | + info: vi.fn(), |
| 66 | + warn: vi.fn(), |
| 67 | + error: vi.fn(), |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +describe("attachGatewayWsMessageHandler post-connect health refresh", () => { |
| 72 | + beforeEach(() => { |
| 73 | + vi.clearAllMocks(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("uses the injected runtime-aware health refresh after hello", async () => { |
| 77 | + let resolveRefresh: (() => void) | undefined; |
| 78 | + const refreshHealthSnapshot = vi.fn( |
| 79 | + () => |
| 80 | + new Promise((resolve) => { |
| 81 | + resolveRefresh = () => resolve({} as never); |
| 82 | + }), |
| 83 | + ) as GatewayRequestContext["refreshHealthSnapshot"]; |
| 84 | + const socket = Object.assign(new EventEmitter(), { |
| 85 | + _receiver: {}, |
| 86 | + send: vi.fn((_payload: string, cb?: (err?: Error) => void) => { |
| 87 | + cb?.(); |
| 88 | + }), |
| 89 | + }) as unknown as WebSocket; |
| 90 | + let client: unknown = null; |
| 91 | + const resolvedAuth: ResolvedGatewayAuth = { |
| 92 | + mode: "token", |
| 93 | + token: "test-token", |
| 94 | + allowTailscale: false, |
| 95 | + }; |
| 96 | + |
| 97 | + attachGatewayWsMessageHandler({ |
| 98 | + socket, |
| 99 | + upgradeReq: { |
| 100 | + headers: { host: "127.0.0.1:19001" }, |
| 101 | + socket: { localAddress: "127.0.0.1", remoteAddress: "127.0.0.1" }, |
| 102 | + } as unknown as IncomingMessage, |
| 103 | + connId: "conn-1", |
| 104 | + remoteAddr: "127.0.0.1", |
| 105 | + localAddr: "127.0.0.1", |
| 106 | + requestHost: "127.0.0.1:19001", |
| 107 | + connectNonce: "nonce-1", |
| 108 | + getResolvedAuth: () => resolvedAuth, |
| 109 | + gatewayMethods: [], |
| 110 | + events: [], |
| 111 | + extraHandlers: {}, |
| 112 | + buildRequestContext: () => ({}) as GatewayRequestContext, |
| 113 | + refreshHealthSnapshot, |
| 114 | + send: vi.fn(), |
| 115 | + close: vi.fn(), |
| 116 | + isClosed: () => false, |
| 117 | + clearHandshakeTimer: vi.fn(), |
| 118 | + getClient: () => client as never, |
| 119 | + setClient: (next) => { |
| 120 | + client = next; |
| 121 | + }, |
| 122 | + setHandshakeState: vi.fn(), |
| 123 | + setCloseCause: vi.fn(), |
| 124 | + setLastFrameMeta: vi.fn(), |
| 125 | + originCheckMetrics: { hostHeaderFallbackAccepted: 0 }, |
| 126 | + logGateway: createLogger() as never, |
| 127 | + logHealth: createLogger() as never, |
| 128 | + logWsControl: createLogger() as never, |
| 129 | + }); |
| 130 | + |
| 131 | + socket.emit( |
| 132 | + "message", |
| 133 | + JSON.stringify({ |
| 134 | + type: "req", |
| 135 | + id: "connect-1", |
| 136 | + method: "connect", |
| 137 | + params: { |
| 138 | + minProtocol: PROTOCOL_VERSION, |
| 139 | + maxProtocol: PROTOCOL_VERSION, |
| 140 | + client: { |
| 141 | + id: "test", |
| 142 | + version: "dev", |
| 143 | + platform: "test", |
| 144 | + mode: "test", |
| 145 | + }, |
| 146 | + auth: { token: "test-token" }, |
| 147 | + role: "operator", |
| 148 | + caps: [], |
| 149 | + }, |
| 150 | + }), |
| 151 | + ); |
| 152 | + |
| 153 | + await vi.waitFor(() => { |
| 154 | + expect(refreshHealthSnapshot).toHaveBeenCalledWith({ probe: true }); |
| 155 | + }); |
| 156 | + resolveRefresh?.(); |
| 157 | + }); |
| 158 | +}); |
0 commit comments