- MCP
- blame on this site's /mcp
- CLI
- abso blame src/engine/test/helpers.ts --json
- JSON
- /christopher/abso-core/blame/src/engine/test/helpers.ts?format=json
137 linesFirst import 99% · pinch 1%Last change pinch in version 78,
137 lines from 2 changes in 2 versions.
init0Start the project1// Shared test helpers: a seeded PRNG and a small harness around graph + shared set.
3import {
4 type Atom,
5 type AtomId,
6 type AtomInput,
7 buildAtom,
8 buildBase,
9 checkDraft,
10 type DraftReport,
11 Graph,
12 type LineId,
13 View,
14} from "../index.ts";
16/** mulberry32: a tiny seeded PRNG. Never Math.random in tests. */
17export class Rng {
18 #state: number;
19 constructor(seed: number) {
20 this.#state = seed >>> 0;
21 }
22 next(): number {
23 this.#state = (this.#state + 0x6d2b79f5) >>> 0;
24 let t = this.#state;
25 t = Math.imul(t ^ (t >>> 15), t | 1);
26 t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
27 return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
28 }
29 /** Integer in [0, n). */
30 int(n: number): number {
31 return Math.floor(this.next() * n);
32 }
33 /** Integer in [lo, hi]. */
34 range(lo: number, hi: number): number {
35 return lo + this.int(hi - lo + 1);
36 }
37 chance(p: number): boolean {
38 return this.next() < p;
39 }
40 pick<T>(items: readonly T[]): T {
41 if (items.length === 0) throw new Error("pick from empty list");
42 return items[this.int(items.length)]!;
43 }
44 shuffle<T>(items: readonly T[]): T[] {
45 const out = [...items];
46 for (let i = out.length - 1; i > 0; i--) {
47 const j = this.int(i + 1);
48 [out[i], out[j]] = [out[j]!, out[i]!];
49 }
50 return out;
51 }
52}
54/** A graph plus a shared set, the way the CLI will hold them. */
55export class Repo {
56 readonly graph: Graph;
57 shared: Set<AtomId>;
58 #clock = 1_000;
60 constructor(graph: Graph, shared: Iterable<AtomId>) {
61 this.graph = graph;
62 this.shared = new Set(shared);
63 }
65 static init(files: Record<string, string>): Repo {
66 const graph = new Graph();
67 graph.add(buildBase(files, { author: "import", message: "starting snapshot", time: 0 }));
68 return new Repo(graph, ["a0"]);
69 }
71 view(set: Iterable<AtomId> = this.shared): View {
72 return new View(this.graph, set);
73 }
75 /** Build without accepting. */
76 draft(
77 input: Omit<AtomInput, "author" | "message"> & { author?: string; message?: string },
78 against?: Iterable<AtomId>,
79 ): Atom {
80 return buildAtom(this.view(against ?? this.shared), {
81 author: "agent",
82 message: "change",
83 time: this.#clock++,
84 ...input,
85 });
86 }
88 /** Check a draft against the shared set. */
89 check(draft: Atom): DraftReport {
90 return checkDraft(this.graph, this.shared, draft);
91 }
93 /**
94 * Build against `against` (default: the shared set), accept it into the
95 * graph and add it to the shared set.
96 */
97 edit(
98 input: Omit<AtomInput, "author" | "message"> & { author?: string; message?: string },
99 against?: Iterable<AtomId>,
100 ): Atom {
101 const atom = this.draft(input, against);
102 this.graph.add(atom);
103 this.shared.add(atom.id);
104 return atom;
105 }
107 files(set: Iterable<AtomId> = this.shared): Record<string, string> {
108 return this.view(set).files();
109 }
111 text(file: string, set: Iterable<AtomId> = this.shared): string {
112 return this.view(set).text(file);
113 }
115 /** The id of the one visible line with this exact text. */
116 lineId(file: string, text: string, set: Iterable<AtomId> = this.shared): LineId {
117 const hits = this.view(set)
118 .lines(file)
119 .filter((l) => l.text === text);
120 if (hits.length !== 1)
121 throw new Error(`expected one line ${JSON.stringify(text)} in ${file}, found ${hits.length}`);
122 return hits[0]!.id;
123 }
125 /** 0-based visible line number of the one line with this text. */
126 lineNo(file: string, text: string): number {
127 const lines = this.view().lines(file);
128 const i = lines.findIndex((l) => l.text === text);
129 if (i < 0) throw new Error(`no line ${JSON.stringify(text)} in ${file}`);
130 return i;
131 }
132}
134/** Lines of text with a trailing newline. */
135export function lines(...items: string[]): string {
pinch78Biome is silent: about 90 hints cleared. String joins as templates, dotted keys, unused imports, variables and a private method removed, optional chains (one kept boolean with === true), the preset field instead of the deprecated recommended. Two rules off where they don't fit: noExplicitAny in the hook modules, whose engine types come from the host, and noTemplateCurlyInString in tests, which hold source text as data.Compare136 return items.map((s) => `${s}\n`).join("");