refactor: eliminate remaining duplicate blocks across draft streams and tests

This commit is contained in:
Peter Steinberger
2026-02-21 23:56:58 +00:00
parent abf3dfc375
commit ad1c07e7c0
16 changed files with 316 additions and 199 deletions

View File

@@ -1,24 +1,18 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { createTrackedTempDirs } from "../test-utils/tracked-temp-dirs.js";
import { SafeOpenError, openFileWithinRoot, readLocalFileSafely } from "./fs-safe.js";
const tempDirs: string[] = [];
async function makeTempDir(prefix: string): Promise<string> {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
tempDirs.push(dir);
return dir;
}
const tempDirs = createTrackedTempDirs();
afterEach(async () => {
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
await tempDirs.cleanup();
});
describe("fs-safe", () => {
it("reads a local file safely", async () => {
const dir = await makeTempDir("openclaw-fs-safe-");
const dir = await tempDirs.make("openclaw-fs-safe-");
const file = path.join(dir, "payload.txt");
await fs.writeFile(file, "hello");
@@ -29,14 +23,14 @@ describe("fs-safe", () => {
});
it("rejects directories", async () => {
const dir = await makeTempDir("openclaw-fs-safe-");
const dir = await tempDirs.make("openclaw-fs-safe-");
await expect(readLocalFileSafely({ filePath: dir })).rejects.toMatchObject({
code: "not-file",
});
});
it("enforces maxBytes", async () => {
const dir = await makeTempDir("openclaw-fs-safe-");
const dir = await tempDirs.make("openclaw-fs-safe-");
const file = path.join(dir, "big.bin");
await fs.writeFile(file, Buffer.alloc(8));
@@ -46,7 +40,7 @@ describe("fs-safe", () => {
});
it.runIf(process.platform !== "win32")("rejects symlinks", async () => {
const dir = await makeTempDir("openclaw-fs-safe-");
const dir = await tempDirs.make("openclaw-fs-safe-");
const target = path.join(dir, "target.txt");
const link = path.join(dir, "link.txt");
await fs.writeFile(target, "target");
@@ -58,8 +52,8 @@ describe("fs-safe", () => {
});
it("blocks traversal outside root", async () => {
const root = await makeTempDir("openclaw-fs-safe-root-");
const outside = await makeTempDir("openclaw-fs-safe-outside-");
const root = await tempDirs.make("openclaw-fs-safe-root-");
const outside = await tempDirs.make("openclaw-fs-safe-outside-");
const file = path.join(outside, "outside.txt");
await fs.writeFile(file, "outside");
@@ -72,8 +66,8 @@ describe("fs-safe", () => {
});
it.runIf(process.platform !== "win32")("blocks symlink escapes under root", async () => {
const root = await makeTempDir("openclaw-fs-safe-root-");
const outside = await makeTempDir("openclaw-fs-safe-outside-");
const root = await tempDirs.make("openclaw-fs-safe-root-");
const outside = await tempDirs.make("openclaw-fs-safe-outside-");
const target = path.join(outside, "outside.txt");
const link = path.join(root, "link.txt");
await fs.writeFile(target, "outside");
@@ -88,7 +82,7 @@ describe("fs-safe", () => {
});
it("returns not-found for missing files", async () => {
const dir = await makeTempDir("openclaw-fs-safe-");
const dir = await tempDirs.make("openclaw-fs-safe-");
const missing = path.join(dir, "missing.txt");
await expect(readLocalFileSafely({ filePath: missing })).rejects.toBeInstanceOf(SafeOpenError);