test: tighten plugin install persistence assertions

This commit is contained in:
Peter Steinberger
2026-05-11 03:40:25 +01:00
parent e72b3b7458
commit 8ccd3e9236

View File

@@ -18,6 +18,22 @@ import {
applyPluginUninstallDirectoryRemoval,
} from "./plugins-cli-test-helpers.js";
function requireMockCallArg(
mockFn: { mock: { calls: unknown[][] } },
label: string,
index = 0,
): Record<string, unknown> {
const arg = mockFn.mock.calls[index]?.[0] as Record<string, unknown> | undefined;
if (!arg) {
throw new Error(`expected ${label} call #${index + 1}`);
}
return arg;
}
function expectRuntimeLogIncludes(fragment: string) {
expect(runtimeLogs.some((log) => log.includes(fragment))).toBe(true);
}
describe("persistPluginInstall", () => {
beforeEach(() => {
resetPluginsCliTestState();
@@ -59,12 +75,15 @@ describe("persistPluginInstall", () => {
});
expect(next).toEqual(enabledConfig);
expect(writePersistedInstalledPluginIndexInstallRecords).toHaveBeenCalledWith({
alpha: expect.objectContaining({
source: "npm",
spec: "alpha@1.0.0",
installPath: "/tmp/alpha",
}),
const persistedRecords = requireMockCallArg(
writePersistedInstalledPluginIndexInstallRecords,
"writePersistedInstalledPluginIndexInstallRecords",
);
expect(persistedRecords.alpha).toEqual({
source: "npm",
spec: "alpha@1.0.0",
installPath: "/tmp/alpha",
installedAt: "2026-04-25T00:00:00.000Z",
});
expect(writeConfigFile).toHaveBeenCalledWith(enabledConfig);
expect(replaceConfigFile).toHaveBeenCalledWith({
@@ -75,16 +94,14 @@ describe("persistPluginInstall", () => {
unsetPaths: [["plugins", "installs"]],
},
});
expect(refreshPluginRegistry).toHaveBeenCalledWith({
config: enabledConfig,
installRecords: {
alpha: expect.objectContaining({
source: "npm",
spec: "alpha@1.0.0",
installPath: "/tmp/alpha",
}),
},
reason: "source-changed",
const refreshParams = requireMockCallArg(refreshPluginRegistry, "refreshPluginRegistry");
expect(refreshParams.config).toBe(enabledConfig);
expect(refreshParams.reason).toBe("source-changed");
expect((refreshParams.installRecords as Record<string, unknown>).alpha).toEqual({
source: "npm",
spec: "alpha@1.0.0",
installPath: "/tmp/alpha",
installedAt: "2026-04-25T00:00:00.000Z",
});
expect(clearPluginRegistryLoadCache).toHaveBeenCalledTimes(1);
});
@@ -123,9 +140,7 @@ describe("persistPluginInstall", () => {
expect(next).toEqual(enabledConfig);
expect(refreshPluginRegistry).toHaveBeenCalled();
expect(runtimeLogs).toEqual(
expect.arrayContaining([expect.stringContaining("Plugin runtime cache invalidation failed")]),
);
expectRuntimeLogIncludes("Plugin runtime cache invalidation failed");
});
it("removes a replaced managed install directory before refreshing the registry", async () => {
@@ -388,9 +403,7 @@ describe("persistPluginInstall", () => {
expect(next).toEqual(enabledConfig);
expect(refreshPluginRegistry).toHaveBeenCalled();
expect(clearPluginRegistryLoadCache).toHaveBeenCalledTimes(1);
expect(runtimeLogs).toEqual(
expect.arrayContaining([expect.stringContaining("Plugin registry refresh failed")]),
);
expectRuntimeLogIncludes("Plugin registry refresh failed");
});
it("removes stale denylist entries before enabling installed plugins", async () => {
@@ -500,11 +513,9 @@ describe("persistPluginInstall", () => {
config: enabledConfig,
onlyPluginIds: ["legacy-memory"],
});
expect(loadPluginManifestRegistry).toHaveBeenCalledWith(
expect.objectContaining({
config: enabledConfig,
}),
);
expect(
requireMockCallArg(loadPluginManifestRegistry, "loadPluginManifestRegistry").config,
).toBe(enabledConfig);
expect(next.plugins?.entries?.["legacy-memory-a"]?.enabled).toBe(true);
expect(next.plugins?.slots?.memory).toBe("legacy-memory");
});
@@ -570,11 +581,9 @@ describe("persistPluginInstall", () => {
});
expect(buildPluginDiagnosticsReport).not.toHaveBeenCalled();
expect(loadPluginManifestRegistry).toHaveBeenCalledWith(
expect.objectContaining({
config: enabledConfig,
}),
);
expect(
requireMockCallArg(loadPluginManifestRegistry, "loadPluginManifestRegistry").config,
).toBe(enabledConfig);
expect(next.plugins?.entries?.["legacy-memory-a"]?.enabled).toBe(true);
expect(next.plugins?.slots?.memory).toBe("memory-b");
});
@@ -626,11 +635,9 @@ describe("persistPluginInstall", () => {
config: enabledConfig,
onlyPluginIds: ["plain"],
});
expect(loadPluginManifestRegistry).toHaveBeenCalledWith(
expect.objectContaining({
config: enabledConfig,
}),
);
expect(
requireMockCallArg(loadPluginManifestRegistry, "loadPluginManifestRegistry").config,
).toBe(enabledConfig);
expect(next).toEqual(enabledConfig);
});
@@ -660,11 +667,16 @@ describe("persistPluginInstall", () => {
expect(next).toEqual(baseConfig);
expect(enablePluginInConfig).not.toHaveBeenCalled();
expect(applyExclusiveSlotSelection).not.toHaveBeenCalled();
expect(writePersistedInstalledPluginIndexInstallRecords).toHaveBeenCalledWith({
"memory-lancedb": expect.objectContaining({
source: "path",
sourcePath: "/app/dist/extensions/memory-lancedb",
}),
const persistedRecords = requireMockCallArg(
writePersistedInstalledPluginIndexInstallRecords,
"writePersistedInstalledPluginIndexInstallRecords",
);
expect(persistedRecords["memory-lancedb"]).toEqual({
source: "path",
spec: "memory-lancedb",
sourcePath: "/app/dist/extensions/memory-lancedb",
installPath: "/app/dist/extensions/memory-lancedb",
installedAt: "2026-04-25T00:00:00.000Z",
});
expect(writeConfigFile).toHaveBeenCalledWith(baseConfig);
});