fix: preserve legacy text across trans helpers

This commit is contained in:
Codex
2026-07-10 19:41:44 +02:00
parent d07830dd17
commit 9f8e325ea5
8 changed files with 253 additions and 64 deletions
+44
View File
@@ -167,6 +167,50 @@ describe("apply-patch v2 fs bulk update path", () => {
}
});
test("preserves legacy single-byte content during ASCII localized replacement", async () => {
const original = Buffer.concat([
Buffer.from("static int old_value = 1;\r\n// legacy ", "ascii"),
Buffer.from([0xca]),
Buffer.from(" comment\r\n", "ascii"),
]);
let captured: ApplyPatchV2BulkReplacementWritePlan | undefined;
const fs: ApplyPatchV2FileSystem = {
async stat() { throw new Error("stat should not be used"); },
async readBlock() { throw new Error("readBlock should not be used"); },
async writeFile() { throw new Error("writeFile should not be used"); },
async deleteFile() { throw new Error("deleteFile should not be used"); },
async readFiles(paths) {
return new Map(paths.map((path) => [path, {
content: original.toString("latin1"),
bytes: original,
encodingStyle: "legacy-single-byte" as const,
}]));
},
async applyReplacementsBulk(_paths, plans) {
captured = plans.get("src/legacy.c");
},
};
const exitCode = await runApplyPatchV2({
executor: { fs },
stdin: Readable.from([[
"*** Begin Patch",
"*** Update File: src/legacy.c",
"@@",
"-static int old_value = 1;",
"+static int new_value = 2;",
"*** End Patch",
].join("\n")]),
stdout: new CaptureWritable(),
stderr: new CaptureWritable(),
});
expect(exitCode).toBe(0);
expect(captured?.encodingStyle).toBe("legacy-single-byte");
expect(captured?.newlineStyle).toBe("crlf");
expect(captured?.originalBytes).toBe(original.length);
expect(captured?.originalSha256).toBe(createHash("sha256").update(original).digest("hex"));
});
test("bulk read failure falls back to block reads but still writes through replacement bulk", async () => {
const files = new Map<string, string>([
["a.md", "first\nold-a\nlast\n"],