|
| 1 | +// E2E text file utility tests cover bounded diagnostic file reads. |
| 2 | +import { mkdirSync, writeFileSync } from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { |
| 6 | +readTextFileBounded, |
| 7 | +readTextFileTail, |
| 8 | +tailText, |
| 9 | +} from "../../scripts/e2e/lib/text-file-utils.mjs"; |
| 10 | +import { cleanupTempDirs, makeTempDir } from "../helpers/temp-dir.js"; |
| 11 | + |
| 12 | +const tempRoots: string[] = []; |
| 13 | + |
| 14 | +function makeTempRoot() { |
| 15 | +return makeTempDir(tempRoots, "openclaw-e2e-text-file-utils-"); |
| 16 | +} |
| 17 | + |
| 18 | +afterEach(() => { |
| 19 | +cleanupTempDirs(tempRoots); |
| 20 | +}); |
| 21 | + |
| 22 | +describe("e2e text file utilities", () => { |
| 23 | +it("keeps short diagnostic text intact and trims long text by byte count", () => { |
| 24 | +expect(tailText("short", 8)).toBe("short"); |
| 25 | +expect(tailText("prefix-tail", 4)).toBe("tail"); |
| 26 | +}); |
| 27 | + |
| 28 | +it("reads only the requested file tail and treats missing or non-file paths as empty", () => { |
| 29 | +const root = makeTempRoot(); |
| 30 | +const file = path.join(root, "output.log"); |
| 31 | +const directory = path.join(root, "nested"); |
| 32 | +mkdirSync(directory); |
| 33 | +writeFileSync(file, "line-one\nline-two\nline-three", "utf8"); |
| 34 | + |
| 35 | +expect(readTextFileTail(file, 10)).toBe("line-three"); |
| 36 | +expect(readTextFileTail(path.join(root, "missing.log"), 10)).toBe(""); |
| 37 | +expect(readTextFileTail(directory, 10)).toBe(""); |
| 38 | +}); |
| 39 | + |
| 40 | +it("returns bounded file text and reports oversize diagnostics with a tail", () => { |
| 41 | +const root = makeTempRoot(); |
| 42 | +const file = path.join(root, "artifact.json"); |
| 43 | +writeFileSync(file, `old prefix\n${"x".repeat(64)}\nfinal tail`, "utf8"); |
| 44 | + |
| 45 | +expect(readTextFileBounded(file, "artifact", 256)).toContain("final tail"); |
| 46 | + |
| 47 | +expect(() => readTextFileBounded(file, "artifact", 32, { tailBytes: 10 })).toThrowError( |
| 48 | +/artifact exceeded 32 bytes: .* Tail: final tail/u, |
| 49 | +); |
| 50 | +}); |
| 51 | +}); |