- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/named-drafts.test.ts?version=295&format=json
212 linesFirst import 99% · rename 1%Last change rename in version 100, 2 versions changed it
You're reading version 295. The newest is 318, and no version since then changed this file.
1// Named drafts (#10): a small shared that builds on a version of shared. Its
2// agents publish into it under the same rules, and it lands in shared as one
3// unit. Shared's other agents don't see any of it until then.
5import assert from "node:assert/strict";
6import { test } from "node:test";
7import { unresolvedConflicts } from "../../../src/engine/index.ts";
8import { MemoryStore, Project, ProjectError } from "../project.ts";
10const START = {
11 "core.ts": "export function step(x: number) {\n return x + 1;\n}\n\nexport const version = 1;\n",
12 "notes.md": "one\ntwo\nthree\n",
13};
15function setup() {
16 const store = new MemoryStore();
17 const project = new Project(store);
18 project.init(START, 0);
19 for (const a of ["ann", "bob", "cat"]) project.sync(a);
20 return { project, store };
21}
23test("two agents work in a named draft; shared sees nothing until it lands", () => {
24 const { project } = setup();
25 assert.deepEqual(project.switchTo("ann", "rewrite", 1), { version: 0, files: {}, collisions: [], draft: "rewrite" });
26 project.switchTo("bob", "rewrite", 1);
28 project.edit("ann", { file: "core.ts", old: " return x + 1;", new: " return x * 2;" }, 2);
29 assert.deepEqual(project.publish("ann", "Double instead", 3), { ok: true, version: 1, changes: 1 });
30 assert.equal(project.version, 0, "shared did not move");
32 // bob gets ann's change by syncing inside the draft; cat in shared gets nothing.
33 assert.deepEqual(project.sync("bob").files, {
34 "core.ts": "export function step(x: number) {\n return x * 2;\n}\n\nexport const version = 1;\n",
35 });
36 assert.deepEqual(project.sync("cat").files, {});
38 project.edit("bob", { file: "notes.md", old: "two\n", new: "two (rewrite)\n" }, 4);
39 project.publish("bob", "Note it", 5);
40 const status = project.status();
41 assert.deepEqual(status.drafts, [{ name: "rewrite", version: 2, sharedBase: 0, changes: 4, agents: ["ann", "bob"] }]);
43 // Landing puts all of it in shared at once.
44 assert.deepEqual(project.land("ann", "Land the rewrite", 6), { ok: true, version: 1, changes: 4 });
45 const cat = project.sync("cat");
46 assert.deepEqual(cat.files, {
47 "core.ts": "export function step(x: number) {\n return x * 2;\n}\n\nexport const version = 1;\n",
48 "notes.md": "one\ntwo (rewrite)\nthree\n",
49 });
50 assert.equal(project.land("ann", "Again", 7).ok && project.status().version, 1, "nothing left to land");
51});
53test("landing is refused when shared changed the same lines, and settled inside the draft", () => {
54 const { project } = setup();
55 project.switchTo("ann", "exp", 1);
56 project.edit("ann", { file: "notes.md", old: "two\n", new: "two (exp)\n" }, 2);
57 project.publish("ann", "Experiment", 3);
59 project.edit("cat", { file: "notes.md", old: "two\n", new: "two (cat)\n" }, 4);
60 project.publish("cat", "Cat edits two", 5);
62 const refused = project.land("ann", "Land exp", 6);
63 assert.equal(refused.ok, false);
64 const c = !refused.ok ? refused.collisions[0]! : undefined;
65 assert.equal(c?.kind, "lines");
66 assert.equal(c?.theirs.agent, "cat");
67 assert.equal(c?.yours.text, "two (exp)\n");
69 // Move the draft onto shared's newest version; ann syncs and sees both lines.
70 assert.deepEqual(project.syncDraft("ann", 7), { draft: "exp", version: 2, sharedBase: 1 });
71 const synced = project.sync("ann");
72 assert.equal(synced.collisions.length, 1);
73 assert.match(synced.files["notes.md"]!, /two \(exp\)\n/);
74 assert.match(synced.files["notes.md"]!, /two \(cat\)\n/);
76 // ann settles it inside the draft, publishes there, and the draft lands.
77 const folder = synced.files["notes.md"]!;
78 project.record("ann", { "notes.md": folder.replace(/two \(.*\)\n.*two \(.*\)\n/s, "two (exp and cat)\n") }, 8);
79 assert.equal(project.publish("ann", "Settle two", 9).ok, true);
80 assert.deepEqual(project.land("ann", "Land exp", 10), { ok: true, version: 2, changes: 4 });
81 assert.equal(project.files()["notes.md"], "one\ntwo (exp and cat)\nthree\n");
82 const shared = new Set(
83 project.graph
84 .atoms()
85 .map((a) => a.id)
86 .filter((id) => project.versionOf(id) !== undefined),
87 );
88 assert.deepEqual(unresolvedConflicts(project.graph, shared), []);
89});
91test("the changed-definition rule applies to landing too", () => {
92 const { project } = setup();
93 project.switchTo("ann", "exp", 1);
94 project.edit("ann", { file: "core.ts", old: "export const version = 1;", new: "export const version = step(1);" }, 2);
95 project.publish("ann", "Use step", 3);
96 project.edit("cat", { file: "core.ts", old: "step(x: number)", new: "step(x: number, by = 1)" }, 4);
97 project.publish("cat", "step takes a size", 5);
98 const refused = project.land("ann", "Land", 6);
99 assert.equal(refused.ok, false);
100 assert.equal(!refused.ok && refused.collisions[0]!.kind, "definition");
101 project.syncDraft("ann", 7);
102 assert.equal(project.land("ann", "Land", 8).ok, true);
103});
105test("switching parks unpushed work in the space you leave, and switching back restores it", () => {
106 const { project } = setup();
107 project.edit("ann", { file: "notes.md", old: "one\n", new: "ONE (shared work)\n" }, 1);
109 // Plain switch: the shared work is parked, the folder becomes the new draft's view.
110 const moved = project.switchTo("ann", "side", 2);
111 assert.deepEqual(moved, { version: 0, files: { "notes.md": "one\ntwo\nthree\n" }, collisions: [], draft: "side" });
112 assert.deepEqual(project.status().agents[0]!.parked, [{ draft: null, draftChanges: 1 }]);
113 assert.equal(project.status().agents[0]!.draftChanges, 0);
115 // Work in the draft, unpublished, then go back: the side work parks, the shared work returns.
116 project.edit("ann", { file: "notes.md", old: "three\n", new: "THREE (side work)\n" }, 3);
117 const back = project.switchTo("ann", null, 4);
118 assert.deepEqual(back.files, { "notes.md": "ONE (shared work)\ntwo\nthree\n" });
119 assert.deepEqual(project.status().agents[0]!.parked, [{ draft: "side", draftChanges: 1 }]);
120 assert.equal(project.status().agents[0]!.draftChanges, 1);
122 // And over again: nothing was lost on either side.
123 assert.deepEqual(project.switchTo("ann", "side", 5).files, { "notes.md": "one\ntwo\nTHREE (side work)\n" });
124 assert.equal(project.publish("ann", "Side work", 6).ok, true);
125 assert.deepEqual(project.switchTo("ann", null, 7).files, { "notes.md": "ONE (shared work)\ntwo\nthree\n" });
126 assert.equal(project.publish("ann", "Shared work", 8).ok, true);
127 assert.equal(project.files()["notes.md"], "ONE (shared work)\ntwo\nthree\n");
128 // Switching to where you already are changes nothing.
129 assert.deepEqual(project.switchTo("ann", null, 9).files, {});
130});
132test("carry takes unpushed changes into a new named draft, and only a new one", () => {
133 const { project } = setup();
134 project.edit("ann", { file: "notes.md", old: "one\n", new: "ONE\n" }, 1);
135 const moved = project.switchTo("ann", "idea", 2, { carry: true });
136 assert.deepEqual(moved.files, {}, "the folder already has the change");
137 assert.equal(project.status().agents[0]!.draftChanges, 1);
138 assert.deepEqual(project.status().agents[0]!.parked, []);
139 project.publish("ann", "Idea", 3);
140 assert.equal(project.version, 0, "it went into the draft, not shared");
141 assert.throws(
142 () => project.switchTo("ann", "idea", 4, { carry: true }),
143 (e) => e instanceof ProjectError && e.code === "bad-request",
144 );
146 // Carrying out of a named draft is refused when the changes build on that draft's own work.
147 project.edit("ann", { file: "notes.md", old: "ONE\n", new: "ONE!\n" }, 5);
148 assert.throws(
149 () => project.switchTo("ann", "idea2", 6, { carry: true }),
150 (e) => e instanceof ProjectError && e.code === "bad-request",
151 );
152 // A plain switch is always fine: the change stays parked in idea.
153 assert.deepEqual(project.switchTo("ann", null, 7).files, { "notes.md": "one\ntwo\nthree\n" });
154 assert.deepEqual(project.status().agents[0]!.parked, [{ draft: "idea", draftChanges: 1 }]);
155});
157test("a log written before parking still loads: its switches carried", () => {
158 const store = new MemoryStore();
159 const project = new Project(store);
160 project.init(START, 0);
161 project.sync("ann");
162 project.edit("ann", { file: "notes.md", old: "one\n", new: "ONE\n" }, 1);
163 project.switchTo("ann", "old", 2, { carry: true });
164 const legacy = store.entries.map((e) =>
165 e.type === "switch" ? { type: e.type, agent: e.agent, space: e.space, version: e.version } : e,
166 );
167 const again = Project.load(legacy as typeof store.entries, new MemoryStore());
168 assert.deepEqual(again.status(), project.status());
169});
171test("news reaches agents in the same space only", () => {
172 const { project } = setup();
173 project.switchTo("ann", "exp", 1);
174 project.switchTo("bob", "exp", 1);
175 project.edit("bob", { file: "notes.md", old: "three\n", new: "three (bob)\n" }, 2);
176 project.edit("cat", { file: "notes.md", old: "three\n", new: "three (cat)\n" }, 2);
177 project.edit("ann", { file: "notes.md", old: "three\n", new: "three (ann)\n" }, 2);
178 project.publish("ann", "Ann in exp", 3);
179 const bob = project.record("bob", {}, 4);
180 const cat = project.record("cat", {}, 4);
181 assert.equal(bob.news.length, 1);
182 assert.match(bob.news[0]!, /\(draft exp, version 1\)/);
183 assert.deepEqual(cat.news, []);
184});
186test("a reload keeps named drafts", () => {
187 const { project, store } = setup();
188 project.switchTo("ann", "exp", 1);
189 project.edit("ann", { file: "notes.md", old: "one\n", new: "uno\n" }, 2);
190 project.publish("ann", "Uno", 3);
191 project.syncDraft("ann", 4);
192 const again = Project.load(store.entries, new MemoryStore());
193 assert.deepEqual(again.status(), project.status());
194 assert.equal(again.land("ann", "Land", 5).ok, true);
195 assert.equal(again.files()["notes.md"], "uno\ntwo\nthree\n");
196});
198test("draft calls need a named draft and a good name", () => {
199 const { project } = setup();
200 assert.throws(
201 () => project.land("ann", "x", 1),
202 (e) => e instanceof ProjectError && e.code === "bad-request",
203 );
204 assert.throws(
205 () => project.syncDraft("ann", 1),
206 (e) => e instanceof ProjectError && e.code === "bad-request",
207 );
208 assert.throws(
209 () => project.switchTo("ann", "bad name", 1),
210 (e) => e instanceof ProjectError && e.code === "bad-request",
211 );
212});