From c9758bf2a090a747ec7fd794d1c895d3deda83ab Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 21 Jun 2026 09:58:18 +0200 Subject: [PATCH] test(scripts): cover e2e text file utilities --- scripts/test-projects.test-support.mjs | 1 + test/scripts/e2e-text-file-utils.test.ts | 51 ++++++++++++++++++++++++ test/scripts/test-projects.test.ts | 1 + 3 files changed, 53 insertions(+) create mode 100644 test/scripts/e2e-text-file-utils.test.ts diff --git a/scripts/test-projects.test-support.mjs b/scripts/test-projects.test-support.mjs index 737a74708131..4bdc695322c8 100644 --- a/scripts/test-projects.test-support.mjs +++ b/scripts/test-projects.test-support.mjs @@ -1749,6 +1749,7 @@ const TOOLING_SOURCE_TEST_TARGETS = new Map([ "scripts/e2e/lib/openwebui/http-probe.mjs", ["test/e2e/qa-lab/runtime/openwebui-probe.e2e.test.ts"], ], + ["scripts/e2e/lib/text-file-utils.mjs", ["test/scripts/e2e-text-file-utils.test.ts"]], [ "scripts/e2e/openwebui-docker.sh", [ diff --git a/test/scripts/e2e-text-file-utils.test.ts b/test/scripts/e2e-text-file-utils.test.ts new file mode 100644 index 000000000000..63bbe5b84df6 --- /dev/null +++ b/test/scripts/e2e-text-file-utils.test.ts @@ -0,0 +1,51 @@ +// E2E text file utility tests cover bounded diagnostic file reads. +import { mkdirSync, writeFileSync } from "node:fs"; +import path from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { + readTextFileBounded, + readTextFileTail, + tailText, +} from "../../scripts/e2e/lib/text-file-utils.mjs"; +import { cleanupTempDirs, makeTempDir } from "../helpers/temp-dir.js"; + +const tempRoots: string[] = []; + +function makeTempRoot() { + return makeTempDir(tempRoots, "openclaw-e2e-text-file-utils-"); +} + +afterEach(() => { + cleanupTempDirs(tempRoots); +}); + +describe("e2e text file utilities", () => { + it("keeps short diagnostic text intact and trims long text by byte count", () => { + expect(tailText("short", 8)).toBe("short"); + expect(tailText("prefix-tail", 4)).toBe("tail"); + }); + + it("reads only the requested file tail and treats missing or non-file paths as empty", () => { + const root = makeTempRoot(); + const file = path.join(root, "output.log"); + const directory = path.join(root, "nested"); + mkdirSync(directory); + writeFileSync(file, "line-one\nline-two\nline-three", "utf8"); + + expect(readTextFileTail(file, 10)).toBe("line-three"); + expect(readTextFileTail(path.join(root, "missing.log"), 10)).toBe(""); + expect(readTextFileTail(directory, 10)).toBe(""); + }); + + it("returns bounded file text and reports oversize diagnostics with a tail", () => { + const root = makeTempRoot(); + const file = path.join(root, "artifact.json"); + writeFileSync(file, `old prefix\n${"x".repeat(64)}\nfinal tail`, "utf8"); + + expect(readTextFileBounded(file, "artifact", 256)).toContain("final tail"); + + expect(() => readTextFileBounded(file, "artifact", 32, { tailBytes: 10 })).toThrowError( + /artifact exceeded 32 bytes: .* Tail: final tail/u, + ); + }); +}); diff --git a/test/scripts/test-projects.test.ts b/test/scripts/test-projects.test.ts index 44702067f9e0..331c7fe82196 100644 --- a/test/scripts/test-projects.test.ts +++ b/test/scripts/test-projects.test.ts @@ -420,6 +420,7 @@ describe("scripts/test-projects changed-target routing", () => { "scripts/e2e/lib/openwebui/http-probe.mjs", ["test/e2e/qa-lab/runtime/openwebui-probe.e2e.test.ts"], ], + ["scripts/e2e/lib/text-file-utils.mjs", ["test/scripts/e2e-text-file-utils.test.ts"]], [ "scripts/e2e/lib/plugins/npm-registry-server.mjs", ["test/scripts/plugins-assertions.test.ts"],