From d204e58e1e5ec8a4df8bbb897d6aa24d54547bd5 Mon Sep 17 00:00:00 2001 From: JohnDoe6345789 Date: Fri, 26 Dec 2025 00:30:35 +0000 Subject: [PATCH] code: nextjs,memory,max (1 files) --- .../sandbox/enforce-max-memory.test.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 frontends/nextjs/src/lib/lua/functions/sandbox/enforce-max-memory.test.ts 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() + }) +})