perf(test): speed up process poll timeout tests

This commit is contained in:
Peter Steinberger
2026-02-15 13:34:40 +00:00
parent 88548784ce
commit b2088d2e1d

View File

@@ -1,56 +1,100 @@
import { afterEach, expect, test } from "vitest"; import { afterEach, expect, test, vi } from "vitest";
import { resetProcessRegistryForTests } from "./bash-process-registry.js"; import type { ProcessSession } from "./bash-process-registry.js";
import { createExecTool } from "./bash-tools.exec.js"; import {
addSession,
appendOutput,
markExited,
resetProcessRegistryForTests,
} from "./bash-process-registry.js";
import { createProcessTool } from "./bash-tools.process.js"; import { createProcessTool } from "./bash-tools.process.js";
afterEach(() => { afterEach(() => {
resetProcessRegistryForTests(); resetProcessRegistryForTests();
}); });
const sleepAndEcho = function createBackgroundSession(id: string): ProcessSession {
process.platform === "win32" return {
? "Start-Sleep -Milliseconds 300; Write-Output done" id,
: "sleep 0.3; echo done"; command: "test",
startedAt: Date.now(),
cwd: "/tmp",
maxOutputChars: 10_000,
pendingMaxOutputChars: 30_000,
totalOutputChars: 0,
pendingStdout: [],
pendingStderr: [],
pendingStdoutChars: 0,
pendingStderrChars: 0,
aggregated: "",
tail: "",
exited: false,
exitCode: undefined,
exitSignal: undefined,
truncated: false,
backgrounded: true,
};
}
test("process poll waits for completion when timeout is provided", async () => { test("process poll waits for completion when timeout is provided", async () => {
const execTool = createExecTool(); vi.useFakeTimers();
const processTool = createProcessTool(); try {
const started = Date.now(); const processTool = createProcessTool();
const run = await execTool.execute("toolcall", { const sessionId = "sess";
command: sleepAndEcho, const session = createBackgroundSession(sessionId);
background: true, addSession(session);
});
expect(run.details.status).toBe("running");
const sessionId = run.details.sessionId;
const poll = await processTool.execute("toolcall", { setTimeout(() => {
action: "poll", appendOutput(session, "stdout", "done\n");
sessionId, markExited(session, 0, null, "completed");
timeout: 2000, }, 10);
});
const elapsedMs = Date.now() - started; const pollPromise = processTool.execute("toolcall", {
const details = poll.details as { status?: string; aggregated?: string }; action: "poll",
expect(details.status).toBe("completed"); sessionId,
expect(details.aggregated ?? "").toContain("done"); timeout: 2000,
expect(elapsedMs).toBeGreaterThanOrEqual(200); });
let resolved = false;
void pollPromise.finally(() => {
resolved = true;
});
await vi.advanceTimersByTimeAsync(200);
expect(resolved).toBe(false);
await vi.advanceTimersByTimeAsync(100);
const poll = await pollPromise;
const details = poll.details as { status?: string; aggregated?: string };
expect(details.status).toBe("completed");
expect(details.aggregated ?? "").toContain("done");
} finally {
vi.useRealTimers();
}
}); });
test("process poll accepts string timeout values", async () => { test("process poll accepts string timeout values", async () => {
const execTool = createExecTool(); vi.useFakeTimers();
const processTool = createProcessTool(); try {
const run = await execTool.execute("toolcall", { const processTool = createProcessTool();
command: sleepAndEcho, const sessionId = "sess-2";
background: true, const session = createBackgroundSession(sessionId);
}); addSession(session);
expect(run.details.status).toBe("running"); setTimeout(() => {
const sessionId = run.details.sessionId; appendOutput(session, "stdout", "done\n");
markExited(session, 0, null, "completed");
}, 10);
const poll = await processTool.execute("toolcall", { const pollPromise = processTool.execute("toolcall", {
action: "poll", action: "poll",
sessionId, sessionId,
timeout: "2000", timeout: "2000",
}); });
const details = poll.details as { status?: string; aggregated?: string }; await vi.advanceTimersByTimeAsync(350);
expect(details.status).toBe("completed"); const poll = await pollPromise;
expect(details.aggregated ?? "").toContain("done"); const details = poll.details as { status?: string; aggregated?: string };
expect(details.status).toBe("completed");
expect(details.aggregated ?? "").toContain("done");
} finally {
vi.useRealTimers();
}
}); });