refactor(test): centralize temporary state-dir env setup

This commit is contained in:
Peter Steinberger
2026-02-21 12:59:14 +00:00
parent 50a8942c07
commit c2874aead7
4 changed files with 88 additions and 61 deletions

View File

@@ -1,45 +1,28 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { describe, expect, it } from "vitest";
import { defaultRuntime } from "../runtime.js";
import {
restoreStateDirEnv,
setStateDirEnv,
snapshotStateDirEnv,
} from "../test-helpers/state-dir-env.js";
import { withStateDirEnv } from "../test-helpers/state-dir-env.js";
import { createCanvasHostHandler } from "./server.js";
describe("canvas host state dir defaults", () => {
let envSnapshot: ReturnType<typeof snapshotStateDirEnv>;
beforeEach(() => {
envSnapshot = snapshotStateDirEnv();
});
afterEach(() => {
restoreStateDirEnv(envSnapshot);
});
it("uses OPENCLAW_STATE_DIR for the default canvas root", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-canvas-state-"));
const stateDir = path.join(tempRoot, "state");
setStateDirEnv(stateDir);
const handler = await createCanvasHostHandler({
runtime: defaultRuntime,
allowInTests: true,
});
await withStateDirEnv("openclaw-canvas-state-", async ({ stateDir }) => {
const handler = await createCanvasHostHandler({
runtime: defaultRuntime,
allowInTests: true,
});
try {
const expectedRoot = await fs.realpath(path.join(stateDir, "canvas"));
const actualRoot = await fs.realpath(handler.rootDir);
expect(actualRoot).toBe(expectedRoot);
const indexPath = path.join(expectedRoot, "index.html");
const indexContents = await fs.readFile(indexPath, "utf8");
expect(indexContents).toContain("OpenClaw Canvas");
} finally {
await handler.close();
await fs.rm(tempRoot, { recursive: true, force: true });
}
try {
const expectedRoot = await fs.realpath(path.join(stateDir, "canvas"));
const actualRoot = await fs.realpath(handler.rootDir);
expect(actualRoot).toBe(expectedRoot);
const indexPath = path.join(expectedRoot, "index.html");
const indexContents = await fs.readFile(indexPath, "utf8");
expect(indexContents).toContain("OpenClaw Canvas");
} finally {
await handler.close();
}
});
});
});