import { describe, expect, test } from "bun:test"; import { MAX_RUNS, normalizePath, parseSarif } from "./sarif"; function run(name: string, results: unknown[] = []) { return { tool: { driver: { name } }, results }; } describe("parseSarif (INT-A11/INT-A31)", () => { test("caps the number of runs and flags truncation", () => { const runs = Array.from({ length: MAX_RUNS + 5 }, (_, index) => run(`tool-${index}`)); const parsed = parseSarif({ version: "2.1.0", runs }); expect(parsed.tools).toHaveLength(MAX_RUNS); expect(parsed.truncated).toBe(true); }); test("bounds rule names, line numbers and paths", () => { const parsed = parseSarif({ version: "2.1.0", runs: [ { tool: { driver: { name: "x", rules: [{ id: "r1", name: "n".repeat(500) }] } }, results: [ { ruleId: "r1", message: { text: "m" }, locations: [ { physicalLocation: { artifactLocation: { uri: "file:///C:/repo/../src/./a.ts" }, region: { startLine: -5, endLine: 10 ** 12 }, }, }, ], }, ], }, ], }); const [result] = parsed.results; expect(result.ruleName).toHaveLength(200); expect(result.startLine).toBe(1); expect(result.endLine).toBe(10_000_000); expect(result.path).toBe("repo/src/a.ts"); }); }); describe("normalizePath", () => { test("drops traversal and drive letters", () => { expect(normalizePath("../../etc/passwd")).toBe("etc/passwd"); expect(normalizePath("C:\\work\\a.ts")).toBe("work/a.ts"); expect(normalizePath("./src/a.ts")).toBe("src/a.ts"); expect(normalizePath("..")).toBeNull(); }); });