-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathintegration.test.ts
More file actions
336 lines (281 loc) · 9.92 KB
/
integration.test.ts
File metadata and controls
336 lines (281 loc) · 9.92 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/// <reference lib="deno.ns" />
// Integration tests using Deno's testing framework
//
// These tests verify the RuntimeAgent works correctly with minimal
// mocked dependencies to test the core integration points.
import { assertEquals, assertExists } from "jsr:@std/assert";
import { makeInMemoryAdapter } from "npm:@livestore/adapter-web";
import { RuntimeAgent } from "../src/runtime-agent.ts";
import { RuntimeConfig } from "../src/config.ts";
import type {
ExecutionContext,
RuntimeAgentEventHandlers,
RuntimeCapabilities,
} from "../src/types.ts";
// Simple mock function creator
interface MockFunction {
(...args: unknown[]): Promise<void>;
calls: unknown[][];
}
const createMockFunction = (): MockFunction => {
const calls: unknown[][] = [];
const fn = (...args: unknown[]) => {
calls.push(args);
return Promise.resolve();
};
(fn as MockFunction).calls = calls;
return fn as MockFunction;
};
Deno.test("RuntimeAgent Integration Tests", async (t) => {
let config: RuntimeConfig;
let capabilities: RuntimeCapabilities;
let handlers: RuntimeAgentEventHandlers;
// Setup for each step
const setup = () => {
capabilities = {
canExecuteCode: true,
canExecuteSql: false,
canExecuteAi: true,
};
handlers = {
onStartup: createMockFunction(),
onShutdown: createMockFunction(),
onConnected: createMockFunction(),
onDisconnected: createMockFunction(),
onExecutionError: createMockFunction(),
};
const adapter = makeInMemoryAdapter({});
config = new RuntimeConfig({
runtimeId: "integration-test-runtime",
runtimeType: "test",
notebookId: "test-notebook-integration",
syncUrl: "ws://localhost:8787", // Not used with adapter
authToken: "test-integration-token",
clientId: "test-integration-client",
userId: "test-user-id",
adapter,
capabilities,
});
};
await t.step("basic functionality", async (t) => {
setup();
await t.step("should create agent instance", () => {
const agent = new RuntimeAgent(config, capabilities, handlers);
assertExists(agent);
assertEquals(typeof agent.start, "function");
assertEquals(typeof agent.shutdown, "function");
assertEquals(typeof agent.onExecution, "function");
assertEquals(typeof agent.keepAlive, "function");
});
setup();
await t.step("should register execution handlers", () => {
const agent = new RuntimeAgent(config, capabilities);
const executionHandler = () => Promise.resolve({ success: true });
agent.onExecution(executionHandler);
// Handler is registered internally
assertEquals(typeof executionHandler, "function");
});
setup();
await t.step("should handle shutdown gracefully", async () => {
const agent = new RuntimeAgent(config, capabilities, handlers);
// Should not throw
await agent.shutdown();
// Handler should be called
assertEquals((handlers.onShutdown as MockFunction).calls.length, 1);
});
setup();
await t.step("should handle multiple shutdown calls", async () => {
const agent = new RuntimeAgent(config, capabilities, handlers);
await agent.shutdown();
await agent.shutdown(); // Second call should be safe
// Should only call handler once
assertEquals((handlers.onShutdown as MockFunction).calls.length, 1);
});
});
await t.step("execution handler", async (t) => {
setup();
await t.step("should accept valid execution handler", () => {
const agent = new RuntimeAgent(config, capabilities);
const handler = (context: ExecutionContext) => {
context.stdout("Test output");
return Promise.resolve({ success: true });
};
agent.onExecution(handler);
assertEquals(typeof handler, "function");
});
setup();
await t.step("should replace previous execution handler", () => {
const agent = new RuntimeAgent(config, capabilities);
const firstHandler = () => Promise.resolve({ success: true });
const secondHandler = () => Promise.resolve({ success: false });
agent.onExecution(firstHandler);
agent.onExecution(secondHandler);
// Both handlers should be functions (replacement is internal)
assertEquals(typeof firstHandler, "function");
assertEquals(typeof secondHandler, "function");
});
});
await t.step("configuration validation", async (t) => {
setup();
await t.step("should accept valid configuration", () => {
const adapter = makeInMemoryAdapter({});
const validConfig = new RuntimeConfig({
runtimeId: "valid-runtime",
runtimeType: "test",
notebookId: "test-notebook",
syncUrl: "ws://localhost:8787", // Not used with adapter
authToken: "valid-token",
clientId: "valid-client",
userId: "test-user-id",
adapter,
capabilities: capabilities,
});
const agent = new RuntimeAgent(validConfig, capabilities);
assertExists(agent);
});
setup();
await t.step("should validate configuration on creation", () => {
// This tests that RuntimeConfig validation works
let error: Error | null = null;
try {
const adapter = makeInMemoryAdapter({});
const config = new RuntimeConfig({
runtimeId: "", // Invalid empty runtime ID
runtimeType: "test",
notebookId: "test",
syncUrl: "ws://localhost:8787", // Not used with adapter
authToken: "token",
clientId: "test-client",
userId: "test-user-id",
adapter,
capabilities: capabilities,
});
config.validate(); // Explicitly call validate
} catch (e) {
error = e as Error;
}
assertExists(error);
assertEquals(
error?.message.includes(
"runtimeId: RUNTIME_ID environment variable",
),
true,
);
});
});
await t.step("lifecycle management", async (t) => {
setup();
await t.step("should handle keepAlive resolution", async () => {
const agent = new RuntimeAgent(config, capabilities);
const keepAlivePromise = agent.keepAlive();
// Shutdown after a brief delay
setTimeout(() => agent.shutdown(), 10);
// Should resolve without throwing
await keepAlivePromise;
});
setup();
await t.step("should handle rapid start/shutdown cycles", async () => {
const agent = new RuntimeAgent(config, capabilities, handlers);
// Multiple cycles should work
await agent.shutdown();
await agent.shutdown();
assertEquals((handlers.onShutdown as MockFunction).calls.length, 1);
});
});
await t.step("output context methods", async (t) => {
setup();
await t.step("should provide context with output methods", () => {
const agent = new RuntimeAgent(config, capabilities);
// Register a handler that captures the context
agent.onExecution((_context) => {
return Promise.resolve({ success: true });
});
// For now, we can't easily test the full execution flow without
// mocking LiveStore heavily, but we can verify the agent structure
assertEquals(typeof agent.onExecution, "function");
// The context will be provided when actual execution happens
// This test verifies the agent accepts the handler correctly
});
});
});
Deno.test("RuntimeConfig", async (t) => {
await t.step("should create valid config with all required fields", () => {
const adapter = makeInMemoryAdapter({});
const config = new RuntimeConfig({
runtimeId: "test-runtime-3",
runtimeType: "test",
notebookId: "test-notebook",
syncUrl: "ws://localhost:8787", // Not used with adapter
authToken: "test-token",
clientId: "test-client-3",
userId: "test-user-id",
adapter,
capabilities: {
canExecuteCode: true,
canExecuteSql: false,
canExecuteAi: false,
},
});
// Verify all required fields are set correctly
assertEquals(config.runtimeId, "test-runtime-3");
assertEquals(config.runtimeType, "test");
assertEquals(config.notebookId, "test-notebook");
assertEquals(config.syncUrl, "ws://localhost:8787");
assertEquals(config.authToken, "test-token");
assertExists(config.sessionId);
});
await t.step("should generate unique session IDs", () => {
const adapter1 = makeInMemoryAdapter({});
const config1 = new RuntimeConfig({
runtimeId: "runtime1",
runtimeType: "python",
notebookId: "notebook1",
syncUrl: "ws://localhost:8787", // Not used with adapter
authToken: "token1",
clientId: "client1",
userId: "test-user-1",
adapter: adapter1,
capabilities: {
canExecuteCode: true,
canExecuteSql: false,
canExecuteAi: false,
},
});
const adapter2 = makeInMemoryAdapter({});
const config2 = new RuntimeConfig({
runtimeId: "runtime2",
runtimeType: "python",
notebookId: "notebook2",
syncUrl: "ws://localhost:8787", // Not used with adapter
authToken: "token2",
clientId: "client2",
userId: "test-user-2",
adapter: adapter2,
capabilities: {
canExecuteCode: true,
canExecuteSql: false,
canExecuteAi: false,
},
});
// Session IDs should be different
assertEquals(config1.sessionId !== config2.sessionId, true);
});
await t.step("should allow custom heartbeat interval", () => {
const adapter = makeInMemoryAdapter({});
const _config = new RuntimeConfig({
runtimeId: "test-ai-runtime",
runtimeType: "test-ai",
notebookId: "test-ai-notebook",
syncUrl: "ws://localhost:8787", // Not used with adapter
authToken: "test-ai-token",
clientId: "test-client-ai",
userId: "test-user-id",
adapter,
capabilities: {
canExecuteCode: true,
canExecuteSql: false,
canExecuteAi: true,
},
});
});
});