mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 08:50:43 +00:00
test: split skills download write fixture
This commit is contained in:
@@ -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<string> {
|
||||
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 };
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user