149 lines · 9.1 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { indexCommit, toPackage } from "../code-index/build.ts"; |
| 4 | import { buildGraph } from "../code-index/graph.ts"; |
| 5 | import { outline } from "../code-index/outline.ts"; |
| 6 | import { CODE_MAP_VERSION, claimBuild, readCodeMap, saveCodeMap } from "../code-index/store.ts"; |
| 7 | import { codeChanges } from "../code-maps.ts"; |
| 8 | import type { ChangeSet, CodeGraph } from "../contract/views.ts"; |
| 9 | import { d1, migrated } from "./d1.ts"; |
| 10 | |
| 11 | const names = (source: string) => outline(source).symbols.map((symbol) => `${symbol.kind} ${symbol.name} ${symbol.start}-${symbol.end}${symbol.exported ? " exported" : ""}`); |
| 12 | |
| 13 | test("an outline finds top-level declarations with their line ranges", () => { |
| 14 | const source = [ |
| 15 | 'import { helper } from "./helper.ts";', |
| 16 | "/** Adds two numbers. */", |
| 17 | "export function add(a: number, b: number): { sum: number } {", |
| 18 | " return { sum: helper(a) + b };", |
| 19 | "}", |
| 20 | "const LIMIT = 3;", |
| 21 | "export const double = (value: number) => value * 2;", |
| 22 | "export type Shape = { kind: string };", |
| 23 | "interface Point {", |
| 24 | " x: number;", |
| 25 | "}", |
| 26 | "enum Color { Red, Green }", |
| 27 | "export default function main() {}", |
| 28 | ].join("\n"); |
| 29 | assert.deepEqual(names(source), ["function add 3-5 exported", "const LIMIT 6-6", "function double 7-7 exported", "type Shape 8-8 exported", "interface Point 9-11", "enum Color 12-12", "function main 13-13 exported"]); |
| 30 | const found = outline(source); |
| 31 | assert.equal(found.symbols[0].doc, "Adds two numbers."); |
| 32 | assert.deepEqual(found.exports, [{ exported: "default", local: "main" }]); |
| 33 | assert.deepEqual(found.imports, [{ local: "helper", module: "./helper.ts", imported: "helper" }]); |
| 34 | assert.deepEqual(found.references[0].map((reference) => reference.name), ["a", "helper", "a", "b"]); |
| 35 | }); |
| 36 | |
| 37 | test("an outline finds class members and marks `this.` uses as member uses", () => { |
| 38 | const source = ["export class Store {", " #items: string[] = [];", " constructor(private readonly name: string) {}", " add(item: string) {", " this.#items.push(item);", " this.notify();", " }", " notify(): void {}", " get size() {", " return this.#items.length;", " }", "}"].join("\n"); |
| 39 | const found = outline(source); |
| 40 | assert.deepEqual(found.symbols.map((symbol) => `${symbol.kind} ${symbol.name} ${symbol.start}-${symbol.end} ${symbol.parent}`), ["class Store 1-12 null", "field #items 2-2 0", "method constructor 3-3 0", "method add 4-7 0", "method notify 8-8 0", "method size 9-11 0"]); |
| 41 | assert.deepEqual(found.references[3].filter((reference) => reference.member).map((reference) => reference.name), ["#items", "notify"]); |
| 42 | assert.deepEqual(found.references[0], []); |
| 43 | }); |
| 44 | |
| 45 | test("an outline reads JSX tags as uses and does not mistake `</` or `/>` for a regular expression", () => { |
| 46 | const source = ['import { Card } from "./card.tsx";', "export function Page({ items }: { items: string[] }) {", " return <Card title={`n ${items.length}`}><List items={items} /></Card>;", "}", "function List({ items }: { items: string[] }) {", " return <ul>{items.map((item) => <li key={item}>{item}</li>)}</ul>;", "}"].join("\n"); |
| 47 | const found = outline(source); |
| 48 | assert.deepEqual(names(source), ["function Page 2-4 exported", "function List 5-7"]); |
| 49 | assert.deepEqual(found.references[0].filter((reference) => reference.call).map((reference) => reference.name), ["Card", "List"]); |
| 50 | }); |
| 51 | |
| 52 | test("the graph resolves uses across relative imports, re-exports and workspace packages", () => { |
| 53 | const graph = buildGraph( |
| 54 | "c1", |
| 55 | [ |
| 56 | { path: "app/main.ts", text: 'import { createStore } from "./store/index.ts";\nimport { format } from "@acme/util/format";\nexport function main() {\n const store = createStore();\n return format(store.name);\n}' }, |
| 57 | { path: "app/store/index.ts", text: 'export { createStore } from "./create.ts";' }, |
| 58 | { path: "app/store/create.ts", text: 'import type { Store } from "./types.ts";\nexport function createStore(): Store {\n return new MemoryStore();\n}\nclass MemoryStore {\n name = "s";\n}' }, |
| 59 | { path: "app/store/types.ts", text: "export interface Store {\n name: string;\n}" }, |
| 60 | { path: "packages/util/format.ts", text: "export function format(value: string) {\n return value.trim();\n}" }, |
| 61 | ], |
| 62 | [{ name: "@acme/util", dir: "packages/util", exports: new Map([["./format", "format.ts"]]) }], |
| 63 | 2, |
| 64 | ); |
| 65 | const edges = graph.edges.map((edge) => `${edge.from} ${edge.kind} ${edge.to}`).sort(); |
| 66 | assert.deepEqual(edges, [ |
| 67 | "app/main.ts#main calls app/store/create.ts#createStore", |
| 68 | "app/main.ts#main calls packages/util/format.ts#format", |
| 69 | "app/store/create.ts#createStore creates app/store/create.ts#MemoryStore", |
| 70 | "app/store/create.ts#createStore types app/store/types.ts#Store", |
| 71 | ]); |
| 72 | assert.equal(graph.skipped, 2); |
| 73 | assert.deepEqual(graph.files.map((file) => file.path), ["app/main.ts", "app/store/create.ts", "app/store/index.ts", "app/store/types.ts", "packages/util/format.ts"]); |
| 74 | }); |
| 75 | |
| 76 | test("a package's exports map becomes repository paths", () => { |
| 77 | const pkg = toPackage("packages/ui/package.json", JSON.stringify({ name: "@acme/ui", exports: { ".": "./index.ts", "./icons": { import: "./icons.tsx" } } })); |
| 78 | assert.deepEqual(pkg && { name: pkg.name, dir: pkg.dir, exports: [...pkg.exports] }, { name: "@acme/ui", dir: "packages/ui", exports: [[".", "index.ts"], ["./icons", "icons.tsx"]] }); |
| 79 | assert.equal(toPackage("package.json", "not json"), null); |
| 80 | }); |
| 81 | |
| 82 | function fakeRepo(): ArtifactsRepo { |
| 83 | const trees: Record<string, ArtifactsTreeEntry[]> = { |
| 84 | root: [ |
| 85 | { name: "src", hash: "src", type: "tree", mode: "40000" }, |
| 86 | { name: "node_modules", hash: "deps", type: "tree", mode: "40000" }, |
| 87 | { name: "package.json", hash: "pkg", type: "blob", mode: "100644" }, |
| 88 | { name: "README.md", hash: "readme", type: "blob", mode: "100644" }, |
| 89 | ], |
| 90 | src: [ |
| 91 | { name: "app.ts", hash: "app", type: "blob", mode: "100644" }, |
| 92 | { name: "env.d.ts", hash: "env", type: "blob", mode: "100644" }, |
| 93 | ], |
| 94 | deps: [{ name: "lib.js", hash: "lib", type: "blob", mode: "100644" }], |
| 95 | }; |
| 96 | const blobs: Record<string, string> = { app: "export function app() {}\n", env: "declare const x: number;", lib: "export function lib() {}", pkg: '{"name":"demo"}', readme: "# demo" }; |
| 97 | return { |
| 98 | readCommit: async (hash: string) => (hash === "c1" ? { hash, treeHash: "root", parents: [], message: "m", author: { name: "A", email: "a@x" }, committer: { name: "A", email: "a@x" }, authoredAt: 1, committedAt: 1 } : null), |
| 99 | readTree: async (hash: string) => trees[hash] ?? null, |
| 100 | readBlob: async (hash: string) => (hash in blobs ? new Blob([blobs[hash]]) : null), |
| 101 | } as unknown as ArtifactsRepo; |
| 102 | } |
| 103 | |
| 104 | test("indexing a commit reads its source files and skips dependencies and declaration files", async () => { |
| 105 | const graph = await indexCommit(fakeRepo(), "c1"); |
| 106 | assert.deepEqual(graph.files, [{ path: "src/app.ts", lines: 2 }]); |
| 107 | assert.deepEqual(graph.symbols.map((symbol) => symbol.id), ["src/app.ts#app"]); |
| 108 | await assert.rejects(indexCommit(fakeRepo(), "missing"), /Commit missing was not found/); |
| 109 | }); |
| 110 | |
| 111 | test("a commit's map is built once, then read back from storage", async () => { |
| 112 | const db = d1(migrated()); |
| 113 | const graph: CodeGraph = { commit: "c1", files: [{ path: "a.ts", lines: 1 }], symbols: [{ id: "a.ts#a", file: "a.ts", name: "a", kind: "function", parent: null, start: 1, end: 1, exported: true, doc: null }], edges: [], skipped: 0 }; |
| 114 | assert.equal(await readCodeMap(db, "r1", "c1", 100), null); |
| 115 | assert.equal(await claimBuild(db, "r1", "c1", 100), true); |
| 116 | assert.equal(await claimBuild(db, "r1", "c1", 101), false); |
| 117 | assert.deepEqual(await readCodeMap(db, "r1", "c1", 102), { kind: "indexing", commit: "c1" }); |
| 118 | await saveCodeMap(db, "r1", graph, 103); |
| 119 | assert.deepEqual(await readCodeMap(db, "r1", "c1", 104), { kind: "ready", graph }); |
| 120 | assert.equal(await claimBuild(db, "r1", "c1", 105), false); |
| 121 | assert.equal(CODE_MAP_VERSION, 1); |
| 122 | }); |
| 123 | |
| 124 | test("a build that never finished is taken over, and an empty commit is stored as empty", async () => { |
| 125 | const db = d1(migrated()); |
| 126 | assert.equal(await claimBuild(db, "r1", "c2", 100), true); |
| 127 | assert.equal(await claimBuild(db, "r1", "c2", 100 + 10 * 60), true); |
| 128 | await saveCodeMap(db, "r1", { commit: "c2", files: [], symbols: [], edges: [], skipped: 0 }, 700); |
| 129 | assert.deepEqual(await readCodeMap(db, "r1", "c2", 701), { kind: "unavailable", commit: "c2", reason: "empty" }); |
| 130 | }); |
| 131 | |
| 132 | test("code changes place added lines and removed lines on the head version", () => { |
| 133 | const changes = { |
| 134 | base: "b", |
| 135 | head: "h", |
| 136 | commits: [], |
| 137 | additions: 2, |
| 138 | deletions: 1, |
| 139 | truncated: false, |
| 140 | files: [ |
| 141 | { path: "a.ts", status: "modified", kind: "text", additions: 2, deletions: 1, before: "one\ntwo\nthree\n", after: "one\nTWO\nthree\nfour\n", truncated: false }, |
| 142 | { path: "img.png", status: "added", kind: "binary", additions: 0, deletions: 0, before: null, after: null, truncated: false }, |
| 143 | ], |
| 144 | } satisfies ChangeSet; |
| 145 | assert.deepEqual(codeChanges(changes), [ |
| 146 | { path: "a.ts", status: "modified", additions: 2, deletions: 1, added: [2, 4], removed: [{ after: 1, text: "two" }] }, |
| 147 | { path: "img.png", status: "added", additions: 0, deletions: 0, added: [], removed: [] }, |
| 148 | ]); |
| 149 | }); |