forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool-invoked.test.ts
More file actions
126 lines (109 loc) · 3.75 KB
/
Copy pathtool-invoked.test.ts
File metadata and controls
126 lines (109 loc) · 3.75 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { describe, it, expect, vi } from "vitest";
import { ActionExecutor } from "../../src/actions/action-executor.js";
import { ToolRegistry } from "../../src/tools/tool-registry.js";
import type { RuntimeEventBus } from "../../src/events/runtime-events.js";
import type { RuntimeContext } from "../../src/runtime/context.js";
describe("runtime.tool.invoked audit event", () => {
function makeSetup() {
const events: any[] = [];
const eventBus = {
emit: vi.fn((e: any) => events.push(e)),
on: vi.fn(),
off: vi.fn(),
listenerCount: vi.fn(() => 0)
} as unknown as RuntimeEventBus;
const logger = {
info: vi.fn(),
warn: vi.fn(),
debug: vi.fn(),
error: vi.fn()
};
const registry = new ToolRegistry();
registry.register({
name: "echo",
description: "Echo tool",
inputSchema: { type: "object", properties: {} },
execute: vi.fn(async ({ payload }: any) => ({ echoed: payload }))
});
const executor = new ActionExecutor(registry, logger as any, eventBus);
const context: RuntimeContext = {
runtimeId: "rt-1",
taskId: "task-42",
agent: { id: "agent-x" } as any,
memory: {} as any,
modelProvider: {} as any,
state: {} as any,
now: new Date().toISOString()
};
return { executor, eventBus, events, context };
}
it("emits runtime.tool.invoked with correct payload on every execution", async () => {
const { executor, events, context } = makeSetup();
await executor.execute("echo", { msg: "hello" }, context);
const invoked = events.find((e) => e.name === "runtime.tool.invoked");
expect(invoked).toBeDefined();
expect(invoked.payload.runtimeId).toBe("rt-1");
expect(invoked.payload.taskId).toBe("task-42");
expect(invoked.payload.agentId).toBe("agent-x");
expect(invoked.payload.toolName).toBe("echo");
expect(invoked.payload.invokedAt).toBeDefined();
expect(typeof invoked.payload.invokedAt).toBe("string");
});
it("emits the event before tool execution begins", async () => {
const callOrder: string[] = [];
const events: any[] = [];
const eventBus = {
emit: vi.fn((e: any) => {
events.push(e);
callOrder.push("emit");
}),
on: vi.fn(),
off: vi.fn(),
listenerCount: vi.fn(() => 0)
} as unknown as RuntimeEventBus;
const registry = new ToolRegistry();
registry.register({
name: "ordered",
description: "Ordered tool",
inputSchema: { type: "object", properties: {} },
execute: vi.fn(async () => {
callOrder.push("execute");
return {};
})
});
const executor = new ActionExecutor(registry, undefined, eventBus);
const context: RuntimeContext = {
runtimeId: "rt-2",
taskId: "t-1",
agent: { id: "a-1" } as any,
memory: {} as any,
modelProvider: {} as any,
state: {} as any,
now: new Date().toISOString()
};
await executor.execute("ordered", {}, context);
expect(callOrder[0]).toBe("emit");
expect(callOrder[1]).toBe("execute");
});
it("works without eventBus (optional dependency)", async () => {
const registry = new ToolRegistry();
registry.register({
name: "noop",
description: "Noop",
inputSchema: { type: "object", properties: {} },
execute: vi.fn(async () => ({ ok: true }))
});
const executor = new ActionExecutor(registry);
const context: RuntimeContext = {
runtimeId: "rt-3",
taskId: "t-2",
agent: { id: "a-2" } as any,
memory: {} as any,
modelProvider: {} as any,
state: {} as any,
now: new Date().toISOString()
};
const result = await executor.execute("noop", {}, context);
expect(result).toEqual({ ok: true });
});
});