- 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=0&format=json
170 linesFirst import 100%Last change First import in version 0, 1 version changed it
You're reading version 0. The newest is 318, and 2 versions 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}
84describe("random commits ingest exactly", () => {
85 for (let c = 0; c < CASES; c++) {
86 const seed = BASE_SEED + c;
87 it(`seed ${seed}`, async () => {
88 const rng = new Rng(seed);
89 const start: Files = {};
90 for (let f = 0; f < rng.range(1, 3); f++) start[`f${f}.txt`] = text(randomLines(rng, 2, 8, `base${f}`));
91 const repo = Repo.init(start);
92 // A few atoms from agents first, so the view has history and parallel heads.
93 for (let a = 0; a < rng.range(0, 3); a++) {
94 const view = repo.view();
95 const path = rng.pick(view.paths());
96 const count = view.lines(path).length;
97 const at = rng.int(count + 1);
98 repo.edit({
99 author: "agent",
100 message: `agent ${a}`,
101 edits: [{ file: path, start: at, end: Math.min(count, at + rng.int(2)), lines: [`agent ${a}`] }],
102 });
103 }
105 const dir = freshPath("view");
106 const view = await writeView(repo.graph, repo.shared, dir, { byLabel: rng.chance(0.3), checkout: false });
107 const viewFiles = sorted(project(repo.graph, repo.shared));
108 let files = viewFiles;
109 let head = view.head;
110 const cache = writeCache();
111 const commits: { oid: string; files: Files }[] = [];
112 for (let n = 0; n < rng.range(1, 4); n++) {
113 files = sorted(randomCommit(rng, files, n));
114 head = await commitInto(dir, files, head, n, cache);
115 commits.push({ oid: head, files });
116 }
118 // Live work in the shared set the person never saw.
119 const live = new Set<AtomId>();
120 if (rng.chance(0.5)) {
121 for (let l = 0; l < rng.range(1, 2); l++) {
122 const v = repo.view();
123 const path = rng.pick(v.paths());
124 const at = rng.int(v.lines(path).length);
125 live.add(
126 repo.edit({
127 author: "live",
128 message: `live ${l}`,
129 edits: [{ file: path, start: at, end: at + 1, lines: [`live ${l}`] }],
130 }).id,
131 );
132 }
133 }
135 const result = await ingest(repo.graph, repo.shared, dir);
136 assert.equal(result.commits.length, commits.length);
137 let before = viewFiles;
138 for (const [i, ingested] of result.commits.entries()) {
139 const expected = commits[i]!;
140 assert.equal(ingested.commit, expected.oid);
141 // The label's state is the commit's tree.
142 assert.deepEqual(sorted(project(repo.graph, stateOf(repo.graph, ingested.label))), expected.files);
143 assert.deepEqual(await commitFiles(dir, expected.oid), expected.files);
144 // One atom per diff hunk, all independent of each other.
145 let hunks = 0;
146 for (const path of new Set([...Object.keys(before), ...Object.keys(expected.files)])) {
147 hunks += diffLines(splitLines(before[path] ?? ""), splitLines(expected.files[path] ?? "")).length;
148 }
149 assert.equal(ingested.hunks.length, hunks);
150 for (const a of ingested.hunks)
151 for (const b of ingested.hunks) if (a < b) assert.ok(independent(repo.graph, a, b));
152 before = expected.files;
153 }
155 const ingestedHunks = new Set(result.commits.flatMap((c) => c.hunks));
156 for (const conflict of result.conflicts) {
157 const [a, b] = conflict.atoms;
158 assert.ok((live.has(a) && ingestedHunks.has(b)) || (live.has(b) && ingestedHunks.has(a)), `${a} ${b}`);
159 }
160 if (live.size === 0) {
161 assert.deepEqual(result.conflicts, []);
162 assert.deepEqual(sorted(project(repo.graph, result.shared)), commits.at(-1)!.files);
163 }
165 const again = await ingest(repo.graph, result.shared, dir);
166 assert.equal(again.atoms.length, 0);
167 assert.equal(again.base.kind, "ingested");
168 });
169 }
170});