Skip to content

Commit c9e12cb

Browse files
committed
test(commands): share backup test fixtures
1 parent 18ae46f commit c9e12cb

3 files changed

Lines changed: 65 additions & 79 deletions

File tree

src/commands/backup.atomic.test.ts

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,13 @@ import os from "node:os";
33
import path from "node:path";
44
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
55
import { createTempHomeEnv, type TempHomeEnv } from "../test-utils/temp-home.js";
6-
import * as backupShared from "./backup-shared.js";
7-
import { resolveBackupPlanFromPaths } from "./backup-shared.js";
86
import { backupCreateCommand } from "./backup.js";
9-
10-
const tarCreateMock = vi.hoisted(() => vi.fn());
11-
const backupVerifyCommandMock = vi.hoisted(() => vi.fn());
12-
13-
vi.mock("tar", () => ({
14-
c: tarCreateMock,
15-
}));
16-
17-
vi.mock("./backup-verify.js", () => ({
18-
backupVerifyCommand: backupVerifyCommandMock,
19-
}));
7+
import {
8+
backupVerifyCommandMock,
9+
createBackupTestRuntime,
10+
mockStateOnlyBackupPlan,
11+
tarCreateMock,
12+
} from "./backup.test-support.js";
2013

2114
describe("backupCreateCommand atomic archive write", () => {
2215
let tempHome: TempHomeEnv;
@@ -54,24 +47,10 @@ describe("backupCreateCommand atomic archive write", () => {
5447
await fs.writeFile(path.join(stateDir, "openclaw.json"), JSON.stringify({}), "utf8");
5548
await fs.writeFile(path.join(stateDir, "state.txt"), "state\n", "utf8");
5649

57-
const runtime = {
58-
log: vi.fn(),
59-
error: vi.fn(),
60-
exit: vi.fn(),
61-
};
50+
const runtime = createBackupTestRuntime();
6251
const outputPath = path.join(archiveDir, params.outputName ?? "backup.tar.gz");
6352

64-
vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue(
65-
await resolveBackupPlanFromPaths({
66-
stateDir,
67-
configPath: path.join(stateDir, "openclaw.json"),
68-
oauthDir: path.join(stateDir, "credentials"),
69-
includeWorkspace: false,
70-
configInsideState: true,
71-
oauthInsideState: true,
72-
nowMs: 123,
73-
}),
74-
);
53+
await mockStateOnlyBackupPlan(stateDir);
7554

7655
return {
7756
archiveDir,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import fs from "node:fs/promises";
2+
import path from "node:path";
3+
import { vi } from "vitest";
4+
import type { RuntimeEnv } from "../runtime.js";
5+
import * as backupShared from "./backup-shared.js";
6+
import { resolveBackupPlanFromPaths } from "./backup-shared.js";
7+
8+
export const tarCreateMock = vi.fn();
9+
export const backupVerifyCommandMock = vi.fn();
10+
11+
vi.mock("tar", () => ({
12+
c: tarCreateMock,
13+
}));
14+
15+
vi.mock("./backup-verify.js", () => ({
16+
backupVerifyCommand: backupVerifyCommandMock,
17+
}));
18+
19+
export function createBackupTestRuntime(): RuntimeEnv {
20+
return {
21+
log: vi.fn(),
22+
error: vi.fn(),
23+
exit: vi.fn(),
24+
} satisfies RuntimeEnv;
25+
}
26+
27+
export async function mockStateOnlyBackupPlan(stateDir: string) {
28+
await fs.writeFile(path.join(stateDir, "openclaw.json"), JSON.stringify({}), "utf8");
29+
vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue(
30+
await resolveBackupPlanFromPaths({
31+
stateDir,
32+
configPath: path.join(stateDir, "openclaw.json"),
33+
oauthDir: path.join(stateDir, "credentials"),
34+
includeWorkspace: false,
35+
configInsideState: true,
36+
oauthInsideState: true,
37+
nowMs: 123,
38+
}),
39+
);
40+
}

src/commands/backup.test.ts

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,12 @@ import {
1212
resolveBackupPlanFromDisk,
1313
} from "./backup-shared.js";
1414
import { backupCreateCommand } from "./backup.js";
15-
16-
const tarCreateMock = vi.hoisted(() => vi.fn());
17-
const backupVerifyCommandMock = vi.hoisted(() => vi.fn());
18-
19-
vi.mock("tar", () => ({
20-
c: tarCreateMock,
21-
}));
22-
23-
vi.mock("./backup-verify.js", () => ({
24-
backupVerifyCommand: backupVerifyCommandMock,
25-
}));
15+
import {
16+
backupVerifyCommandMock,
17+
createBackupTestRuntime,
18+
mockStateOnlyBackupPlan,
19+
tarCreateMock,
20+
} from "./backup.test-support.js";
2621

2722
describe("backup commands", () => {
2823
let tempHome: TempHomeEnv;
@@ -63,21 +58,13 @@ describe("backup commands", () => {
6358
await tempHome.restore();
6459
});
6560

66-
function createRuntime(): RuntimeEnv {
67-
return {
68-
log: vi.fn(),
69-
error: vi.fn(),
70-
exit: vi.fn(),
71-
} satisfies RuntimeEnv;
72-
}
73-
7461
async function withInvalidWorkspaceBackupConfig<T>(fn: (runtime: RuntimeEnv) => Promise<T>) {
7562
const stateDir = path.join(tempHome.home, ".openclaw");
7663
const configPath = path.join(tempHome.home, "custom-config.json");
7764
process.env.OPENCLAW_CONFIG_PATH = configPath;
7865
await fs.writeFile(path.join(stateDir, "openclaw.json"), JSON.stringify({}), "utf8");
7966
await fs.writeFile(configPath, '{"agents": { defaults: { workspace: ', "utf8");
80-
const runtime = createRuntime();
67+
const runtime = createBackupTestRuntime();
8168

8269
try {
8370
return await fn(runtime);
@@ -175,7 +162,7 @@ describe("backup commands", () => {
175162
await fs.writeFile(path.join(stateDir, "state.txt"), "state\n", "utf8");
176163
await fs.writeFile(path.join(externalWorkspace, "SOUL.md"), "# external\n", "utf8");
177164

178-
const runtime = createRuntime();
165+
const runtime = createBackupTestRuntime();
179166

180167
const nowMs = Date.UTC(2026, 2, 9, 0, 0, 0);
181168
vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue(
@@ -269,18 +256,8 @@ describe("backup commands", () => {
269256
const stateDir = path.join(tempHome.home, ".openclaw");
270257
await fs.writeFile(path.join(stateDir, "openclaw.json"), JSON.stringify({}), "utf8");
271258

272-
const runtime = createRuntime();
273-
vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue(
274-
await resolveBackupPlanFromPaths({
275-
stateDir,
276-
configPath: path.join(stateDir, "openclaw.json"),
277-
oauthDir: path.join(stateDir, "credentials"),
278-
includeWorkspace: false,
279-
configInsideState: true,
280-
oauthInsideState: true,
281-
nowMs: 123,
282-
}),
283-
);
259+
const runtime = createBackupTestRuntime();
260+
await mockStateOnlyBackupPlan(stateDir);
284261

285262
await expect(
286263
backupCreateCommand(runtime, {
@@ -301,18 +278,8 @@ describe("backup commands", () => {
301278
await fs.writeFile(path.join(stateDir, "openclaw.json"), JSON.stringify({}), "utf8");
302279
await fs.symlink(stateDir, symlinkPath);
303280

304-
const runtime = createRuntime();
305-
vi.spyOn(backupShared, "resolveBackupPlanFromDisk").mockResolvedValue(
306-
await resolveBackupPlanFromPaths({
307-
stateDir,
308-
configPath: path.join(stateDir, "openclaw.json"),
309-
oauthDir: path.join(stateDir, "credentials"),
310-
includeWorkspace: false,
311-
configInsideState: true,
312-
oauthInsideState: true,
313-
nowMs: 123,
314-
}),
315-
);
281+
const runtime = createBackupTestRuntime();
282+
await mockStateOnlyBackupPlan(stateDir);
316283

317284
await expect(
318285
backupCreateCommand(runtime, {
@@ -344,7 +311,7 @@ describe("backup commands", () => {
344311
}),
345312
);
346313

347-
const runtime = createRuntime();
314+
const runtime = createBackupTestRuntime();
348315

349316
const nowMs = Date.UTC(2026, 2, 9, 1, 2, 3);
350317
const result = await backupCreateCommand(runtime, { nowMs });
@@ -383,7 +350,7 @@ describe("backup commands", () => {
383350
}),
384351
);
385352

386-
const runtime = createRuntime();
353+
const runtime = createBackupTestRuntime();
387354

388355
const nowMs = Date.UTC(2026, 2, 9, 1, 3, 4);
389356
const result = await backupCreateCommand(runtime, { nowMs });
@@ -414,7 +381,7 @@ describe("backup commands", () => {
414381
}),
415382
);
416383

417-
const runtime = createRuntime();
384+
const runtime = createBackupTestRuntime();
418385

419386
const result = await backupCreateCommand(runtime, {
420387
output: existingArchive,
@@ -467,7 +434,7 @@ describe("backup commands", () => {
467434
}),
468435
);
469436

470-
const runtime = createRuntime();
437+
const runtime = createBackupTestRuntime();
471438

472439
const result = await backupCreateCommand(runtime, {
473440
dryRun: true,
@@ -485,7 +452,7 @@ describe("backup commands", () => {
485452
process.env.OPENCLAW_CONFIG_PATH = configPath;
486453
await fs.writeFile(configPath, '{"agents": { defaults: { workspace: ', "utf8");
487454

488-
const runtime = createRuntime();
455+
const runtime = createBackupTestRuntime();
489456

490457
try {
491458
const result = await backupCreateCommand(runtime, {

0 commit comments

Comments
 (0)