import { mkdtemp, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { afterEach, beforeEach, vi } from "vitest"; /** * Points the registry and credential store at a fresh temp directory for each test and * resets the module cache, so modules that read env at import time pick it up. */ export function withTempDataDir() { const dir = { path: "" }; beforeEach(async () => { dir.path = await mkdtemp(path.join(os.tmpdir(), "cameras-test-")); vi.stubEnv("CAMERA_REGISTRY_FILE", path.join(dir.path, "cameras.json")); vi.stubEnv("CAMERA_CREDENTIALS_FILE", path.join(dir.path, "credentials.json")); vi.stubEnv("CAMERA_CREDENTIALS_KEY", "test-key"); vi.stubEnv("ONVIF_USERNAME", ""); vi.stubEnv("ONVIF_PASSWORD", ""); vi.resetModules(); }); afterEach(async () => { await rm(dir.path, { recursive: true, force: true }); }); return dir; }