diff --git a/src/agents/skills-install.download.test.ts b/src/agents/skills-install.download.test.ts index 918da9c661d..fe93bebedd1 100644 --- a/src/agents/skills-install.download.test.ts +++ b/src/agents/skills-install.download.test.ts @@ -27,37 +27,49 @@ vi.mock("../infra/fs-pinned-write-helper.js", async () => { const fs = await import("node:fs/promises"); const path = await import("node:path"); const { pipeline } = await import("node:stream/promises"); + + type PinnedWriteParams = { + rootPath: string; + relativeParentPath: string; + basename: string; + mkdir: boolean; + mode: number; + input: + | { kind: "buffer"; data: string | Buffer; encoding?: BufferEncoding } + | { kind: "stream"; stream: NodeJS.ReadableStream }; + }; + + async function resolveParentPath(params: PinnedWriteParams): Promise { + const parentPath = params.relativeParentPath + ? path.join(params.rootPath, ...params.relativeParentPath.split("/")) + : params.rootPath; + if (params.mkdir) { + await fs.mkdir(parentPath, { recursive: true }); + } + return parentPath; + } + + async function writePinnedTarget(params: PinnedWriteParams, targetPath: string) { + if (params.input.kind === "buffer") { + await fs.writeFile(targetPath, params.input.data, { + encoding: params.input.encoding, + mode: params.mode, + }); + return; + } + const handle = await fs.open(targetPath, "w", params.mode); + try { + await pipeline(params.input.stream, handle.createWriteStream()); + } finally { + await handle.close().catch(() => undefined); + } + } + return { - runPinnedWriteHelper: async (params: { - rootPath: string; - relativeParentPath: string; - basename: string; - mkdir: boolean; - mode: number; - input: - | { kind: "buffer"; data: string | Buffer; encoding?: BufferEncoding } - | { kind: "stream"; stream: NodeJS.ReadableStream }; - }) => { - const parentPath = params.relativeParentPath - ? path.join(params.rootPath, ...params.relativeParentPath.split("/")) - : params.rootPath; - if (params.mkdir) { - await fs.mkdir(parentPath, { recursive: true }); - } + runPinnedWriteHelper: async (params: PinnedWriteParams) => { + const parentPath = await resolveParentPath(params); const targetPath = path.join(parentPath, params.basename); - if (params.input.kind === "buffer") { - await fs.writeFile(targetPath, params.input.data, { - encoding: params.input.encoding, - mode: params.mode, - }); - } else { - const handle = await fs.open(targetPath, "w", params.mode); - try { - await pipeline(params.input.stream, handle.createWriteStream()); - } finally { - await handle.close().catch(() => undefined); - } - } + await writePinnedTarget(params, targetPath); const stat = await fs.stat(targetPath); return { dev: stat.dev, ino: stat.ino }; },