-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsandbox.test.js
More file actions
447 lines (360 loc) · 16 KB
/
sandbox.test.js
File metadata and controls
447 lines (360 loc) · 16 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Basic sandbox functionality tests
import { describe, it, expect, beforeEach } from 'vitest';
import { SandboxBuilder } from '../lib.js';
import { expectThrowsWithCode, expectRejectsWithCode } from './test-helpers.js';
// ── SandboxBuilder ───────────────────────────────────────────────────
describe('SandboxBuilder', () => {
it('should create a builder with defaults', () => {
const builder = new SandboxBuilder();
expect(builder).toBeInstanceOf(SandboxBuilder);
});
it('should support method chaining on setters', () => {
const builder = new SandboxBuilder();
const returned = builder.setHeapSize(8 * 1024 * 1024);
// Builder setters should return `this` for chaining
expect(returned).toBe(builder);
});
it('should chain multiple setters', () => {
const builder = new SandboxBuilder();
const result = builder
.setHeapSize(8 * 1024 * 1024)
.setScratchSize(1024 * 1024)
.setInputBufferSize(4096)
.setOutputBufferSize(4096);
expect(result).toBe(builder);
});
it('should build a proto sandbox', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
expect(proto).toBeDefined();
});
it('should throw CONSUMED on double build()', async () => {
const builder = new SandboxBuilder();
await builder.build();
await expectRejectsWithCode(builder.build(), 'ERR_CONSUMED');
});
it('should throw CONSUMED on setters after build()', async () => {
const builder = new SandboxBuilder();
await builder.build();
expectThrowsWithCode(() => builder.setHeapSize(1024), 'ERR_CONSUMED');
});
// ── Validation ───────────────────────────────────────────────────
it('should reject zero heap size', () => {
const builder = new SandboxBuilder();
expectThrowsWithCode(() => builder.setHeapSize(0), 'ERR_INVALID_ARG');
});
it('should reject zero scratch size', () => {
const builder = new SandboxBuilder();
expectThrowsWithCode(() => builder.setScratchSize(0), 'ERR_INVALID_ARG');
});
it('should reject zero input buffer size', () => {
const builder = new SandboxBuilder();
expectThrowsWithCode(() => builder.setInputBufferSize(0), 'ERR_INVALID_ARG');
});
it('should reject zero output buffer size', () => {
const builder = new SandboxBuilder();
expectThrowsWithCode(() => builder.setOutputBufferSize(0), 'ERR_INVALID_ARG');
});
});
// ── ProtoJSSandbox ───────────────────────────────────────────────────
describe('ProtoJSSandbox', () => {
it('should load the JavaScript runtime', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
expect(sandbox).toBeDefined();
});
it('should throw CONSUMED on double loadRuntime()', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
await proto.loadRuntime();
await expectRejectsWithCode(proto.loadRuntime(), 'ERR_CONSUMED');
});
});
// ── JSSandbox ────────────────────────────────────────────────────────
describe('JSSandbox', () => {
let sandbox;
beforeEach(async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
sandbox = await proto.loadRuntime();
});
it('should add a handler without throwing', () => {
// addHandler is sync and returns void; no throw means success
sandbox.addHandler('handler', 'function handler(e) { return e; }');
});
it('should reject empty handler name on add', () => {
expectThrowsWithCode(
() => sandbox.addHandler('', 'function handler(e) { return e; }'),
'ERR_INVALID_ARG'
);
});
it('should remove a handler without throwing', () => {
sandbox.addHandler('handler', 'function handler(e) { return e; }');
sandbox.removeHandler('handler');
});
it('should reject empty handler name on remove', () => {
expectThrowsWithCode(() => sandbox.removeHandler(''), 'ERR_INVALID_ARG');
});
it('should clear handlers without throwing', () => {
sandbox.addHandler('handler', 'function handler(e) { return e; }');
sandbox.clearHandlers();
});
it('should get a loaded sandbox', async () => {
sandbox.addHandler('handler', 'function handler(e) { return e; }');
const loaded = await sandbox.getLoadedSandbox();
expect(loaded).toBeDefined();
});
it('should report poisoned state', () => {
expect(sandbox.poisoned).toBe(false);
});
it('should throw CONSUMED on double getLoadedSandbox()', async () => {
sandbox.addHandler('handler', 'function handler(e) { return e; }');
await sandbox.getLoadedSandbox();
await expectRejectsWithCode(sandbox.getLoadedSandbox(), 'ERR_CONSUMED');
});
it('should throw CONSUMED on operations after getLoadedSandbox()', async () => {
sandbox.addHandler('handler', 'function handler(e) { return e; }');
await sandbox.getLoadedSandbox();
expectThrowsWithCode(
() => sandbox.addHandler('another', 'function handler(e) { return e; }'),
'ERR_CONSUMED'
);
});
});
// ── LoadedJSSandbox ──────────────────────────────────────────────────
describe('LoadedJSSandbox', () => {
let loaded;
beforeEach(async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
sandbox.addHandler(
'handler',
`
function handler(event) {
event.message = 'Hello, ' + event.name + '!';
return event;
}
`
);
loaded = await sandbox.getLoadedSandbox();
});
it('should handle events and return correct data', async () => {
const result = await loaded.callHandler('handler', { name: 'World' }, { gc: false });
expect(result.message).toBe('Hello, World!');
expect(result.name).toBe('World');
});
it('should handle events with default options (no third arg)', async () => {
const result = await loaded.callHandler('handler', { name: 'Default' });
expect(result.message).toBe('Hello, Default!');
expect(result.name).toBe('Default');
expect(loaded.poisoned).toBe(false);
});
it('should handle events with explicit gc: true', async () => {
const result = await loaded.callHandler('handler', { name: 'GC' }, { gc: true });
expect(result.message).toBe('Hello, GC!');
expect(result.name).toBe('GC');
expect(loaded.poisoned).toBe(false);
});
it('should not be poisoned initially', () => {
expect(loaded.poisoned).toBe(false);
});
it('should take and restore snapshots', async () => {
await loaded.callHandler('handler', { name: 'Test' }, { gc: false });
const snapshot = await loaded.snapshot();
expect(snapshot).toBeDefined();
await loaded.restore(snapshot);
expect(loaded.poisoned).toBe(false);
});
it('should unload back to JSSandbox', async () => {
const jsSandbox = await loaded.unload();
expect(jsSandbox).toBeDefined();
});
it('should throw CONSUMED on double unload()', async () => {
await loaded.unload();
await expectRejectsWithCode(loaded.unload(), 'ERR_CONSUMED');
});
it('should throw CONSUMED on callHandler after unload', async () => {
await loaded.unload();
await expectRejectsWithCode(
loaded.callHandler('handler', {}, { gc: false }),
'ERR_CONSUMED'
);
});
it('should reject empty handler name on callHandler', async () => {
await expectRejectsWithCode(loaded.callHandler('', {}, { gc: false }), 'ERR_INVALID_ARG');
});
it('should provide an interrupt handle', () => {
// interruptHandle is now a getter, not a method
const handle = loaded.interruptHandle;
expect(handle).toBeDefined();
expect(typeof handle.kill).toBe('function');
});
});
// ── Calculator (functional test) ─────────────────────────────────────
describe('Calculator example', () => {
let loaded;
beforeEach(async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
sandbox.addHandler(
'handler',
`
function handler(event) {
const a = event.a;
const b = event.b;
const op = event.operation;
let result;
switch(op) {
case 'add': result = a + b; break;
case 'subtract': result = a - b; break;
case 'multiply': result = a * b; break;
case 'divide': result = b !== 0 ? a / b : 'Error: Division by zero'; break;
default: result = 'Error: Unknown operation';
}
event.result = result;
return event;
}
`
);
loaded = await sandbox.getLoadedSandbox();
});
it('should add numbers', async () => {
const result = await loaded.callHandler(
'handler',
{ a: 10, b: 5, operation: 'add' },
{ gc: false }
);
expect(result.result).toBe(15);
});
it('should subtract numbers', async () => {
const result = await loaded.callHandler(
'handler',
{ a: 50, b: 30, operation: 'subtract' },
{ gc: false }
);
expect(result.result).toBe(20);
});
it('should multiply numbers', async () => {
const result = await loaded.callHandler(
'handler',
{ a: 20, b: 4, operation: 'multiply' },
{ gc: false }
);
expect(result.result).toBe(80);
});
it('should divide numbers', async () => {
const result = await loaded.callHandler(
'handler',
{ a: 100, b: 25, operation: 'divide' },
{ gc: false }
);
expect(result.result).toBe(4);
});
});
// ── dispose() ────────────────────────────────────────────────────────
describe('JSSandbox.dispose()', () => {
it('should make subsequent method calls throw ERR_CONSUMED', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
sandbox.dispose();
expectThrowsWithCode(
() => sandbox.addHandler('h', 'function handler() {}'),
'ERR_CONSUMED'
);
});
it('should be a no-op when called twice', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
sandbox.dispose();
sandbox.dispose(); // should not throw
});
it('should make poisoned getter throw ERR_CONSUMED', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
sandbox.dispose();
expectThrowsWithCode(() => sandbox.poisoned, 'ERR_CONSUMED');
});
});
describe('LoadedJSSandbox.dispose()', () => {
it('should make subsequent callHandler reject with ERR_CONSUMED', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
sandbox.addHandler('handler', 'function handler() { return { ok: true }; }');
const loaded = await sandbox.getLoadedSandbox();
await loaded.dispose();
await expectRejectsWithCode(loaded.callHandler('handler', {}), 'ERR_CONSUMED');
});
it('should be a no-op when called twice', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
sandbox.addHandler('handler', 'function handler() { return {}; }');
const loaded = await sandbox.getLoadedSandbox();
await loaded.dispose();
await loaded.dispose(); // should not throw
});
it('should make poisoned getter throw ERR_CONSUMED', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
sandbox.addHandler('handler', 'function handler() { return {}; }');
const loaded = await sandbox.getLoadedSandbox();
await loaded.dispose();
expectThrowsWithCode(() => loaded.poisoned, 'ERR_CONSUMED');
});
it('should make interruptHandle getter throw ERR_CONSUMED', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
sandbox.addHandler('handler', 'function handler() { return {}; }');
const loaded = await sandbox.getLoadedSandbox();
await loaded.dispose();
expectThrowsWithCode(() => loaded.interruptHandle, 'ERR_CONSUMED');
});
it('should make lastCallStats getter throw ERR_CONSUMED', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
sandbox.addHandler('handler', 'function handler() { return {}; }');
const loaded = await sandbox.getLoadedSandbox();
await loaded.dispose();
expectThrowsWithCode(() => loaded.lastCallStats, 'ERR_CONSUMED');
});
});
// ── unload() consumption ─────────────────────────────────────────────
describe('LoadedJSSandbox.unload()', () => {
it('should make poisoned getter throw ERR_CONSUMED after unload', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
sandbox.addHandler('handler', 'function handler() { return {}; }');
const loaded = await sandbox.getLoadedSandbox();
await loaded.unload();
expectThrowsWithCode(() => loaded.poisoned, 'ERR_CONSUMED');
});
it('should make interruptHandle getter throw ERR_CONSUMED after unload', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
sandbox.addHandler('handler', 'function handler() { return {}; }');
const loaded = await sandbox.getLoadedSandbox();
await loaded.unload();
expectThrowsWithCode(() => loaded.interruptHandle, 'ERR_CONSUMED');
});
it('should make lastCallStats getter throw ERR_CONSUMED after unload', async () => {
const builder = new SandboxBuilder();
const proto = await builder.build();
const sandbox = await proto.loadRuntime();
sandbox.addHandler('handler', 'function handler() { return {}; }');
const loaded = await sandbox.getLoadedSandbox();
await loaded.unload();
expectThrowsWithCode(() => loaded.lastCallStats, 'ERR_CONSUMED');
});
});