diff --git a/frontends/nextjs/src/lib/lua/functions/sandbox/enforce-max-memory.test.ts b/frontends/nextjs/src/lib/lua/functions/sandbox/enforce-max-memory.test.ts new file mode 100644 index 000000000..b2bdaa31d --- /dev/null +++ b/frontends/nextjs/src/lib/lua/functions/sandbox/enforce-max-memory.test.ts @@ -0,0 +1,26 @@ +import { describe, it, expect } from 'vitest' +import type { SandboxedLuaEngineState } from './types' +import { enforceMaxMemory } from './enforce-max-memory' + +const createState = (usageBytes: number, maxMemory: number): SandboxedLuaEngineState => + ({ + maxMemory, + getLuaMemoryUsageBytes: () => usageBytes, + }) as SandboxedLuaEngineState + +describe('enforceMaxMemory', () => { + it('does not throw when usage is under the limit', () => { + const state = createState(512, 1024) + expect(() => enforceMaxMemory.call(state)).not.toThrow() + }) + + it('throws when usage exceeds the limit', () => { + const state = createState(2048, 1024) + expect(() => enforceMaxMemory.call(state)).toThrow(/Memory limit exceeded/) + }) + + it('does not throw when maxMemory is disabled', () => { + const state = createState(2048, 0) + expect(() => enforceMaxMemory.call(state)).not.toThrow() + }) +})