mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 07:30:43 +00:00
test(commands): share backup test fixtures
This commit is contained in:
@@ -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,
|
||||
|
||||
40
src/commands/backup.test-support.ts
Normal file
40
src/commands/backup.test-support.ts
Normal file
@@ -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,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -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<T>(fn: (runtime: RuntimeEnv) => Promise<T>) {
|
||||
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, {
|
||||
|
||||
Reference in New Issue
Block a user