- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/gitview/test/random.test.ts?version=151&format=json
171 linesFirst import 97% · pinch 2% · infra-pipeline 1%Last change infra-pipeline in version 93, 3 versions changed it
You're reading version 151. The newest is 318, and no version since then changed this file.
1// Property: random git commits on top of a random view ingest exactly. For
2// every commit, the state its label names projects to the commit's tree; the
3// hunks of one commit are independent atoms; with no live work the shared
4// projection equals the last commit; live atoms only ever conflict with
5// ingested hunks; and ingesting again adds nothing.
6// PROP_SEED=<seed> PROP_CASES=1 node --test src/gitview/test/random.test.ts
8import assert from "node:assert/strict";
9import fs from "node:fs";
10import { after, describe, it } from "node:test";
11import { writeCommit, writeRef } from "isomorphic-git";
12import { type AtomId, diffLines, independent, project, splitLines, stateOf } from "../../engine/index.ts";
13import { Repo, Rng } from "../../engine/test/helpers.ts";
14import { gitContext, ingest, writeCache, writeFilesTree, writeView } from "../index.ts";
15import { cleanup, commitFiles, freshPath, sorted } from "./helpers.ts";
17after(cleanup);
19const BASE_SEED = Number(process.env.PROP_SEED ?? 20261003);
20const CASES = Number(process.env.PROP_CASES ?? 30);
22// A small vocabulary, so files repeat lines and diffs have choices to make.
23const WORDS = ["{", "}", "x", "y", "return x;", "", "let a = 1;", "let b = 2;"];
25type Files = Record<string, string>;
27function randomLines(rng: Rng, min: number, max: number, tag: string): string[] {
28 return Array.from({ length: rng.range(min, max) }, (_, i) => (rng.chance(0.5) ? rng.pick(WORDS) : `${tag} ${i}`));
29}
31const text = (lines: readonly string[]): string => lines.map((l) => `${l}\n`).join("");
33/** One person's commit: a few random line edits, sometimes a new or deleted file. */
34function randomCommit(rng: Rng, files: Files, n: number): Files {
35 const out: Files = { ...files };
36 const ops = rng.range(1, 4);
37 for (let k = 0; k < ops; k++) {
38 const paths = Object.keys(out).sort();
39 const roll = rng.next();
40 if (paths.length === 0 || roll < 0.12) {
41 out[`dir${rng.int(2)}/new${n}_${k}.txt`] = text(randomLines(rng, 1, 4, `c${n}`));
42 continue;
43 }
44 const path = rng.pick(paths);
45 if (roll < 0.18 && paths.length > 1) {
46 delete out[path];
47 continue;
48 }
49 const lines = splitLines(out[path]!);
50 const start = rng.int(lines.length + 1);
51 const end = Math.min(lines.length, start + rng.int(3));
52 const fresh = randomLines(rng, start === end ? 1 : 0, 3, `c${n}.${k}`);
53 lines.splice(start, end - start, ...fresh);
54 if (lines.length === 0) delete out[path];
55 else out[path] = text(lines);
56 }
57 return out;
58}
60/** Write a commit straight into the repo (no working tree), as plain git would. */
61async function commitInto(
62 dir: string,
63 files: Files,
64 parent: string,
65 n: number,
66 cache: ReturnType<typeof writeCache>,
67): Promise<string> {
68 const tree = await writeFilesTree(gitContext(dir), files, cache);
69 const who = {
70 name: n % 2 === 0 ? "Pat" : "Sam",
71 email: "p@example.com",
72 timestamp: 1_700_000_000 + 60 * n,
73 timezoneOffset: 0,
74 };
75 const oid = await writeCommit({
76 fs,
77 dir,
78 commit: { message: `commit ${n}\n`, tree, parent: [parent], author: who, committer: who },
79 });
80 await writeRef({ fs, dir, ref: "refs/heads/main", value: oid, force: true });
81 return oid;
82}
84// The seeds share nothing (each has its own temp folder and cache), so 8 run at once: 81 s one by one, about 11 s so.
85describe("random commits ingest exactly", { concurrency: 8 }, () => {
86 for (let c = 0; c < CASES; c++) {
87 const seed = BASE_SEED + c;
88 it(`seed ${seed}`, async () => {
89 const rng = new Rng(seed);
90 const start: Files = {};
91 for (let f = 0; f < rng.range(1, 3); f++) start[`f${f}.txt`] = text(randomLines(rng, 2, 8, `base${f}`));
92 const repo = Repo.init(start);
93 // A few atoms from agents first, so the view has history and parallel heads.
94 for (let a = 0; a < rng.range(0, 3); a++) {
95 const view = repo.view();
96 const path = rng.pick(view.paths());
97 const count = view.lines(path).length;
98 const at = rng.int(count + 1);
99 repo.edit({
100 author: "agent",
101 message: `agent ${a}`,
102 edits: [{ file: path, start: at, end: Math.min(count, at + rng.int(2)), lines: [`agent ${a}`] }],
103 });
104 }
106 const dir = freshPath("view");
107 const view = await writeView(repo.graph, repo.shared, dir, { byLabel: rng.chance(0.3), checkout: false });
108 const viewFiles = sorted(project(repo.graph, repo.shared));
109 let files = viewFiles;
110 let head = view.head;
111 const cache = writeCache();
112 const commits: { oid: string; files: Files }[] = [];
113 for (let n = 0; n < rng.range(1, 4); n++) {
114 files = sorted(randomCommit(rng, files, n));
115 head = await commitInto(dir, files, head, n, cache);
116 commits.push({ oid: head, files });
117 }
119 // Live work in the shared set the person never saw.
120 const live = new Set<AtomId>();
121 if (rng.chance(0.5)) {
122 for (let l = 0; l < rng.range(1, 2); l++) {
123 const v = repo.view();
124 const path = rng.pick(v.paths());
125 const at = rng.int(v.lines(path).length);
126 live.add(
127 repo.edit({
128 author: "live",
129 message: `live ${l}`,
130 edits: [{ file: path, start: at, end: at + 1, lines: [`live ${l}`] }],
131 }).id,
132 );
133 }
134 }
136 const result = await ingest(repo.graph, repo.shared, dir);
137 assert.equal(result.commits.length, commits.length);
138 let before = viewFiles;
139 for (const [i, ingested] of result.commits.entries()) {
140 const expected = commits[i]!;
141 assert.equal(ingested.commit, expected.oid);
142 // The label's state is the commit's tree.
143 assert.deepEqual(sorted(project(repo.graph, stateOf(repo.graph, ingested.label))), expected.files);
144 assert.deepEqual(await commitFiles(dir, expected.oid), expected.files);
145 // One atom per diff hunk, all independent of each other.
146 let hunks = 0;
147 for (const path of new Set([...Object.keys(before), ...Object.keys(expected.files)])) {
148 hunks += diffLines(splitLines(before[path] ?? ""), splitLines(expected.files[path] ?? "")).length;
149 }
150 assert.equal(ingested.hunks.length, hunks);
151 for (const a of ingested.hunks)
152 for (const b of ingested.hunks) if (a < b) assert.ok(independent(repo.graph, a, b));
153 before = expected.files;
154 }
156 const ingestedHunks = new Set(result.commits.flatMap((c) => c.hunks));
157 for (const conflict of result.conflicts) {
158 const [a, b] = conflict.atoms;
159 assert.ok((live.has(a) && ingestedHunks.has(b)) || (live.has(b) && ingestedHunks.has(a)), `${a} ${b}`);
160 }
161 if (live.size === 0) {
162 assert.deepEqual(result.conflicts, []);
163 assert.deepEqual(sorted(project(repo.graph, result.shared)), commits.at(-1)!.files);
164 }
166 const again = await ingest(repo.graph, result.shared, dir);
167 assert.equal(again.atoms.length, 0);
168 assert.equal(again.base.kind, "ingested");
169 });
170 }
171});