






















1-import { EventEmitter } from "node:events";
21import { describe, expect, it, vi } from "vitest";
3-import type { WebSocketServer } from "ws";
42import {
53GATEWAY_CLIENT_MODES,
64GATEWAY_CLIENT_NAMES,
@@ -13,75 +11,34 @@ import {
1311GATEWAY_STARTUP_UNAVAILABLE_REASON,
1412} from "../../../packages/gateway-protocol/src/startup-unavailable.js";
1513import { attachGatewayWsConnectionHandler } from "./ws-connection.js";
16-17-function createLogger() {
18-return {
19-debug: vi.fn(),
20-info: vi.fn(),
21-warn: vi.fn(),
22-error: vi.fn(),
23-};
24-}
25-26-function createRequestContext() {
27-return {
28-unsubscribeAllSessionEvents: vi.fn(),
29-nodeRegistry: { unregister: vi.fn() },
30-nodeUnsubscribeAll: vi.fn(),
31-};
32-}
14+import {
15+attachGatewayWsForTest,
16+createGatewayWsTestLogger,
17+createGatewayWsTestRequestContext,
18+createGatewayWsTestSocket,
19+} from "./ws-connection.test-helpers.js";
33203421describe("attachGatewayWsConnectionHandler startup readiness", () => {
3522it("returns a retryable startup-unavailable connect response while sidecars are pending", async () => {
36-const listeners = new Map<string, (...args: unknown[]) => void>();
37-const wss = {
38-on: vi.fn((event: string, handler: (...args: unknown[]) => void) => {
39-listeners.set(event, handler);
40-}),
41-} as unknown as WebSocketServer;
4223const sent: unknown[] = [];
43-const socket = Object.assign(new EventEmitter(), {
44-_socket: {
45-remoteAddress: "127.0.0.1",
46-remotePort: 1234,
47-localAddress: "127.0.0.1",
48-localPort: 5678,
49-},
50-send: vi.fn((data: string, cb?: (err?: Error) => void) => {
24+const socket = createGatewayWsTestSocket({
25+closeEmits: true,
26+onSend: (data) => {
5127sent.push(JSON.parse(data));
52-cb?.();
53-}),
54-close: vi.fn((code?: number, reason?: string) => {
55-socket.emit("close", code ?? 1000, Buffer.from(reason ?? ""));
56-}),
28+},
5729});
58-const upgradeReq = {
59-headers: { host: "127.0.0.1:19001" },
60-socket: { localAddress: "127.0.0.1" },
61-};
62-const logWsControl = createLogger();
30+const logWsControl = createGatewayWsTestLogger();
633164-attachGatewayWsConnectionHandler({
65- wss,
66-clients: new Set(),
67-preauthConnectionBudget: { release: vi.fn() } as never,
68-port: 19001,
69-resolvedAuth: { mode: "none", allowTailscale: false },
70-isStartupPending: () => true,
71-gatewayMethods: [],
72-events: [],
73-refreshHealthSnapshot: vi.fn(async () => ({}) as never),
74-logGateway: createLogger() as never,
75-logHealth: createLogger() as never,
76-logWsControl: logWsControl as never,
77-extraHandlers: {},
78-broadcast: vi.fn(),
79-buildRequestContext: () => createRequestContext() as never,
32+attachGatewayWsForTest({
33+attach: attachGatewayWsConnectionHandler,
34+ socket,
35+options: {
36+resolvedAuth: { mode: "none", allowTailscale: false },
37+isStartupPending: () => true,
38+logWsControl: logWsControl as never,
39+buildRequestContext: () => createGatewayWsTestRequestContext() as never,
40+},
8041});
81-82-const onConnection = listeners.get("connection");
83-expect(onConnection).toBeTypeOf("function");
84-onConnection?.(socket, upgradeReq);
8542socket.emit(
8643"message",
8744JSON.stringify({
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。