mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 04:27:41 +00:00
refactor(test): centralize temporary state-dir env setup
This commit is contained in:
@@ -1,37 +1,16 @@
|
||||
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 {
|
||||
restoreStateDirEnv,
|
||||
setStateDirEnv,
|
||||
snapshotStateDirEnv,
|
||||
} from "../test-helpers/state-dir-env.js";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { withStateDirEnv } from "../test-helpers/state-dir-env.js";
|
||||
import { loadOrCreateDeviceIdentity } from "./device-identity.js";
|
||||
|
||||
describe("device identity state dir defaults", () => {
|
||||
let envSnapshot: ReturnType<typeof snapshotStateDirEnv>;
|
||||
|
||||
beforeEach(() => {
|
||||
envSnapshot = snapshotStateDirEnv();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
restoreStateDirEnv(envSnapshot);
|
||||
});
|
||||
|
||||
it("writes the default identity file under OPENCLAW_STATE_DIR", async () => {
|
||||
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-identity-state-"));
|
||||
const stateDir = path.join(tempRoot, "state");
|
||||
setStateDirEnv(stateDir);
|
||||
const identity = loadOrCreateDeviceIdentity();
|
||||
|
||||
try {
|
||||
await withStateDirEnv("openclaw-identity-state-", async ({ stateDir }) => {
|
||||
const identity = loadOrCreateDeviceIdentity();
|
||||
const identityPath = path.join(stateDir, "identity", "device.json");
|
||||
const raw = JSON.parse(await fs.readFile(identityPath, "utf8")) as { deviceId?: string };
|
||||
expect(raw.deviceId).toBe(identity.deviceId);
|
||||
} finally {
|
||||
await fs.rm(tempRoot, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user