test: share jsonl socket accept helper

This commit is contained in:
Peter Steinberger
2026-04-19 00:37:58 +01:00
parent a5ea6d3cf4
commit 2172bf1cdd

View File

@@ -20,6 +20,11 @@ async function listenOnSocket(server: net.Server, socketPath: string): Promise<b
}
}
function acceptDoneValue(msg: unknown): number | null | undefined {
const value = msg as { type?: string; value?: number };
return value.type === "done" ? (value.value ?? null) : undefined;
}
describe.runIf(process.platform !== "win32")("requestJsonlSocket", () => {
it("ignores malformed and non-accepted lines until one is accepted", async () => {
await withTempDir({ prefix: "openclaw-jsonl-socket-" }, async (dir) => {
@@ -42,10 +47,7 @@ describe.runIf(process.platform !== "win32")("requestJsonlSocket", () => {
socketPath,
requestLine: '{"hello":"world"}',
timeoutMs: 500,
accept: (msg) => {
const value = msg as { type?: string; value?: number };
return value.type === "done" ? (value.value ?? null) : undefined;
},
accept: acceptDoneValue,
}),
).resolves.toBe(42);
} finally {
@@ -79,10 +81,7 @@ describe.runIf(process.platform !== "win32")("requestJsonlSocket", () => {
socketPath,
requestLine: '{"hello":"world"}',
timeoutMs: 500,
accept: (msg) => {
const value = msg as { type?: string; value?: number };
return value.type === "done" ? (value.value ?? null) : undefined;
},
accept: acceptDoneValue,
}),
).resolves.toBe(7);
expect(receivedBuffer).toBe('{"hello":"world"}\n');