Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/error-reporter/src/sanitize.byte-length.test.ts
T
2026-07-08 16:15:37 +02:00

48 lines
1.7 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { sanitizeErrorReportValue, __internal } from "./sanitize.js";
/**
* The byte-length computation has three layers: Buffer (Node), TextEncoder
* (browser / edge), and a `value.length` last resort. Node always provides
* Buffer, so these tests stub the globals to exercise the runtime-portability
* fallbacks the source comment promises. The truncation guarantee (large blobs
* never leak verbatim) must hold in every runtime.
*/
describe("sanitizeErrorReportValue byte-length fallbacks", () => {
afterEach(() => {
vi.unstubAllGlobals();
});
it("uses the TextEncoder fallback to truncate when Buffer is unavailable", () => {
vi.stubGlobal("Buffer", undefined);
const huge = "a".repeat(__internal.MAX_STRING_BYTES + 16);
const result = sanitizeErrorReportValue({ blob: huge }) as { blob: string };
expect(result.blob).toMatch(/^<string truncated, \d+ bytes>$/);
expect(result.blob).not.toContain("aaa");
});
it("still truncates via String#length when neither Buffer nor TextEncoder exist", () => {
vi.stubGlobal("Buffer", undefined);
vi.stubGlobal("TextEncoder", undefined);
const huge = "b".repeat(__internal.MAX_STRING_BYTES + 1);
const result = sanitizeErrorReportValue({ blob: huge }) as { blob: string };
expect(result.blob).toMatch(/^<string truncated, \d+ bytes>$/);
expect(result.blob).not.toContain("bbb");
});
it("does not truncate a sub-limit string when running on the fallback paths", () => {
vi.stubGlobal("Buffer", undefined);
vi.stubGlobal("TextEncoder", undefined);
const small = "ok";
const result = sanitizeErrorReportValue({ blob: small }) as { blob: string };
expect(result.blob).toBe(small);
});
});