- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/history.test.ts?version=310&format=json
193 linesFirst import 98% · rename 2%Last change rename in version 100, 2 versions changed it
You're reading version 310. The newest is 318, and no version since then changed this file.
1// Everyday commands git has (#27): log, show, diff of the draft, and
2// restore (discard one file's unpublished changes).
4import assert from "node:assert/strict";
5import { test } from "node:test";
6import { MemoryStore, Project, ProjectError } from "../project.ts";
8const START = { "a.txt": "one\ntwo\nthree\n", "b.txt": "x\n" };
10function setup() {
11 const store = new MemoryStore();
12 const project = new Project(store);
13 project.init(START, 0);
14 project.sync("ann");
15 project.sync("bob");
16 return { project, store };
17}
19test("log lists versions newest first, and show gives one with its files before and after", () => {
20 const { project } = setup();
21 project.edit("ann", { file: "a.txt", old: "two\n", new: "TWO\n" }, 1);
22 project.publish("ann", "Shout two", 2);
23 project.edit("bob", { file: "c.txt", content: "new\n" }, 3);
24 project.publish("bob", "Add c", 4);
26 const log = project.log();
27 assert.equal(log.version, 2);
28 assert.deepEqual(
29 log.versions.map((v) => [v.version, v.agent, v.message, v.changes, v.files]),
30 [
31 [2, "bob", "Add c", 1, ["c.txt"]],
32 [1, "ann", "Shout two", 1, ["a.txt"]],
33 [0, "init", "Start the project", 1, ["a.txt", "b.txt"]],
34 ],
35 );
36 assert.equal(project.log({ limit: 1 }).versions.length, 1);
38 const shown = project.show(1);
39 assert.equal(shown.message, "Shout two");
40 assert.deepEqual(shown.diff, [{ path: "a.txt", before: "one\ntwo\nthree\n", after: "one\nTWO\nthree\n" }]);
41 assert.deepEqual(project.show(2).diff, [{ path: "c.txt", before: null, after: "new\n" }]);
42 assert.deepEqual(
43 project.show(0).diff.map((f) => f.before),
44 [null, null],
45 );
46 assert.throws(
47 () => project.show(9),
48 (e) => e instanceof ProjectError && e.code === "bad-request",
49 );
50});
52test("log and show work for a named draft", () => {
53 const { project } = setup();
54 project.switchTo("ann", "exp", 1);
55 project.edit("ann", { file: "b.txt", old: "x", new: "y" }, 2);
56 project.publish("ann", "Y", 3);
57 const log = project.log({ draft: "exp" });
58 assert.deepEqual(
59 log.versions.map((v) => v.message),
60 ["Y", "Start draft exp"],
61 );
62 assert.deepEqual(project.show(1, { draft: "exp" }).diff, [{ path: "b.txt", before: "x\n", after: "y\n" }]);
63 assert.throws(
64 () => project.log({ draft: "nope" }),
65 (e) => e instanceof ProjectError,
66 );
67});
69test("diff shows what the draft changes, file by file", () => {
70 const { project } = setup();
71 assert.deepEqual(project.diff("ann").files, []);
72 project.edit("ann", { file: "a.txt", old: "one\n", new: "uno\n" }, 1);
73 project.edit("ann", { file: "b.txt", content: "x\ny\n" }, 2);
74 project.edit("ann", { file: "b.txt", content: "x\n" }, 3); // back as it was: no change left
75 assert.deepEqual(project.diff("ann"), {
76 draft: null,
77 base: 0,
78 files: [{ path: "a.txt", before: "one\ntwo\nthree\n", after: "uno\ntwo\nthree\n" }],
79 });
80});
82test("restore drops one file’s unpushed changes and keeps the rest", () => {
83 const { project } = setup();
84 project.edit("ann", { file: "a.txt", old: "one\n", new: "uno\n" }, 1);
85 project.edit("ann", { file: "a.txt", old: "three\n", new: "tres\n" }, 2);
86 project.edit("ann", { file: "b.txt", old: "x\n", new: "xx\n" }, 3);
87 const answer = project.restore("ann", ["a.txt"], 4);
88 assert.deepEqual(answer, { files: { "a.txt": "one\ntwo\nthree\n" }, dropped: 2 });
89 assert.deepEqual(project.status().agents[0]!.draftChanges, 1);
90 assert.deepEqual(
91 project.diff("ann").files.map((f) => f.path),
92 ["b.txt"],
93 );
94 // A file the draft never touched comes back unchanged and isn't listed.
95 assert.deepEqual(project.restore("ann", ["nothing.txt"], 5), { files: {}, dropped: 0 });
96 // Restoring everything empties the draft.
97 assert.deepEqual(project.restore("ann", null, 6), { files: { "b.txt": "x\n" }, dropped: 1 });
98 assert.equal(project.status().agents[0]!.draftChanges, 0);
99});
101test("restoring one of two recorded files keeps the other, and reloads the same", () => {
102 const { project, store } = setup();
103 project.record("ann", { "a.txt": "ONE\ntwo\nthree\n", "b.txt": "X\n" }, 1);
104 const answer = project.restore("ann", ["a.txt"], 2);
105 assert.deepEqual(answer, { files: { "a.txt": "one\ntwo\nthree\n" }, dropped: 1 });
106 assert.deepEqual(
107 project.diff("ann").files.map((f) => f.path),
108 ["b.txt"],
109 );
110 project.publish("ann", "Only b", 3);
111 assert.deepEqual(project.files(), { "a.txt": "one\ntwo\nthree\n", "b.txt": "X\n" });
112 assert.deepEqual(Project.load(store.entries, new MemoryStore()).status(), project.status());
113});
115test("a restored change no longer collides", () => {
116 const { project } = setup();
117 project.edit("ann", { file: "a.txt", old: "two\n", new: "two (ann)\n" }, 1);
118 project.edit("bob", { file: "a.txt", old: "two\n", new: "two (bob)\n" }, 1);
119 project.publish("bob", "bob", 2);
120 assert.equal(project.publish("ann", "ann", 3).ok, false);
121 project.restore("ann", ["a.txt"], 4);
122 assert.deepEqual(project.publish("ann", "nothing", 5), { ok: true, version: 1, changes: 0 });
123});
125test("one file's history, a file at a version, and a diff between versions", () => {
126 const { project } = setup();
127 project.edit("ann", { file: "a.txt", old: "one\n", new: "uno\n" }, 1);
128 project.publish("ann", "a once", 2);
129 project.edit("bob", { file: "b.txt", old: "x\n", new: "y\n" }, 3);
130 project.publish("bob", "b", 4);
131 project.sync("ann");
132 project.edit("ann", { file: "a.txt", old: "three\n", new: "tres\n" }, 5);
133 project.publish("ann", "a twice", 6);
135 assert.deepEqual(
136 project.log({ path: "a.txt" }).versions.map((v) => v.version),
137 [3, 1, 0],
138 );
139 assert.deepEqual(project.fileAt("a.txt", 1), { path: "a.txt", version: 1, text: "uno\ntwo\nthree\n" });
140 assert.deepEqual(project.fileAt("c.txt", 1).text, null);
141 assert.deepEqual(project.compare(1, 3).files, [
142 { path: "a.txt", before: "uno\ntwo\nthree\n", after: "uno\ntwo\ntres\n" },
143 { path: "b.txt", before: "x\n", after: "y\n" },
144 ]);
145 assert.deepEqual(
146 project.compare(3, 1).files.map((f) => f.after),
147 ["uno\ntwo\nthree\n", "x\n"],
148 );
149 assert.equal(project.viewAt(0).text("a.txt"), "one\ntwo\nthree\n");
150 assert.throws(
151 () => project.fileAt("a.txt", 9),
152 (e) => e instanceof ProjectError,
153 );
154});
156test("amend corrects the message of the agent's newest push; the log keeps the original", () => {
157 const store = new MemoryStore();
158 const project = new Project(store);
159 project.init({ "a.txt": "a\n" }, 0);
160 assert.throws(() => project.amend("ann", "x", 1), /nothing to amend/);
161 project.edit("ann", { file: "a.txt", old: "a", new: "b" }, 1);
162 project.publish("ann", "Fix typo in paser", 2);
163 project.sync("bob");
164 project.edit("bob", { file: "a.txt", old: "b", new: "c" }, 3);
165 project.publish("bob", "C", 4);
166 // ann's newest publish is version 1, even with bob's after it.
167 assert.deepEqual(project.amend("ann", "Fix typo in parser", 5), {
168 version: 1,
169 draft: null,
170 message: "Fix typo in parser",
171 was: "Fix typo in paser",
172 });
173 const v1 = project.log().versions.find((v) => v.version === 1)!;
174 assert.deepEqual([v1.message, v1.originalMessage], ["Fix typo in parser", "Fix typo in paser"]);
175 assert.equal(project.files()["a.txt"], "c\n");
176 assert.deepEqual(Project.load(store.entries, new MemoryStore()).log(), project.log());
177});
179test("the log lists a push's net changes: a file made and removed before the push is not one (#37)", () => {
180 const project = new Project(new MemoryStore());
181 project.init({ "a.txt": "a\n" }, 0);
182 project.record("b5", { "src/clock.ts": "clock\n", "src/rng.ts": "rng\n", "notes/builder5.md": "notes\n" }, 1);
183 project.record("b5", { "src/clock.ts": null, "src/rng.ts": null }, 2);
184 project.publish("b5", "Notes only", 3);
185 const v1 = project.log().versions.find((v) => v.version === 1)!;
186 assert.deepEqual(v1.files, ["notes/builder5.md"]);
187 assert.deepEqual(
188 project.show(1).diff.map((f) => f.path),
189 ["notes/builder5.md"],
190 );
191 assert.deepEqual(project.log({ path: "src/clock.ts" }).versions, []);
192 assert.deepEqual(project.log().versions.find((v) => v.version === 0)!.files, ["a.txt"]);
193});