fix: use fs backend for host apply-patch

This commit is contained in:
Codex
2026-06-26 18:12:44 +00:00
parent c8fb148db3
commit 590feb7d9f
7 changed files with 175 additions and 20 deletions
+58
View File
@@ -1,5 +1,7 @@
import { createHash } from "node:crypto";
import { describe, expect, test } from "bun:test";
import {
createPosixApplyPatchFileSystem,
createSshStdoutForwarder,
formatSshStdoutTruncationHint,
parseSshInvocation,
@@ -9,6 +11,10 @@ import {
windowsPowerShellScriptPrelude,
} from "./ssh";
function sha256Hex(text: string): string {
return createHash("sha256").update(text, "utf8").digest("hex");
}
describe("ssh windows PowerShell safety prelude", () => {
test("shadows ConvertTo-Json and strips filesystem ETS metadata", () => {
const prelude = windowsPowerShellScriptPrelude("C:\\test");
@@ -59,6 +65,58 @@ describe("ssh windows fs read-only operations", () => {
});
});
describe("ssh host apply-patch fs backend", () => {
test("uses POSIX bulk fs operations for host routes", async () => {
const invocation = parseSshInvocation("D601:/mnt/f/Work/ConStart", ["apply-patch"]);
const text = "alpha\nold\nomega\n";
const calls: Array<{ operation: string; args: string[]; input?: string }> = [];
const fs = createPosixApplyPatchFileSystem(invocation, async (command, input) => {
const operation = command[4] ?? "";
const args = command.slice(5);
calls.push({ operation, args, input });
if (operation === "read-bulk-b64") {
return {
exitCode: 0,
stderr: "",
stdout: [
"UNIDESK_APPLY_PATCH_V2_BULK_READ 1",
[
Buffer.from("docs/test.md", "utf8").toString("base64"),
String(Buffer.byteLength(text, "utf8")),
sha256Hex(text),
Buffer.from(text, "utf8").toString("base64"),
].join(" "),
"",
].join("\n"),
};
}
if (operation === "apply-replacements-bulk-stdin") {
expect(input).toContain(Buffer.from("docs/test.md", "utf8").toString("base64"));
return { exitCode: 0, stdout: "", stderr: "" };
}
throw new Error(`unexpected operation ${operation}`);
});
const files = await fs.readFiles!(["docs/test.md"]);
await fs.applyReplacementsBulk!(["docs/test.md"], new Map([[
"docs/test.md",
{
path: "docs/test.md",
originalBytes: Buffer.byteLength(text, "utf8"),
originalSha256: sha256Hex(text),
finalBytes: Buffer.byteLength("alpha\nnew\nomega\n", "utf8"),
finalSha256: sha256Hex("alpha\nnew\nomega\n"),
replacements: [[1, 1, ["new"]]],
},
]]));
expect(files.get("docs/test.md")).toBe(text);
expect(calls.map((call) => call.operation)).toEqual(["read-bulk-b64", "apply-replacements-bulk-stdin"]);
expect(calls[0]?.args).toEqual(["docs/test.md"]);
expect(calls[1]?.args).toEqual(["1"]);
});
});
describe("ssh stdout bounded streaming", () => {
test("uses bounded defaults and clamps env override", () => {
expect(sshStdoutStreamMaxBytes({} as NodeJS.ProcessEnv)).toBe(256 * 1024);