mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 09:47:40 +00:00
refactor(agents): dedupe plugin hooks and test helpers
This commit is contained in:
@@ -6,50 +6,39 @@ import { createHookRunner } from "./hooks.js";
|
||||
import { createMockPluginRegistry } from "./hooks.test-helpers.js";
|
||||
|
||||
describe("subagent hook runner methods", () => {
|
||||
const baseRequester = {
|
||||
channel: "discord",
|
||||
accountId: "work",
|
||||
to: "channel:123",
|
||||
threadId: "456",
|
||||
};
|
||||
|
||||
const baseSubagentCtx = {
|
||||
runId: "run-1",
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
};
|
||||
|
||||
it("runSubagentSpawning invokes registered subagent_spawning hooks", async () => {
|
||||
const handler = vi.fn(async () => ({ status: "ok", threadBindingReady: true as const }));
|
||||
const registry = createMockPluginRegistry([{ hookName: "subagent_spawning", handler }]);
|
||||
const runner = createHookRunner(registry);
|
||||
const event = {
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
agentId: "main",
|
||||
label: "research",
|
||||
mode: "session" as const,
|
||||
requester: baseRequester,
|
||||
threadRequested: true,
|
||||
};
|
||||
const ctx = {
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
};
|
||||
|
||||
const result = await runner.runSubagentSpawning(
|
||||
{
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
agentId: "main",
|
||||
label: "research",
|
||||
mode: "session",
|
||||
requester: {
|
||||
channel: "discord",
|
||||
accountId: "work",
|
||||
to: "channel:123",
|
||||
threadId: "456",
|
||||
},
|
||||
threadRequested: true,
|
||||
},
|
||||
{
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
},
|
||||
);
|
||||
const result = await runner.runSubagentSpawning(event, ctx);
|
||||
|
||||
expect(handler).toHaveBeenCalledWith(
|
||||
{
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
agentId: "main",
|
||||
label: "research",
|
||||
mode: "session",
|
||||
requester: {
|
||||
channel: "discord",
|
||||
accountId: "work",
|
||||
to: "channel:123",
|
||||
threadId: "456",
|
||||
},
|
||||
threadRequested: true,
|
||||
},
|
||||
{
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
},
|
||||
);
|
||||
expect(handler).toHaveBeenCalledWith(event, ctx);
|
||||
expect(result).toMatchObject({ status: "ok", threadBindingReady: true });
|
||||
});
|
||||
|
||||
@@ -57,50 +46,19 @@ describe("subagent hook runner methods", () => {
|
||||
const handler = vi.fn();
|
||||
const registry = createMockPluginRegistry([{ hookName: "subagent_spawned", handler }]);
|
||||
const runner = createHookRunner(registry);
|
||||
const event = {
|
||||
runId: "run-1",
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
agentId: "main",
|
||||
label: "research",
|
||||
mode: "run" as const,
|
||||
requester: baseRequester,
|
||||
threadRequested: true,
|
||||
};
|
||||
|
||||
await runner.runSubagentSpawned(
|
||||
{
|
||||
runId: "run-1",
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
agentId: "main",
|
||||
label: "research",
|
||||
mode: "run",
|
||||
requester: {
|
||||
channel: "discord",
|
||||
accountId: "work",
|
||||
to: "channel:123",
|
||||
threadId: "456",
|
||||
},
|
||||
threadRequested: true,
|
||||
},
|
||||
{
|
||||
runId: "run-1",
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
},
|
||||
);
|
||||
await runner.runSubagentSpawned(event, baseSubagentCtx);
|
||||
|
||||
expect(handler).toHaveBeenCalledWith(
|
||||
{
|
||||
runId: "run-1",
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
agentId: "main",
|
||||
label: "research",
|
||||
mode: "run",
|
||||
requester: {
|
||||
channel: "discord",
|
||||
accountId: "work",
|
||||
to: "channel:123",
|
||||
threadId: "456",
|
||||
},
|
||||
threadRequested: true,
|
||||
},
|
||||
{
|
||||
runId: "run-1",
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
},
|
||||
);
|
||||
expect(handler).toHaveBeenCalledWith(event, baseSubagentCtx);
|
||||
});
|
||||
|
||||
it("runSubagentDeliveryTarget invokes registered subagent_delivery_target hooks", async () => {
|
||||
@@ -114,48 +72,18 @@ describe("subagent hook runner methods", () => {
|
||||
}));
|
||||
const registry = createMockPluginRegistry([{ hookName: "subagent_delivery_target", handler }]);
|
||||
const runner = createHookRunner(registry);
|
||||
const event = {
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
requesterOrigin: baseRequester,
|
||||
childRunId: "run-1",
|
||||
spawnMode: "session" as const,
|
||||
expectsCompletionMessage: true,
|
||||
};
|
||||
|
||||
const result = await runner.runSubagentDeliveryTarget(
|
||||
{
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
requesterOrigin: {
|
||||
channel: "discord",
|
||||
accountId: "work",
|
||||
to: "channel:123",
|
||||
threadId: "456",
|
||||
},
|
||||
childRunId: "run-1",
|
||||
spawnMode: "session",
|
||||
expectsCompletionMessage: true,
|
||||
},
|
||||
{
|
||||
runId: "run-1",
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
},
|
||||
);
|
||||
const result = await runner.runSubagentDeliveryTarget(event, baseSubagentCtx);
|
||||
|
||||
expect(handler).toHaveBeenCalledWith(
|
||||
{
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
requesterOrigin: {
|
||||
channel: "discord",
|
||||
accountId: "work",
|
||||
to: "channel:123",
|
||||
threadId: "456",
|
||||
},
|
||||
childRunId: "run-1",
|
||||
spawnMode: "session",
|
||||
expectsCompletionMessage: true,
|
||||
},
|
||||
{
|
||||
runId: "run-1",
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
},
|
||||
);
|
||||
expect(handler).toHaveBeenCalledWith(event, baseSubagentCtx);
|
||||
expect(result).toEqual({
|
||||
origin: {
|
||||
channel: "discord",
|
||||
@@ -166,44 +94,40 @@ describe("subagent hook runner methods", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("runSubagentDeliveryTarget returns undefined when no matching hooks are registered", async () => {
|
||||
const registry = createMockPluginRegistry([]);
|
||||
const runner = createHookRunner(registry);
|
||||
const result = await runner.runSubagentDeliveryTarget(
|
||||
{
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
requesterOrigin: baseRequester,
|
||||
childRunId: "run-1",
|
||||
spawnMode: "session",
|
||||
expectsCompletionMessage: true,
|
||||
},
|
||||
baseSubagentCtx,
|
||||
);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it("runSubagentEnded invokes registered subagent_ended hooks", async () => {
|
||||
const handler = vi.fn();
|
||||
const registry = createMockPluginRegistry([{ hookName: "subagent_ended", handler }]);
|
||||
const runner = createHookRunner(registry);
|
||||
const event = {
|
||||
targetSessionKey: "agent:main:subagent:child",
|
||||
targetKind: "subagent" as const,
|
||||
reason: "subagent-complete",
|
||||
sendFarewell: true,
|
||||
accountId: "work",
|
||||
runId: "run-1",
|
||||
outcome: "ok" as const,
|
||||
};
|
||||
|
||||
await runner.runSubagentEnded(
|
||||
{
|
||||
targetSessionKey: "agent:main:subagent:child",
|
||||
targetKind: "subagent",
|
||||
reason: "subagent-complete",
|
||||
sendFarewell: true,
|
||||
accountId: "work",
|
||||
runId: "run-1",
|
||||
outcome: "ok",
|
||||
},
|
||||
{
|
||||
runId: "run-1",
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
},
|
||||
);
|
||||
await runner.runSubagentEnded(event, baseSubagentCtx);
|
||||
|
||||
expect(handler).toHaveBeenCalledWith(
|
||||
{
|
||||
targetSessionKey: "agent:main:subagent:child",
|
||||
targetKind: "subagent",
|
||||
reason: "subagent-complete",
|
||||
sendFarewell: true,
|
||||
accountId: "work",
|
||||
runId: "run-1",
|
||||
outcome: "ok",
|
||||
},
|
||||
{
|
||||
runId: "run-1",
|
||||
childSessionKey: "agent:main:subagent:child",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
},
|
||||
);
|
||||
expect(handler).toHaveBeenCalledWith(event, baseSubagentCtx);
|
||||
});
|
||||
|
||||
it("hasHooks returns true for registered subagent hooks", () => {
|
||||
|
||||
Reference in New Issue
Block a user