mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 18:14:31 +00:00
refactor(core): extract shared dedup helpers
This commit is contained in:
@@ -100,6 +100,26 @@ vi.mock("./translator.js", () => ({
|
||||
describe("serveAcpGateway startup", () => {
|
||||
let serveAcpGateway: typeof import("./server.js").serveAcpGateway;
|
||||
|
||||
function getMockGateway() {
|
||||
const gateway = mockState.gateways[0];
|
||||
if (!gateway) {
|
||||
throw new Error("Expected mocked gateway instance");
|
||||
}
|
||||
return gateway;
|
||||
}
|
||||
|
||||
function captureProcessSignalHandlers() {
|
||||
const signalHandlers = new Map<NodeJS.Signals, () => void>();
|
||||
const onceSpy = vi.spyOn(process, "once").mockImplementation(((
|
||||
signal: NodeJS.Signals,
|
||||
handler: () => void,
|
||||
) => {
|
||||
signalHandlers.set(signal, handler);
|
||||
return process;
|
||||
}) as typeof process.once);
|
||||
return { signalHandlers, onceSpy };
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
({ serveAcpGateway } = await import("./server.js"));
|
||||
});
|
||||
@@ -117,25 +137,14 @@ describe("serveAcpGateway startup", () => {
|
||||
});
|
||||
|
||||
it("waits for gateway hello before creating AgentSideConnection", async () => {
|
||||
const signalHandlers = new Map<NodeJS.Signals, () => void>();
|
||||
const onceSpy = vi.spyOn(process, "once").mockImplementation(((
|
||||
signal: NodeJS.Signals,
|
||||
handler: () => void,
|
||||
) => {
|
||||
signalHandlers.set(signal, handler);
|
||||
return process;
|
||||
}) as typeof process.once);
|
||||
const { signalHandlers, onceSpy } = captureProcessSignalHandlers();
|
||||
|
||||
try {
|
||||
const servePromise = serveAcpGateway({});
|
||||
await Promise.resolve();
|
||||
|
||||
expect(mockState.agentSideConnectionCtor).not.toHaveBeenCalled();
|
||||
const gateway = mockState.gateways[0];
|
||||
if (!gateway) {
|
||||
throw new Error("Expected mocked gateway instance");
|
||||
}
|
||||
|
||||
const gateway = getMockGateway();
|
||||
gateway.emitHello();
|
||||
await vi.waitFor(() => {
|
||||
expect(mockState.agentSideConnectionCtor).toHaveBeenCalledTimes(1);
|
||||
@@ -159,11 +168,7 @@ describe("serveAcpGateway startup", () => {
|
||||
const servePromise = serveAcpGateway({});
|
||||
await Promise.resolve();
|
||||
|
||||
const gateway = mockState.gateways[0];
|
||||
if (!gateway) {
|
||||
throw new Error("Expected mocked gateway instance");
|
||||
}
|
||||
|
||||
const gateway = getMockGateway();
|
||||
gateway.emitConnectError("connect failed");
|
||||
await expect(servePromise).rejects.toThrow("connect failed");
|
||||
expect(mockState.agentSideConnectionCtor).not.toHaveBeenCalled();
|
||||
@@ -177,14 +182,7 @@ describe("serveAcpGateway startup", () => {
|
||||
token: undefined,
|
||||
password: "resolved-secret-password",
|
||||
});
|
||||
const signalHandlers = new Map<NodeJS.Signals, () => void>();
|
||||
const onceSpy = vi.spyOn(process, "once").mockImplementation(((
|
||||
signal: NodeJS.Signals,
|
||||
handler: () => void,
|
||||
) => {
|
||||
signalHandlers.set(signal, handler);
|
||||
return process;
|
||||
}) as typeof process.once);
|
||||
const { signalHandlers, onceSpy } = captureProcessSignalHandlers();
|
||||
|
||||
try {
|
||||
const servePromise = serveAcpGateway({});
|
||||
@@ -200,10 +198,7 @@ describe("serveAcpGateway startup", () => {
|
||||
password: "resolved-secret-password",
|
||||
});
|
||||
|
||||
const gateway = mockState.gateways[0];
|
||||
if (!gateway) {
|
||||
throw new Error("Expected mocked gateway instance");
|
||||
}
|
||||
const gateway = getMockGateway();
|
||||
gateway.emitHello();
|
||||
await vi.waitFor(() => {
|
||||
expect(mockState.agentSideConnectionCtor).toHaveBeenCalledTimes(1);
|
||||
|
||||
Reference in New Issue
Block a user