Download src/lib/coding/__tests__/harness.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 4.39 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/coding/__tests__/harness.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/coding/__tests__/harness.test.ts
-
curl -L -o harness.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/coding/__tests__/harness.test.ts
4.39 kB
| import { runJavaScriptTests } from "../harness"; | |
| import type { TestCase } from "../types"; | |
| describe("runJavaScriptTests", () => { | |
| it("passes every test for a correct solution", () => { | |
| const code = `function add(a, b) { return a + b; }`; | |
| const tests: TestCase[] = [ | |
| { name: "1+1", input: [1, 1], expected: 2 }, | |
| { name: "2+3", input: [2, 3], expected: 5 }, | |
| ]; | |
| const result = runJavaScriptTests(code, "add", tests); | |
| expect(result.allPassed).toBe(true); | |
| expect(result.passed).toBe(2); | |
| expect(result.total).toBe(2); | |
| expect(result.results.every((r) => r.passed)).toBe(true); | |
| }); | |
| it("works when the entry function is declared with const", () => { | |
| const code = `const square = (n) => n * n;`; | |
| const result = runJavaScriptTests(code, "square", [ | |
| { name: "3", input: [3], expected: 9 }, | |
| ]); | |
| expect(result.allPassed).toBe(true); | |
| }); | |
| it("reports failing tests with the actual returned value", () => { | |
| const code = `function add(a, b) { return a - b; }`; | |
| const result = runJavaScriptTests(code, "add", [ | |
| { name: "1+1", input: [1, 1], expected: 2 }, | |
| ]); | |
| expect(result.allPassed).toBe(false); | |
| expect(result.passed).toBe(0); | |
| expect(result.results[0].passed).toBe(false); | |
| expect(result.results[0].actual).toBe(0); | |
| }); | |
| it("deep-compares objects and arrays", () => { | |
| const code = `function pair(a, b) { return { a, b, list: [a, b] }; }`; | |
| const result = runJavaScriptTests(code, "pair", [ | |
| { name: "obj", input: [1, 2], expected: { a: 1, b: 2, list: [1, 2] } }, | |
| ]); | |
| expect(result.allPassed).toBe(true); | |
| }); | |
| it("captures console.log output as stdout", () => { | |
| const code = `function f(x) { console.log("hello", x); return x; }`; | |
| const result = runJavaScriptTests(code, "f", [ | |
| { name: "log", input: [42], expected: 42 }, | |
| ]); | |
| expect(result.stdout.join("\n")).toContain("hello 42"); | |
| }); | |
| it("isolates a per-test runtime error without aborting the run", () => { | |
| const code = `function f(x) { if (x === 0) throw new Error("boom"); return 10 / x; }`; | |
| const result = runJavaScriptTests(code, "f", [ | |
| { name: "throws", input: [0], expected: 0 }, | |
| { name: "ok", input: [2], expected: 5 }, | |
| ]); | |
| expect(result.passed).toBe(1); | |
| expect(result.results[0].passed).toBe(false); | |
| expect(result.results[0].error).toContain("boom"); | |
| expect(result.results[1].passed).toBe(true); | |
| }); | |
| it("reports a compile error for invalid syntax", () => { | |
| const code = `function f( { return ; }`; | |
| const result = runJavaScriptTests(code, "f", [ | |
| { name: "x", input: [1], expected: 1 }, | |
| ]); | |
| expect(result.allPassed).toBe(false); | |
| expect(result.passed).toBe(0); | |
| expect(result.compileError).toBeTruthy(); | |
| }); | |
| it("reports a compile error when the entry function is missing", () => { | |
| const code = `function other() { return 1; }`; | |
| const result = runJavaScriptTests(code, "add", [ | |
| { name: "x", input: [1, 1], expected: 2 }, | |
| ]); | |
| expect(result.compileError).toBeTruthy(); | |
| expect(result.allPassed).toBe(false); | |
| }); | |
| it("terminates an infinite loop via the timeout instead of hanging", () => { | |
| const code = `function f() { while (true) {} }`; | |
| const result = runJavaScriptTests(code, "f", [ | |
| { name: "loop", input: [], expected: 1 }, | |
| ], { timeoutMs: 200 }); | |
| expect(result.allPassed).toBe(false); | |
| // Either a global timeout (compileError) or a per-test timeout error. | |
| const timedOut = | |
| Boolean(result.compileError) || | |
| result.results.some((r) => /time/i.test(r.error ?? "")); | |
| expect(timedOut).toBe(true); | |
| }); | |
| it("does not leak Node globals (require is unavailable)", () => { | |
| const code = `function f() { return typeof require; }`; | |
| const result = runJavaScriptTests(code, "f", [ | |
| { name: "no require", input: [], expected: "undefined" }, | |
| ]); | |
| expect(result.allPassed).toBe(true); | |
| }); | |
| it("counts only non-error passing tests in the ratio", () => { | |
| const code = `function f(x) { return x * 2; }`; | |
| const tests: TestCase[] = [ | |
| { name: "a", input: [1], expected: 2 }, | |
| { name: "b", input: [2], expected: 4 }, | |
| { name: "c", input: [3], expected: 99 }, | |
| ]; | |
| const result = runJavaScriptTests(code, "f", tests); | |
| expect(result.passed).toBe(2); | |
| expect(result.total).toBe(3); | |
| }); | |
| }); | |