test: dedupe and optimize test suites
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig, ConfigFileSnapshot } from "../config/types.openclaw.js";
|
||||
import type { UpdateRunResult } from "../infra/update-runner.js";
|
||||
import { captureEnv } from "../test-utils/env.js";
|
||||
@@ -120,23 +118,15 @@ const { updateCommand, registerUpdateCli, updateStatusCommand, updateWizardComma
|
||||
await import("./update-cli.js");
|
||||
|
||||
describe("update-cli", () => {
|
||||
let fixtureRoot = "";
|
||||
const fixtureRoot = "/tmp/openclaw-update-tests";
|
||||
let fixtureCount = 0;
|
||||
|
||||
const createCaseDir = async (prefix: string) => {
|
||||
const createCaseDir = (prefix: string) => {
|
||||
const dir = path.join(fixtureRoot, `${prefix}-${fixtureCount++}`);
|
||||
// Tests only need a stable path; the directory does not have to exist because all I/O is mocked.
|
||||
return dir;
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-update-tests-"));
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await fs.rm(fixtureRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const baseConfig = {} as OpenClawConfig;
|
||||
const baseSnapshot: ConfigFileSnapshot = {
|
||||
path: "/tmp/openclaw-config.json",
|
||||
@@ -186,8 +176,17 @@ describe("update-cli", () => {
|
||||
return call;
|
||||
};
|
||||
|
||||
const makeOkUpdateResult = (overrides: Partial<UpdateRunResult> = {}): UpdateRunResult =>
|
||||
({
|
||||
status: "ok",
|
||||
mode: "git",
|
||||
steps: [],
|
||||
durationMs: 100,
|
||||
...overrides,
|
||||
}) as UpdateRunResult;
|
||||
|
||||
const setupNonInteractiveDowngrade = async () => {
|
||||
const tempDir = await createCaseDir("openclaw-update");
|
||||
const tempDir = createCaseDir("openclaw-update");
|
||||
setTty(false);
|
||||
readPackageVersion.mockResolvedValue("2.0.0");
|
||||
|
||||
@@ -332,55 +331,53 @@ describe("update-cli", () => {
|
||||
expect(parsed.channel.value).toBe("stable");
|
||||
});
|
||||
|
||||
it("defaults to dev channel for git installs when unset", async () => {
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue({
|
||||
status: "ok",
|
||||
mode: "git",
|
||||
steps: [],
|
||||
durationMs: 100,
|
||||
});
|
||||
it.each([
|
||||
{
|
||||
name: "defaults to dev channel for git installs when unset",
|
||||
mode: "git" as const,
|
||||
options: {},
|
||||
prepare: async () => {},
|
||||
expectedChannel: "dev" as const,
|
||||
expectedTag: undefined as string | undefined,
|
||||
},
|
||||
{
|
||||
name: "defaults to stable channel for package installs when unset",
|
||||
mode: "npm" as const,
|
||||
options: { yes: true },
|
||||
prepare: async () => {
|
||||
const tempDir = createCaseDir("openclaw-update");
|
||||
mockPackageInstallStatus(tempDir);
|
||||
},
|
||||
expectedChannel: "stable" as const,
|
||||
expectedTag: "latest",
|
||||
},
|
||||
{
|
||||
name: "uses stored beta channel when configured",
|
||||
mode: "git" as const,
|
||||
options: {},
|
||||
prepare: async () => {
|
||||
vi.mocked(readConfigFileSnapshot).mockResolvedValue({
|
||||
...baseSnapshot,
|
||||
config: { update: { channel: "beta" } } as OpenClawConfig,
|
||||
});
|
||||
},
|
||||
expectedChannel: "beta" as const,
|
||||
expectedTag: undefined as string | undefined,
|
||||
},
|
||||
])("$name", async ({ mode, options, prepare, expectedChannel, expectedTag }) => {
|
||||
await prepare();
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(makeOkUpdateResult({ mode }));
|
||||
|
||||
await updateCommand({});
|
||||
await updateCommand(options);
|
||||
|
||||
expectUpdateCallChannel("dev");
|
||||
});
|
||||
|
||||
it("defaults to stable channel for package installs when unset", async () => {
|
||||
const tempDir = await createCaseDir("openclaw-update");
|
||||
|
||||
mockPackageInstallStatus(tempDir);
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue({
|
||||
status: "ok",
|
||||
mode: "npm",
|
||||
steps: [],
|
||||
durationMs: 100,
|
||||
});
|
||||
|
||||
await updateCommand({ yes: true });
|
||||
|
||||
const call = expectUpdateCallChannel("stable");
|
||||
expect(call?.tag).toBe("latest");
|
||||
});
|
||||
|
||||
it("uses stored beta channel when configured", async () => {
|
||||
vi.mocked(readConfigFileSnapshot).mockResolvedValue({
|
||||
...baseSnapshot,
|
||||
config: { update: { channel: "beta" } } as OpenClawConfig,
|
||||
});
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue({
|
||||
status: "ok",
|
||||
mode: "git",
|
||||
steps: [],
|
||||
durationMs: 100,
|
||||
});
|
||||
|
||||
await updateCommand({});
|
||||
|
||||
expectUpdateCallChannel("beta");
|
||||
const call = expectUpdateCallChannel(expectedChannel);
|
||||
if (expectedTag !== undefined) {
|
||||
expect(call?.tag).toBe(expectedTag);
|
||||
}
|
||||
});
|
||||
|
||||
it("falls back to latest when beta tag is older than release", async () => {
|
||||
const tempDir = await createCaseDir("openclaw-update");
|
||||
const tempDir = createCaseDir("openclaw-update");
|
||||
|
||||
mockPackageInstallStatus(tempDir);
|
||||
vi.mocked(readConfigFileSnapshot).mockResolvedValue({
|
||||
@@ -391,12 +388,11 @@ describe("update-cli", () => {
|
||||
tag: "latest",
|
||||
version: "1.2.3-1",
|
||||
});
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue({
|
||||
status: "ok",
|
||||
mode: "npm",
|
||||
steps: [],
|
||||
durationMs: 100,
|
||||
});
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(
|
||||
makeOkUpdateResult({
|
||||
mode: "npm",
|
||||
}),
|
||||
);
|
||||
|
||||
await updateCommand({});
|
||||
|
||||
@@ -405,15 +401,14 @@ describe("update-cli", () => {
|
||||
});
|
||||
|
||||
it("honors --tag override", async () => {
|
||||
const tempDir = await createCaseDir("openclaw-update");
|
||||
const tempDir = createCaseDir("openclaw-update");
|
||||
|
||||
vi.mocked(resolveOpenClawPackageRoot).mockResolvedValue(tempDir);
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue({
|
||||
status: "ok",
|
||||
mode: "npm",
|
||||
steps: [],
|
||||
durationMs: 100,
|
||||
});
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(
|
||||
makeOkUpdateResult({
|
||||
mode: "npm",
|
||||
}),
|
||||
);
|
||||
|
||||
await updateCommand({ tag: "next" });
|
||||
|
||||
@@ -422,14 +417,7 @@ describe("update-cli", () => {
|
||||
});
|
||||
|
||||
it("updateCommand outputs JSON when --json is set", async () => {
|
||||
const mockResult: UpdateRunResult = {
|
||||
status: "ok",
|
||||
mode: "git",
|
||||
steps: [],
|
||||
durationMs: 100,
|
||||
};
|
||||
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(mockResult);
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(makeOkUpdateResult());
|
||||
vi.mocked(defaultRuntime.log).mockClear();
|
||||
|
||||
await updateCommand({ json: true });
|
||||
@@ -464,14 +452,7 @@ describe("update-cli", () => {
|
||||
});
|
||||
|
||||
it("updateCommand restarts daemon by default", async () => {
|
||||
const mockResult: UpdateRunResult = {
|
||||
status: "ok",
|
||||
mode: "git",
|
||||
steps: [],
|
||||
durationMs: 100,
|
||||
};
|
||||
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(mockResult);
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(makeOkUpdateResult());
|
||||
vi.mocked(runDaemonRestart).mockResolvedValue(true);
|
||||
|
||||
await updateCommand({});
|
||||
@@ -480,18 +461,11 @@ describe("update-cli", () => {
|
||||
});
|
||||
|
||||
it("updateCommand continues after doctor sub-step and clears update flag", async () => {
|
||||
const mockResult: UpdateRunResult = {
|
||||
status: "ok",
|
||||
mode: "git",
|
||||
steps: [],
|
||||
durationMs: 100,
|
||||
};
|
||||
|
||||
const envSnapshot = captureEnv(["OPENCLAW_UPDATE_IN_PROGRESS"]);
|
||||
const randomSpy = vi.spyOn(Math, "random").mockReturnValue(0);
|
||||
try {
|
||||
delete process.env.OPENCLAW_UPDATE_IN_PROGRESS;
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(mockResult);
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(makeOkUpdateResult());
|
||||
vi.mocked(runDaemonRestart).mockResolvedValue(true);
|
||||
vi.mocked(doctorCommand).mockResolvedValue(undefined);
|
||||
vi.mocked(defaultRuntime.log).mockClear();
|
||||
@@ -515,14 +489,7 @@ describe("update-cli", () => {
|
||||
});
|
||||
|
||||
it("updateCommand skips restart when --no-restart is set", async () => {
|
||||
const mockResult: UpdateRunResult = {
|
||||
status: "ok",
|
||||
mode: "git",
|
||||
steps: [],
|
||||
durationMs: 100,
|
||||
};
|
||||
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(mockResult);
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(makeOkUpdateResult());
|
||||
|
||||
await updateCommand({ restart: false });
|
||||
|
||||
@@ -530,14 +497,7 @@ describe("update-cli", () => {
|
||||
});
|
||||
|
||||
it("updateCommand skips success message when restart does not run", async () => {
|
||||
const mockResult: UpdateRunResult = {
|
||||
status: "ok",
|
||||
mode: "git",
|
||||
steps: [],
|
||||
durationMs: 100,
|
||||
};
|
||||
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(mockResult);
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(makeOkUpdateResult());
|
||||
vi.mocked(runDaemonRestart).mockResolvedValue(false);
|
||||
vi.mocked(defaultRuntime.log).mockClear();
|
||||
|
||||
@@ -547,35 +507,35 @@ describe("update-cli", () => {
|
||||
expect(logLines.some((line) => line.includes("Daemon restarted successfully."))).toBe(false);
|
||||
});
|
||||
|
||||
it("updateCommand validates timeout option", async () => {
|
||||
it.each([
|
||||
{
|
||||
name: "update command",
|
||||
run: async () => await updateCommand({ timeout: "invalid" }),
|
||||
requireTty: false,
|
||||
},
|
||||
{
|
||||
name: "update status command",
|
||||
run: async () => await updateStatusCommand({ timeout: "invalid" }),
|
||||
requireTty: false,
|
||||
},
|
||||
{
|
||||
name: "update wizard command",
|
||||
run: async () => await updateWizardCommand({ timeout: "invalid" }),
|
||||
requireTty: true,
|
||||
},
|
||||
])("validates timeout option for $name", async ({ run, requireTty }) => {
|
||||
setTty(requireTty);
|
||||
vi.mocked(defaultRuntime.error).mockClear();
|
||||
vi.mocked(defaultRuntime.exit).mockClear();
|
||||
|
||||
await updateCommand({ timeout: "invalid" });
|
||||
|
||||
expect(defaultRuntime.error).toHaveBeenCalledWith(expect.stringContaining("timeout"));
|
||||
expect(defaultRuntime.exit).toHaveBeenCalledWith(1);
|
||||
});
|
||||
|
||||
it("updateStatusCommand validates timeout option", async () => {
|
||||
vi.mocked(defaultRuntime.error).mockClear();
|
||||
vi.mocked(defaultRuntime.exit).mockClear();
|
||||
|
||||
await updateStatusCommand({ timeout: "invalid" });
|
||||
await run();
|
||||
|
||||
expect(defaultRuntime.error).toHaveBeenCalledWith(expect.stringContaining("timeout"));
|
||||
expect(defaultRuntime.exit).toHaveBeenCalledWith(1);
|
||||
});
|
||||
|
||||
it("persists update channel when --channel is set", async () => {
|
||||
const mockResult: UpdateRunResult = {
|
||||
status: "ok",
|
||||
mode: "git",
|
||||
steps: [],
|
||||
durationMs: 100,
|
||||
};
|
||||
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(mockResult);
|
||||
vi.mocked(runGatewayUpdate).mockResolvedValue(makeOkUpdateResult());
|
||||
|
||||
await updateCommand({ channel: "beta" });
|
||||
|
||||
@@ -586,26 +546,31 @@ describe("update-cli", () => {
|
||||
expect(call?.update?.channel).toBe("beta");
|
||||
});
|
||||
|
||||
it("requires confirmation on downgrade when non-interactive", async () => {
|
||||
it.each([
|
||||
{
|
||||
name: "requires confirmation without --yes",
|
||||
options: {},
|
||||
shouldExit: true,
|
||||
shouldRunUpdate: false,
|
||||
},
|
||||
{
|
||||
name: "allows downgrade with --yes",
|
||||
options: { yes: true },
|
||||
shouldExit: false,
|
||||
shouldRunUpdate: true,
|
||||
},
|
||||
])("$name in non-interactive mode", async ({ options, shouldExit, shouldRunUpdate }) => {
|
||||
await setupNonInteractiveDowngrade();
|
||||
await updateCommand(options);
|
||||
|
||||
await updateCommand({});
|
||||
|
||||
expect(defaultRuntime.error).toHaveBeenCalledWith(
|
||||
expect.stringContaining("Downgrade confirmation required."),
|
||||
const downgradeMessageSeen = vi
|
||||
.mocked(defaultRuntime.error)
|
||||
.mock.calls.some((call) => String(call[0]).includes("Downgrade confirmation required."));
|
||||
expect(downgradeMessageSeen).toBe(shouldExit);
|
||||
expect(vi.mocked(defaultRuntime.exit).mock.calls.some((call) => call[0] === 1)).toBe(
|
||||
shouldExit,
|
||||
);
|
||||
expect(defaultRuntime.exit).toHaveBeenCalledWith(1);
|
||||
});
|
||||
|
||||
it("allows downgrade with --yes in non-interactive mode", async () => {
|
||||
await setupNonInteractiveDowngrade();
|
||||
|
||||
await updateCommand({ yes: true });
|
||||
|
||||
expect(defaultRuntime.error).not.toHaveBeenCalledWith(
|
||||
expect.stringContaining("Downgrade confirmation required."),
|
||||
);
|
||||
expect(runGatewayUpdate).toHaveBeenCalled();
|
||||
expect(vi.mocked(runGatewayUpdate).mock.calls.length > 0).toBe(shouldRunUpdate);
|
||||
});
|
||||
|
||||
it("updateWizardCommand requires a TTY", async () => {
|
||||
@@ -621,19 +586,8 @@ describe("update-cli", () => {
|
||||
expect(defaultRuntime.exit).toHaveBeenCalledWith(1);
|
||||
});
|
||||
|
||||
it("updateWizardCommand validates timeout option", async () => {
|
||||
setTty(true);
|
||||
vi.mocked(defaultRuntime.error).mockClear();
|
||||
vi.mocked(defaultRuntime.exit).mockClear();
|
||||
|
||||
await updateWizardCommand({ timeout: "invalid" });
|
||||
|
||||
expect(defaultRuntime.error).toHaveBeenCalledWith(expect.stringContaining("timeout"));
|
||||
expect(defaultRuntime.exit).toHaveBeenCalledWith(1);
|
||||
});
|
||||
|
||||
it("updateWizardCommand offers dev checkout and forwards selections", async () => {
|
||||
const tempDir = await createCaseDir("openclaw-update-wizard");
|
||||
const tempDir = createCaseDir("openclaw-update-wizard");
|
||||
const envSnapshot = captureEnv(["OPENCLAW_GIT_DIR"]);
|
||||
try {
|
||||
setTty(true);
|
||||
|
||||
Reference in New Issue
Block a user