From 93801281938b93d60ae15c39aee7eef19d8325c6 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 19 Apr 2026 02:29:01 +0100 Subject: [PATCH] test: share backup temp home helper --- src/commands/backup.atomic.test.ts | 9 ++---- src/commands/backup.test-support.ts | 6 ++++ src/commands/backup.test.ts | 49 +++++++++++------------------ 3 files changed, 26 insertions(+), 38 deletions(-) diff --git a/src/commands/backup.atomic.test.ts b/src/commands/backup.atomic.test.ts index f2d5a50f38b..3f519f57fe1 100644 --- a/src/commands/backup.atomic.test.ts +++ b/src/commands/backup.atomic.test.ts @@ -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(); }); diff --git a/src/commands/backup.test-support.ts b/src/commands/backup.test-support.ts index c98672f7fb6..277a60c95a2 100644 --- a/src/commands/backup.test-support.ts +++ b/src/commands/backup.test-support.ts @@ -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( diff --git a/src/commands/backup.test.ts b/src/commands/backup.test.ts index d82502dba9c..e93b97ff613 100644 --- a/src/commands/backup.test.ts +++ b/src/commands/backup.test.ts @@ -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, });