mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-01 20:31:19 +00:00
fix: harden update dev switch and refresh changelog
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { Command } from "commander";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
@@ -145,6 +146,7 @@ const { runDaemonRestart, runDaemonInstall } = await import("./daemon-cli.js");
|
||||
const { doctorCommand } = await import("../commands/doctor.js");
|
||||
const { defaultRuntime } = await import("../runtime.js");
|
||||
const { updateCommand, updateStatusCommand, updateWizardCommand } = await import("./update-cli.js");
|
||||
const { resolveGitInstallDir } = await import("./update-cli/shared.js");
|
||||
|
||||
describe("update-cli", () => {
|
||||
const fixtureRoot = "/tmp/openclaw-update-tests";
|
||||
@@ -714,6 +716,92 @@ describe("update-cli", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("persists the requested channel only after a successful package update", async () => {
|
||||
const tempDir = createCaseDir("openclaw-update");
|
||||
mockPackageInstallStatus(tempDir);
|
||||
|
||||
await updateCommand({ channel: "beta", yes: true });
|
||||
|
||||
const installCallIndex = vi
|
||||
.mocked(runCommandWithTimeout)
|
||||
.mock.calls.findIndex(
|
||||
(call) =>
|
||||
Array.isArray(call[0]) &&
|
||||
call[0][0] === "npm" &&
|
||||
call[0][1] === "i" &&
|
||||
call[0][2] === "-g",
|
||||
);
|
||||
expect(installCallIndex).toBeGreaterThanOrEqual(0);
|
||||
expect(writeConfigFile).toHaveBeenCalledTimes(1);
|
||||
expect(writeConfigFile).toHaveBeenCalledWith({
|
||||
update: {
|
||||
channel: "beta",
|
||||
},
|
||||
});
|
||||
expect(
|
||||
vi.mocked(runCommandWithTimeout).mock.invocationCallOrder[installCallIndex] ?? 0,
|
||||
).toBeLessThan(
|
||||
vi.mocked(writeConfigFile).mock.invocationCallOrder[0] ?? Number.MAX_SAFE_INTEGER,
|
||||
);
|
||||
});
|
||||
|
||||
it("does not persist the requested channel when the package update fails", async () => {
|
||||
const tempDir = createCaseDir("openclaw-update");
|
||||
mockPackageInstallStatus(tempDir);
|
||||
vi.mocked(runCommandWithTimeout).mockImplementation(async (argv) => {
|
||||
if (Array.isArray(argv) && argv[0] === "npm" && argv[1] === "i" && argv[2] === "-g") {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "install failed",
|
||||
code: 1,
|
||||
signal: null,
|
||||
killed: false,
|
||||
termination: "exit",
|
||||
};
|
||||
}
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "",
|
||||
code: 0,
|
||||
signal: null,
|
||||
killed: false,
|
||||
termination: "exit",
|
||||
};
|
||||
});
|
||||
|
||||
await updateCommand({ channel: "beta", yes: true });
|
||||
|
||||
expect(writeConfigFile).not.toHaveBeenCalled();
|
||||
expect(defaultRuntime.exit).toHaveBeenCalledWith(1);
|
||||
});
|
||||
|
||||
it("keeps the requested channel when plugin sync writes config after update", async () => {
|
||||
const tempDir = createCaseDir("openclaw-update");
|
||||
mockPackageInstallStatus(tempDir);
|
||||
syncPluginsForUpdateChannel.mockImplementation(async ({ config }) => ({
|
||||
changed: true,
|
||||
config,
|
||||
summary: {
|
||||
switchedToBundled: [],
|
||||
switchedToNpm: [],
|
||||
warnings: [],
|
||||
errors: [],
|
||||
},
|
||||
}));
|
||||
updateNpmInstalledPlugins.mockImplementation(async ({ config }) => ({
|
||||
changed: false,
|
||||
config,
|
||||
outcomes: [],
|
||||
}));
|
||||
|
||||
await updateCommand({ channel: "beta", yes: true });
|
||||
|
||||
const lastWrite = vi.mocked(writeConfigFile).mock.calls.at(-1)?.[0] as
|
||||
| { update?: { channel?: string } }
|
||||
| undefined;
|
||||
expect(lastWrite?.update?.channel).toBe("beta");
|
||||
});
|
||||
|
||||
it("updateCommand handles service env refresh and restart behavior", async () => {
|
||||
const cases = [
|
||||
{
|
||||
@@ -1040,4 +1128,12 @@ describe("update-cli", () => {
|
||||
expect(call?.channel).toBe("dev");
|
||||
});
|
||||
});
|
||||
|
||||
it("uses ~/openclaw as the default dev checkout directory", async () => {
|
||||
const homedirSpy = vi.spyOn(os, "homedir").mockReturnValue("/tmp/oc-home");
|
||||
await withEnvAsync({ OPENCLAW_GIT_DIR: undefined }, async () => {
|
||||
expect(resolveGitInstallDir()).toBe("/tmp/oc-home/openclaw");
|
||||
});
|
||||
homedirSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user