- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/changed-definition.test.ts?format=json
308 linesFirst import 99% · rename 1%Last change rename in version 100, 2 versions changed it
1// The changed-definition rule (#11): publishing is refused when the draft's
2// new lines use a name whose definition changed in work published after the
3// agent's base. Git merges these without a word.
5import assert from "node:assert/strict";
6import { test } from "node:test";
7import { MemoryStore, Project } from "../project.ts";
9const START = {
10 "src/parse.ts": [
11 "export function parsePattern(text: string): Node {",
12 ' return { kind: "lit", text };',
13 "}",
14 "",
15 "export interface Node {",
16 " kind: string;",
17 " text: string;",
18 "}",
19 ].join("\n"),
20 "src/match.ts": [
21 'import { parsePattern } from "./parse.ts";',
22 "",
23 "export function run(p: string) {",
24 " return p;",
25 "}",
26 ].join("\n"),
27 "README.md": "Uses parsePattern.\n",
28};
30function setup() {
31 const project = new Project(new MemoryStore());
32 project.init(START, 0);
33 project.sync("lib");
34 project.sync("user");
35 return project;
36}
38/** user adds a call to parsePattern in match.ts. */
39function userCalls(project: Project) {
40 project.edit(
41 "user",
42 { file: "src/match.ts", old: " return p;\n", new: " const tree = parsePattern(p);\n return tree;\n" },
43 1,
44 );
45}
47test("a signature change pushed first refuses a draft that calls it", () => {
48 const project = setup();
49 userCalls(project);
50 project.edit(
51 "lib",
52 {
53 file: "src/parse.ts",
54 old: "parsePattern(text: string): Node",
55 new: "parsePattern(text: string, flags: string): Node",
56 },
57 2,
58 );
59 assert.equal(project.publish("lib", "parsePattern takes flags", 3).ok, true);
61 const refused = project.publish("user", "run parses", 4);
62 assert.equal(refused.ok, false);
63 assert.deepEqual(!refused.ok && refused.collisions, [
64 {
65 kind: "definition",
66 name: "parsePattern",
67 file: "src/match.ts",
68 yours: { lines: [4, 4], text: " const tree = parsePattern(p);\n" },
69 theirs: {
70 file: "src/parse.ts",
71 lines: [1, 1],
72 text: "export function parsePattern(text: string, flags: string): Node {\n",
73 agent: "lib",
74 message: "parsePattern takes flags",
75 version: 1,
76 },
77 },
78 ]);
80 // After a sync the agent has seen the new definition, so publishing goes through.
81 project.sync("user");
82 assert.equal(project.publish("user", "run parses", 5).ok, true);
83});
85test("the drafting agent hears about it with its next edit", () => {
86 const project = setup();
87 userCalls(project);
88 project.edit(
89 "lib",
90 { file: "src/parse.ts", old: "parsePattern(text: string)", new: "parsePattern(source: string)" },
91 2,
92 );
93 project.publish("lib", "Rename the parameter", 3);
94 const answer = project.edit("user", { file: "src/match.ts", old: "return tree;", new: "return tree ;" }, 4);
95 assert.deepEqual(answer.ok && answer.news, [
96 'lib pushed "Rename the parameter" (version 1), which changes the definition of parsePattern (src/parse.ts line 1), ' +
97 "and your draft uses parsePattern in src/match.ts line 4; run abso pull to see the new definition.",
98 ]);
99 assert.deepEqual(project.edit("user", { file: "src/match.ts", old: "return tree ;", new: "return tree;" }, 5), {
100 ok: true,
101 news: [],
102 });
103});
105test("a body edit, or a change to a name the draft does not use, goes through", () => {
106 const project = setup();
107 userCalls(project);
108 project.edit(
109 "lib",
110 { file: "src/parse.ts", old: ' return { kind: "lit", text };', new: ' return { kind: "literal", text };' },
111 2,
112 );
113 project.publish("lib", "Body only", 3);
114 assert.equal(project.publish("user", "run parses", 4).ok, true);
115});
117test("a type whose shape changed refuses a draft that uses it", () => {
118 const project = setup();
119 project.edit(
120 "user",
121 { file: "src/match.ts", old: "import { parsePattern }", new: "import { parsePattern, Node }" },
122 1,
123 );
124 project.edit(
125 "user",
126 {
127 file: "src/match.ts",
128 old: "export function run(p: string) {",
129 new: "export function run(p: string, n?: Node) {",
130 },
131 1,
132 );
133 project.edit(
134 "lib",
135 { file: "src/parse.ts", old: " text: string;\n}", new: " text: string;\n kids: Node[];\n}" },
136 2,
137 );
138 project.publish("lib", "Nodes have kids", 3);
139 const refused = project.publish("user", "run takes a node", 4);
140 assert.equal(refused.ok, false);
141 assert.equal(!refused.ok && refused.collisions[0]!.name, "Node");
142});
144test("a definition that disappears refuses a draft that uses the name", () => {
145 const project = setup();
146 userCalls(project);
147 project.edit("lib", { file: "src/parse.ts", old: "function parsePattern(", new: "function parseRegex(" }, 2);
148 project.publish("lib", "Rename parsePattern to parseRegex", 3);
149 const refused = project.publish("user", "run parses", 4);
150 assert.equal(refused.ok, false);
151 const c = !refused.ok ? refused.collisions[0]! : undefined;
152 assert.equal(c?.kind, "definition");
153 assert.equal(c?.name, "parsePattern");
154 assert.equal(c?.theirs.text, "");
155});
157test("a name the draft defines itself is its own, and prose doesn't count", () => {
158 const project = setup();
159 project.edit(
160 "user",
161 {
162 file: "src/match.ts",
163 old: " return p;\n",
164 new: " const parsePattern = (x: string) => x;\n return parsePattern(p);\n",
165 },
166 1,
167 );
168 project.edit("user", { file: "README.md", content: "Uses parsePattern a lot.\n" }, 1);
169 project.edit("lib", { file: "src/parse.ts", old: "parsePattern(text: string)", new: "parsePattern(t: string)" }, 2);
170 project.publish("lib", "Shorter name", 3);
171 assert.equal(project.publish("user", "Local helper", 4).ok, true);
172});
174test("my own pushed change to a definition never refuses my next push", () => {
175 const project = setup();
176 project.edit(
177 "lib",
178 { file: "src/parse.ts", old: "parsePattern(text: string)", new: 'parsePattern(text: string, flags = "")' },
179 1,
180 );
181 project.publish("lib", "flags", 2);
182 project.edit("lib", { file: "src/match.ts", old: " return p;\n", new: ' return parsePattern(p, "g");\n' }, 3);
183 assert.equal(project.publish("lib", "use flags", 4).ok, true);
184});
186test("an import says which definition a use means", () => {
187 const project = new Project(new MemoryStore());
188 project.init(
189 {
190 "src/a/util.ts": "export function shapeOf(x: string) {\n return x;\n}\n",
191 "src/b/util.ts": "export function shapeOf(x: number) {\n return x;\n}\n",
192 "src/main.ts": 'import { shapeOf } from "./a/util.ts";\n\nexport const top = 1;\n',
193 },
194 0,
195 );
196 project.sync("lib");
197 project.sync("user");
198 project.edit(
199 "user",
200 { file: "src/main.ts", old: "export const top = 1;", new: 'export const top = shapeOf("1");' },
201 1,
202 );
203 // A change to the other file's shapeOf doesn't touch the one main.ts imports.
204 project.edit("lib", { file: "src/b/util.ts", old: "shapeOf(x: number)", new: "shapeOf(x: bigint)" }, 2);
205 project.publish("lib", "b takes bigint", 3);
206 assert.equal(project.publish("user", "top uses shapeOf", 4).ok, true);
207 // A change to the imported one does.
208 project.edit("user", { file: "src/main.ts", old: 'shapeOf("1")', new: 'shapeOf("2")' }, 5);
209 project.edit("lib", { file: "src/a/util.ts", old: "shapeOf(x: string)", new: "shapeOf(x: string, y: string)" }, 6);
210 project.publish("lib", "a takes two", 7);
211 const refused = project.publish("user", "two", 8);
212 assert.equal(refused.ok, false);
213 assert.equal(!refused.ok && refused.collisions[0]!.theirs.file, "src/a/util.ts");
214});
216test("Python imports scope a use too", () => {
217 const project = new Project(new MemoryStore());
218 project.init(
219 {
220 "pkg/one.py": "def build_tree(x):\n return x\n",
221 "pkg/two.py": "def build_tree(x):\n return x\n",
222 "app.py": "from pkg.one import build_tree\n\nresult = 1\n",
223 },
224 0,
225 );
226 project.sync("lib");
227 project.sync("user");
228 project.edit("user", { file: "app.py", old: "result = 1", new: "result = build_tree(1)" }, 1);
229 project.edit("lib", { file: "pkg/two.py", old: "def build_tree(x):", new: "def build_tree(x, y):" }, 2);
230 project.publish("lib", "two takes y", 3);
231 assert.equal(project.publish("user", "use it", 4).ok, true);
232});
234test("a local name, a parameter or a property that shares a name with another file's definition is not a use of it", () => {
235 // From the regex trial (#25): news fired on a fuzzer's own caps and body
236 // against other files' definitions of the same names.
237 const project = new Project(new MemoryStore());
238 project.init({ "meta.ts": "export const caps = 1;\n", "fuzz.ts": "export function go() {\n return 0;\n}\n" }, 0);
239 project.sync("fuzzer");
240 project.sync("meta");
241 project.edit(
242 "fuzzer",
243 {
244 file: "fuzz.ts",
245 old: " return 0;",
246 new: " const r = { caps: 2 };\n return r.caps + run(r);\n}\nfunction run(caps: object) {\n return caps ? 1 : 0;",
247 },
248 1,
249 );
250 project.edit("meta", { file: "meta.ts", old: "caps = 1", new: "caps = 2" }, 2);
251 project.publish("meta", "More caps", 3);
252 assert.equal(project.publish("fuzzer", "Fuzz with caps", 4).ok, true);
254 // A local inside another file's function is not a definition either.
255 project.sync("fuzzer");
256 project.sync("meta");
257 project.record("meta", { "parse.ts": "export function parse() {\n const body = 1;\n return body;\n}\n" }, 5);
258 project.publish("meta", "Parser", 6);
259 project.sync("fuzzer");
260 project.sync("meta");
261 project.edit(
262 "fuzzer",
263 { file: "fuzz.ts", old: " return caps ? 1 : 0;", new: " const body = caps;\n return body ? 1 : 0;" },
264 7,
265 );
266 project.edit("meta", { file: "parse.ts", old: "const body = 1;", new: "const body = 2;" }, 8);
267 project.publish("meta", "Other body", 9);
268 assert.equal(project.publish("fuzzer", "Fuzz with a body", 10).ok, true);
270 // A use through an import still means the definition, and is refused when it changed.
271 project.sync("fuzzer");
272 project.sync("meta");
273 project.record("fuzzer", { "use.ts": 'import { caps } from "./meta.ts";\nexport const twice = caps * 2;\n' }, 11);
274 project.edit("meta", { file: "meta.ts", old: "caps = 2", new: "caps = 3" }, 12);
275 project.publish("meta", "Even more caps", 13);
276 const refused = project.publish("fuzzer", "Use caps", 14);
277 assert.equal(!refused.ok && refused.collisions[0]!.name, "caps");
278});
280test("only names the change added count: editing a line that already used the name is not a use (#25)", () => {
281 const project = new Project(new MemoryStore());
282 project.init(
283 {
284 "lib.ts": "export function measure(s: string) {\n return s.length;\n}\n",
285 "use.ts": 'import { measure } from "./lib.ts";\nexport const a = measure("ab");\nexport const b = 1;\n',
286 },
287 0,
288 );
289 project.sync("lib");
290 project.sync("app");
291 // app edits a line that already used measure (a different argument), and adds an unrelated line.
292 project.edit("app", { file: "use.ts", old: 'measure("ab")', new: 'measure("abc")' }, 1);
293 project.edit("app", { file: "use.ts", old: "export const b = 1;", new: "export const b = 2;" }, 1);
294 project.edit("lib", { file: "lib.ts", old: "measure(s: string)", new: "measure(s: string, trim = false)" }, 2);
295 project.publish("lib", "measure can trim", 3);
296 const edited = project.edit("app", { file: "use.ts", old: "b = 2", new: "b = 3" }, 4);
297 assert.deepEqual(edited.ok && edited.news, []);
298 assert.equal(project.publish("app", "Longer string", 5).ok, true);
300 // A use the change adds still counts.
301 project.sync("app");
302 project.sync("lib");
303 project.edit("app", { file: "use.ts", old: "export const b = 3;", new: 'export const b = measure("x");' }, 6);
304 project.edit("lib", { file: "lib.ts", old: "trim = false", new: "trim = true" }, 7);
305 project.publish("lib", "Trim by default", 8);
306 const refused = project.publish("app", "b from measure", 9);
307 assert.equal(!refused.ok && refused.collisions[0]!.name, "measure");
308});