|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import { dirname, join } from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { beforeAll, describe, expect, it } from "vitest"; |
| 5 | +import { extractFile, loadGrammars } from "../extraction/index.js"; |
| 6 | +import type { FileExtraction } from "../extraction/index.js"; |
| 7 | + |
| 8 | +const FIXTURES_DIR = join(dirname(fileURLToPath(import.meta.url)), "fixtures"); |
| 9 | + |
| 10 | +describe("Graph Extraction Regression", () => { |
| 11 | + beforeAll(async () => { |
| 12 | + await loadGrammars(["typescript", "javascript", "tsx", "jsx"]); |
| 13 | + }); |
| 14 | + |
| 15 | + const extractFixture = (filename: string): FileExtraction => { |
| 16 | + const path = join(FIXTURES_DIR, filename); |
| 17 | + const source = readFileSync(path, "utf-8"); |
| 18 | + const result = extractFile(`fixtures/${filename}`, source); |
| 19 | + expect(result).not.toBeNull(); |
| 20 | + return result!; |
| 21 | + }; |
| 22 | + |
| 23 | + const node = (result: FileExtraction, kind: string, name: string) => |
| 24 | + result.nodes.find((n) => n.kind === kind && n.name === name); |
| 25 | + const hasEdge = (result: FileExtraction, kind: string, targetName: string) => |
| 26 | + result.edges.some((e) => e.kind === kind && e.targetName === targetName); |
| 27 | + const hasContainsEdge = (result: FileExtraction, sourceId: string, targetId: string) => |
| 28 | + result.edges.some((e) => e.kind === "contains" && e.source === sourceId && e.target === targetId); |
| 29 | + |
| 30 | + describe("TypeScript Edge Cases", () => { |
| 31 | + let result: FileExtraction; |
| 32 | + beforeAll(() => { |
| 33 | + result = extractFixture("typescript-edge-cases.ts"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("detects language correctly", () => { |
| 37 | + expect(result.language).toBe("typescript"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("extracts interfaces, types, and enums", () => { |
| 41 | + expect(node(result, "interface", "ProcessorOptions")).toBeDefined(); |
| 42 | + expect(node(result, "type_alias", "Status")).toBeDefined(); |
| 43 | + expect(node(result, "enum", "ErrorCode")).toBeDefined(); |
| 44 | + expect(node(result, "enum_member", "Timeout")).toBeDefined(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("extracts classes with visibility modifiers and return types", () => { |
| 48 | + const cls = node(result, "class", "Processor"); |
| 49 | + expect(cls).toBeDefined(); |
| 50 | + |
| 51 | + const options = node(result, "property", "options"); |
| 52 | + expect(options).toBeDefined(); |
| 53 | + expect(options!.visibility).toBe("protected"); |
| 54 | + |
| 55 | + const run = node(result, "method", "run"); |
| 56 | + expect(run).toBeDefined(); |
| 57 | + expect(run!.visibility).toBe("public"); |
| 58 | + expect(run!.isAsync).toBe(true); |
| 59 | + expect(run!.returnType).toBe("Promise<void>"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("captures qualified names and containment", () => { |
| 63 | + const cls = node(result, "class", "Processor"); |
| 64 | + const run = node(result, "method", "run"); |
| 65 | + expect(run!.qualifiedName).toBe("Processor::run"); |
| 66 | + expect(hasContainsEdge(result, cls!.id, run!.id)).toBe(true); |
| 67 | + }); |
| 68 | + |
| 69 | + it("captures imports and calls", () => { |
| 70 | + expect(hasEdge(result, "imports", "external-lib")).toBe(true); |
| 71 | + expect(hasEdge(result, "calls", "externalHelper")).toBe(true); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe("JavaScript Edge Cases", () => { |
| 76 | + let result: FileExtraction; |
| 77 | + beforeAll(() => { |
| 78 | + result = extractFixture("javascript-edge-cases.js"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("detects language correctly", () => { |
| 82 | + expect(result.language).toBe("javascript"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("extracts classes, static methods, and calls", () => { |
| 86 | + expect(node(result, "class", "Manager")).toBeDefined(); |
| 87 | + |
| 88 | + const create = node(result, "method", "create"); |
| 89 | + expect(create).toBeDefined(); |
| 90 | + expect(create!.isStatic).toBe(true); |
| 91 | + |
| 92 | + expect(hasEdge(result, "instantiates", "Manager")).toBe(true); |
| 93 | + expect(hasEdge(result, "calls", "api.save")).toBe(true); |
| 94 | + }); |
| 95 | + |
| 96 | + it("degrades gracefully on ambiguous syntax", () => { |
| 97 | + // The file should parse and extract the valid symbols despite any syntax errors |
| 98 | + expect(node(result, "function", "withWeirdSyntax")).toBeDefined(); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe("TSX Components", () => { |
| 103 | + let result: FileExtraction; |
| 104 | + beforeAll(() => { |
| 105 | + result = extractFixture("tsx-component.tsx"); |
| 106 | + }); |
| 107 | + |
| 108 | + it("detects language correctly", () => { |
| 109 | + expect(result.language).toBe("tsx"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("extracts components, arrow functions, and calls", () => { |
| 113 | + expect(node(result, "interface", "Props")).toBeDefined(); |
| 114 | + expect(node(result, "function", "Widget")).toBeDefined(); |
| 115 | + |
| 116 | + // Arrow functions inside functions are local variables and are not extracted as nodes |
| 117 | + // But the calls they make should attribute to the enclosing function |
| 118 | + expect(hasEdge(result, "calls", "setCount")).toBe(true); |
| 119 | + }); |
| 120 | + |
| 121 | + it("captures JSX component usage via imports", () => { |
| 122 | + expect(hasEdge(result, "imports", "react")).toBe(true); |
| 123 | + expect(hasEdge(result, "imports", "./Header")).toBe(true); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe("JSX Components", () => { |
| 128 | + let result: FileExtraction; |
| 129 | + beforeAll(() => { |
| 130 | + result = extractFixture("jsx-component.jsx"); |
| 131 | + }); |
| 132 | + |
| 133 | + it("detects language correctly", () => { |
| 134 | + expect(result.language).toBe("jsx"); |
| 135 | + }); |
| 136 | + |
| 137 | + it("extracts functions and internal arrow functions", () => { |
| 138 | + const page = node(result, "function", "Page"); |
| 139 | + expect(page).toBeDefined(); |
| 140 | + |
| 141 | + // Local arrow functions are not extracted, but their instantiations attribute to the parent |
| 142 | + expect(hasEdge(result, "instantiates", "Promise")).toBe(true); |
| 143 | + }); |
| 144 | + |
| 145 | + it("captures promise instantiations", () => { |
| 146 | + expect(hasEdge(result, "instantiates", "Promise")).toBe(true); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments