- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/pluck.test.ts?format=json
92 linesFirst import 100%Last change First import in version 0, 1 version changed it
1// Pluck (#27): take some versions of a named draft into shared, checked for
2// collisions like a land. The draft stays as it is; landing it later brings
3// in only what's left.
5import assert from "node:assert/strict";
6import { test } from "node:test";
7import { MemoryStore, Project } from "../project.ts";
9const START = {
10 "a.ts": "export function size(x: string) {\n return x.length;\n}\n",
11 "b.ts": 'import { size } from "./a.ts";\nexport const n = 1;\n',
12};
14function setup() {
15 const store = new MemoryStore();
16 const project = new Project(store);
17 project.init(START, 0);
18 project.sync("lead");
19 project.switchTo("ann", "feature", 1);
20 return { project, store };
21}
23test("pluck one version of a draft; landing the draft later brings the rest", () => {
24 const { project } = setup();
25 project.edit("ann", { file: "a.ts", old: "x.length", new: "x.length + 0" }, 2);
26 project.publish("ann", "Body tweak", 3); // feature v1
27 project.edit("ann", { file: "b.ts", old: "n = 1", new: "n = 2" }, 4);
28 project.publish("ann", "Two", 5); // feature v2
29 const plucked = project.pluck("lead", "feature", [2], "Just the two", 6);
30 assert.deepEqual(plucked, {
31 ok: true,
32 version: 1,
33 changes: 2,
34 plucked: [{ version: 2, agent: "ann", message: "Two" }],
35 });
36 assert.equal(project.files()["b.ts"], 'import { size } from "./a.ts";\nexport const n = 2;\n');
37 assert.equal(project.files()["a.ts"], START["a.ts"]);
38 const landed = project.land("ann", "The rest", 7);
39 assert.equal(landed.ok, true);
40 assert.equal(project.files()["a.ts"], START["a.ts"].replace("x.length", "x.length + 0"));
41});
43test("a version that builds on another is refused with what it needs, unless that comes too", () => {
44 const { project } = setup();
45 project.edit("ann", { file: "a.ts", old: "x.length", new: "x.length * 2" }, 2);
46 project.publish("ann", "Double", 3); // v1
47 project.edit("ann", { file: "a.ts", old: "x.length * 2", new: "x.length * 3" }, 4);
48 project.publish("ann", "Triple", 5); // v2 builds on v1's line
49 assert.deepEqual(project.pluck("lead", "feature", [2], "Triple only", 6), {
50 ok: false,
51 needs: [{ version: 1, agent: "ann", message: "Double" }],
52 collisions: [],
53 });
54 const both = project.pluck("lead", "feature", [2], "Triple", 6, { withDependencies: true });
55 assert.deepEqual(both.ok && both.plucked.map((p) => p.version), [1, 2]);
56 assert.match(project.files()["a.ts"]!, /x\.length \* 3/);
57});
59test("a pluck that collides with newer shared work is refused like a land", () => {
60 const { project } = setup();
61 project.edit("ann", { file: "b.ts", old: "n = 1", new: "n = 2" }, 2);
62 project.publish("ann", "Two", 3);
63 project.edit("lead", { file: "b.ts", old: "n = 1", new: "n = 3" }, 4);
64 project.publish("lead", "Three", 5);
65 const refused = project.pluck("lead", "feature", [1], "Two", 6);
66 assert.equal(refused.ok, false);
67 assert.equal(!refused.ok && refused.collisions[0]!.kind, "lines");
68});
70test("the rest of a draft lands without colliding with its own plucked definition change", () => {
71 const { project } = setup();
72 project.edit("ann", { file: "a.ts", old: "size(x: string)", new: "size(x: string, y = 0)" }, 2);
73 project.publish("ann", "Size takes y", 3); // v1 changes the definition
74 project.edit("ann", { file: "b.ts", old: "n = 1", new: 'n = size("ab", 1)' }, 4);
75 project.publish("ann", "Use size with y", 5); // v2 uses it
76 assert.equal(project.pluck("lead", "feature", [1], "Size takes y", 6).ok, true);
77 assert.equal(project.land("ann", "Use it", 7).ok, true);
78});
80test("bad plucks are plain errors; a log with a pluck loads back the same", () => {
81 const { project, store } = setup();
82 project.edit("ann", { file: "b.ts", old: "n = 1", new: "n = 2" }, 2);
83 project.publish("ann", "Two", 3);
84 assert.throws(() => project.pluck("lead", "nope", [1], "x", 4), /no named draft/);
85 assert.throws(() => project.pluck("lead", "feature", [5], "x", 4), /no version 5/);
86 assert.throws(() => project.pluck("lead", "feature", [1], "", 4), /message/);
87 project.pluck("lead", "feature", [1], "Two", 4);
88 assert.throws(() => project.pluck("lead", "feature", [1], "Again", 5), /nothing that shared doesn't have/);
89 const again = Project.load(store.entries, new MemoryStore());
90 assert.deepEqual(again.files(), project.files());
91 assert.deepEqual(again.status(), project.status());
92});