test: share backup temp home helper

This commit is contained in:
Peter Steinberger
2026-04-19 02:29:01 +01:00
parent 5bbfa40255
commit 9380128193
3 changed files with 26 additions and 38 deletions

View File

@@ -7,6 +7,7 @@ import {
backupVerifyCommandMock,
createBackupTestRuntime,
mockStateOnlyBackupPlan,
resetBackupTempHome,
tarCreateMock,
} from "./backup.test-support.js";
@@ -15,18 +16,12 @@ const { backupCreateCommand } = await import("./backup.js");
describe("backupCreateCommand atomic archive write", () => {
let tempHome: TempHomeEnv;
async function resetTempHome() {
await fs.rm(tempHome.home, { recursive: true, force: true });
await fs.mkdir(path.join(tempHome.home, ".openclaw"), { recursive: true });
delete process.env.OPENCLAW_CONFIG_PATH;
}
beforeAll(async () => {
tempHome = await createTempHomeEnv("openclaw-backup-atomic-test-");
});
beforeEach(async () => {
await resetTempHome();
await resetBackupTempHome(tempHome);
tarCreateMock.mockReset();
backupVerifyCommandMock.mockReset();
});

View File

@@ -24,6 +24,12 @@ export function createBackupTestRuntime(): RuntimeEnv {
} satisfies RuntimeEnv;
}
export async function resetBackupTempHome(tempHome: { home: string }) {
await fs.rm(tempHome.home, { recursive: true, force: true });
await fs.mkdir(path.join(tempHome.home, ".openclaw"), { recursive: true });
delete process.env.OPENCLAW_CONFIG_PATH;
}
export async function mockStateOnlyBackupPlan(stateDir: string) {
await fs.writeFile(path.join(stateDir, "openclaw.json"), JSON.stringify({}), "utf8");
vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue(

View File

@@ -15,6 +15,7 @@ import {
backupVerifyCommandMock,
createBackupTestRuntime,
mockStateOnlyBackupPlan,
resetBackupTempHome,
tarCreateMock,
} from "./backup.test-support.js";
@@ -23,10 +24,19 @@ const { backupCreateCommand } = await import("./backup.js");
describe("backup commands", () => {
let tempHome: TempHomeEnv;
async function resetTempHome() {
await fs.rm(tempHome.home, { recursive: true, force: true });
await fs.mkdir(path.join(tempHome.home, ".openclaw"), { recursive: true });
delete process.env.OPENCLAW_CONFIG_PATH;
async function mockWorkspaceBackupPlan(stateDir: string, workspaceDir: string, nowMs: number) {
vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue(
await resolveBackupPlanFromPaths({
stateDir,
configPath: path.join(stateDir, "openclaw.json"),
oauthDir: path.join(stateDir, "credentials"),
workspaceDirs: [workspaceDir],
includeWorkspace: true,
configInsideState: true,
oauthInsideState: true,
nowMs,
}),
);
}
beforeAll(async () => {
@@ -34,7 +44,7 @@ describe("backup commands", () => {
});
beforeEach(async () => {
await resetTempHome();
await resetBackupTempHome(tempHome);
tarCreateMock.mockReset();
tarCreateMock.mockImplementation(async ({ file }: { file: string }) => {
await fs.writeFile(file, "archive-bytes", "utf8");
@@ -299,22 +309,11 @@ describe("backup commands", () => {
await fs.mkdir(workspaceDir, { recursive: true });
await fs.writeFile(path.join(workspaceDir, "SOUL.md"), "# soul\n", "utf8");
vi.spyOn(process, "cwd").mockReturnValue(workspaceDir);
vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue(
await resolveBackupPlanFromPaths({
stateDir,
configPath: path.join(stateDir, "openclaw.json"),
oauthDir: path.join(stateDir, "credentials"),
workspaceDirs: [workspaceDir],
includeWorkspace: true,
configInsideState: true,
oauthInsideState: true,
nowMs: Date.UTC(2026, 2, 9, 1, 2, 3),
}),
);
const nowMs = Date.UTC(2026, 2, 9, 1, 2, 3);
await mockWorkspaceBackupPlan(stateDir, workspaceDir, nowMs);
const runtime = createBackupTestRuntime();
const nowMs = Date.UTC(2026, 2, 9, 1, 2, 3);
const result = await backupCreateCommand(runtime, { nowMs });
expect(result.archivePath).toBe(
@@ -328,20 +327,8 @@ describe("backup commands", () => {
try {
await fs.symlink(workspaceDir, workspaceLink);
vi.mocked(process.cwd).mockReturnValue(workspaceLink);
vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue(
await resolveBackupPlanFromPaths({
stateDir,
configPath: path.join(stateDir, "openclaw.json"),
oauthDir: path.join(stateDir, "credentials"),
workspaceDirs: [workspaceDir],
includeWorkspace: true,
configInsideState: true,
oauthInsideState: true,
nowMs: Date.UTC(2026, 2, 9, 1, 3, 4),
}),
);
const symlinkNowMs = Date.UTC(2026, 2, 9, 1, 3, 4);
await mockWorkspaceBackupPlan(stateDir, workspaceDir, symlinkNowMs);
const symlinkResult = await backupCreateCommand(createBackupTestRuntime(), {
nowMs: symlinkNowMs,
});