- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/files-reads.test.ts?version=100&format=json
324 linesFirst import 100% · rename 1%Last change rename in version 100, 2 versions changed it
You're reading version 100. The newest is 318, and no version since then changed this file.
1// Reads for the Files pages (#41, docs/website/stories/files.md): a folder
2// with each entry's last change, a path's life, any spelling, lines followed
3// forward, files as an agent has them, whether drafts would collide, and a
4// file's history across moves.
6import assert from "node:assert/strict";
7import { test } from "node:test";
8import { blame, folderBlame, why } from "../blame.ts";
9import { MemoryStore, Project } from "../project.ts";
11function setup() {
12 const project = new Project(new MemoryStore());
13 project.init(
14 {
15 "README.md": "# game\n",
16 "src/coast/tide.ts": "export const tide = 1;\nexport const wave = 2;\nexport const foam = 3;\n",
17 "src/sky.ts": "export const sky = 1;\n",
18 },
19 0,
20 );
21 for (const a of ["ann", "bob", "cat"]) project.sync(a);
22 return project;
23}
25test("a folder lists its folders and files, each with its last change", () => {
26 const p = setup();
27 p.edit("ann", { file: "src/sky.ts", old: "1", new: "2" }, 10);
28 p.publish("ann", "sky is 2", 11); // v1
29 p.edit("bob", { file: "src/coast/tide.ts", old: "tide = 1", new: "tide = 9" }, 20);
30 p.publish("bob", "high tide", 21); // v2
31 const root = p.tree();
32 assert.deepEqual(
33 root.entries.map((e) => [e.kind, e.name, e.files, e.last?.version, e.last?.agent]),
34 [
35 ["folder", "src", 2, 2, "bob"],
36 ["file", "README.md", undefined, 0, "init"],
37 ],
38 );
39 const src = p.tree("SRC"); // any spelling
40 assert.equal(src.folder, "src");
41 assert.deepEqual(
42 src.entries.map((e) => [e.name, e.last?.version, e.last?.message]),
43 [
44 ["coast", 2, "high tide"],
45 ["sky.ts", 1, "sky is 2"],
46 ],
47 );
48 // At an earlier version, the last change is the one then.
49 assert.deepEqual(
50 p.tree("src", { version: 1 }).entries.map((e) => e.last?.version),
51 [0, 1],
52 );
53 assert.throws(() => p.tree("nope"), /there is no folder nope/);
54 // Kept up as versions land.
55 p.sync("ann");
56 p.edit("ann", { file: "README.md", old: "game", new: "Game" }, 30);
57 p.publish("ann", "capital", 31); // v3
58 assert.equal(p.tree().entries[1]!.last?.version, 3);
59});
61test("a path's life: versions that changed it, the version that made it, and the one that took it out", () => {
62 const p = setup();
63 p.record("ann", { "src/new.ts": "x\n" }, 10);
64 p.publish("ann", "new file", 11); // v1
65 p.sync("bob");
66 p.record("bob", { "src/new.ts": null }, 20);
67 p.publish("bob", "not needed", 21); // v2
68 assert.deepEqual(p.pathLife("src/new.ts", { version: 1 }), {
69 path: "src/new.ts",
70 version: 1,
71 exists: true,
72 versions: 1,
73 last: { version: 1, agent: "ann", time: 11, message: "new file" },
74 made: { version: 1, agent: "ann", time: 11, message: "new file" },
75 });
76 const gone = p.pathLife("SRC/NEW.TS");
77 assert.deepEqual(
78 [gone.path, gone.exists, gone.versions, gone.removed?.version, gone.removed?.agent],
79 ["src/new.ts", false, 2, 2, "bob"],
80 );
81 // Before it existed: the version that makes it next.
82 const before = p.pathLife("src/new.ts", { version: 0 });
83 assert.deepEqual([before.exists, before.next?.version, before.removed], [false, 1, undefined]);
84});
86test("a move shows in both lives: where the file went, and where it came from", () => {
87 const p = setup();
88 p.move("ann", "src/sky.ts", "src/air/sky.ts", 10);
89 p.publish("ann", "sky into air", 11); // v1
90 assert.equal(p.pathLife("src/sky.ts").removed?.to, "src/air/sky.ts");
91 assert.equal(p.pathLife("src/air/sky.ts").made?.from, "src/sky.ts");
92});
94test("reads take any spelling and answer with the stored one", () => {
95 const p = setup();
96 assert.deepEqual(
97 [p.fileAt("Src/Coast/Tide.TS", 0).path, p.fileAt("Src/Coast/Tide.TS", 0).text?.split("\n")[0]],
98 ["src/coast/tide.ts", "export const tide = 1;"],
99 );
100 assert.equal(blame(p, "SRC/SKY.TS").file, "src/sky.ts");
101 assert.equal(why(p, "src/Sky.ts", 1).file, "src/sky.ts");
102 p.edit("ann", { file: "src/sky.ts", old: "1", new: "2" }, 10);
103 p.publish("ann", "sky is 2", 11);
104 const log = p.log({ path: "SRC/sky.ts" });
105 assert.deepEqual([log.path, log.versions.map((v) => v.version)], ["src/sky.ts", [1, 0]]);
106 p.note("bob", { file: "src/sky.ts", from: 1, text: "why 2?" }, 12);
107 assert.equal(p.notes({ file: "Src/Sky.ts" }).notes.length, 1);
108 // A project holding two spellings (one from before #49's checks, loaded from its log) answers each exactly.
109 const old = new MemoryStore();
110 new Project(old).init({ "a.md": "lower\n", "X.md": "upper\n" }, 0);
111 const q = Project.load(JSON.parse(JSON.stringify(old.entries).replaceAll("X.md", "A.md")), new MemoryStore());
112 assert.equal(q.fileAt("A.md", 0).text, "upper\n");
113 assert.equal(q.fileAt("a.md", 0).text, "lower\n");
114 assert.equal(q.fileAt("a.MD", 0).text, null, "two matches: answered exactly, and a.MD itself is not there");
115});
117test("lines followed forward: new numbers after edits above them, into a moved file, and the version that took lines out", () => {
118 const p = setup();
119 p.edit(
120 "ann",
121 { file: "src/coast/tide.ts", old: "export const tide = 1;\n", new: "// tides\nexport const tide = 1;\n" },
122 10,
123 );
124 p.publish("ann", "comment", 11); // v1
125 p.sync("bob");
126 p.edit("bob", { file: "src/coast/tide.ts", old: "export const foam = 3;\n", new: "" }, 20);
127 p.publish("bob", "no foam", 21); // v2
128 const f = p.follow("src/coast/tide.ts", 0, 1, 3);
129 assert.deepEqual(f.lines, [
130 { was: 1, file: "src/coast/tide.ts", now: 2 },
131 { was: 2, file: "src/coast/tide.ts", now: 3 },
132 { was: 3, now: null, removed: 2 },
133 ]);
134 assert.deepEqual(
135 [f.ranges, f.removed, f.at],
136 [[{ file: "src/coast/tide.ts", from: 2, to: 3 }], [{ from: 3, to: 3, version: 2 }], 2],
137 );
138 // Up to a chosen version only.
139 assert.deepEqual(p.follow("src/coast/tide.ts", 0, 3, 3, { at: 1 }).lines, [
140 { was: 3, file: "src/coast/tide.ts", now: 4 },
141 ]);
142 // Into a moved file.
143 p.sync("cat");
144 p.move("cat", "src/coast/tide.ts", "src/sea/tide.ts", 30);
145 p.publish("cat", "coast is sea", 31); // v3
146 assert.deepEqual(p.follow("src/coast/tide.ts", 0, 1, 2).ranges, [{ file: "src/sea/tide.ts", from: 2, to: 3 }]);
147 assert.throws(() => p.follow("src/coast/tide.ts", 0, 0, 2), /ask for from\.\.to within 1-3/);
148 assert.throws(() => p.follow("src/coast/tide.ts", 2, 1, 1, { at: 1 }), /follow goes forward/);
149 assert.throws(() => p.follow("nope.ts", 0, 1, 1), /nope\.ts isn't in shared at version 0/);
150});
152test("a dropped version takes its lines out too, and follow says which drop", () => {
153 const p = setup();
154 p.edit(
155 "ann",
156 { file: "src/sky.ts", old: "export const sky = 1;\n", new: "export const sky = 1;\nexport const cloud = 2;\n" },
157 10,
158 );
159 p.publish("ann", "cloud", 11); // v1
160 p.drop("bob", [1], "no clouds", 20); // v2
161 assert.deepEqual(p.follow("src/sky.ts", 1, 2, 2).removed, [{ from: 2, to: 2, version: 2 }]);
162});
164test("files as an agent has them: its own new files in, and an unknown agent isn't made", () => {
165 const p = setup();
166 p.record("ann", { "notes/plan.md": "plan\n" }, 10);
167 assert.deepEqual(p.pathsOf("ann"), {
168 agent: "ann",
169 version: 0,
170 draft: null,
171 files: ["README.md", "notes/plan.md", "src/coast/tide.ts", "src/sky.ts"],
172 });
173 assert.throws(() => p.pathsOf("nobody"), /there is no agent "nobody"/);
174 assert.ok(!p.status().agents.some((a) => a.agent === "nobody"));
175});
177test("drafts that would collide by the push rule: lines, a definition, and a name defined twice", () => {
178 const p = setup();
179 p.edit("ann", { file: "src/coast/tide.ts", old: "wave = 2", new: "wave = 20" }, 10);
180 p.edit("bob", { file: "src/coast/tide.ts", old: "wave = 2", new: "wave = 22" }, 11);
181 let c = p.collisions();
182 assert.deepEqual(c.agents, ["ann", "bob"]);
183 assert.deepEqual(
184 c.collisions.map((x) => [x.kind, x.file, x.first.agent, x.first.lines, x.second.agent, x.second.lines]),
185 [["lines", "src/coast/tide.ts", "ann", [2, 2], "bob", [2, 2]]],
186 );
187 assert.equal(c.version, 0);
188 // Narrowed to a file or folder; elsewhere, none.
189 assert.equal(p.collisions({ file: "SRC/coast/tide.ts" }).collisions.length, 1);
190 assert.equal(p.collisions({ folder: "src/coast" }).collisions.length, 1);
191 assert.equal(p.collisions({ file: "src/sky.ts" }).collisions.length, 0);
192 // The publish rule agrees: once ann publishes, bob is refused.
193 p.publish("ann", "wave 20", 12);
194 assert.equal(p.publish("bob", "wave 22", 13).ok, false);
196 // A definition: cat changes sky; bob's new line uses it.
197 const q = setup();
198 q.edit("cat", { file: "src/sky.ts", old: "export const sky = 1;", new: 'export const sky = "blue";' }, 10);
199 q.record("bob", { "src/use.ts": "import { sky } from './sky.ts';\nexport const twice = sky * 2;\n" }, 12);
200 c = q.collisions();
201 const def = c.collisions.filter((x) => x.kind === "definition");
202 assert.deepEqual(
203 def.map((x) => [x.name, x.first.agent, x.first.file, x.second.agent, x.second.file]),
204 [["sky", "cat", "src/sky.ts", "bob", "src/use.ts"]],
205 );
207 // Two agents each define the same name at the top of one file.
208 const r = setup();
209 r.edit(
210 "ann",
211 {
212 file: "src/sky.ts",
213 old: "export const sky = 1;\n",
214 new: "export const sky = 1;\nexport function rain() { return 1; }\n",
215 },
216 10,
217 );
218 r.edit(
219 "bob",
220 {
221 file: "src/sky.ts",
222 old: "export const sky = 1;\n",
223 new: "export function rain() { return 2; }\nexport const sky = 1;\n",
224 },
225 11,
226 );
227 const dup = r.collisions().collisions.filter((x) => x.kind === "duplicate");
228 assert.deepEqual(
229 dup.map((x) => [x.name, x.first.agent, x.second.agent]),
230 [["rain", "ann", "bob"]],
231 );
232});
234test("with follow, a file's history goes back through abso mv: versions under its old name come with that name", () => {
235 const p = setup();
236 p.edit("ann", { file: "src/sky.ts", old: "1", new: "2" }, 10);
237 p.publish("ann", "sky is 2", 11); // v1
238 p.sync("bob");
239 p.move("bob", "src/sky.ts", "src/air/sky.ts", 20);
240 p.publish("bob", "sky into air", 21); // v2
241 p.sync("ann");
242 p.edit("ann", { file: "src/air/sky.ts", old: "2", new: "3" }, 30);
243 p.publish("ann", "sky is 3", 31); // v3
244 const log = p.log({ path: "src/air/sky.ts", follow: true });
245 assert.deepEqual(
246 log.versions.map((v) => [v.version, v.path]),
247 [
248 [3, undefined],
249 [2, undefined],
250 [1, "src/sky.ts"],
251 [0, "src/sky.ts"],
252 ],
253 );
254 assert.deepEqual(log.movedFrom, [{ path: "src/sky.ts", version: 2 }]);
255 // Paging still works across the move.
256 assert.deepEqual(
257 p.log({ path: "src/air/sky.ts", follow: true, limit: 2, before: 2 }).versions.map((v) => v.version),
258 [1, 0],
259 );
260 // Without follow, as before: the versions that changed this name only.
261 assert.deepEqual(
262 p.log({ path: "src/air/sky.ts" }).versions.map((v) => [v.version, v.path]),
263 [
264 [3, undefined],
265 [2, undefined],
266 ],
267 );
268 // The old name's own history is unchanged.
269 assert.deepEqual(
270 p.log({ path: "src/sky.ts" }).versions.map((v) => v.version),
271 [2, 1, 0],
272 );
273});
275test("who wrote a folder: its lines counted by author, most first", () => {
276 const p = setup();
277 p.edit(
278 "ann",
279 {
280 file: "src/coast/tide.ts",
281 old: "export const foam = 3;\n",
282 new: "export const foam = 3;\nexport const a = 1;\nexport const b = 2;\n",
283 },
284 10,
285 );
286 p.publish("ann", "two more", 11);
287 assert.deepEqual(folderBlame(p, "src"), {
288 folder: "src",
289 version: 1,
290 files: 2,
291 lines: 6,
292 authors: [
293 { agent: "init", lines: 4 },
294 { agent: "ann", lines: 2 },
295 ],
296 });
297 assert.equal(folderBlame(p, "").lines, 7);
298 assert.equal(folderBlame(p, "Src/Coast").folder, "src/coast");
299 assert.throws(() => folderBlame(p, "nope"), /there is no folder nope/);
300});
302test("why says what the line's change built on: the versions that brought in what it depends on directly, nearest first", () => {
303 const p = setup();
304 p.edit(
305 "ann",
306 { file: "src/sky.ts", old: "export const sky = 1;\n", new: "export const sky = 1;\nexport const cloud = 2;\n" },
307 10,
308 );
309 p.publish("ann", "cloud", 11); // v1
310 p.sync("bob");
311 p.edit(
312 "bob",
313 { file: "src/sky.ts", old: "export const cloud = 2;\n", new: "export const cloud = 2;\nexport const rain = 3;\n" },
314 20,
315 );
316 p.publish("bob", "rain", 21); // v2
317 const w = why(p, "src/sky.ts", 3);
318 assert.deepEqual(
319 w.builtOn,
320 [{ version: 1, agent: "ann", message: "cloud" }],
321 "what it depends on directly: the line it was written after",
322 );
323 assert.deepEqual(why(p, "src/sky.ts", 1).builtOn, [], "the first version built on nothing");
324});