import { expect, test } from "bun:test"; import { isTestPath, outline, repoIndex, resolveImports } from "../src/describe/links.ts"; const repo = repoIndex([ "src/cli/cli.v", "src/cli/cmd_man.v", "src/fmt/fmt.v", "src/fmt/fmt_test.v", "web/app.ts", "web/lib/util.ts", "web/lib/index.ts", "zig/main.zig", "zig/db.zig", "rs/src/main.rs", "rs/src/store.rs", "rs/src/store/pool.rs", "go/internal/view/view.go", "go/cmd/main.go", "py/app/models.py", "py/app/views.py", "kt/app/src/main/java/com/x/license/Store.kt", "kt/app/src/main/java/com/x/Main.kt", "c/include/koh.h", "c/src/koh.c", ]); const links = (path: string, text: string) => resolveImports(path, outline(path, text)?.imports ?? [], repo); test("finds a file's project imports in each language, and nothing outside the repo", () => { expect(links("src/cli/cli.v", "module cli\n\nimport os\nimport src.fmt\n")).toEqual(["src/fmt/"]); expect( links( "web/app.ts", 'import { a } from "./lib/util";\nimport x from "hono";\nimport "./lib";\n', ), ).toEqual(["web/lib/util.ts", "web/lib/index.ts"]); expect( links("zig/main.zig", 'const std = @import("std");\nconst db = @import("db.zig");\n'), ).toEqual(["zig/db.zig"]); expect(links("rs/src/main.rs", "mod store;\nuse crate::store::Pool;\nuse std::io;\n")).toEqual([ "rs/src/store.rs", ]); expect(links("rs/src/store.rs", "mod pool;\n")).toEqual(["rs/src/store/pool.rs"]); expect( links("go/cmd/main.go", 'import (\n\t"fmt"\n\t"example.com/me/app/internal/view"\n)\n'), ).toEqual(["go/internal/view/"]); expect(links("py/app/views.py", "from .models import User\nimport os\n")).toEqual([ "py/app/models.py", ]); expect( links( "kt/app/src/main/java/com/x/Main.kt", "package com.x\n\nimport com.x.license.Store\nimport android.content.Context\n", ), ).toEqual(["kt/app/src/main/java/com/x/license/Store.kt"]); expect(links("c/src/koh.c", '#include \n#include "koh.h"\n')).toEqual([ "c/include/koh.h", ]); }); test("lists what a file exports", () => { expect( outline( "a.v", "pub fn run() {}\nfn hidden() {}\npub fn (r Repo) save() {}\npub struct Repo {}\n", )?.exports, ).toEqual(["run", "save", "Repo"]); expect( outline("a.ts", "export function go() {}\nexport type T = 1;\nconst x = 1;\n")?.exports, ).toEqual(["go", "T"]); expect(outline("a.zig", "pub fn open() void {}\nfn close() void {}\n")?.exports).toEqual([ "open", ]); expect(outline("notes.md", "# notes\n")).toBeNull(); }); test("tells tests from the program", () => { for (const path of ["src/fmt/fmt_test.v", "web/a.test.ts", "tests/x.ts", "py/test_views.py"]) { expect([path, isTestPath(path)]).toEqual([path, true]); } for (const path of ["src/fmt/fmt.v", "web/testing.ts", "src/latest.v"]) { expect([path, isTestPath(path)]).toEqual([path, false]); } });