mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 21:08:25 +00:00
refactor: eliminate jscpd clones and boost tests
This commit is contained in:
@@ -573,6 +573,43 @@ export async function connectOk(ws: WebSocket, opts?: Parameters<typeof connectR
|
||||
return res.payload as { type: "hello-ok" };
|
||||
}
|
||||
|
||||
export async function connectWebchatClient(params: {
|
||||
port: number;
|
||||
origin?: string;
|
||||
client?: NonNullable<Parameters<typeof connectReq>[1]>["client"];
|
||||
}): Promise<WebSocket> {
|
||||
const origin = params.origin ?? `http://127.0.0.1:${params.port}`;
|
||||
const ws = new WebSocket(`ws://127.0.0.1:${params.port}`, {
|
||||
headers: { origin },
|
||||
});
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const timer = setTimeout(() => reject(new Error("timeout waiting for ws open")), 10_000);
|
||||
const onOpen = () => {
|
||||
clearTimeout(timer);
|
||||
ws.off("error", onError);
|
||||
resolve();
|
||||
};
|
||||
const onError = (err: Error) => {
|
||||
clearTimeout(timer);
|
||||
ws.off("open", onOpen);
|
||||
reject(err);
|
||||
};
|
||||
ws.once("open", onOpen);
|
||||
ws.once("error", onError);
|
||||
});
|
||||
await connectOk(ws, {
|
||||
client:
|
||||
params.client ??
|
||||
({
|
||||
id: GATEWAY_CLIENT_NAMES.WEBCHAT,
|
||||
version: "1.0.0",
|
||||
platform: "test",
|
||||
mode: GATEWAY_CLIENT_MODES.WEBCHAT,
|
||||
} as NonNullable<Parameters<typeof connectReq>[1]>["client"]),
|
||||
});
|
||||
return ws;
|
||||
}
|
||||
|
||||
export async function rpcReq<T extends Record<string, unknown>>(
|
||||
ws: WebSocket,
|
||||
method: string,
|
||||
|
||||
Reference in New Issue
Block a user