fix: stabilize tests and logging

This commit is contained in:
Peter Steinberger
2026-01-18 18:43:31 +00:00
parent 57dd0505a3
commit ab340c82fb
46 changed files with 700 additions and 335 deletions

View File

@@ -4,7 +4,7 @@ import os from "node:os";
import path from "node:path";
import JSZip from "jszip";
import * as tar from "tar";
import { afterEach, describe, expect, it, vi } from "vitest";
import { afterEach, describe, expect, it } from "vitest";
const tempDirs: string[] = [];
@@ -15,22 +15,6 @@ function makeTempDir() {
return dir;
}
async function withStateDir<T>(stateDir: string, fn: () => Promise<T>) {
const prev = process.env.CLAWDBOT_STATE_DIR;
process.env.CLAWDBOT_STATE_DIR = stateDir;
vi.resetModules();
try {
return await fn();
} finally {
if (prev === undefined) {
delete process.env.CLAWDBOT_STATE_DIR;
} else {
process.env.CLAWDBOT_STATE_DIR = prev;
}
vi.resetModules();
}
}
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
try {
@@ -72,10 +56,9 @@ describe("installHooksFromArchive", () => {
const buffer = await zip.generateAsync({ type: "nodebuffer" });
fs.writeFileSync(archivePath, buffer);
const result = await withStateDir(stateDir, async () => {
const { installHooksFromArchive } = await import("./install.js");
return await installHooksFromArchive({ archivePath });
});
const hooksDir = path.join(stateDir, "hooks");
const { installHooksFromArchive } = await import("./install.js");
const result = await installHooksFromArchive({ archivePath, hooksDir });
expect(result.ok).toBe(true);
if (!result.ok) return;
@@ -121,10 +104,9 @@ describe("installHooksFromArchive", () => {
);
await tar.c({ cwd: workDir, file: archivePath }, ["package"]);
const result = await withStateDir(stateDir, async () => {
const { installHooksFromArchive } = await import("./install.js");
return await installHooksFromArchive({ archivePath });
});
const hooksDir = path.join(stateDir, "hooks");
const { installHooksFromArchive } = await import("./install.js");
const result = await installHooksFromArchive({ archivePath, hooksDir });
expect(result.ok).toBe(true);
if (!result.ok) return;
@@ -155,10 +137,9 @@ describe("installHooksFromPath", () => {
);
fs.writeFileSync(path.join(hookDir, "handler.ts"), "export default async () => {};\n");
const result = await withStateDir(stateDir, async () => {
const { installHooksFromPath } = await import("./install.js");
return await installHooksFromPath({ path: hookDir });
});
const hooksDir = path.join(stateDir, "hooks");
const { installHooksFromPath } = await import("./install.js");
const result = await installHooksFromPath({ path: hookDir, hooksDir });
expect(result.ok).toBe(true);
if (!result.ok) return;