- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/definitions.test.ts?version=309&format=json
80 linesFirst import 80% · codex-capacity-chair 13% · engine 8%Last change codex-capacity-chair in version 175, 3 versions changed it
You're reading version 309. The newest is 318, and no version since then changed this file.
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { buildBase, Graph, View } from "../../../src/engine/index.ts";
4import { definitionsIn } from "../definitions.ts";
6function defs(files: Record<string, string>): Record<string, string[]> {
7 const graph = new Graph();
8 const a0 = buildBase(files, { author: "t", message: "t" });
9 graph.add(a0);
10 const out: Record<string, string[]> = {};
11 for (const [name, list] of definitionsIn(new View(graph, [a0.id]))) out[name] = list.map((d) => d.text);
12 return out;
13}
15test("function headers stop at the body; blocks of types are whole", () => {
16 const found = defs({
17 "a.ts": [
18 "export function parsePattern(text: string,",
19 " flags: string): Node {",
20 " return compileNode(text);",
21 "}",
22 "export const matchAll = (re: Node, s: string) => {",
23 " return [];",
24 "};",
25 "const MAX_DEPTH = 10;",
26 "export interface Node {",
27 " kind: string;",
28 " kids: Node[];",
29 "}",
30 'type Flag = "g" | "i";',
31 "class Engine {",
32 " async runOnce(input: string): Promise<void> {",
33 " helperCall(input);",
34 " }",
35 "}",
36 ].join("\n"),
37 });
38 assert.deepEqual(found, {
39 parsePattern: ["export function parsePattern(text: string,\n flags: string): Node {"],
40 matchAll: ["export const matchAll = (re: Node, s: string) => {"],
41 MAX_DEPTH: ["const MAX_DEPTH = 10;"],
42 Node: ["export interface Node {\n kind: string;\n kids: Node[];\n}"],
43 Flag: ['type Flag = "g" | "i";'],
44 // Only top-level declarations: the method runOnce is reached through a dot, never imported.
45 Engine: ["class Engine {"],
46 });
47});
49test("Python, Go and Rust shapes; calls are not definitions", () => {
50 const found = defs({
51 "a.py": "def compile_pattern(text,\n flags=0):\n return run_it(text)\nMAX_STEPS = 100\n",
52 "b.go": "func (m *Matcher) MatchString(s string) bool {\n\treturn false\n}\n",
53 "c.rs":
54 "pub fn find_at(hay: &str, at: usize) -> Option<usize> {\n None\n}\npub struct Span {\n start: usize,\n}\n",
55 "d.md": "function notCode(x) {\n",
56 });
57 assert.deepEqual(found, {
58 compile_pattern: ["def compile_pattern(text,\n flags=0):"],
59 MAX_STEPS: ["MAX_STEPS = 100"],
60 MatchString: ["func (m *Matcher) MatchString(s string) bool {"],
61 find_at: ["pub fn find_at(hay: &str, at: usize) -> Option<usize> {"],
62 Span: ["pub struct Span {\n start: usize,\n}"],
63 });
64});
66test("data files hold no definitions: JSON, CSV and lock files are passed over, however code-like a line looks (abso issue 158)", () => {
67 const codeLike = "export function parsePattern(text: string): Node {\n return compileNode(text);\n}\n";
68 assert.deepEqual(defs({ "fixtures/a.json": codeLike, "data/b.csv": codeLike, "c.lock": codeLike }), {});
69 assert.deepEqual(Object.keys(defs({ "src/a.ts": codeLike })), ["parsePattern"]);
70});
72test("definition scanning does not retain the original file line array", (t) => {
73 const graph = new Graph();
74 graph.add(buildBase({ "a.ts": "export function parseThing() { return 1; }\n" }, { author: "t", message: "t" }));
75 const view = new View(graph, ["a0"]);
76 t.mock.method(view, "lines", () => {
77 throw Error("original file line array forbidden");
78 });
79 assert.deepEqual([...definitionsIn(view).keys()], ["parseThing"]);
80});