Files
TJWaterAgent/tests/runtime/opencodeToolOutputCleanup.test.ts
T
jiang 004c9bb72d
Generic Container CI/CD / test-build-publish (push) Successful in 1m53s
Agent CI/CD v2 / build-test-publish-and-deploy (push) Successful in 1m53s
fix(agent): stabilize large tool results
2026-08-25 12:02:48 +08:00

63 lines
2.1 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import {
lstat,
mkdir,
mkdtemp,
readFile,
rm,
stat,
symlink,
utimes,
writeFile,
} from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { cleanupExpiredToolOutputs } from "../../src/runtime/opencodeToolOutputCleanup.js";
describe("cleanupExpiredToolOutputs", () => {
test("removes only expired regular tool output files", async () => {
const directory = await mkdtemp(join(tmpdir(), "opencode-tool-output-cleanup-"));
const expired = join(directory, "tool_expired");
const current = join(directory, "tool_current");
const unrelated = join(directory, "keep.txt");
const toolDirectory = join(directory, "tool_directory");
const toolSymlink = join(directory, "tool_symlink");
try {
await Promise.all([
writeFile(expired, "expired"),
writeFile(current, "current"),
writeFile(unrelated, "unrelated"),
mkdir(toolDirectory),
]);
await symlink(unrelated, toolSymlink);
const now = Date.now();
const old = new Date(now - 8 * 24 * 60 * 60 * 1000);
await utimes(expired, old, old);
const result = await cleanupExpiredToolOutputs(
directory,
7 * 24 * 60 * 60 * 1000,
now,
);
expect(result).toEqual({ removed: 1, scanned: 5 });
await expect(stat(expired)).rejects.toMatchObject({ code: "ENOENT" });
expect(await readFile(current, "utf8")).toBe("current");
expect(await readFile(unrelated, "utf8")).toBe("unrelated");
expect((await stat(toolDirectory)).isDirectory()).toBe(true);
expect((await lstat(toolSymlink)).isSymbolicLink()).toBe(true);
} finally {
await rm(directory, { force: true, recursive: true });
}
});
test("treats a missing tool output directory as empty", async () => {
const directory = join(tmpdir(), `missing-tool-output-${crypto.randomUUID()}`);
await expect(cleanupExpiredToolOutputs(directory, 1_000)).resolves.toEqual({
removed: 0,
scanned: 0,
});
});
});