From c9e12cbd32ee09e09a9eeb6cf0ed5ace4f89e867 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 12 Apr 2026 10:23:50 +0100 Subject: [PATCH] test(commands): share backup test fixtures --- src/commands/backup.atomic.test.ts | 37 ++++------------ src/commands/backup.test-support.ts | 40 +++++++++++++++++ src/commands/backup.test.ts | 67 ++++++++--------------------- 3 files changed, 65 insertions(+), 79 deletions(-) create mode 100644 src/commands/backup.test-support.ts diff --git a/src/commands/backup.atomic.test.ts b/src/commands/backup.atomic.test.ts index 692f3ae7c3b..73e5d5b03c9 100644 --- a/src/commands/backup.atomic.test.ts +++ b/src/commands/backup.atomic.test.ts @@ -3,20 +3,13 @@ import os from "node:os"; import path from "node:path"; import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import { createTempHomeEnv, type TempHomeEnv } from "../test-utils/temp-home.js"; -import * as backupShared from "./backup-shared.js"; -import { resolveBackupPlanFromPaths } from "./backup-shared.js"; import { backupCreateCommand } from "./backup.js"; - -const tarCreateMock = vi.hoisted(() => vi.fn()); -const backupVerifyCommandMock = vi.hoisted(() => vi.fn()); - -vi.mock("tar", () => ({ - c: tarCreateMock, -})); - -vi.mock("./backup-verify.js", () => ({ - backupVerifyCommand: backupVerifyCommandMock, -})); +import { + backupVerifyCommandMock, + createBackupTestRuntime, + mockStateOnlyBackupPlan, + tarCreateMock, +} from "./backup.test-support.js"; describe("backupCreateCommand atomic archive write", () => { let tempHome: TempHomeEnv; @@ -54,24 +47,10 @@ describe("backupCreateCommand atomic archive write", () => { await fs.writeFile(path.join(stateDir, "openclaw.json"), JSON.stringify({}), "utf8"); await fs.writeFile(path.join(stateDir, "state.txt"), "state\n", "utf8"); - const runtime = { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn(), - }; + const runtime = createBackupTestRuntime(); const outputPath = path.join(archiveDir, params.outputName ?? "backup.tar.gz"); - vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue( - await resolveBackupPlanFromPaths({ - stateDir, - configPath: path.join(stateDir, "openclaw.json"), - oauthDir: path.join(stateDir, "credentials"), - includeWorkspace: false, - configInsideState: true, - oauthInsideState: true, - nowMs: 123, - }), - ); + await mockStateOnlyBackupPlan(stateDir); return { archiveDir, diff --git a/src/commands/backup.test-support.ts b/src/commands/backup.test-support.ts new file mode 100644 index 00000000000..c98672f7fb6 --- /dev/null +++ b/src/commands/backup.test-support.ts @@ -0,0 +1,40 @@ +import fs from "node:fs/promises"; +import path from "node:path"; +import { vi } from "vitest"; +import type { RuntimeEnv } from "../runtime.js"; +import * as backupShared from "./backup-shared.js"; +import { resolveBackupPlanFromPaths } from "./backup-shared.js"; + +export const tarCreateMock = vi.fn(); +export const backupVerifyCommandMock = vi.fn(); + +vi.mock("tar", () => ({ + c: tarCreateMock, +})); + +vi.mock("./backup-verify.js", () => ({ + backupVerifyCommand: backupVerifyCommandMock, +})); + +export function createBackupTestRuntime(): RuntimeEnv { + return { + log: vi.fn(), + error: vi.fn(), + exit: vi.fn(), + } satisfies RuntimeEnv; +} + +export async function mockStateOnlyBackupPlan(stateDir: string) { + await fs.writeFile(path.join(stateDir, "openclaw.json"), JSON.stringify({}), "utf8"); + vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue( + await resolveBackupPlanFromPaths({ + stateDir, + configPath: path.join(stateDir, "openclaw.json"), + oauthDir: path.join(stateDir, "credentials"), + includeWorkspace: false, + configInsideState: true, + oauthInsideState: true, + nowMs: 123, + }), + ); +} diff --git a/src/commands/backup.test.ts b/src/commands/backup.test.ts index 904c8698207..53d44c9ca2d 100644 --- a/src/commands/backup.test.ts +++ b/src/commands/backup.test.ts @@ -12,17 +12,12 @@ import { resolveBackupPlanFromDisk, } from "./backup-shared.js"; import { backupCreateCommand } from "./backup.js"; - -const tarCreateMock = vi.hoisted(() => vi.fn()); -const backupVerifyCommandMock = vi.hoisted(() => vi.fn()); - -vi.mock("tar", () => ({ - c: tarCreateMock, -})); - -vi.mock("./backup-verify.js", () => ({ - backupVerifyCommand: backupVerifyCommandMock, -})); +import { + backupVerifyCommandMock, + createBackupTestRuntime, + mockStateOnlyBackupPlan, + tarCreateMock, +} from "./backup.test-support.js"; describe("backup commands", () => { let tempHome: TempHomeEnv; @@ -63,21 +58,13 @@ describe("backup commands", () => { await tempHome.restore(); }); - function createRuntime(): RuntimeEnv { - return { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn(), - } satisfies RuntimeEnv; - } - async function withInvalidWorkspaceBackupConfig(fn: (runtime: RuntimeEnv) => Promise) { const stateDir = path.join(tempHome.home, ".openclaw"); const configPath = path.join(tempHome.home, "custom-config.json"); process.env.OPENCLAW_CONFIG_PATH = configPath; await fs.writeFile(path.join(stateDir, "openclaw.json"), JSON.stringify({}), "utf8"); await fs.writeFile(configPath, '{"agents": { defaults: { workspace: ', "utf8"); - const runtime = createRuntime(); + const runtime = createBackupTestRuntime(); try { return await fn(runtime); @@ -175,7 +162,7 @@ describe("backup commands", () => { await fs.writeFile(path.join(stateDir, "state.txt"), "state\n", "utf8"); await fs.writeFile(path.join(externalWorkspace, "SOUL.md"), "# external\n", "utf8"); - const runtime = createRuntime(); + const runtime = createBackupTestRuntime(); const nowMs = Date.UTC(2026, 2, 9, 0, 0, 0); vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue( @@ -269,18 +256,8 @@ describe("backup commands", () => { const stateDir = path.join(tempHome.home, ".openclaw"); await fs.writeFile(path.join(stateDir, "openclaw.json"), JSON.stringify({}), "utf8"); - const runtime = createRuntime(); - vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue( - await resolveBackupPlanFromPaths({ - stateDir, - configPath: path.join(stateDir, "openclaw.json"), - oauthDir: path.join(stateDir, "credentials"), - includeWorkspace: false, - configInsideState: true, - oauthInsideState: true, - nowMs: 123, - }), - ); + const runtime = createBackupTestRuntime(); + await mockStateOnlyBackupPlan(stateDir); await expect( backupCreateCommand(runtime, { @@ -301,18 +278,8 @@ describe("backup commands", () => { await fs.writeFile(path.join(stateDir, "openclaw.json"), JSON.stringify({}), "utf8"); await fs.symlink(stateDir, symlinkPath); - const runtime = createRuntime(); - vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue( - await resolveBackupPlanFromPaths({ - stateDir, - configPath: path.join(stateDir, "openclaw.json"), - oauthDir: path.join(stateDir, "credentials"), - includeWorkspace: false, - configInsideState: true, - oauthInsideState: true, - nowMs: 123, - }), - ); + const runtime = createBackupTestRuntime(); + await mockStateOnlyBackupPlan(stateDir); await expect( backupCreateCommand(runtime, { @@ -344,7 +311,7 @@ describe("backup commands", () => { }), ); - const runtime = createRuntime(); + const runtime = createBackupTestRuntime(); const nowMs = Date.UTC(2026, 2, 9, 1, 2, 3); const result = await backupCreateCommand(runtime, { nowMs }); @@ -383,7 +350,7 @@ describe("backup commands", () => { }), ); - const runtime = createRuntime(); + const runtime = createBackupTestRuntime(); const nowMs = Date.UTC(2026, 2, 9, 1, 3, 4); const result = await backupCreateCommand(runtime, { nowMs }); @@ -414,7 +381,7 @@ describe("backup commands", () => { }), ); - const runtime = createRuntime(); + const runtime = createBackupTestRuntime(); const result = await backupCreateCommand(runtime, { output: existingArchive, @@ -467,7 +434,7 @@ describe("backup commands", () => { }), ); - const runtime = createRuntime(); + const runtime = createBackupTestRuntime(); const result = await backupCreateCommand(runtime, { dryRun: true, @@ -485,7 +452,7 @@ describe("backup commands", () => { process.env.OPENCLAW_CONFIG_PATH = configPath; await fs.writeFile(configPath, '{"agents": { defaults: { workspace: ', "utf8"); - const runtime = createRuntime(); + const runtime = createBackupTestRuntime(); try { const result = await backupCreateCommand(runtime, {