mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-18 02:31:36 +00:00
81 lines
2.9 KiB
TypeScript
81 lines
2.9 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { getMemoryCapabilityRegistration, registerMemoryCapability } from "./memory-state.js";
|
|
import {
|
|
createPluginRegistrationTransaction,
|
|
type PluginProcessGlobalState,
|
|
restorePluginProcessGlobalState,
|
|
snapshotPluginProcessGlobalState,
|
|
} from "./plugin-registration-transaction.js";
|
|
import { createEmptyPluginRegistry } from "./registry-empty.js";
|
|
|
|
describe("plugin registration transaction", () => {
|
|
let initialProcessGlobalState: PluginProcessGlobalState;
|
|
|
|
beforeEach(() => {
|
|
initialProcessGlobalState = snapshotPluginProcessGlobalState();
|
|
});
|
|
|
|
afterEach(() => {
|
|
restorePluginProcessGlobalState(initialProcessGlobalState);
|
|
});
|
|
|
|
it("rolls back registry writes and restores prior process-global capability state", () => {
|
|
const registry = createEmptyPluginRegistry();
|
|
const activePromptBuilder = () => ["active"];
|
|
const failedResolver = () => "failed";
|
|
const rollbackGlobalSideEffects = vi.fn();
|
|
registerMemoryCapability("active-memory", { promptBuilder: activePromptBuilder });
|
|
|
|
const transaction = createPluginRegistrationTransaction({
|
|
registry,
|
|
rollbackGlobalSideEffects,
|
|
});
|
|
registry.hostedMediaResolvers.push({
|
|
pluginId: "failed-plugin",
|
|
resolver: failedResolver,
|
|
source: "failed-plugin",
|
|
});
|
|
registry.gatewayHandlers.failed = async () => {};
|
|
registerMemoryCapability("failed-memory", { promptBuilder: () => ["failed"] });
|
|
|
|
transaction.rollback();
|
|
|
|
expect(rollbackGlobalSideEffects).toHaveBeenCalledOnce();
|
|
expect(registry.hostedMediaResolvers).toStrictEqual([]);
|
|
expect(registry.gatewayHandlers).toStrictEqual({});
|
|
expect(getMemoryCapabilityRegistration()).toEqual({
|
|
pluginId: "active-memory",
|
|
capability: { promptBuilder: activePromptBuilder },
|
|
});
|
|
});
|
|
|
|
it("keeps snapshot registry writes while restoring globals for non-activating commits", () => {
|
|
const registry = createEmptyPluginRegistry();
|
|
const activePromptBuilder = () => ["active"];
|
|
const snapshotResolver = () => "snapshot";
|
|
registerMemoryCapability("active-memory", { promptBuilder: activePromptBuilder });
|
|
|
|
const transaction = createPluginRegistrationTransaction({ registry });
|
|
registry.hostedMediaResolvers.push({
|
|
pluginId: "snapshot-plugin",
|
|
resolver: snapshotResolver,
|
|
source: "snapshot-plugin",
|
|
});
|
|
registerMemoryCapability("snapshot-memory", { promptBuilder: () => ["snapshot"] });
|
|
|
|
transaction.commit({ activate: false });
|
|
|
|
expect(registry.hostedMediaResolvers).toEqual([
|
|
{
|
|
pluginId: "snapshot-plugin",
|
|
resolver: snapshotResolver,
|
|
source: "snapshot-plugin",
|
|
},
|
|
]);
|
|
expect(getMemoryCapabilityRegistration()).toEqual({
|
|
pluginId: "active-memory",
|
|
capability: { promptBuilder: activePromptBuilder },
|
|
});
|
|
});
|
|
});
|