From 8be929a1c2b22dd6730c71f33a499c1b78003a49 Mon Sep 17 00:00:00 2001 From: Matteo Di Mattia <44900574+SIRTHEO@users.noreply.github.com> Date: Tue, 9 Jun 2026 19:56:09 +0200 Subject: [PATCH] test(graph): add Lua extractor + whitelist-discovery tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Address maintainer review on PR #67. - Add a dedicated `describe("Lua")` block locking in qualified `Table.method` / `T:m()` names, `local function`, both `T.f = function() … end` and `local f = function() … end` assignment forms, call attribution to the enclosing function, top-level calls falling back to ``, and dotted-callee resolution. - Move the former "handles Lua via regex fallback" test out of the regex-fallback block (Lua now routes to extractFromLua) and give it real assertions; drop "Lua" from that block's title. - Add a whitelist `.gitignore` regression test (`/*` then `!/src/`) asserting files under the re-included dir are discovered; export getGraphableFiles so the test can exercise discovery directly. - Match the dynamic-grammar style for the Lua parse call (`parse("lua" as unknown as Lang, source)`). --- src/services/code-graph.ts | 2 +- src/services/graph-symbols.ts | 2 +- tests/unit/graph-discovery.test.ts | 45 +++++++++++ tests/unit/graph-symbols.test.ts | 118 +++++++++++++++++++++++++---- 4 files changed, 150 insertions(+), 17 deletions(-) create mode 100644 tests/unit/graph-discovery.test.ts diff --git a/src/services/code-graph.ts b/src/services/code-graph.ts index a08acda..e656c2c 100644 --- a/src/services/code-graph.ts +++ b/src/services/code-graph.ts @@ -579,7 +579,7 @@ export function getAstGrepLang(ext: string): Lang | string | null { * Get all source files in a project for graph analysis. * Includes files with known AST grammars and any extra extensions. */ -async function getGraphableFiles( +export async function getGraphableFiles( projectPath: string, extraExts?: Set, ): Promise { diff --git a/src/services/graph-symbols.ts b/src/services/graph-symbols.ts index 0630aff..e0b9878 100644 --- a/src/services/graph-symbols.ts +++ b/src/services/graph-symbols.ts @@ -178,7 +178,7 @@ function extractFromLua( language: string, moduleSym: SymbolNode, ): ExtractedSymbols { - const root = parse("lua", source).root(); + const root = parse("lua" as unknown as Lang, source).root(); const symbols: SymbolNode[] = [moduleSym]; const scopes: ScopeFrame[] = []; const NAME = new Set(["dot_index_expression", "method_index_expression", "identifier"]); diff --git a/tests/unit/graph-discovery.test.ts b/tests/unit/graph-discovery.test.ts new file mode 100644 index 0000000..b800f6e --- /dev/null +++ b/tests/unit/graph-discovery.test.ts @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Giancarlo Erra - Altaire Limited + +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { ensureDynamicLanguages, getGraphableFiles } from "../../src/services/code-graph.js"; + +// Regression for the whitelist .gitignore discovery fix: a `/*` then `!/src/` +// pattern ignores everything at the root but re-includes `src/`. The old walk +// passed `src` (no trailing slash) to shouldIgnore, which `/*` matched, so the +// walk bailed and produced an empty graph. Passing `src/` lets it descend and +// the files under the re-included directory are actually picked up. +describe("getGraphableFiles — whitelist .gitignore", () => { + let root: string; + + beforeAll(() => { + ensureDynamicLanguages(); + root = fs.mkdtempSync(path.join(os.tmpdir(), "socraticode-discovery-")); + fs.mkdirSync(path.join(root, "src"), { recursive: true }); + fs.writeFileSync(path.join(root, ".gitignore"), "/*\n!/src/\n"); + fs.writeFileSync( + path.join(root, "src", "mod.lua"), + "local function f()\n return 1\nend\nreturn f\n", + ); + // A root-level file the `/*` pattern should keep ignored. + fs.writeFileSync(path.join(root, "ignored.lua"), "return 1\n"); + }); + + afterAll(() => { + try { + fs.rmSync(root, { recursive: true, force: true }); + } catch { + // ignore cleanup errors + } + }); + + it("descends into re-included src/ and discovers its files", async () => { + const files = await getGraphableFiles(root); + expect(files).toContain("src/mod.lua"); + // The `/*` pattern still ignores top-level entries that are not re-included. + expect(files).not.toContain("ignored.lua"); + }); +}); diff --git a/tests/unit/graph-symbols.test.ts b/tests/unit/graph-symbols.test.ts index 99f4710..cd6c47c 100644 --- a/tests/unit/graph-symbols.test.ts +++ b/tests/unit/graph-symbols.test.ts @@ -370,7 +370,109 @@ main() { }); }); - describe("Regex fallback (Dart, Lua, Svelte, Vue, unknown)", () => { + describe("Lua", () => { + it("extracts namespace-table, method, local, and assignment function forms", () => { + const src = ` +local T = {} + +function T.method(a) + return a +end + +function T:m() + return self +end + +local function helper() + return 1 +end + +T.f = function() + return 2 +end + +return T +`; + const out = extractSymbolsAndCalls(src, "lua" as unknown as Lang, ".lua", "init.lua"); + const names = out.symbols.map((s) => s.name); + const qnames = out.symbols.map((s) => s.qualifiedName); + expect(names).toContain(""); + // qualified Table.method / T:m() forms resolve to precise method symbols + expect(qnames).toContain("T.method"); + expect(qnames).toContain("T:m"); + // `local function` keeps its bare name + expect(names).toContain("helper"); + // `T.f = function() … end` assignment form + expect(qnames).toContain("T.f"); + const kinds = out.symbols.filter((s) => s.qualifiedName === "T.method").map((s) => s.kind); + expect(kinds).toContain("method"); + // colon-call form is also a method; the plain local function is not + expect(out.symbols.find((s) => s.qualifiedName === "T:m")?.kind).toBe("method"); + expect(out.symbols.find((s) => s.qualifiedName === "helper")?.kind).toBe("function"); + }); + + it("attributes calls to the enclosing function", () => { + const src = ` +function greet(name) + return "hi " .. name +end + +local function helper() + return greet("x") +end +`; + const out = extractSymbolsAndCalls(src, "lua" as unknown as Lang, ".lua", "init.lua"); + expect(out.symbols.some((s) => s.name === "")).toBe(true); + const greetCall = out.rawCalls.find((c) => c.calleeName === "greet"); + expect(greetCall).toBeDefined(); + expect(greetCall?.callerId).toContain("::helper#"); + }); + + it("extracts the `local f = function() … end` assignment form", () => { + const src = ` +local f = function() + return 1 +end + +local g = function() + return f() +end +`; + const out = extractSymbolsAndCalls(src, "lua" as unknown as Lang, ".lua", "init.lua"); + const names = out.symbols.map((s) => s.name); + expect(names).toContain("f"); + expect(names).toContain("g"); + expect(out.symbols.find((s) => s.qualifiedName === "f")?.kind).toBe("function"); + // call to `f` lives inside `g`, so it is attributed to `g` + const fCall = out.rawCalls.find((c) => c.calleeName === "f"); + expect(fCall?.callerId).toContain("::g#"); + }); + + it("attributes top-level calls to and resolves dotted callees", () => { + const src = ` +local M = require("mod") + +M.setup() + +local function run() + M.start() +end +`; + const out = extractSymbolsAndCalls(src, "lua" as unknown as Lang, ".lua", "init.lua"); + // calls outside any function fall back to the synthetic scope + const requireCall = out.rawCalls.find((c) => c.calleeName === "require"); + expect(requireCall?.callerId).toContain("::#"); + // dotted callee `M.setup` resolves to the trailing identifier + const setupCall = out.rawCalls.find((c) => c.calleeName === "setup"); + expect(setupCall).toBeDefined(); + expect(setupCall?.callerId).toContain("::#"); + // `M.start()` lives inside `run`, so it is attributed there + const startCall = out.rawCalls.find((c) => c.calleeName === "start"); + expect(startCall?.callerId).toContain("::run#"); + }); + }); + + describe("Regex fallback (Dart, Svelte, Vue, unknown)", () => { it("handles Dart via regex fallback", () => { const src = ` String greet(String name) { @@ -389,20 +491,6 @@ class Foo { expect(names.length).toBeGreaterThanOrEqual(1); }); - it("handles Lua via regex fallback", () => { - const src = ` -function greet(name) - return "hi " .. name -end - -local function helper() - return greet("x") -end -`; - const out = extractSymbolsAndCalls(src, "lua" as unknown as Lang, ".lua", "init.lua"); - expect(out.symbols.some((s) => s.name === "")).toBe(true); - }); - it("handles unknown language without throwing", () => { const src = "some random text\nwith no recognizable structure"; const out = extractSymbolsAndCalls(