forked from openai/codex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply-patch.test.ts
More file actions
318 lines (262 loc) · 7.52 KB
/
apply-patch.test.ts
File metadata and controls
318 lines (262 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import {
ActionType,
apply_commit,
assemble_changes,
DiffError,
identify_files_added,
identify_files_needed,
load_files,
patch_to_commit,
process_patch,
text_to_patch,
} from "../src/utils/agent/apply-patch.js";
import { test, expect } from "vitest";
function createInMemoryFS(initialFiles: Record<string, string>) {
const files: Record<string, string> = { ...initialFiles };
const writes: Record<string, string> = {};
const removals: Array<string> = [];
const openFn = (p: string): string => {
const file = files[p];
if (typeof file === "string") {
return file;
} else {
throw new Error(`File not found: ${p}`);
}
};
const writeFn = (p: string, content: string): void => {
files[p] = content;
writes[p] = content;
};
const removeFn = (p: string): void => {
delete files[p];
removals.push(p);
};
return { openFn, writeFn, removeFn, writes, removals, files };
}
test("process_patch - update file", () => {
const patch = `*** Begin Patch
*** Update File: a.txt
@@
-hello
+hello world
*** End Patch`;
const fs = createInMemoryFS({ "a.txt": "hello" });
const result = process_patch(patch, fs.openFn, fs.writeFn, fs.removeFn);
expect(result).toBe("Done!");
expect(fs.writes).toEqual({ "a.txt": "hello world" });
expect(fs.removals).toEqual([]);
});
test("process_patch - add file", () => {
const patch = `*** Begin Patch
*** Add File: b.txt
+new content
*** End Patch`;
const fs = createInMemoryFS({});
process_patch(patch, fs.openFn, fs.writeFn, fs.removeFn);
expect(fs.writes).toEqual({ "b.txt": "new content" });
expect(fs.removals).toEqual([]);
});
test("process_patch - delete file", () => {
const patch = `*** Begin Patch
*** Delete File: c.txt
*** End Patch`;
const fs = createInMemoryFS({ "c.txt": "to be removed" });
process_patch(patch, fs.openFn, fs.writeFn, fs.removeFn);
expect(fs.writes).toEqual({});
expect(fs.removals).toEqual(["c.txt"]);
});
test("identify_files_needed & identify_files_added", () => {
const patch = `*** Begin Patch
*** Update File: a.txt
*** Delete File: b.txt
*** Add File: c.txt
*** End Patch`;
expect(identify_files_needed(patch).sort()).toEqual(
["a.txt", "b.txt"].sort(),
);
expect(identify_files_added(patch)).toEqual(["c.txt"]);
});
test("process_patch - update file with multiple chunks", () => {
const original = "line1\nline2\nline3\nline4";
const patch = `*** Begin Patch
*** Update File: multi.txt
@@
line1
-line2
+line2 updated
line3
+inserted line
line4
*** End Patch`;
const fs = createInMemoryFS({ "multi.txt": original });
process_patch(patch, fs.openFn, fs.writeFn, fs.removeFn);
const expected = "line1\nline2 updated\nline3\ninserted line\nline4";
expect(fs.writes).toEqual({ "multi.txt": expected });
expect(fs.removals).toEqual([]);
});
test("process_patch - move file (rename)", () => {
const patch = `*** Begin Patch
*** Update File: old.txt
*** Move to: new.txt
@@
-old
+new
*** End Patch`;
const fs = createInMemoryFS({ "old.txt": "old" });
process_patch(patch, fs.openFn, fs.writeFn, fs.removeFn);
expect(fs.writes).toEqual({ "new.txt": "new" });
expect(fs.removals).toEqual(["old.txt"]);
});
test("process_patch - combined add, update, delete", () => {
const patch = `*** Begin Patch
*** Add File: added.txt
+added contents
*** Update File: upd.txt
@@
-old value
+new value
*** Delete File: del.txt
*** End Patch`;
const fs = createInMemoryFS({
"upd.txt": "old value",
"del.txt": "delete me",
});
process_patch(patch, fs.openFn, fs.writeFn, fs.removeFn);
expect(fs.writes).toEqual({
"added.txt": "added contents",
"upd.txt": "new value",
});
expect(fs.removals).toEqual(["del.txt"]);
});
test("process_patch - readme edit", () => {
const original = `
#### Fix an issue
\`\`\`sh
# First, copy an error
# Then, start codex with interactive mode
codex
# Or you can pass in via command line argument
codex "Fix this issue: $(pbpaste)"
# Or even as a task (it should use your current repo and branch)
codex -t "Fix this issue: $(pbpaste)"
\`\`\`
`;
const patch = `*** Begin Patch
*** Update File: README.md
@@
codex -t "Fix this issue: $(pbpaste)"
\`\`\`
+
+hello
*** End Patch`;
const expected = `
#### Fix an issue
\`\`\`sh
# First, copy an error
# Then, start codex with interactive mode
codex
# Or you can pass in via command line argument
codex "Fix this issue: $(pbpaste)"
# Or even as a task (it should use your current repo and branch)
codex -t "Fix this issue: $(pbpaste)"
\`\`\`
hello
`;
const fs = createInMemoryFS({ "README.md": original });
process_patch(patch, fs.openFn, fs.writeFn, fs.removeFn);
expect(fs.writes).toEqual({ "README.md": expected });
});
test("process_patch - invalid patch throws DiffError", () => {
const patch = `*** Begin Patch
*** Update File: missing.txt
@@
+something
*** End Patch`;
const fs = createInMemoryFS({});
expect(() =>
process_patch(patch, fs.openFn, fs.writeFn, fs.removeFn),
).toThrow(DiffError);
});
test("process_patch - tolerates omitted space for keep line", () => {
const original = "line1\nline2\nline3";
const patch = `*** Begin Patch\n*** Update File: foo.txt\n@@\n line1\n-line2\n+some new line2\nline3\n*** End Patch`;
const fs = createInMemoryFS({ "foo.txt": original });
process_patch(patch, fs.openFn, fs.writeFn, fs.removeFn);
expect(fs.files["foo.txt"]).toBe("line1\nsome new line2\nline3");
});
test("assemble_changes correctly detects add, update and delete", () => {
const orig = {
"a.txt": "old",
"b.txt": "keep",
"c.txt": "remove",
};
const updated = {
"a.txt": "new", // update
"b.txt": "keep", // unchanged – should be ignored
"c.txt": undefined as unknown as string, // delete
"d.txt": "created", // add
};
const commit = assemble_changes(orig, updated).changes;
expect(commit["a.txt"]).toEqual({
type: ActionType.UPDATE,
old_content: "old",
new_content: "new",
});
expect(commit["c.txt"]).toEqual({
type: ActionType.DELETE,
old_content: "remove",
});
expect(commit["d.txt"]).toEqual({
type: ActionType.ADD,
new_content: "created",
});
// unchanged files should not appear in commit
expect(commit).not.toHaveProperty("b.txt");
});
test("text_to_patch + patch_to_commit handle update and add", () => {
const originalFiles = {
"a.txt": "old line",
};
const patch = `*** Begin Patch
*** Update File: a.txt
@@
-old line
+new line
*** Add File: b.txt
+content new
*** End Patch`;
const [parsedPatch] = text_to_patch(patch, originalFiles);
const commit = patch_to_commit(parsedPatch, originalFiles).changes;
expect(commit["a.txt"]).toEqual({
type: ActionType.UPDATE,
old_content: "old line",
new_content: "new line",
});
expect(commit["b.txt"]).toEqual({
type: ActionType.ADD,
new_content: "content new",
});
});
test("load_files throws DiffError when file is missing", () => {
const { openFn } = createInMemoryFS({ "exists.txt": "hi" });
// intentionally include a missing file in the list
expect(() => load_files(["exists.txt", "missing.txt"], openFn)).toThrow(
DiffError,
);
});
test("apply_commit correctly performs move / rename operations", () => {
const commit = {
changes: {
"old.txt": {
type: ActionType.UPDATE,
old_content: "old",
new_content: "new",
move_path: "new.txt",
},
},
};
const { writeFn, removeFn, writes, removals } = createInMemoryFS({});
apply_commit(commit, writeFn, removeFn);
expect(writes).toEqual({ "new.txt": "new" });
expect(removals).toEqual(["old.txt"]);
});