openclaw/test/scripts/sqlite-reliability-process.test.ts
Peter Steinberger 65cb5ab547
test(scripts): remove low-value tests (batch d152) (#163549)
* test(scripts): deslop s1015 tests

* test(scripts): deslop s1023 tests

* test(scripts): deslop s1026 tests

* test(scripts): deslop s1027 tests

* test(scripts): deslop s1020 tests

* test(scripts): deslop s1031 tests

* test(scripts): deslop s1038 tests

* test(scripts): deslop s1025 tests

* test(scripts): deslop s1028 tests

* test(scripts): deslop s1037 tests

* test(scripts): retain rollout ancestry coverage
2026-10-02 07:23:04 -07:00

128 lines
4.2 KiB
TypeScript

import { ChildProcess } from "node:child_process";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { waitForReliabilityWorkerMessage } from "../../scripts/lib/sqlite-reliability-process.js";
import { waitForWriterMessage } from "../../scripts/lib/sqlite-reliability-writer.js";
function waitForReady(
child: ChildProcess,
overrides: Partial<Parameters<typeof waitForReliabilityWorkerMessage>[0]> = {},
) {
return waitForReliabilityWorkerMessage({
child,
matches: (message) => message === "ready",
timeoutMs: 30_000,
timeoutMessage: () => "worker timed out",
exitMessage: (code, signal) => `worker exited: ${code}/${signal}`,
...overrides,
});
}
function expectWaitCleanedUp(child: ChildProcess) {
for (const event of ["message", "error", "exit"]) {
expect(child.listenerCount(event)).toBe(0);
}
expect(vi.getTimerCount()).toBe(0);
}
describe("SQLite reliability worker messages", () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it("rejects child errors without replacing the original error", async () => {
const child = new ChildProcess();
const error = new Error("IPC failed");
const ready = waitForReady(child);
child.emit("error", error);
await expect(ready).rejects.toBe(error);
expectWaitCleanedUp(child);
});
it("formats premature exit diagnostics when the child exits", async () => {
const child = new ChildProcess();
let stderr = "before";
const ready = waitForReady(child, {
exitMessage: (code, signal) => `exit ${code}/${signal}: ${stderr}`,
});
stderr = "last stderr";
child.emit("exit", null, "SIGKILL");
await expect(ready).rejects.toThrow("exit null/SIGKILL: last stderr");
expectWaitCleanedUp(child);
});
it("honors the configured timeout and reads final diagnostics", async () => {
const timeoutMs = 120_000;
const child = new ChildProcess();
let stderr = "before";
const ready = waitForReady(child, {
timeoutMs,
timeoutMessage: () => `timeout: ${stderr}`,
});
const rejected = expect(ready).rejects.toThrow("timeout: last stderr");
await vi.advanceTimersByTimeAsync(timeoutMs - 1);
expect(child.listenerCount("message")).toBe(1);
stderr = "last stderr";
await vi.advanceTimersByTimeAsync(1);
await rejected;
expectWaitCleanedUp(child);
});
it.each(["action", "matches"] as const)("cleans up when %s throws", async (source) => {
const child = new ChildProcess();
const error = new Error(`${source} failed`);
const ready = waitForReady(child, {
action: () => {
if (source === "action") {
throw error;
}
child.emit("message", "ready");
},
matches: () => {
throw error;
},
});
await expect(ready).rejects.toBe(error);
expectWaitCleanedUp(child);
});
it("rejects a writer error payload instead of waiting for the requested message", async () => {
const child = new ChildProcess();
const result = waitForWriterMessage({ child, stderr: [], stopped: false }, "result");
child.emit("message", { kind: "error", error: "write failed" });
await expect(result).rejects.toThrow("SQLite reliability writer failed: write failed");
expectWaitCleanedUp(child);
});
it("waits for the synchronous writer reply while preserving other listeners", async () => {
const child = new ChildProcess();
const observed: unknown[] = [];
const observe = (message: unknown) => observed.push(message);
child.on("message", observe);
const payload = { kind: "result", batchesCommitted: 2, rowsCommitted: 16 };
const result = await waitForWriterMessage(
{ child, stderr: [], stopped: false },
"result",
() => {
child.emit("message", { kind: "ready" });
expect(child.listenerCount("message")).toBe(2);
child.emit("message", payload);
},
);
expect(result.batchesCommitted).toBe(2);
expect(result.rowsCommitted).toBe(16);
expect(result).toBe(payload);
expect(observed).toEqual([{ kind: "ready" }, payload]);
expect(child.listeners("message")).toEqual([observe]);
child.off("message", observe);
expectWaitCleanedUp(child);
});
});