import ts from "typescript"; import { describe, expect, it } from "vitest"; import { enclosingSymbol, fileRoute, normalizePath, parseSource, pathPattern, rootName, snakePlural, walk, } from "../src/adapters/ast.js"; function firstCall(code: string, file = "x.ts"): ts.CallExpression { let found: ts.CallExpression | undefined; walk(parseSource(file, code), (n) => { if (!found && ts.isCallExpression(n)) found = n; }); if (!found) throw new Error("no call"); return found; } describe("pathPattern", () => { const p = (expr: string) => pathPattern(firstCall(`f(${expr})`).arguments[0]); it("keeps literals", () => expect(p(`"/api/customers"`)).toEqual({ path: "/api/customers", resolved: true, kind: "literal" })); it("drops a base-url prefix and names params", () => expect(p("`${process.env.NEXT_PUBLIC_API_URL}/api/customers/${customer.id}`")).toEqual({ path: "/api/customers/:id", resolved: true, kind: "template", base: "process.env.NEXT_PUBLIC_API_URL", })); it("handles concatenation", () => expect(p(`API_BASE + "/customers/" + id`)).toEqual({ path: "/customers/:id", resolved: true, kind: "template", base: "API_BASE", })); it("strips origin and query", () => expect(p(`"https://pay.example.com/v1/charges?x=1"`)).toEqual({ path: "/v1/charges", resolved: true, kind: "literal", })); it("unwraps encodeURIComponent", () => expect(p("`/customers/${encodeURIComponent(id)}`")).toEqual({ path: "/customers/:id", resolved: true, kind: "template", })); it("gives up on a bare variable", () => expect(p("url")).toBeUndefined()); it("strips a leading dynamic prefix immediately followed by '/', even when its name doesn't look like a base", () => expect(p("`${cfg}/api/customers`")).toEqual({ path: "/api/customers", resolved: true, kind: "literal", base: "cfg", })); it("does not strip a dynamic part that isn't first", () => expect(p("`/api/customers/${cfg}`")).toEqual({ path: "/api/customers/:cfg", resolved: true, kind: "template", })); }); describe("enclosingSymbol", () => { it("names functions, methods and arrow consts", () => { const sf = parseSource("x.ts", ` function useThing() { return async () => fetch("/a"); } class CustomerService { async create() { db.from("customers"); } } const getCustomer = async () => fetch("/b"); fetch("/c");`); const symbols: string[] = []; walk(sf, (n) => { if (ts.isCallExpression(n) && /fetch|from/.test(n.expression.getText())) symbols.push(enclosingSymbol(n)); }); expect(symbols).toEqual(["useThing", "CustomerService.create", "getCustomer", ""]); }); it("names methods and arrow properties of an object literal assigned to a variable", () => { const sf = parseSource("x.ts", ` export const orderService = { async create(data) { return db.from("orders"); }, find: async (id) => db.from("orders"), remove: function (id) { return db.from("orders"); }, };`); const symbols: string[] = []; walk(sf, (n) => { if (ts.isCallExpression(n) && /from/.test(n.expression.getText())) symbols.push(enclosingSymbol(n)); }); expect(symbols).toEqual(["orderService.create", "orderService.find", "orderService.remove"]); }); }); describe("enclosingSymbol — CommonJS and default-export objects", () => { const names = (code: string) => { const sf = parseSource("x.js", code); const out: string[] = []; walk(sf, (n) => { if (ts.isCallExpression(n) && /from/.test(n.expression.getText())) out.push(enclosingSymbol(n)); }); return out; }; it("names methods and property functions of an object assigned to module.exports as exports.", () => { expect(names(` module.exports = { async list() { return db.from("a"); }, find: async (id) => db.from("b"), remove: function (id) { return db.from("c"); }, };`)).toEqual(["exports.list", "exports.find", "exports.remove"]); }); it("names methods of an object literal in export default as default.", () => { expect(names(` export default { list() { return db.from("a"); }, find: (id) => db.from("b"), };`)).toEqual(["default.list", "default.find"]); }); it("names class property arrows and anonymous default classes", () => { expect(names(` export default class { run() { return db.from("a"); } create = async () => db.from("b"); }`)).toEqual([".run", ".create"]); }); }); describe("helpers", () => { it("normalizes paths", () => { expect(normalizePath("customers//x/")).toBe("/customers/x"); expect(normalizePath("")).toBe("/"); }); it("builds file routes", () => { expect(fileRoute(["(shop)", "customers", "[id]", "edit"])).toBe("/customers/:id/edit"); expect(fileRoute(["docs", "[...slug]"])).toBe("/docs/:slug*"); expect(fileRoute([])).toBe("/"); }); it("finds the root name, skipping this", () => { expect(rootName(firstCall("this.prisma.invoice.create()").expression)).toBe("prisma"); expect(rootName(firstCall("db.from('x')").expression)).toBe("db"); }); it("pluralises table names like Lucid", () => { expect(snakePlural("Customer")).toBe("customers"); expect(snakePlural("CustomerContact")).toBe("customer_contacts"); expect(snakePlural("Category")).toBe("categories"); expect(snakePlural("Address")).toBe("addresses"); }); }); describe("enclosingSymbol — inline CommonJS exports and anonymous defaults", () => { const names = (file: string, code: string) => { const sf = parseSource(file, code); const out: string[] = []; walk(sf, (n) => { if (ts.isCallExpression(n) && /from/.test(n.expression.getText())) out.push(enclosingSymbol(n)); }); return out; }; it("names functions assigned to exports.x / module.exports.x by the property", () => { expect(names("x.js", ` exports.helper = function helper() { return db.from("a"); }; exports.list = function () { return db.from("b"); }; module.exports.x = () => db.from("c");`)).toEqual(["helper", "list", "x"]); }); it("names module.exports = function main by its name, and anonymous ones default", () => { expect(names("x.js", `module.exports = function main() { return db.from("a"); };`)).toEqual(["main"]); expect(names("y.js", `module.exports = () => db.from("a");`)).toEqual(["default"]); }); it("names anonymous default-export functions default", () => { expect(names("x.ts", `export default function () { return db.from("a"); }`)).toEqual(["default"]); expect(names("y.ts", `export default () => db.from("a");`)).toEqual(["default"]); expect(names("z.ts", `export default function handler() { return db.from("a"); }`)).toEqual(["handler"]); }); });