forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-runtime.test.ts
More file actions
195 lines (180 loc) · 5.34 KB
/
Copy pathagent-runtime.test.ts
File metadata and controls
195 lines (180 loc) · 5.34 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { describe, expect, it } from "vitest";
import {
AgentRuntime,
InMemoryRuntimeLogger,
RuntimeEventBus
} from "../src/index.js";
describe("AgentRuntime", () => {
it("executes a happy-path task and records memory", async () => {
const logger = new InMemoryRuntimeLogger();
const runtime = new AgentRuntime({
runtimeId: "runtime-test",
logger
});
runtime.registerTool({
name: "echo",
description: "Echoes a provided message.",
execute({ payload, context }) {
return {
echoed: String((payload as { message: string }).message),
agentId: context.agent.agentId
};
}
});
await runtime.start();
const result = await runtime.executeTask<
{ message: string },
{ echoed: string; agentId: string }
>({
taskId: "task-1",
agentId: "agent-1",
toolName: "echo",
input: "Echo this payload",
payload: { message: "hello" }
});
const memory = await runtime
.getDependencies()
.memoryStore.listByAgent("agent-1");
expect(result.output).toEqual({ echoed: "hello", agentId: "agent-1" });
expect(memory).toHaveLength(1);
expect(memory[0]?.taskId).toBe("task-1");
expect(
logger.entries.some((entry) => entry.message === "Runtime started.")
).toBe(true);
});
it("emits lifecycle events for startup and task completion", async () => {
const eventBus = new RuntimeEventBus();
const events: string[] = [];
eventBus.on("runtime.started", (event) => {
events.push(event.name);
});
eventBus.on("runtime.task.received", (event) => {
events.push(event.name);
});
eventBus.on("runtime.task.completed", (event) => {
events.push(event.name);
});
const runtime = new AgentRuntime({
runtimeId: "runtime-events",
eventBus
});
runtime.registerTool({
name: "noop",
description: "Returns a static result.",
execute() {
return { ok: true };
}
});
await runtime.start();
await runtime.executeTask({
taskId: "task-2",
agentId: "agent-2",
toolName: "noop",
input: "Run noop",
payload: {}
});
expect(events).toEqual([
"runtime.started",
"runtime.task.received",
"runtime.task.completed"
]);
expect(events.indexOf("runtime.task.received")).toBeLessThan(
events.indexOf("runtime.task.completed")
);
});
it("emits task received before task failed", async () => {
const eventBus = new RuntimeEventBus();
const events: string[] = [];
eventBus.on("runtime.task.received", (event) => {
events.push(event.name);
});
eventBus.on("runtime.task.failed", (event) => {
events.push(event.name);
});
const runtime = new AgentRuntime({
runtimeId: "runtime-failed-events",
eventBus
});
runtime.registerTool({
name: "fail",
description: "Throws an error.",
execute() {
throw new Error("Tool execution failed.");
}
});
await runtime.start();
await expect(
runtime.executeTask({
taskId: "task-failed-events",
agentId: "agent-failed-events",
toolName: "fail",
input: "Fail this task",
payload: {}
})
).rejects.toThrow("Tool execution failed.");
expect(events).toEqual(["runtime.task.received", "runtime.task.failed"]);
expect(events.indexOf("runtime.task.received")).toBeLessThan(
events.indexOf("runtime.task.failed")
);
});
it("rejects execution before startup", async () => {
const runtime = new AgentRuntime({ runtimeId: "runtime-not-started" });
await expect(
runtime.executeTask({
taskId: "task-3",
agentId: "agent-3",
toolName: "missing",
input: "Should fail",
payload: {}
})
).rejects.toMatchObject({
code: "RUNTIME_NOT_STARTED"
});
});
it("surfaces tool lookup failures as typed runtime errors", async () => {
const runtime = new AgentRuntime({ runtimeId: "runtime-missing-tool" });
await runtime.start();
await expect(
runtime.executeTask({
taskId: "task-4",
agentId: "agent-4",
toolName: "missing",
input: "Invoke a missing tool",
payload: {}
})
).rejects.toMatchObject({
code: "TOOL_NOT_FOUND"
});
});
it("stops the runtime, emits runtime.stopped event, and rejects subsequent tasks", async () => {
const eventBus = new RuntimeEventBus();
const stoppedEvents: { runtimeId: string; occurredAt: string }[] = [];
eventBus.on("runtime.stopped", (event) => {
stoppedEvents.push(event.payload);
});
const runtime = new AgentRuntime({
runtimeId: "runtime-stop-test",
eventBus
});
await runtime.start();
await runtime.stop();
expect(stoppedEvents).toHaveLength(1);
expect(stoppedEvents[0]?.runtimeId).toBe("runtime-stop-test");
expect(stoppedEvents[0]?.occurredAt).toBeDefined();
// Subsequent task execution rejects
await expect(
runtime.executeTask({
taskId: "task-post-stop",
agentId: "agent-stop",
toolName: "echo",
input: "Run after stop",
payload: {}
})
).rejects.toMatchObject({
code: "RUNTIME_NOT_STARTED"
});
// Calling stop again is idempotent
await runtime.stop();
expect(stoppedEvents).toHaveLength(1);
});
});