mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 17:01:53 +03:00
fix: stabilize tests and logging
This commit is contained in:
+46
-7
@@ -1,21 +1,60 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import JSZip from "jszip";
|
||||
import sharp from "sharp";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
|
||||
import { isPathWithinBase } from "../../test/helpers/paths.js";
|
||||
import { withTempHome } from "../../test/helpers/temp-home.js";
|
||||
|
||||
describe("media store", () => {
|
||||
let store: typeof import("./store.js");
|
||||
let home = "";
|
||||
const envSnapshot: Record<string, string | undefined> = {};
|
||||
|
||||
const snapshotEnv = () => {
|
||||
for (const key of ["HOME", "USERPROFILE", "HOMEDRIVE", "HOMEPATH", "CLAWDBOT_STATE_DIR"]) {
|
||||
envSnapshot[key] = process.env[key];
|
||||
}
|
||||
};
|
||||
|
||||
const restoreEnv = () => {
|
||||
for (const [key, value] of Object.entries(envSnapshot)) {
|
||||
if (value === undefined) delete process.env[key];
|
||||
else process.env[key] = value;
|
||||
}
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
snapshotEnv();
|
||||
home = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-test-home-"));
|
||||
process.env.HOME = home;
|
||||
process.env.USERPROFILE = home;
|
||||
process.env.CLAWDBOT_STATE_DIR = path.join(home, ".clawdbot");
|
||||
if (process.platform === "win32") {
|
||||
const match = home.match(/^([A-Za-z]:)(.*)$/);
|
||||
if (match) {
|
||||
process.env.HOMEDRIVE = match[1];
|
||||
process.env.HOMEPATH = match[2] || "\\";
|
||||
}
|
||||
}
|
||||
await fs.mkdir(path.join(home, ".clawdbot"), { recursive: true });
|
||||
store = await import("./store.js");
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
restoreEnv();
|
||||
try {
|
||||
await fs.rm(home, { recursive: true, force: true });
|
||||
} catch {
|
||||
// ignore cleanup failures in tests
|
||||
}
|
||||
});
|
||||
|
||||
async function withTempStore<T>(
|
||||
fn: (store: typeof import("./store.js"), home: string) => Promise<T>,
|
||||
): Promise<T> {
|
||||
return await withTempHome(async (home) => {
|
||||
vi.resetModules();
|
||||
const store = await import("./store.js");
|
||||
return await fn(store, home);
|
||||
});
|
||||
return await fn(store, home);
|
||||
}
|
||||
|
||||
it("creates and returns media directory", async () => {
|
||||
|
||||
Reference in New Issue
Block a user