mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 10:11:24 +00:00
refactor(channels): dedupe transport and gateway test scaffolds
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
|
||||
import { describe, expect, test, vi } from "vitest";
|
||||
import { WebSocket } from "ws";
|
||||
import type { GatewayClient } from "./client.js";
|
||||
import { CONFIG_PATH } from "../config/config.js";
|
||||
import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js";
|
||||
import { GatewayClient } from "./client.js";
|
||||
|
||||
vi.mock("../infra/update-runner.js", () => ({
|
||||
runGatewayUpdate: vi.fn(async () => ({
|
||||
@@ -18,32 +18,18 @@ vi.mock("../infra/update-runner.js", () => ({
|
||||
}));
|
||||
|
||||
import { runGatewayUpdate } from "../infra/update-runner.js";
|
||||
import { sleep } from "../utils.js";
|
||||
import {
|
||||
connectOk,
|
||||
installGatewayTestHooks,
|
||||
onceMessage,
|
||||
rpcReq,
|
||||
startServerWithClient,
|
||||
} from "./test-helpers.js";
|
||||
import { connectGatewayClient } from "./test-helpers.e2e.js";
|
||||
import { connectOk, installGatewayTestHooks, onceMessage, rpcReq } from "./test-helpers.js";
|
||||
import { installConnectedControlUiServerSuite } from "./test-with-server.js";
|
||||
|
||||
installGatewayTestHooks({ scope: "suite" });
|
||||
|
||||
let server: Awaited<ReturnType<typeof startServerWithClient>>["server"];
|
||||
let ws: WebSocket;
|
||||
let port: number;
|
||||
|
||||
beforeAll(async () => {
|
||||
const started = await startServerWithClient(undefined, { controlUiEnabled: true });
|
||||
server = started.server;
|
||||
installConnectedControlUiServerSuite((started) => {
|
||||
ws = started.ws;
|
||||
port = started.port;
|
||||
await connectOk(ws);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
ws.close();
|
||||
await server.close();
|
||||
});
|
||||
|
||||
const connectNodeClient = async (params: {
|
||||
@@ -57,16 +43,8 @@ const connectNodeClient = async (params: {
|
||||
if (!token) {
|
||||
throw new Error("OPENCLAW_GATEWAY_TOKEN is required for node test clients");
|
||||
}
|
||||
let settled = false;
|
||||
let resolveReady: (() => void) | null = null;
|
||||
let rejectReady: ((err: Error) => void) | null = null;
|
||||
const ready = new Promise<void>((resolve, reject) => {
|
||||
resolveReady = resolve;
|
||||
rejectReady = reject;
|
||||
});
|
||||
const client = new GatewayClient({
|
||||
return await connectGatewayClient({
|
||||
url: `ws://127.0.0.1:${params.port}`,
|
||||
connectDelayMs: 0,
|
||||
token,
|
||||
role: "node",
|
||||
clientName: GATEWAY_CLIENT_NAMES.NODE_HOST,
|
||||
@@ -78,36 +56,8 @@ const connectNodeClient = async (params: {
|
||||
scopes: [],
|
||||
commands: params.commands,
|
||||
onEvent: params.onEvent,
|
||||
onHelloOk: () => {
|
||||
if (settled) {
|
||||
return;
|
||||
}
|
||||
settled = true;
|
||||
resolveReady?.();
|
||||
},
|
||||
onConnectError: (err) => {
|
||||
if (settled) {
|
||||
return;
|
||||
}
|
||||
settled = true;
|
||||
rejectReady?.(err);
|
||||
},
|
||||
onClose: (code, reason) => {
|
||||
if (settled) {
|
||||
return;
|
||||
}
|
||||
settled = true;
|
||||
rejectReady?.(new Error(`gateway closed (${code}): ${reason}`));
|
||||
},
|
||||
timeoutMessage: "timeout waiting for node to connect",
|
||||
});
|
||||
client.start();
|
||||
await Promise.race([
|
||||
ready,
|
||||
sleep(10_000).then(() => {
|
||||
throw new Error("timeout waiting for node to connect");
|
||||
}),
|
||||
]);
|
||||
return client;
|
||||
};
|
||||
|
||||
async function waitForSignal(check: () => boolean, timeoutMs = 2000) {
|
||||
|
||||
Reference in New Issue
Block a user