import { describe, expect, test } from "bun:test"; import { executeJavaScriptSandbox, executeMvelSandbox } from "./sandbox.js"; import { lintMvelCode } from "./lint-mvel.js"; describe("MVEL collection and completion subset", () => { test.each(["HashMap", "Map", "java.util.HashMap"])("returns the final Map expression constructed with %s", async kind => { const result = await executeMvelSandbox(`Map result = new ${kind}(); result.put("value", 7); result;`); expect(result.success).toBe(true); expect(result.returnValue).toEqual({ value: 7 }); }); test.each(["ArrayList", "List", "java.util.ArrayList"])("supports List constructor %s", async kind => { const result = await executeMvelSandbox(`List result = new ${kind}(); result.add(7); result;`); expect(result.success).toBe(true); expect(result.returnValue).toEqual([7]); const direct = await executeMvelSandbox(`new ${kind}().size();`); expect(direct.success).toBe(true); expect(direct.returnValue).toBe(0); }); test("counts Map keys including length and prototype names, without double counting replacements", async () => { const result = await executeMvelSandbox(`Map m = new HashMap(); List sizes = new ArrayList(); sizes.add(m.size()); m.put("length", 99); sizes.add(m.size()); m.put("length", 100); sizes.add(m.size()); m.put("__proto__", 8); sizes.add(m.size()); sizes.add(m.get("absent")); sizes;`); expect(result.success).toBe(true); expect(result.returnValue).toEqual([0, 1, 1, 2, null]); }); test("supports nested receivers, nested arguments, indexed inputs and collection return values", async () => { const nested = await executeMvelSandbox(`Map m = new HashMap(); List a = new ArrayList(); a.add(Math.max(2, 3)); m.put("items", a); m.get("items").add(m.get("items").size()); data.get("rows")[0].put("x", 2); a.add((data.get("rows")[0]).size()); m.get("items");`, { data: { rows: [{}] } }); expect(nested.success).toBe(true); expect(nested.returnValue).toEqual([3, 1, 1]); }); test("preserves literal contents while ignoring comments during conversion", async () => { const literal = 'String x = new java.util.HashMap(); .put("x", 1).size() / 2 ? a : b;'; const result = await executeMvelSandbox(`/* new Unsafe(); return 8; */ // new ArrayList(2) ${JSON.stringify(literal)}; // final comment`); expect(result.success).toBe(true); expect(result.returnValue).toBe(literal); }); test.each(["result", "(result);", "result;;; // final comment", "result /* ; return 0; */"])("supports tail expression %s", async tail => { const result = await executeMvelSandbox(`var result = 7; ${tail}`); expect(result.success).toBe(true); expect(result.returnValue).toBe(7); }); test("evaluates a terminal expression once and preserves put's previous-value return", async () => { const increment = await executeMvelSandbox("var count = 0; ++count;"); expect(increment.success).toBe(true); expect(increment.returnValue).toBe(1); const put = await executeMvelSandbox('Map m = new HashMap(); m.put("x", 1); m.put("x", 2);'); expect(put.success).toBe(true); expect(put.returnValue).toBe(1); }); test("preserves division, ternary, classic for and explicit return compatibility", async () => { const result = await executeMvelSandbox("int total = 0; for (int i = 0; i < 3; i++) { total += i; } if (total == 3) { total += 2; } else { total = 0; } return total > 0 ? total / 2 : 0;"); expect(result.success).toBe(true); expect(result.returnValue).toBe(2.5); const assignment = await executeMvelSandbox("var x = 0; x = 9;"); expect(assignment.returnValue).toBe(9); }); test("does not reject existing JavaScript-compatible blocks before a terminal result", async () => { const result = await executeMvelSandbox('function value() { return 8; } var result = 0; try { throw "probe"; } catch (error) { result = value(); } finally { result += 1; } result;'); expect(result.success).toBe(true); expect(result.returnValue).toBe(9); }); test.each(["new java.util.Date();", "new HashMap(5);", "new java.util.ArrayList(2);", "Map m = new HashMap(); m;", "if (true) { 7; }", "var value = 7;", "for (item : items) { item; } item;"])("rejects unsupported syntax or completion: %s", async code => { const result = await executeMvelSandbox(code); expect(result.success).toBe(false); expect(result.error?.name).toBe("UnsupportedMvelSyntax"); }); test("fails explicitly for invalid receiver types and List bounds", async () => { for (const code of ['value.size();', 'value.get(1);']) { const result = await executeMvelSandbox(code, { value: code.includes("get") ? [] : 7 }); expect(result.success).toBe(false); } }); test("keeps the execution time budget and JavaScript completion behavior", async () => { const result = await executeMvelSandbox("while (true) {} 7;", {}, { timeoutMs: 10 }); expect(result.success).toBe(false); expect(result.error?.message).toContain("timed out"); const js = await executeJavaScriptSandbox("7;"); expect(js.success).toBe(true); expect(js.returnValue).toBeUndefined(); }); test("lint accepts a final expression without changing explicit hasReturn meaning", () => { const result = lintMvelCode('Map result = new java.util.HashMap(); result.put("x", 7); result;'); expect(result.hasReturn).toBe(false); expect(result.issues).toEqual([]); expect(lintMvelCode("var x = 1;").issues.map(issue => issue.rule)).toContain("missing-return"); }); });