@criogaid/pi-hashline-edit 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -12
- package/README.zh-CN.md +12 -12
- package/THIRD_PARTY_NOTICES.md +0 -6
- package/package.json +1 -1
- package/src/core/AGENTS.md +4 -5
- package/src/core/apply.test.ts +96 -271
- package/src/core/apply.ts +62 -169
- package/src/core/case-fold.ts +8 -67
- package/src/core/character-set.test.ts +2 -3
- package/src/core/character-set.ts +5 -5
- package/src/core/errors.test.ts +23 -7
- package/src/core/errors.ts +32 -5
- package/src/core/hash.test.ts +8 -16
- package/src/core/hash.ts +3 -23
- package/src/core/lines.ts +2 -4
- package/src/core/ranges.test.ts +2 -1
- package/src/core/ranges.ts +1 -1
- package/src/core/regex.ts +3 -4
- package/src/core/replace.test.ts +47 -1
- package/src/core/replace.ts +19 -13
- package/src/core/search-analysis.test.ts +24 -27
- package/src/core/search-analysis.ts +34 -65
- package/src/core/search-matrix.testing.ts +0 -2
- package/src/core/search.ts +1 -1
- package/src/core/text.test.ts +5 -5
- package/src/core/text.ts +5 -12
- package/src/core/types.ts +8 -44
- package/src/index.ts +8 -8
- package/src/integration/config-registration.test.ts +8 -10
- package/src/integration/fusion-dispatch.test.ts +4 -7
- package/src/integration/grep-errors.test.ts +3 -3
- package/src/integration/grep-flags.test.ts +5 -19
- package/src/integration/grep-js-completeness.test.ts +45 -18
- package/src/integration/grep-line-candidates.test.ts +7 -10
- package/src/integration/grep-matrix.test.ts +6 -9
- package/src/integration/grep-order.test.ts +4 -4
- package/src/integration/grep-path-matrix.test.ts +10 -9
- package/src/integration/grep-prefilter-coverage.test.ts +5 -8
- package/src/integration/grep-process-limits.test.ts +76 -0
- package/src/integration/grep-regex.test.ts +10 -10
- package/src/integration/grep-rg.test.ts +166 -66
- package/src/integration/grep-scan-budget.test.ts +9 -9
- package/src/integration/grep-translation.test.ts +18 -32
- package/src/integration/large-text.test.ts +6 -52
- package/src/integration/model-budget-lifecycle.test.ts +10 -9
- package/src/integration/mutation-file-types.testing.ts +4 -4
- package/src/integration/npm-package.testing.ts +3 -3
- package/src/integration/publication-read-changes.test.ts +161 -83
- package/src/integration/read-changes.test.ts +4 -4
- package/src/integration/replace-worker-boundary.test.ts +22 -12
- package/src/integration/report-replay.test.ts +23 -26
- package/src/integration/utf8-errors.test.ts +4 -4
- package/src/pi/AGENTS.md +18 -17
- package/src/pi/action-fusion.test.ts +241 -353
- package/src/pi/action-fusion.testing.ts +38 -0
- package/src/pi/action-fusion.ts +150 -125
- package/src/pi/anchor-format.test.ts +9 -9
- package/src/pi/anchor-format.ts +6 -8
- package/src/pi/argument-diagnostics.ts +25 -28
- package/src/pi/argument-error.ts +2 -1
- package/src/pi/argument-format.test.ts +19 -18
- package/src/pi/argument-validation.test.ts +334 -283
- package/src/pi/argument-validation.ts +5 -8
- package/src/pi/budgets.ts +6 -8
- package/src/pi/codemode.test.ts +7 -2
- package/src/pi/config.test.ts +7 -3
- package/src/pi/config.ts +12 -9
- package/src/pi/diagnostic-buffer.test.ts +34 -13
- package/src/pi/diagnostic-buffer.ts +1 -2
- package/src/pi/directory-sync.test.ts +0 -1
- package/src/pi/edit-tool.ts +86 -211
- package/src/pi/error-text.ts +10 -9
- package/src/pi/execute.test.ts +249 -345
- package/src/pi/failure-context.test.ts +146 -127
- package/src/pi/failure-context.ts +45 -94
- package/src/pi/failure-path.test.ts +182 -155
- package/src/pi/file-commit.test.ts +156 -71
- package/src/pi/file-commit.ts +126 -146
- package/src/pi/file-snapshot.test.ts +3 -5
- package/src/pi/fusion-card.test.ts +26 -22
- package/src/pi/fusion-card.ts +16 -24
- package/src/pi/grep-candidates.test.ts +19 -120
- package/src/pi/grep-candidates.ts +25 -76
- package/src/pi/grep-matrix.test.ts +5 -5
- package/src/pi/grep-order.test.ts +16 -15
- package/src/pi/grep-output.ts +94 -107
- package/src/pi/grep-render.ts +1 -1
- package/src/pi/grep-reread.ts +97 -0
- package/src/pi/grep-rg.testing.ts +70 -0
- package/src/pi/grep-scope.ts +41 -43
- package/src/pi/grep-search.ts +27 -39
- package/src/pi/grep-tool.ts +63 -106
- package/src/pi/grep.test.ts +177 -156
- package/src/pi/model-budgets.test.ts +27 -24
- package/src/pi/model-budgets.ts +6 -38
- package/src/pi/mutation-outcome.testing.ts +0 -1
- package/src/pi/mutation-result.test.ts +44 -35
- package/src/pi/mutation-result.ts +52 -21
- package/src/pi/mutation-runner.ts +134 -74
- package/src/pi/output-budgets.test.ts +86 -42
- package/src/pi/path.ts +1 -1
- package/src/pi/pi.test.ts +18 -13
- package/src/pi/read-tool.ts +18 -23
- package/src/pi/read.test.ts +22 -41
- package/src/pi/regex-worker-contract.ts +3 -13
- package/src/pi/regex-worker.ts +1 -10
- package/src/pi/registration.test.ts +85 -51
- package/src/pi/render.test.ts +88 -59
- package/src/pi/render.ts +64 -65
- package/src/pi/replace-regex.ts +14 -2
- package/src/pi/replace-tool.ts +22 -94
- package/src/pi/replace-worker.ts +1 -1
- package/src/pi/replace.test.ts +53 -38
- package/src/pi/report-cause.ts +3 -8
- package/src/pi/report-failure.testing.ts +10 -3
- package/src/pi/report-schema.ts +251 -148
- package/src/pi/report.test.ts +215 -67
- package/src/pi/report.ts +50 -98
- package/src/pi/result-safety.test.ts +241 -247
- package/src/pi/rg-process.test.ts +77 -63
- package/src/pi/rg-process.ts +53 -75
- package/src/pi/rg-regex.ts +40 -400
- package/src/pi/schema.ts +35 -0
- package/src/pi/text-stream.test.ts +0 -21
- package/src/pi/text-stream.ts +14 -21
- package/src/pi/tool-factories.testing.ts +59 -0
- package/src/pi/write-tool.ts +36 -71
- package/src/pi/write.test.ts +31 -34
- package/src/testing/regex-translation-corpus.ts +12 -12
- package/src/testing/run-tests.ts +1 -1
- package/src/pi/replace-worker-contract.ts +0 -10
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import fsPromises from "node:fs/promises";
|
|
2
|
+
import { syncBuiltinESMExports } from "node:module";
|
|
1
3
|
import {
|
|
2
4
|
chmod,
|
|
3
5
|
lstat,
|
|
@@ -19,34 +21,29 @@ import { test } from "node:test";
|
|
|
19
21
|
import { spawn } from "node:child_process";
|
|
20
22
|
import {
|
|
21
23
|
commitFile,
|
|
22
|
-
commitReplacement,
|
|
23
24
|
FileMutationError,
|
|
24
|
-
fileRevision,
|
|
25
25
|
readEditableSnapshot,
|
|
26
26
|
type CommitResult,
|
|
27
27
|
type FreshnessObservation,
|
|
28
28
|
} from "./file-commit.ts";
|
|
29
29
|
import { byteRevision } from "./file-revision.ts";
|
|
30
|
+
import { HashlineError, PATH_ERROR_MESSAGES } from "../core/errors.ts";
|
|
30
31
|
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
31
|
-
import {
|
|
32
|
+
import { commandCompletion } from "./action-fusion.testing.ts";
|
|
33
|
+
import { makeWriteOverride } from "./tool-factories.testing.ts";
|
|
32
34
|
import { callTool } from "./tool-call.testing.ts";
|
|
35
|
+
import { describeMutationFailure } from "./mutation-result.ts";
|
|
33
36
|
import { reportOf } from "./report.ts";
|
|
37
|
+
import { DEFAULT_OUTPUT_BUDGETS } from "./tool-factories.testing.ts";
|
|
34
38
|
|
|
35
|
-
// Checked by tsc;
|
|
39
|
+
// Checked by tsc; this state cannot be returned by the commit layer.
|
|
36
40
|
function invalidCommitStates() {
|
|
37
|
-
// @ts-expect-error A newly created target is always published.
|
|
38
|
-
const unpublishedCreate: CommitResult = {
|
|
39
|
-
created: true,
|
|
40
|
-
publication: "NOT_PUBLISHED",
|
|
41
|
-
publishedRevision: "r",
|
|
42
|
-
observedRevision: "r",
|
|
43
|
-
};
|
|
44
41
|
// @ts-expect-error A no-op has a confirmed observed revision.
|
|
45
42
|
const unobservedNoop: CommitResult = {
|
|
46
|
-
created: false,
|
|
47
43
|
publication: "NOT_PUBLISHED",
|
|
48
44
|
publishedRevision: "r",
|
|
49
45
|
};
|
|
46
|
+
|
|
50
47
|
// @ts-expect-error A known freshness state cannot carry a read error.
|
|
51
48
|
const knownError: FreshnessObservation = {
|
|
52
49
|
freshness: "changed",
|
|
@@ -120,8 +117,8 @@ test("independent create commits race at the no-replace publication primitive",
|
|
|
120
117
|
const rejected = results.find(
|
|
121
118
|
(result) => result.status === "rejected",
|
|
122
119
|
) as PromiseRejectedResult;
|
|
123
|
-
assert.ok(rejected.reason instanceof
|
|
124
|
-
assert.equal(rejected.reason.
|
|
120
|
+
assert.ok(rejected.reason instanceof HashlineError);
|
|
121
|
+
assert.equal(rejected.reason.errorCode, "TARGET_EXISTS");
|
|
125
122
|
assert.match(await readFile(target, "utf8"), /^winner-[ab]\n$/);
|
|
126
123
|
}));
|
|
127
124
|
|
|
@@ -129,16 +126,47 @@ test("create never overwrites an existing target", async () =>
|
|
|
129
126
|
withTemp(async (dir) => {
|
|
130
127
|
const target = join(dir, "existing.txt");
|
|
131
128
|
await writeFile(target, "original\n");
|
|
132
|
-
const revision = await
|
|
129
|
+
const revision = byteRevision(await readFile(target));
|
|
133
130
|
await assert.rejects(
|
|
134
131
|
commitFile(target, "replacement\n", { mode: "create" }),
|
|
135
|
-
(error: unknown) =>
|
|
136
|
-
error instanceof FileMutationError && error.publication === "NOT_PUBLISHED",
|
|
132
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === "TARGET_EXISTS",
|
|
137
133
|
);
|
|
138
|
-
assert.equal(await
|
|
134
|
+
assert.equal(byteRevision(await readFile(target)), revision);
|
|
139
135
|
assert.equal(await readFile(target, "utf8"), "original\n");
|
|
140
136
|
}));
|
|
141
137
|
|
|
138
|
+
test("short low-level reads → complete mutation snapshots before revision binding", async (t) =>
|
|
139
|
+
withTemp(async (dir) => {
|
|
140
|
+
const target = join(dir, "short-read.txt");
|
|
141
|
+
const expected = Buffer.from("x".repeat(66_722));
|
|
142
|
+
const shortRead = 65_512;
|
|
143
|
+
await writeFile(target, expected);
|
|
144
|
+
const nativeOpen = fsPromises.open;
|
|
145
|
+
let lowLevelReads = 0;
|
|
146
|
+
t.mock.method(fsPromises, "open", async (...args: Parameters<typeof nativeOpen>) => {
|
|
147
|
+
const handle = await nativeOpen(...args);
|
|
148
|
+
if (args[0] !== target) return handle;
|
|
149
|
+
const nativeRead = handle.read.bind(handle);
|
|
150
|
+
t.mock.method(handle, "read", async (...readArgs: Parameters<typeof nativeRead>) => {
|
|
151
|
+
const result = await nativeRead(...readArgs);
|
|
152
|
+
return lowLevelReads++ === 0
|
|
153
|
+
? { ...result, bytesRead: Math.min(result.bytesRead, shortRead) }
|
|
154
|
+
: result;
|
|
155
|
+
});
|
|
156
|
+
return handle;
|
|
157
|
+
});
|
|
158
|
+
syncBuiltinESMExports();
|
|
159
|
+
try {
|
|
160
|
+
const snapshot = await readEditableSnapshot(target);
|
|
161
|
+
assert.equal(snapshot.text, expected.toString("utf8"));
|
|
162
|
+
assert.equal(snapshot.baseRevision, byteRevision(expected));
|
|
163
|
+
assert.ok(lowLevelReads >= 2);
|
|
164
|
+
} finally {
|
|
165
|
+
t.mock.restoreAll();
|
|
166
|
+
syncBuiltinESMExports();
|
|
167
|
+
}
|
|
168
|
+
}));
|
|
169
|
+
|
|
142
170
|
test("replacement exposes complete old or new content during concurrent reads", async (t) =>
|
|
143
171
|
withTemp(async (dir) => {
|
|
144
172
|
if (process.platform === "win32")
|
|
@@ -216,7 +244,7 @@ test("replacement rejects a multi-hardlink target without changing either link",
|
|
|
216
244
|
assert.equal((await stat(target)).nlink, 2);
|
|
217
245
|
}));
|
|
218
246
|
|
|
219
|
-
test("existing
|
|
247
|
+
test("files are replaced or created → preserve existing modes and apply umask to new files", async (t) =>
|
|
220
248
|
withTemp(async (dir) => {
|
|
221
249
|
if (process.platform === "win32")
|
|
222
250
|
return t.skip("Windows mode bits are not an ACL preservation test");
|
|
@@ -227,14 +255,19 @@ test("existing private and executable permissions survive replacement; new files
|
|
|
227
255
|
assert.equal((await stat(existing)).mode & 0o777, 0o750);
|
|
228
256
|
const created = join(dir, "created.txt");
|
|
229
257
|
await commitFile(created, "new\n", { mode: "create" });
|
|
230
|
-
assert.equal((await stat(created)).mode & 0o777,
|
|
258
|
+
assert.equal((await stat(created)).mode & 0o777, 0o666 & ~process.umask());
|
|
231
259
|
}));
|
|
232
260
|
|
|
233
261
|
test("directories are rejected instead of entering regular-file publication", async () =>
|
|
234
262
|
withTemp(async (dir) => {
|
|
235
263
|
const target = join(dir, "directory");
|
|
236
264
|
await mkdir(target);
|
|
237
|
-
await assert.rejects(commitFile(target, "bad\n", { mode: "overwrite" }),
|
|
265
|
+
await assert.rejects(commitFile(target, "bad\n", { mode: "overwrite" }), (error: unknown) => {
|
|
266
|
+
assert.ok(error instanceof HashlineError);
|
|
267
|
+
assert.equal(error.errorCode, "NOT_REGULAR_FILE");
|
|
268
|
+
assert.equal(error.message, PATH_ERROR_MESSAGES.NOT_REGULAR_FILE);
|
|
269
|
+
return true;
|
|
270
|
+
});
|
|
238
271
|
}));
|
|
239
272
|
|
|
240
273
|
test("Windows shared access failure preserves the target, skips then_run, and releases the queue", async (t) =>
|
|
@@ -244,12 +277,16 @@ test("Windows shared access failure preserves the target, skips then_run, and re
|
|
|
244
277
|
await writeFile(target, "original\n");
|
|
245
278
|
const holder = await holdWindowsExclusive(target);
|
|
246
279
|
let commandRuns = 0;
|
|
280
|
+
const context = {
|
|
281
|
+
cwd: dir,
|
|
282
|
+
executeTool: async () => {
|
|
283
|
+
commandRuns++;
|
|
284
|
+
return commandCompletion({ content: [{ type: "text", text: "ok" }] });
|
|
285
|
+
},
|
|
286
|
+
};
|
|
247
287
|
const tool = makeWriteOverride(
|
|
248
288
|
dir,
|
|
249
|
-
createActionFusionExecutor(
|
|
250
|
-
commandRuns++;
|
|
251
|
-
return { status: "succeeded", result: { content: [{ type: "text", text: "ok" }] } };
|
|
252
|
-
}),
|
|
289
|
+
createActionFusionExecutor(() => {}),
|
|
253
290
|
);
|
|
254
291
|
try {
|
|
255
292
|
const failed = await callTool(
|
|
@@ -260,7 +297,7 @@ test("Windows shared access failure preserves the target, skips then_run, and re
|
|
|
260
297
|
mode: "overwrite",
|
|
261
298
|
then_run: { command: "deterministic-command" },
|
|
262
299
|
},
|
|
263
|
-
{ toolCallId: "shared-failure", ctx:
|
|
300
|
+
{ toolCallId: "shared-failure", ctx: context },
|
|
264
301
|
);
|
|
265
302
|
assert.equal(failed.isError, true);
|
|
266
303
|
const report = reportOf(failed, "write");
|
|
@@ -299,7 +336,7 @@ test("Windows shared access failure preserves the target, skips then_run, and re
|
|
|
299
336
|
mode: "overwrite",
|
|
300
337
|
then_run: { command: "deterministic-command" },
|
|
301
338
|
},
|
|
302
|
-
{ toolCallId: "shared-retry", ctx:
|
|
339
|
+
{ toolCallId: "shared-retry", ctx: context },
|
|
303
340
|
);
|
|
304
341
|
assert.equal(result.isError, undefined);
|
|
305
342
|
assert.equal(reportOf(result, "write")?.then_run?.status, "succeeded");
|
|
@@ -322,8 +359,13 @@ test("cancellation racing publication never reports a settled operation as unpub
|
|
|
322
359
|
publication = result.publication;
|
|
323
360
|
assert.equal(publication, "PUBLISHED");
|
|
324
361
|
} catch (error) {
|
|
325
|
-
assert.ok(error instanceof
|
|
326
|
-
|
|
362
|
+
assert.ok(error instanceof HashlineError);
|
|
363
|
+
if (!(error instanceof FileMutationError)) assert.equal(error.errorCode, "OPERATION_ABORTED");
|
|
364
|
+
const reported = describeMutationFailure("write", error, {
|
|
365
|
+
budget: DEFAULT_OUTPUT_BUDGETS,
|
|
366
|
+
}).publication;
|
|
367
|
+
assert.ok(reported);
|
|
368
|
+
publication = reported;
|
|
327
369
|
}
|
|
328
370
|
const finalContent = await readFile(target, "utf8");
|
|
329
371
|
assert.ok(finalContent === oldContent || finalContent === newContent);
|
|
@@ -335,7 +377,7 @@ test("bound overwrite → reports published and observed revisions for the publi
|
|
|
335
377
|
withTemp(async (dir) => {
|
|
336
378
|
const target = join(dir, "versions.txt");
|
|
337
379
|
await writeFile(target, "before\n");
|
|
338
|
-
const baseRevision = await
|
|
380
|
+
const baseRevision = byteRevision(await readFile(target));
|
|
339
381
|
const result = await commitFile(target, "after\n", {
|
|
340
382
|
mode: "overwrite",
|
|
341
383
|
baseRevision,
|
|
@@ -356,9 +398,7 @@ test("commit rejects lossy UTF-8 output before modifying or creating files", asy
|
|
|
356
398
|
await assert.rejects(
|
|
357
399
|
commitFile(path, content, { mode: path === existing ? "overwrite" : "create" }),
|
|
358
400
|
(error: unknown) =>
|
|
359
|
-
error instanceof
|
|
360
|
-
error.publication === "NOT_PUBLISHED" &&
|
|
361
|
-
error.errorCode === "INVALID_UNICODE",
|
|
401
|
+
error instanceof HashlineError && error.errorCode === "INVALID_UNICODE",
|
|
362
402
|
);
|
|
363
403
|
}
|
|
364
404
|
}
|
|
@@ -376,13 +416,12 @@ test("identical commits preserve the file and still enforce mode, revision, and
|
|
|
376
416
|
const content = "\uFEFFsame\r\nbytes\n";
|
|
377
417
|
await writeFile(target, content);
|
|
378
418
|
const before = await stat(target);
|
|
379
|
-
const revision = await
|
|
419
|
+
const revision = byteRevision(await readFile(target));
|
|
380
420
|
const result = await commitFile(target, content, {
|
|
381
421
|
mode: "overwrite",
|
|
382
422
|
baseRevision: revision,
|
|
383
423
|
});
|
|
384
424
|
assert.deepEqual(result, {
|
|
385
|
-
created: false,
|
|
386
425
|
publishedRevision: revision,
|
|
387
426
|
observedRevision: revision,
|
|
388
427
|
publication: "NOT_PUBLISHED",
|
|
@@ -396,26 +435,36 @@ test("identical commits preserve the file and still enforce mode, revision, and
|
|
|
396
435
|
await assert.rejects(
|
|
397
436
|
commitFile(target, content, { mode: "overwrite", baseRevision: "stale" }),
|
|
398
437
|
(error: unknown) =>
|
|
399
|
-
error instanceof
|
|
438
|
+
error instanceof HashlineError &&
|
|
400
439
|
error.errorCode === "FILE_CHANGED" &&
|
|
401
|
-
error.
|
|
402
|
-
error.publication === "NOT_PUBLISHED" &&
|
|
403
|
-
/before publication/.test(error.message),
|
|
440
|
+
error.message === "File changed after it was read.",
|
|
404
441
|
);
|
|
405
442
|
await assert.rejects(
|
|
406
443
|
commitFile(target, content, { mode: "overwrite", signal: AbortSignal.abort() }),
|
|
407
|
-
(error: unknown) =>
|
|
408
|
-
error instanceof FileMutationError && error.publication === "NOT_PUBLISHED",
|
|
409
|
-
);
|
|
410
|
-
await assert.rejects(
|
|
411
|
-
commitFile(join(dir, "missing"), "", { mode: "overwrite" }),
|
|
412
|
-
/does not exist/,
|
|
444
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === "OPERATION_ABORTED",
|
|
413
445
|
);
|
|
414
446
|
const created = await commitFile(join(dir, "empty"), "", { mode: "create" });
|
|
415
|
-
assert.equal(created.created, true);
|
|
416
447
|
assert.equal(created.publication, "PUBLISHED");
|
|
417
448
|
}));
|
|
418
449
|
|
|
450
|
+
test("missing overwrite target → suggests create only without a base revision", async () =>
|
|
451
|
+
withTemp(async (dir) => {
|
|
452
|
+
const missing = join(dir, "missing");
|
|
453
|
+
for (const baseRevision of [undefined, "previous-revision"]) {
|
|
454
|
+
await assert.rejects(
|
|
455
|
+
commitFile(missing, "", { mode: "overwrite", baseRevision }),
|
|
456
|
+
(error: unknown) => {
|
|
457
|
+
assert.ok(error instanceof HashlineError);
|
|
458
|
+
assert.equal(error.errorCode, "PATH_NOT_FOUND");
|
|
459
|
+
assert.equal(error.message, PATH_ERROR_MESSAGES.PATH_NOT_FOUND);
|
|
460
|
+
assert.deepEqual(error.facts, { missing: [missing] });
|
|
461
|
+
assert.equal(error.next, baseRevision === undefined ? "useCreate" : undefined);
|
|
462
|
+
return true;
|
|
463
|
+
},
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
}));
|
|
467
|
+
|
|
419
468
|
test("file changes after the read → bound commits reject changed and unchanged output", async () =>
|
|
420
469
|
withTemp(async (dir) => {
|
|
421
470
|
const target = join(dir, "target.txt");
|
|
@@ -426,25 +475,16 @@ test("file changes after the read → bound commits reject changed and unchanged
|
|
|
426
475
|
|
|
427
476
|
// External process modifies target after snapshot is read.
|
|
428
477
|
await writeFile(target, "line 1\nmodified line 2\n");
|
|
429
|
-
const externalRevision = await
|
|
478
|
+
const externalRevision = byteRevision(await readFile(target));
|
|
430
479
|
|
|
431
480
|
for (const content of [snapshot.text, "replacement\n"]) {
|
|
432
|
-
|
|
433
|
-
() => commitReplacement(target, content, snapshot.baseRevision),
|
|
481
|
+
await assert.rejects(
|
|
434
482
|
() =>
|
|
435
483
|
commitFile(target, content, { mode: "overwrite", baseRevision: snapshot.baseRevision }),
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
error instanceof FileMutationError &&
|
|
441
|
-
error.stage === "prepare" &&
|
|
442
|
-
error.publication === "NOT_PUBLISHED" &&
|
|
443
|
-
error.errorCode === "FILE_CHANGED",
|
|
444
|
-
);
|
|
445
|
-
assert.equal(await readFile(target, "utf8"), "line 1\nmodified line 2\n");
|
|
446
|
-
assert.equal(await fileRevision(target), externalRevision);
|
|
447
|
-
}
|
|
484
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === "FILE_CHANGED",
|
|
485
|
+
);
|
|
486
|
+
assert.equal(await readFile(target, "utf8"), "line 1\nmodified line 2\n");
|
|
487
|
+
assert.equal(byteRevision(await readFile(target)), externalRevision);
|
|
448
488
|
}
|
|
449
489
|
|
|
450
490
|
// Without baseRevision, overwrite hashes the current target instead of using the snapshot.
|
|
@@ -458,11 +498,10 @@ test("file changes after the read → bound commits reject changed and unchanged
|
|
|
458
498
|
|
|
459
499
|
// When target does not change externally, no-op commit succeeds and confirms observed revision.
|
|
460
500
|
const freshSnapshot = await readEditableSnapshot(target);
|
|
461
|
-
const noopResult = await
|
|
462
|
-
|
|
463
|
-
freshSnapshot.
|
|
464
|
-
|
|
465
|
-
);
|
|
501
|
+
const noopResult = await commitFile(target, freshSnapshot.text, {
|
|
502
|
+
mode: "overwrite",
|
|
503
|
+
baseRevision: freshSnapshot.baseRevision,
|
|
504
|
+
});
|
|
466
505
|
assert.equal(noopResult.publication, "NOT_PUBLISHED");
|
|
467
506
|
assert.equal(noopResult.publishedRevision, freshSnapshot.baseRevision);
|
|
468
507
|
assert.equal(noopResult.observedRevision, freshSnapshot.baseRevision);
|
|
@@ -477,10 +516,56 @@ test("no-op commit with baseRevision rejects if target was deleted externally",
|
|
|
477
516
|
await rm(target);
|
|
478
517
|
|
|
479
518
|
await assert.rejects(
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
519
|
+
commitFile(target, snapshot.text, {
|
|
520
|
+
mode: "overwrite",
|
|
521
|
+
baseRevision: snapshot.baseRevision,
|
|
522
|
+
}),
|
|
523
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === "PATH_NOT_FOUND",
|
|
485
524
|
);
|
|
486
525
|
}));
|
|
526
|
+
|
|
527
|
+
test("commitFile temporary publication → handles existing and missing parents and preserves other failures", async (t) =>
|
|
528
|
+
withTemp(async (dir) => {
|
|
529
|
+
const originalMkdir = fsPromises.mkdir;
|
|
530
|
+
const originalMkdtemp = fsPromises.mkdtemp;
|
|
531
|
+
for (const scenario of ["existing", "nested", "denied"] as const) {
|
|
532
|
+
const parent = scenario === "nested" ? join(dir, "new", "nested") : dir;
|
|
533
|
+
const target = join(parent, `${scenario}.txt`);
|
|
534
|
+
let mkdirCalls = 0;
|
|
535
|
+
const denied = Object.assign(new Error("denied"), { code: "EPERM" });
|
|
536
|
+
try {
|
|
537
|
+
t.mock.method(fsPromises, "mkdir", async (...args: Parameters<typeof originalMkdir>) => {
|
|
538
|
+
mkdirCalls++;
|
|
539
|
+
if (scenario !== "nested") throw denied;
|
|
540
|
+
return originalMkdir(...args);
|
|
541
|
+
});
|
|
542
|
+
t.mock.method(
|
|
543
|
+
fsPromises,
|
|
544
|
+
"mkdtemp",
|
|
545
|
+
async (...args: Parameters<typeof originalMkdtemp>) => {
|
|
546
|
+
if (scenario === "denied") throw denied;
|
|
547
|
+
return originalMkdtemp(...args);
|
|
548
|
+
},
|
|
549
|
+
);
|
|
550
|
+
syncBuiltinESMExports();
|
|
551
|
+
if (scenario === "denied") {
|
|
552
|
+
await assert.rejects(
|
|
553
|
+
commitFile(target, "data", { mode: "create" }),
|
|
554
|
+
(error: unknown) =>
|
|
555
|
+
error instanceof HashlineError &&
|
|
556
|
+
error.errorCode === "PUBLISH_FAILED" &&
|
|
557
|
+
error.cause === denied,
|
|
558
|
+
);
|
|
559
|
+
await assert.rejects(readFile(target), { code: "ENOENT" });
|
|
560
|
+
} else {
|
|
561
|
+
const result = await commitFile(target, "data", { mode: "create" });
|
|
562
|
+
assert.equal(result.publication, "PUBLISHED");
|
|
563
|
+
assert.equal(await readFile(target, "utf8"), "data");
|
|
564
|
+
}
|
|
565
|
+
assert.equal(mkdirCalls, scenario === "nested" ? 1 : 0);
|
|
566
|
+
} finally {
|
|
567
|
+
t.mock.restoreAll();
|
|
568
|
+
syncBuiltinESMExports();
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}));
|