-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathruntime-agent-adapter-injection.test.ts
More file actions
280 lines (224 loc) · 7.4 KB
/
runtime-agent-adapter-injection.test.ts
File metadata and controls
280 lines (224 loc) · 7.4 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
/// <reference lib="deno.ns" />
// RuntimeAgent adapter injection tests
//
// These tests verify the new adapter/store injection functionality
// that allows passing custom LiveStore adapters and stores to RuntimeAgent.
import { assertEquals, assertExists } from "jsr:@std/assert";
import { crypto } from "jsr:@std/crypto";
import {
RuntimeAgent,
type RuntimeAgentOptions,
type RuntimeCapabilities,
RuntimeConfig,
} from "@runt/lib";
import { makeInMemoryAdapter } from "npm:@livestore/adapter-web";
import { makeAdapter } from "npm:@livestore/adapter-node";
// Helper function for creating test configs since createBaseRuntimeConfig moved to pyodide package
function createTestRuntimeConfig(
_args: string[],
defaults: Partial<RuntimeAgentOptions> = {},
): RuntimeConfig {
// Create default in-memory adapter for testing
const defaultAdapter = makeInMemoryAdapter({});
const config: RuntimeAgentOptions = {
runtimeId: "test-runtime-id",
runtimeType: "test-runtime",
syncUrl: "ws://fake-url:9999",
authToken: "test-token",
notebookId: "test-notebook",
capabilities: {
canExecuteCode: true,
canExecuteSql: false,
canExecuteAi: false,
},
clientId: "test-client",
userId: "test-user-id",
adapter: defaultAdapter,
...defaults,
};
return new RuntimeConfig(config);
}
Deno.test("RuntimeAgent adapter injection", async (t) => {
await t.step(
"should work with default adapter (backward compatibility)",
() => {
const config = createTestRuntimeConfig([], {
clientId: "test-client-backward-compat",
userId: "test-user-id",
notebookId: "test-notebook",
syncUrl: "ws://fake-url:9999", // Will fail but that's expected
});
const capabilities: RuntimeCapabilities = {
canExecuteCode: true,
canExecuteSql: false,
canExecuteAi: false,
};
const agent = new RuntimeAgent(config, capabilities);
// Should work exactly as before - no changes needed
assertExists(agent);
assertEquals(agent.config.notebookId, "test-notebook");
},
);
await t.step("should accept custom in-memory adapter", async () => {
// Create custom in-memory adapter
const adapter = makeInMemoryAdapter({
// No sync backend needed for pure in-memory testing
});
const config = createTestRuntimeConfig([], {
adapter,
clientId: "test-client-adapter",
userId: "test-user-id",
notebookId: "adapter-test",
syncUrl: "ws://fake-url:9999",
});
const capabilities: RuntimeCapabilities = {
canExecuteCode: true,
canExecuteSql: false,
canExecuteAi: false,
};
const agent = new RuntimeAgent(config, capabilities);
assertExists(agent);
assertEquals(agent.config.notebookId, "adapter-test");
// Test that we can start with custom adapter (won't try to sync)
await agent.start();
// Verify store is available
assertExists(agent.store);
await agent.shutdown();
});
await t.step(
"should accept custom adapter without explicit clientId",
async () => {
// Create custom in-memory adapter
const adapter = makeInMemoryAdapter({});
const config = createTestRuntimeConfig([], {
adapter,
clientId: "test-client-generated",
userId: "test-user-id",
notebookId: "adapter-test-2",
authToken: "test-token",
syncUrl: "ws://fake-url:9999",
});
const capabilities: RuntimeCapabilities = {
canExecuteCode: true,
canExecuteSql: false,
canExecuteAi: false,
};
const agent = new RuntimeAgent(config, capabilities);
await agent.start();
// Verify store was created successfully
assertExists(agent.store);
await agent.shutdown();
},
);
await t.step("should generate clientId for custom adapter", async () => {
const runtimeId = `runtime-${crypto.randomUUID()}`;
const adapter = makeInMemoryAdapter({});
const config = createTestRuntimeConfig([], {
adapter,
runtimeId,
userId: "test-user-id",
notebookId: "clientid-test",
syncUrl: "ws://fake-url:9999",
});
const capabilities: RuntimeCapabilities = {
canExecuteCode: true,
canExecuteSql: false,
canExecuteAi: false,
};
const agent = new RuntimeAgent(config, capabilities);
await agent.start();
// The generated clientId should be "runtime-{runtimeId}"
// We can't directly verify this without accessing internals,
// but we can verify the store was created successfully
assertExists(agent.store);
await agent.shutdown();
});
await t.step("should use explicit clientId when provided", async () => {
const adapter = makeInMemoryAdapter({});
const explicitClientId = "my-custom-client-id";
const config = createTestRuntimeConfig([], {
adapter,
clientId: explicitClientId,
userId: "test-user-id",
notebookId: "explicit-clientid-test",
syncUrl: "ws://fake-url:9999",
});
const capabilities: RuntimeCapabilities = {
canExecuteCode: true,
canExecuteSql: false,
canExecuteAi: false,
};
const agent = new RuntimeAgent(config, capabilities);
await agent.start();
// Store should be created successfully with custom clientId
assertExists(agent.store);
await agent.shutdown();
});
await t.step("should handle multiple agents with same adapter", async () => {
// Create shared adapter
const adapter = makeInMemoryAdapter({});
// Create two agents using the same adapter
const config1 = createTestRuntimeConfig([], {
adapter,
clientId: "client-1",
notebookId: "shared-adapter-1",
runtimeId: "agent-1",
authToken: "token1",
syncUrl: "ws://fake-url:9999",
});
const config2 = createTestRuntimeConfig([], {
adapter,
clientId: "client-2",
notebookId: "shared-adapter-2",
runtimeId: "agent-2",
authToken: "token2",
syncUrl: "ws://fake-url:9999",
});
const capabilities: RuntimeCapabilities = {
canExecuteCode: true,
canExecuteSql: false,
canExecuteAi: false,
};
const agent1 = new RuntimeAgent(config1, capabilities);
const agent2 = new RuntimeAgent(config2, capabilities);
await agent1.start();
await agent2.start();
// Both agents should have their own stores but use same adapter type
assertExists(agent1.store);
assertExists(agent2.store);
await agent1.shutdown();
await agent2.shutdown();
});
await t.step("should work with file system adapter", async () => {
// Create temporary directory for test
const tempDir = `/tmp/runt-test-${crypto.randomUUID()}`;
const fsAdapter = makeAdapter({
storage: {
type: "fs",
baseDirectory: tempDir,
},
});
const config = createTestRuntimeConfig([], {
adapter: fsAdapter,
clientId: "fs-client",
notebookId: "fs-test",
authToken: "test-token",
syncUrl: "ws://fake-url:9999",
});
const capabilities: RuntimeCapabilities = {
canExecuteCode: true,
canExecuteSql: false,
canExecuteAi: false,
};
const agent = new RuntimeAgent(config, capabilities);
await agent.start();
assertExists(agent.store);
await agent.shutdown();
// Clean up temp directory
try {
await Deno.remove(tempDir, { recursive: true });
} catch {
// Ignore cleanup errors
}
});
});