forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction-executor.test.ts
More file actions
97 lines (83 loc) · 3.04 KB
/
Copy pathaction-executor.test.ts
File metadata and controls
97 lines (83 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { describe, it, expect } from "vitest";
import { ActionExecutor } from "../../src/actions/action-executor.js";
import { ToolRegistry } from "../../src/tools/tool-registry.js";
describe("ActionExecutor tool dispatch and payload passthrough (Issue #115)", () => {
it("dispatches to the correct registered tool by name", async () => {
const registry = new ToolRegistry();
registry.register({
name: "add",
description: "Adds two numbers",
execute: ({ payload }) => ({
sum: (payload as any).a + (payload as any).b
})
});
registry.register({
name: "multiply",
description: "Multiplies two numbers",
execute: ({ payload }) => ({
product: (payload as any).a * (payload as any).b
})
});
const executor = new ActionExecutor(registry);
const ctx = {} as any;
const addResult = await executor.execute("add", { a: 3, b: 4 }, ctx);
expect(addResult).toEqual({ sum: 7 });
const mulResult = await executor.execute("multiply", { a: 3, b: 4 }, ctx);
expect(mulResult).toEqual({ product: 12 });
});
it("passes payload through to tool execute without modification", async () => {
const registry = new ToolRegistry();
let receivedPayload: unknown = null;
registry.register({
name: "capture",
description: "Captures payload for inspection",
execute: ({ payload }) => {
receivedPayload = payload;
return { ok: true };
}
});
const executor = new ActionExecutor(registry);
const complexPayload = {
nested: { arr: [1, 2, 3], flag: true },
label: "test"
};
await executor.execute("capture", complexPayload, {} as any);
expect(receivedPayload).toBe(complexPayload);
});
it("passes context through to tool execute", async () => {
const registry = new ToolRegistry();
let receivedContext: any = null;
registry.register({
name: "ctxCapture",
description: "Captures context for inspection",
execute: ({ context }) => {
receivedContext = context;
return { ok: true };
}
});
const executor = new ActionExecutor(registry);
const mockContext = { runtimeId: "r1", taskId: "t1" } as any;
await executor.execute("ctxCapture", {}, mockContext);
expect(receivedContext).toBe(mockContext);
});
it("throws TOOL_NOT_FOUND for unregistered tool names", async () => {
const registry = new ToolRegistry();
const executor = new ActionExecutor(registry);
await expect(
executor.execute("nonexistent", {}, {} as any)
).rejects.toThrow(/not registered/);
});
it("returns async tool results correctly", async () => {
const registry = new ToolRegistry();
registry.register({
name: "asyncTool",
description: "Returns a promise",
execute: async ({ payload }) => {
return { value: (payload as any).x * 10 };
}
});
const executor = new ActionExecutor(registry);
const result = await executor.execute("asyncTool", { x: 5 }, {} as any);
expect(result).toEqual({ value: 50 });
});
});