- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/known-paths.test.ts?format=json
124 linescapacity 100%Last change capacity in version 303, 2 versions changed it
1// Whether a file exists, answered without laying it out (abso Capacity, View.knowPaths): a published version's view
2// on a writer answers from the kept path list, and a draft's view asks its base for every file the draft doesn't
3// touch. Each must say what the in-memory project says, at every version and in every draft: files deleted or
4// renamed by a published version, and by a draft, by record and by moving lines.
6import assert from "node:assert/strict";
7import { test } from "node:test";
8import { storedArray } from "../../../src/engine/state-source.ts";
9import { SqlState } from "../../../src/engine/test/storage-helper.ts";
10import { MemoryStore, Project } from "../project.ts";
11import type { Entry, ProjectStore } from "../project-types.ts";
13function writer() {
14 const storage = new SqlState();
15 const entries = storedArray<Entry>(storage, "canonical-log");
16 const store: ProjectStore = {
17 append(batch) {
18 for (const entry of batch) entries.push(entry);
19 },
20 };
21 return new Project(store, { writerStorage: storage });
22}
24const AREA = "export function area(r: number) {\n return 3.14 * r * r;\n}\n";
25const FILES = {
26 "src/shapes.ts": `// shapes\n${AREA}`,
27 "src/old-name.ts": "export const x = 1;\n",
28 "src/gone.ts": "export const y = 2;\n",
29 "src/keep.ts": 'import { x } from "./old-name";\n',
30 "notes.md": "notes\n",
31};
33/** Every name to ask about: each path any version or draft has had, and names that never existed. */
34const NAMES = [
35 ...Object.keys(FILES),
36 "src/new-name.ts",
37 "src/geometry.ts",
38 "src/draft-new.ts",
39 "src/draft-renamed.ts",
40 "src/never.ts",
41 "src/old-name",
42 "src/old-name.js",
43];
45function history(project: Project) {
46 let t = 0;
47 project.init(FILES, t);
48 project.sync("v0");
49 // Version 1: ann deletes a file and renames another with record.
50 project.record(
51 "ann",
52 { "src/gone.ts": null, "src/old-name.ts": null, "src/new-name.ts": FILES["src/old-name.ts"] },
53 ++t,
54 );
55 assert.equal(project.publish("ann", "Delete gone, rename old-name", ++t).ok, true);
56 project.sync("v1");
57 // Version 2: ann moves area() into a new file by moving its lines.
58 project.edit("ann", { file: "src/shapes.ts", old: AREA, new: "" }, ++t);
59 project.edit("ann", { file: "src/geometry.ts", content: AREA }, ++t);
60 assert.equal(project.publish("ann", "Move area", ++t).ok, true);
61 for (const agent of ["v2", "bob", "cy", "dee", "eve"]) project.sync(agent);
62 // Drafts over version 2: bob deletes a file, cy renames one with record, dee moves lines into a new file, eve adds one.
63 project.record("bob", { "src/keep.ts": null }, ++t);
64 project.record("cy", { "src/new-name.ts": null, "src/draft-renamed.ts": FILES["src/old-name.ts"] }, ++t);
65 project.edit("dee", { file: "src/geometry.ts", old: AREA, new: "" }, ++t);
66 project.edit("dee", { file: "src/draft-new.ts", content: AREA }, ++t);
67 project.record("eve", { "src/draft-new.ts": "new\n" }, ++t);
68}
70const exists = (project: Project, agent: string) => {
71 const view = project.draftOf(agent)!.view;
72 return Object.fromEntries(NAMES.map((name) => [name, view.hasFile(name)]));
73};
75test("whether each file exists, at every version and in every draft, as the in-memory project says", () => {
76 const oracle = new Project(new MemoryStore());
77 const project = writer();
78 history(oracle);
79 history(project);
80 for (const agent of ["v0", "v1", "v2", "bob", "cy", "dee", "eve"]) {
81 // Before any layout, and again after every file has been read.
82 assert.deepEqual(exists(project, agent), exists(oracle, agent), agent);
83 const view = project.draftOf(agent)!.view;
84 for (const path of view.paths()) view.lines(path);
85 assert.deepEqual(exists(project, agent), exists(oracle, agent), `${agent} after reading`);
86 assert.deepEqual(view.paths(), oracle.draftOf(agent)!.view.paths(), `${agent} paths`);
87 }
88 // The cases the test is for, so a history change can't empty it.
89 const at = (agent: string) => exists(oracle, agent);
90 assert.equal(at("v0")["src/gone.ts"], true);
91 assert.equal(at("v1")["src/gone.ts"], false);
92 assert.equal(at("v1")["src/old-name.ts"], false);
93 assert.equal(at("v1")["src/new-name.ts"], true);
94 assert.equal(at("v2")["src/geometry.ts"], true);
95 assert.equal(at("bob")["src/keep.ts"], false);
96 assert.equal(at("cy")["src/new-name.ts"], false);
97 assert.equal(at("cy")["src/draft-renamed.ts"], true);
98 assert.equal(at("dee")["src/draft-new.ts"], true);
99 assert.equal(at("dee")["src/geometry.ts"], false);
100 assert.equal(at("eve")["src/draft-new.ts"], true);
101 for (const [version, agent] of [
102 [0, "v0"],
103 [1, "v1"],
104 [2, "v2"],
105 ] as const)
106 assert.deepEqual(project.pathsAt(version).files, oracle.draftOf(agent)!.view.paths(), `paths at ${version}`);
107});
109test("an agent's files on a writer, from its base version's kept list when it has nothing of its own, as the in-memory project says", () => {
110 // abso - Performance (abso 711): a status for an agent behind shared listed its files by laying out every known file.
111 const oracle = new Project(new MemoryStore());
112 const project = writer();
113 history(oracle);
114 history(project);
115 // v0 and v1 are behind shared with nothing of their own, v2 is at shared, and bob, cy, dee and eve have drafts.
116 for (const agent of ["v0", "v1", "v2", "bob", "cy", "dee", "eve"]) {
117 assert.deepEqual(project.agentPaths(agent), oracle.agentPaths(agent), agent);
118 assert.deepEqual(project.pathsOf(agent), oracle.pathsOf(agent), `${agent} for a reader`);
119 }
120 assert.deepEqual(project.agentPaths("v0").files, project.pathsAt(0).files);
121 assert.equal(project.agentPaths("v0").files.includes("src/gone.ts"), true);
122 assert.equal(project.agentPaths("v1").files.includes("src/gone.ts"), false);
123 assert.equal(project.agentPaths("eve").files.includes("src/draft-new.ts"), true);
124});