Files
openclaw/src/cli/plugins-install-record-commit.test.ts
Peter Steinberger d678bcfcc7 fix: hot reload plugin management changes (#75976)
Summary:
- The PR changes Gateway reload planning, CLI plugin install-index writes, plugin runtime/cache cleanup, docs, changelog, and tests so plugin enable/disable hot reloads while install/update/uninstall stay restart-backed.
- Reproducibility: yes. The earlier blocker has a source-level reproduction: run an external plugin install/up ...  watches config and only the managed plugin index changes; the PR now tests that path and queues a restart.

ClawSweeper fixups:
- Included follow-up commit: fix: hot reload plugin management changes
- Included follow-up commit: fix(clawsweeper): address review for automerge-openclaw-openclaw-7597…
- Ran the ClawSweeper repair loop before final review.

Validation:
- ClawSweeper review passed for head 860594f722.
- Required merge gates passed before the squash merge.

Prepared head SHA: 860594f722
Review: https://github.com/openclaw/openclaw/pull/75976#issuecomment-4363168379

Co-authored-by: Peter Steinberger <steipete@gmail.com>
Co-authored-by: clawsweeper <274271284+clawsweeper[bot]@users.noreply.github.com>
2026-05-02 13:19:24 +00:00

211 lines
5.9 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import type { PluginInstallRecord } from "../config/types.plugins.js";
const mocks = vi.hoisted(() => ({
loadInstalledPluginIndexInstallRecords: vi.fn(),
replaceConfigFile: vi.fn(),
writePersistedInstalledPluginIndexInstallRecords: vi.fn(),
}));
vi.mock("../config/config.js", () => ({
replaceConfigFile: mocks.replaceConfigFile,
}));
vi.mock("../plugins/installed-plugin-index-records.js", async (importOriginal) => {
const actual =
await importOriginal<typeof import("../plugins/installed-plugin-index-records.js")>();
return {
...actual,
loadInstalledPluginIndexInstallRecords: mocks.loadInstalledPluginIndexInstallRecords,
writePersistedInstalledPluginIndexInstallRecords:
mocks.writePersistedInstalledPluginIndexInstallRecords,
};
});
import {
commitConfigWithPendingPluginInstalls,
commitConfigWriteWithPendingPluginInstalls,
} from "./plugins-install-record-commit.js";
describe("commitConfigWithPendingPluginInstalls", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.loadInstalledPluginIndexInstallRecords.mockResolvedValue({});
mocks.replaceConfigFile.mockResolvedValue(undefined);
mocks.writePersistedInstalledPluginIndexInstallRecords.mockResolvedValue(undefined);
});
it("moves pending plugin install records into the plugin index before writing stripped config", async () => {
const existingRecords: Record<string, PluginInstallRecord> = {
existing: {
source: "npm",
spec: "existing@1.0.0",
},
};
const pendingRecords: Record<string, PluginInstallRecord> = {
demo: {
source: "npm",
spec: "demo@1.0.0",
},
};
mocks.loadInstalledPluginIndexInstallRecords.mockResolvedValue(existingRecords);
const nextConfig: OpenClawConfig = {
plugins: {
entries: {
demo: { enabled: true },
},
installs: pendingRecords,
},
};
const result = await commitConfigWithPendingPluginInstalls({
nextConfig,
baseHash: "config-1",
});
expect(mocks.writePersistedInstalledPluginIndexInstallRecords).toHaveBeenCalledWith({
...existingRecords,
...pendingRecords,
});
expect(mocks.replaceConfigFile).toHaveBeenCalledWith({
nextConfig: {
plugins: {
entries: {
demo: { enabled: true },
},
},
},
baseHash: "config-1",
writeOptions: {
afterWrite: { mode: "restart", reason: "plugin source changed" },
unsetPaths: [["plugins", "installs"]],
},
});
expect(result).toEqual({
config: {
plugins: {
entries: {
demo: { enabled: true },
},
},
},
installRecords: {
...existingRecords,
...pendingRecords,
},
movedInstallRecords: true,
});
});
it("does not add restart intent when pending records match the plugin index", async () => {
const existingRecords: Record<string, PluginInstallRecord> = {
demo: {
source: "npm",
spec: "demo@1.0.0",
},
};
mocks.loadInstalledPluginIndexInstallRecords.mockResolvedValue(existingRecords);
await commitConfigWithPendingPluginInstalls({
nextConfig: {
plugins: {
installs: existingRecords,
},
},
baseHash: "config-1",
});
expect(mocks.replaceConfigFile).toHaveBeenCalledWith({
nextConfig: {},
baseHash: "config-1",
writeOptions: {
unsetPaths: [["plugins", "installs"]],
},
});
});
it("rolls back plugin index writes when the config write fails", async () => {
const existingRecords: Record<string, PluginInstallRecord> = {
existing: {
source: "npm",
spec: "existing@1.0.0",
},
};
mocks.loadInstalledPluginIndexInstallRecords.mockResolvedValue(existingRecords);
mocks.replaceConfigFile.mockRejectedValue(new Error("config changed"));
await expect(
commitConfigWithPendingPluginInstalls({
nextConfig: {
plugins: {
installs: {
demo: {
source: "npm",
spec: "demo@1.0.0",
},
},
},
},
}),
).rejects.toThrow("config changed");
expect(mocks.writePersistedInstalledPluginIndexInstallRecords).toHaveBeenNthCalledWith(1, {
existing: {
source: "npm",
spec: "existing@1.0.0",
},
demo: {
source: "npm",
spec: "demo@1.0.0",
},
});
expect(mocks.writePersistedInstalledPluginIndexInstallRecords).toHaveBeenNthCalledWith(
2,
existingRecords,
);
});
it("uses a plain config write when no pending plugin install records exist", async () => {
const nextConfig: OpenClawConfig = {
gateway: {
mode: "local",
},
};
const result = await commitConfigWithPendingPluginInstalls({ nextConfig });
expect(mocks.loadInstalledPluginIndexInstallRecords).not.toHaveBeenCalled();
expect(mocks.writePersistedInstalledPluginIndexInstallRecords).not.toHaveBeenCalled();
expect(mocks.replaceConfigFile).toHaveBeenCalledWith({
nextConfig,
});
expect(result).toEqual({
config: nextConfig,
installRecords: {},
movedInstallRecords: false,
});
});
it("supports non-replace config writers without adding an undefined write options argument", async () => {
const writeConfigFile = vi.fn(async () => undefined);
const nextConfig: OpenClawConfig = {
gateway: {
mode: "local",
},
};
const result = await commitConfigWriteWithPendingPluginInstalls({
nextConfig,
commit: writeConfigFile,
});
expect(writeConfigFile).toHaveBeenCalledWith(nextConfig);
expect(result).toEqual({
config: nextConfig,
installRecords: {},
movedInstallRecords: false,
});
});
});