-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathquickjs-runner.test.ts
More file actions
314 lines (288 loc) · 9.07 KB
/
Copy pathquickjs-runner.test.ts
File metadata and controls
314 lines (288 loc) · 9.07 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect } from 'vitest';
import { QuickJSScriptRunner, SandboxError } from './quickjs-runner.js';
import type { ScriptContext, ScriptRunOptions } from './script-runner.js';
const runner = new QuickJSScriptRunner();
const hookOpts: ScriptRunOptions = { origin: { kind: 'hook', name: 't' } };
const actionOpts: ScriptRunOptions = { origin: { kind: 'action', name: 't' } };
function ctx(over: Partial<ScriptContext> = {}): ScriptContext {
return { input: {}, ...over };
}
describe('QuickJSScriptRunner — L1 expression', () => {
it('evaluates a numeric expression', async () => {
const r = await runner.evalExpression(
{ language: 'expression', source: '1 + 2 * 3' },
ctx(),
hookOpts,
);
expect(r.value).toBe(7);
});
it('evaluates against ctx.input via the wrapper', async () => {
const r = await runner.run(
{ language: 'expression', source: '40 + 2' },
ctx({ input: { x: 1 } }),
hookOpts,
);
expect(r.value).toBe(42);
});
});
describe('QuickJSScriptRunner — L2 hook script', () => {
it('mutates ctx.input via JSON return', async () => {
// Hook style: read ctx.input, return modified shape.
const r = await runner.runScript(
{
language: 'js',
source: 'return { ok: true, doubled: ctx.input.n * 2 };',
capabilities: [],
},
ctx({ input: { n: 21 } }),
hookOpts,
);
expect(r.value).toEqual({ ok: true, doubled: 42 });
});
it('respects the timeoutMs cap', async () => {
await expect(
runner.runScript(
{
language: 'js',
source: 'while (true) {}',
capabilities: [],
timeoutMs: 50,
},
ctx(),
hookOpts,
),
).rejects.toThrow();
});
it('rejects use of api.read without capability', async () => {
let called = 0;
const api = {
object: (n: string) => ({
count: (..._args: unknown[]) => {
called++;
return 1;
},
}),
};
await expect(
runner.runScript(
{
language: 'js',
source: "return ctx.api.object('opportunity').count({ a: ctx.input.id });",
capabilities: [], // no api.read
},
ctx({ input: { id: 'x' }, api }),
hookOpts,
),
).rejects.toThrow(/api\.read/);
expect(called).toBe(0);
});
it('allows api.read when capability is granted', async () => {
const api = {
object: (_n: string) => ({
count: (_filter: unknown) => 7,
}),
};
const r = await runner.runScript(
{
language: 'js',
source: "return ctx.api.object('o').count({ x: 1 });",
capabilities: ['api.read'],
},
ctx({ input: {}, api }),
hookOpts,
);
expect(r.value).toBe(7);
});
it('rejects log calls without log capability', async () => {
const log = { info: () => {}, warn: () => {}, error: () => {} };
await expect(
runner.runScript(
{
language: 'js',
source: "ctx.log.info('hi'); return 1;",
capabilities: [],
},
ctx({ log }),
hookOpts,
),
).rejects.toThrow(/'log'/);
});
it('crypto.uuid requires capability', async () => {
await expect(
runner.runScript(
{ language: 'js', source: 'return ctx.crypto.randomUUID();', capabilities: [] },
ctx(),
hookOpts,
),
).rejects.toThrow(/crypto\.uuid/);
const r = await runner.runScript(
{ language: 'js', source: 'return ctx.crypto.randomUUID();', capabilities: ['crypto.uuid'] },
ctx(),
hookOpts,
);
expect(typeof r.value).toBe('string');
expect((r.value as string).length).toBeGreaterThanOrEqual(36);
});
it('reports script-thrown errors with origin name', async () => {
await expect(
runner.runScript(
{ language: 'js', source: "throw new Error('bad');", capabilities: [] },
ctx(),
{ origin: { kind: 'hook', name: 'oops' } },
),
).rejects.toThrow(/hook 'oops'/);
});
});
describe('QuickJSScriptRunner — L2 action script', () => {
it('passes input as the first argument and returns a value', async () => {
const r = await runner.runScript(
{
language: 'js',
source: 'return { sum: input.a + input.b, who: ctx.user?.id };',
capabilities: [],
},
{ input: { a: 2, b: 3 }, user: { id: 'u1' } },
actionOpts,
);
expect(r.value).toEqual({ sum: 5, who: 'u1' });
});
});
describe('QuickJSScriptRunner — async host APIs', () => {
it('awaits Promise return values from host APIs (asyncified)', async () => {
const api = { object: () => ({ count: async () => 7 }) };
const r = await runner.runScript(
{
language: 'js',
source: "return await ctx.api.object('o').count({});",
capabilities: ['api.read'],
},
ctx({ api }),
hookOpts,
);
expect(r.value).toBe(7);
});
it('propagates rejections from async host APIs as SandboxError', async () => {
const api = {
object: () => ({
count: async () => {
throw new Error('db is on fire');
},
}),
};
await expect(
runner.runScript(
{
language: 'js',
source: "return await ctx.api.object('o').count({});",
capabilities: ['api.read'],
},
ctx({ api }),
hookOpts,
),
).rejects.toThrow(/db is on fire/);
});
it('captures direct ctx.input mutations into result.mutatedInput', async () => {
const r = await runner.runScript(
{
language: 'js',
source: "ctx.input.normalized = (ctx.input.raw || '').toUpperCase();",
capabilities: [],
},
{ input: { raw: 'abc-9' } },
hookOpts,
);
expect(r.mutatedInput).toMatchObject({ raw: 'abc-9', normalized: 'ABC-9' });
});
});
describe('QuickJSScriptRunner — long-running async host work (pump budget)', () => {
// Regression: an action's single `ctx.api.update(...)` can synchronously drive
// a large amount of awaited host work — e.g. a record-change flow that the
// engine runs inline inside the afterUpdate hook chain (see tianshun-mtc
// `lead_apply_convert` → `lead_convert_approval`). From the sandbox's view
// that is ONE asyncified host call that takes many event-loop turns to settle.
//
// The pump loop must bound that wait by the configured `timeoutMs`, NOT by a
// fixed iteration count: a legitimately-progressing call that needs >1000
// event-loop turns but finishes well within the timeout must still resolve.
// The old fixed `pumps < 1000` cap fired in ~tens of ms and surfaced as
// "did not resolve after 1000 pump iterations" — the exact production error.
it('resolves an action whose host call settles after >1000 event-loop turns', async () => {
const TURNS = 1500; // comfortably exceeds the old 1000-pump cap
let observed = 0;
const api = {
object: () => ({
// One asyncified host call that internally needs many macrotask turns
// before its promise settles — mirrors a CRUD write that synchronously
// runs a long downstream automation.
update: async () => {
for (let i = 0; i < TURNS; i++) {
await new Promise<void>((resolve) => setImmediate(resolve));
}
observed = TURNS;
return { ok: true };
},
}),
};
const r = await runner.runScript(
{
language: 'js',
source: "return await ctx.api.object('wid').update({ id: 'x', status: 'pending' });",
capabilities: ['api.write'],
timeoutMs: 30000,
},
ctx({ api }),
actionOpts,
);
expect(r.value).toEqual({ ok: true });
expect(observed).toBe(TURNS);
}, 40000);
it('resolves an action that makes >1000 sequential host calls', async () => {
const N = 1500;
let calls = 0;
const api = {
object: () => ({
update: async () => {
calls++;
return { i: calls };
},
}),
};
const r = await runner.runScript(
{
language: 'js',
source: `
const o = ctx.api.object('wid');
for (let i = 0; i < ${N}; i++) { await o.update({ id: 'x', i }); }
return { calls: ${N} };
`,
capabilities: ['api.write'],
timeoutMs: 30000,
},
ctx({ api }),
actionOpts,
);
expect(r.value).toEqual({ calls: N });
expect(calls).toBe(N);
}, 40000);
it('still enforces the timeout on a host call that never settles', async () => {
const api = {
object: () => ({
// Never resolves — must be killed by the deadline, not hang forever.
update: () => new Promise<never>(() => {}),
}),
};
await expect(
runner.runScript(
{
language: 'js',
source: "return await ctx.api.object('wid').update({ id: 'x' });",
capabilities: ['api.write'],
timeoutMs: 300,
},
ctx({ api }),
actionOpts,
),
).rejects.toThrow(/timeout/i);
}, 10000);
});