mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
test: optimize hot-path test runtime
This commit is contained in:
@@ -8,6 +8,29 @@ type TranscriptLine = {
|
||||
message?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
const sessionEntryState = vi.hoisted(() => ({
|
||||
transcriptPath: "",
|
||||
sessionId: "",
|
||||
}));
|
||||
|
||||
vi.mock("../session-utils.js", async (importOriginal) => {
|
||||
const original = await importOriginal<typeof import("../session-utils.js")>();
|
||||
return {
|
||||
...original,
|
||||
loadSessionEntry: () => ({
|
||||
cfg: {},
|
||||
storePath: path.join(path.dirname(sessionEntryState.transcriptPath), "sessions.json"),
|
||||
entry: {
|
||||
sessionId: sessionEntryState.sessionId,
|
||||
sessionFile: sessionEntryState.transcriptPath,
|
||||
},
|
||||
canonicalKey: "main",
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
const { chatHandlers } = await import("./chat.js");
|
||||
|
||||
function createActiveRun(sessionKey: string, sessionId: string) {
|
||||
const now = Date.now();
|
||||
return {
|
||||
@@ -44,29 +67,13 @@ async function readTranscriptLines(transcriptPath: string): Promise<TranscriptLi
|
||||
});
|
||||
}
|
||||
|
||||
async function importChatHandlersWithSession(transcriptPath: string, sessionId: string) {
|
||||
vi.resetModules();
|
||||
vi.doMock("../session-utils.js", async (importOriginal) => {
|
||||
const original = await importOriginal();
|
||||
return {
|
||||
...original,
|
||||
loadSessionEntry: () => ({
|
||||
cfg: {},
|
||||
storePath: path.join(path.dirname(transcriptPath), "sessions.json"),
|
||||
entry: {
|
||||
sessionId,
|
||||
sessionFile: transcriptPath,
|
||||
},
|
||||
canonicalKey: "main",
|
||||
}),
|
||||
};
|
||||
});
|
||||
return import("./chat.js");
|
||||
function setMockSessionEntry(transcriptPath: string, sessionId: string) {
|
||||
sessionEntryState.transcriptPath = transcriptPath;
|
||||
sessionEntryState.sessionId = sessionId;
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
describe("chat abort transcript persistence", () => {
|
||||
@@ -77,7 +84,7 @@ describe("chat abort transcript persistence", () => {
|
||||
const runId = "idem-abort-run-1";
|
||||
await writeTranscriptHeader(transcriptPath, sessionId);
|
||||
|
||||
const { chatHandlers } = await importChatHandlersWithSession(transcriptPath, sessionId);
|
||||
setMockSessionEntry(transcriptPath, sessionId);
|
||||
const respond = vi.fn();
|
||||
const context = {
|
||||
chatAbortControllers: new Map([[runId, createActiveRun("main", sessionId)]]),
|
||||
@@ -142,7 +149,7 @@ describe("chat abort transcript persistence", () => {
|
||||
const sessionId = "sess-main";
|
||||
await writeTranscriptHeader(transcriptPath, sessionId);
|
||||
|
||||
const { chatHandlers } = await importChatHandlersWithSession(transcriptPath, sessionId);
|
||||
setMockSessionEntry(transcriptPath, sessionId);
|
||||
const respond = vi.fn();
|
||||
const context = {
|
||||
chatAbortControllers: new Map([
|
||||
@@ -203,7 +210,7 @@ describe("chat abort transcript persistence", () => {
|
||||
const sessionId = "sess-main";
|
||||
await writeTranscriptHeader(transcriptPath, sessionId);
|
||||
|
||||
const { chatHandlers } = await importChatHandlersWithSession(transcriptPath, sessionId);
|
||||
setMockSessionEntry(transcriptPath, sessionId);
|
||||
const respond = vi.fn();
|
||||
const context = {
|
||||
chatAbortControllers: new Map([["run-stop-1", createActiveRun("main", sessionId)]]),
|
||||
|
||||
@@ -12,20 +12,6 @@ describe("runCommandWithTimeout", () => {
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("passes env overrides to child", async () => {
|
||||
const result = await runCommandWithTimeout(
|
||||
[process.execPath, "-e", 'process.stdout.write(process.env.OPENCLAW_TEST_ENV ?? "")'],
|
||||
{
|
||||
timeoutMs: 5_000,
|
||||
env: { OPENCLAW_TEST_ENV: "ok" },
|
||||
},
|
||||
);
|
||||
|
||||
expect(result.code).toBe(0);
|
||||
expect(result.stdout).toBe("ok");
|
||||
expect(result.termination).toBe("exit");
|
||||
});
|
||||
|
||||
it("merges custom env with process.env", async () => {
|
||||
const envSnapshot = captureEnv(["OPENCLAW_BASE_ENV"]);
|
||||
process.env.OPENCLAW_BASE_ENV = "base";
|
||||
@@ -56,12 +42,12 @@ describe("runCommandWithTimeout", () => {
|
||||
[process.execPath, "-e", "setTimeout(() => {}, 10_000)"],
|
||||
{
|
||||
timeoutMs: 5_000,
|
||||
noOutputTimeoutMs: 300,
|
||||
noOutputTimeoutMs: 200,
|
||||
},
|
||||
);
|
||||
|
||||
const durationMs = Date.now() - startedAt;
|
||||
expect(durationMs).toBeLessThan(2_500);
|
||||
expect(durationMs).toBeLessThan(1_500);
|
||||
expect(result.termination).toBe("no-output-timeout");
|
||||
expect(result.noOutputTimedOut).toBe(true);
|
||||
expect(result.code).not.toBe(0);
|
||||
@@ -72,7 +58,7 @@ describe("runCommandWithTimeout", () => {
|
||||
[
|
||||
process.execPath,
|
||||
"-e",
|
||||
'let i=0; const t=setInterval(() => { process.stdout.write("."); i += 1; if (i >= 5) { clearInterval(t); process.exit(0); } }, 50);',
|
||||
'let i=0; const t=setInterval(() => { process.stdout.write("."); i += 1; if (i >= 4) { clearInterval(t); process.exit(0); } }, 30);',
|
||||
],
|
||||
{
|
||||
timeoutMs: 5_000,
|
||||
@@ -83,14 +69,14 @@ describe("runCommandWithTimeout", () => {
|
||||
expect(result.code).toBe(0);
|
||||
expect(result.termination).toBe("exit");
|
||||
expect(result.noOutputTimedOut).toBe(false);
|
||||
expect(result.stdout.length).toBeGreaterThanOrEqual(5);
|
||||
expect(result.stdout.length).toBeGreaterThanOrEqual(4);
|
||||
});
|
||||
|
||||
it("reports global timeout termination when overall timeout elapses", async () => {
|
||||
const result = await runCommandWithTimeout(
|
||||
[process.execPath, "-e", "setTimeout(() => {}, 10_000)"],
|
||||
{
|
||||
timeoutMs: 200,
|
||||
timeoutMs: 120,
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ describe("process supervisor", () => {
|
||||
mode: "child",
|
||||
argv: [process.execPath, "-e", "setTimeout(() => {}, 10_000)"],
|
||||
timeoutMs: 5_000,
|
||||
noOutputTimeoutMs: 200,
|
||||
noOutputTimeoutMs: 120,
|
||||
stdinMode: "pipe-closed",
|
||||
});
|
||||
const exit = await run.wait();
|
||||
|
||||
Reference in New Issue
Block a user