Skip to content

Commit 10bfe43

Browse files
committed
test(plugins): cover hook context chaining
1 parent 4cbd152 commit 10bfe43

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

‎src/plugins/loader.test.ts‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,6 +2796,101 @@ module.exports = { id: "throws-after-import", register() {} };`,
27962796
clearInternalHooks();
27972797
});
27982798

2799+
it("runs consecutive plugin hook handlers on the shared context without config leakage", async () => {
2800+
useNoBundledPlugins();
2801+
const firstPlugin = writePlugin({
2802+
id: "hook-context-first",
2803+
filename: "hook-context-first.cjs",
2804+
body: `module.exports = {
2805+
id: "hook-context-first",
2806+
register(api) {
2807+
api.registerHook(
2808+
"agent:bootstrap",
2809+
(event) => {
2810+
event.messages.push("first:" + (event.context.pluginConfig?.marker ?? "missing"));
2811+
event.context.bootstrapFiles = [
2812+
{ name: "FIRST.md", content: "FIRST_HANDLER_MUTATION" },
2813+
];
2814+
event.context.note = "mutated-by-first-handler";
2815+
event.context.pluginConfig = { marker: "leaked-first-config" };
2816+
},
2817+
{ name: "hook-context-first" },
2818+
);
2819+
},
2820+
};`,
2821+
});
2822+
const secondPlugin = writePlugin({
2823+
id: "hook-context-second",
2824+
filename: "hook-context-second.cjs",
2825+
body: `module.exports = {
2826+
id: "hook-context-second",
2827+
register(api) {
2828+
api.registerHook(
2829+
"agent:bootstrap",
2830+
(event) => {
2831+
event.messages.push("second:" + (event.context.pluginConfig?.marker ?? "none"));
2832+
event.messages.push(event.context.bootstrapFiles?.[0]?.name ?? "missing-mutation");
2833+
event.messages.push(event.context.note ?? "missing-note");
2834+
},
2835+
{ name: "hook-context-second" },
2836+
);
2837+
},
2838+
};`,
2839+
});
2840+
for (const plugin of [firstPlugin, secondPlugin]) {
2841+
fs.writeFileSync(
2842+
path.join(plugin.dir, "openclaw.plugin.json"),
2843+
JSON.stringify(
2844+
{
2845+
id: plugin.id,
2846+
configSchema: { type: "object" },
2847+
},
2848+
null,
2849+
2,
2850+
),
2851+
"utf-8",
2852+
);
2853+
}
2854+
2855+
clearInternalHooks();
2856+
2857+
loadOpenClawPlugins({
2858+
cache: false,
2859+
workspaceDir: firstPlugin.dir,
2860+
config: {
2861+
plugins: {
2862+
load: { paths: [firstPlugin.file, secondPlugin.file] },
2863+
allow: ["hook-context-first", "hook-context-second"],
2864+
entries: {
2865+
"hook-context-first": {
2866+
config: {
2867+
marker: "first-config-visible",
2868+
},
2869+
},
2870+
},
2871+
},
2872+
},
2873+
onlyPluginIds: ["hook-context-first", "hook-context-second"],
2874+
});
2875+
2876+
const event = createInternalHookEvent("agent", "bootstrap", "agent:bootstrap", {
2877+
bootstrapFiles: [{ name: "AGENTS.md", content: "ORIGINAL_CONTEXT" }],
2878+
});
2879+
await triggerInternalHook(event);
2880+
expect(event.messages).toEqual([
2881+
"first:first-config-visible",
2882+
"second:none",
2883+
"FIRST.md",
2884+
"mutated-by-first-handler",
2885+
]);
2886+
expect(event.context).toStrictEqual({
2887+
bootstrapFiles: [{ name: "FIRST.md", content: "FIRST_HANDLER_MUTATION" }],
2888+
note: "mutated-by-first-handler",
2889+
});
2890+
2891+
clearInternalHooks();
2892+
});
2893+
27992894
it("injects plugin config into internal hook event context", async () => {
28002895
useNoBundledPlugins();
28012896
const plugin = writePlugin({

0 commit comments

Comments
 (0)