Add tests.

This commit is contained in:
Sam Rose 2024-02-20 10:59:04 +00:00
parent 93b18b81e0
commit a866677080
No known key found for this signature in database
2 changed files with 44 additions and 3 deletions

View File

@ -2135,5 +2135,48 @@ describe.each([
}
)
})
it("should not carry over context between formulas", async () => {
const js = Buffer.from(`return $("[text]");`).toString("base64")
const table = await config.createTable({
name: "table",
type: "table",
schema: {
text: {
name: "text",
type: FieldType.STRING,
},
formula: {
name: "formula",
type: FieldType.FORMULA,
formula: `{{ js "${js}"}}`,
formulaType: FormulaType.DYNAMIC,
},
},
})
for (let i = 0; i < 10; i++) {
await config.api.row.save(table._id!, { text: `foo${i}` })
}
const { rows } = await config.api.row.search(table._id!)
expect(rows).toHaveLength(10)
const formulaValues = rows.map(r => r.formula)
expect(formulaValues).toEqual(
expect.arrayContaining([
"foo0",
"foo1",
"foo2",
"foo3",
"foo4",
"foo5",
"foo6",
"foo7",
"foo8",
"foo9",
])
)
})
})
})

View File

@ -24,13 +24,11 @@ export function init() {
const bbCtx = context.getCurrentContext()!
if (!bbCtx.vm) {
const vm = new IsolatedVM({
bbCtx.vm = new IsolatedVM({
memoryLimit: env.JS_RUNNER_MEMORY_LIMIT,
invocationTimeout: env.JS_PER_INVOCATION_TIMEOUT_MS,
isolateAccumulatedTimeout: env.JS_PER_REQUEST_TIMEOUT_MS,
}).withHelpers()
bbCtx.vm = vm
}
const { helpers, ...rest } = ctx
return bbCtx.vm.withContext(rest, () => bbCtx.vm!.execute(js))