openclaw/src/plugins/slots.test.ts

107 lines
3.3 KiB
TypeScript
Raw Normal View History

2026-01-18 11:26:50 -05:00
import { describe, expect, it } from "vitest";
2026-01-30 03:15:10 +01:00
import type { OpenClawConfig } from "../config/config.js";
2026-01-18 11:26:50 -05:00
import { applyExclusiveSlotSelection } from "./slots.js";
describe("applyExclusiveSlotSelection", () => {
const createMemoryConfig = (plugins?: OpenClawConfig["plugins"]): OpenClawConfig => ({
plugins: {
...plugins,
entries: {
...plugins?.entries,
memory: {
enabled: true,
...plugins?.entries?.memory,
2026-01-18 11:26:50 -05:00
},
},
},
});
2026-01-18 11:26:50 -05:00
const runMemorySelection = (config: OpenClawConfig, selectedId = "memory") =>
applyExclusiveSlotSelection({
2026-01-18 11:26:50 -05:00
config,
selectedId,
2026-01-18 11:26:50 -05:00
selectedKind: "memory",
registry: {
plugins: [
{ id: "memory-core", kind: "memory" },
{ id: "memory", kind: "memory" },
],
},
});
it("selects the slot and disables other entries for the same kind", () => {
const config = createMemoryConfig({
slots: { memory: "memory-core" },
entries: { "memory-core": { enabled: true } },
});
const result = runMemorySelection(config);
2026-01-18 11:26:50 -05:00
expect(result.changed).toBe(true);
expect(result.config.plugins?.slots?.memory).toBe("memory");
expect(result.config.plugins?.entries?.["memory-core"]?.enabled).toBe(false);
expect(result.warnings).toContain(
'Exclusive slot "memory" switched from "memory-core" to "memory".',
);
2026-01-18 19:36:46 +00:00
expect(result.warnings).toContain('Disabled other "memory" slot plugins: memory-core.');
2026-01-18 11:26:50 -05:00
});
it("does nothing when the slot already matches", () => {
const config = createMemoryConfig({
slots: { memory: "memory" },
});
2026-01-18 11:26:50 -05:00
const result = applyExclusiveSlotSelection({
config,
selectedId: "memory",
selectedKind: "memory",
registry: { plugins: [{ id: "memory", kind: "memory" }] },
});
expect(result.changed).toBe(false);
expect(result.warnings).toHaveLength(0);
expect(result.config).toBe(config);
});
it("warns when the slot falls back to a default", () => {
const config = createMemoryConfig();
2026-01-18 11:26:50 -05:00
const result = applyExclusiveSlotSelection({
config,
selectedId: "memory",
selectedKind: "memory",
registry: { plugins: [{ id: "memory", kind: "memory" }] },
});
expect(result.changed).toBe(true);
expect(result.warnings).toContain(
'Exclusive slot "memory" switched from "memory-core" to "memory".',
);
});
it("keeps disabled competing plugins disabled without adding disable warnings", () => {
const config = createMemoryConfig({
entries: {
"memory-core": { enabled: false },
},
});
const result = runMemorySelection(config);
expect(result.changed).toBe(true);
expect(result.config.plugins?.entries?.["memory-core"]?.enabled).toBe(false);
expect(result.warnings).toContain(
'Exclusive slot "memory" switched from "memory-core" to "memory".',
);
expect(result.warnings).not.toContain('Disabled other "memory" slot plugins: memory-core.');
});
2026-01-18 11:26:50 -05:00
it("skips changes when no exclusive slot applies", () => {
2026-01-30 03:15:10 +01:00
const config: OpenClawConfig = {};
2026-01-18 11:26:50 -05:00
const result = applyExclusiveSlotSelection({
config,
selectedId: "custom",
});
expect(result.changed).toBe(false);
expect(result.warnings).toHaveLength(0);
expect(result.config).toBe(config);
});
});