const { test, expect } = require("bun:test"); const { SourceMap } = require("node:module"); test("SourceMap class exists", () => { expect(SourceMap).toBeDefined(); expect(typeof SourceMap).toBe("function"); expect(SourceMap.name).toBe("SourceMap"); }); test("SourceMap constructor requires payload", () => { expect(() => { new SourceMap(); }).toThrowErrorMatchingInlineSnapshot(`"The "payload" argument must be of type object. Received undefined"`); }); test("SourceMap payload must be an object", () => { expect(() => { new SourceMap("not an object"); }).toThrowErrorMatchingInlineSnapshot( `"The "payload" argument must be of type object. Received type string ('not an object')"`, ); }); test("SourceMap instance has expected methods", () => { const sourceMap = new SourceMap({ version: 3, sources: ["test.js"], mappings: "AAAA", }); expect(typeof sourceMap.findOrigin).toBe("function"); expect(typeof sourceMap.findEntry).toBe("function"); expect(sourceMap.findOrigin.length).toBe(2); expect(sourceMap.findEntry.length).toBe(2); }); test("SourceMap payload getter", () => { const payload = { version: 3, sources: ["test.js"], mappings: "AAAA", }; const sourceMap = new SourceMap(payload); expect(sourceMap.payload).toBe(payload); }); test("SourceMap lineLengths getter", () => { const payload = { version: 3, sources: ["test.js"], mappings: "AAAA", }; const lineLengths = [10, 20, 30]; const sourceMap = new SourceMap(payload, { lineLengths }); expect(sourceMap.lineLengths).toBe(lineLengths); }); test("SourceMap lineLengths undefined when not provided", () => { const sourceMap = new SourceMap({ version: 3, sources: ["test.js"], mappings: "AAAA", }); expect(sourceMap.lineLengths).toBeUndefined(); }); test("SourceMap findEntry returns mapping data", () => { const sourceMap = new SourceMap({ version: 3, sources: ["test.js"], mappings: "AAAA", }); const result = sourceMap.findEntry(0, 0); expect(result).toMatchInlineSnapshot(` { "generatedColumn": 0, "generatedLine": 0, "name": undefined, "originalColumn": 0, "originalLine": 0, "originalSource": "test.js", } `); }); test("SourceMap findOrigin returns origin data", () => { const sourceMap = new SourceMap({ version: 3, sources: ["test.js"], mappings: "AAAA", }); const result = sourceMap.findOrigin(0, 0); expect(result).toMatchInlineSnapshot(` { "column": 0, "fileName": "test.js", "line": 0, "name": undefined, } `); }); test("SourceMap with names returns name property correctly", () => { const sourceMap = new SourceMap({ version: 3, sources: ["test.js"], names: ["myFunction", "myVariable"], mappings: "AAAAA,CAACC", // Both segments reference names }); const result = sourceMap.findEntry(0, 0); const resultWithName = sourceMap.findEntry(0, 6); expect(result).toMatchInlineSnapshot(` { "generatedColumn": 0, "generatedLine": 0, "name": "myFunction", "originalColumn": 0, "originalLine": 0, "originalSource": "test.js", } `); expect(resultWithName).toMatchInlineSnapshot(` { "generatedColumn": 1, "generatedLine": 0, "name": "myVariable", "originalColumn": 1, "originalLine": 0, "originalSource": "test.js", } `); }); test("SourceMap without names has undefined name property", () => { const sourceMap = new SourceMap({ version: 3, sources: ["test.js"], mappings: "AAAA", }); const result = sourceMap.findEntry(0, 0); expect(result).toMatchInlineSnapshot(` { "generatedColumn": 0, "generatedLine": 0, "name": undefined, "originalColumn": 0, "originalLine": 0, "originalSource": "test.js", } `); }); test("SourceMap with invalid name index has undefined name property", () => { const sourceMap = new SourceMap({ version: 3, sources: ["test.js"], mappings: "AAAAA,CAACC", // Both segments reference names }); const result = sourceMap.findEntry(0, 0); expect(result).toMatchInlineSnapshot(` { "generatedColumn": 0, "generatedLine": 0, "name": undefined, "originalColumn": 0, "originalLine": 0, "originalSource": "test.js", } `); });