/** * A brief is fitted, never refused; a refusal names the field and the fix. * The long input below is the shape of the 15 Nov 2026 case: a pasted chat * log in a "what would you like to write about?" box. */ import { test } from "node:test"; import assert from "node:assert/strict"; import { PROMPT_TEXT_MAX_CHARS, fitPromptText, explainInputIssues } from "@bitbaum/ai-kit"; const CHAT = Array.from( { length: 40 }, (_, i) => `[11:${String(i).padStart(2, "0")}, 15.11.2026] g: message number ${i} with some words in it`, ).join("\n"); test("text within the limit is kept whole, trimmed", () => { const r = fitPromptText(` ${CHAT} `); assert.equal(r.text, CHAT); assert.equal(r.clipped, false); assert.equal(r.chars, CHAT.length); }); test("non-strings become empty, never throw", () => { for (const v of [undefined, null, 42, {}, []]) { assert.deepEqual(fitPromptText(v), { text: "", clipped: false, chars: 0 }); } }); test("long text keeps its start and end and says how much was left out", () => { const r = fitPromptText(CHAT, 600); assert.equal(r.clipped, true); assert.ok(r.text.length <= 600, `length ${r.text.length}`); assert.ok(r.text.startsWith("[11:00, 15.11.2026]")); assert.ok(r.text.endsWith("message number 39 with some words in it")); assert.match(r.text, /\[… [\d,]+ characters left out …\]/); }); test("the default limit holds a pasted page of notes", () => { assert.ok(PROMPT_TEXT_MAX_CHARS >= 8000); const huge = "word ".repeat(10_000); const r = fitPromptText(huge); assert.ok(r.text.length <= PROMPT_TEXT_MAX_CHARS); assert.equal(r.clipped, true); }); // zod v4 issue shapes, copied from real safeParse output. test("too long names the field, its length, the limit and the fix", () => { const msg = explainInputIssues( [{ code: "too_big", origin: "string", maximum: 300, path: ["topic"], message: "Too big" }], { input: { topic: CHAT }, labels: { topic: "Your topic" } }, ); assert.match(msg, /^Your topic is [\d,]+ characters; the limit is 300\. Shorten it/); assert.doesNotMatch(msg, /invalid request/i); }); test("missing, empty, enum and type problems each say what to do", () => { assert.equal( explainInputIssues([{ code: "invalid_type", expected: "string", path: ["parentText"] }], { input: {}, }), "Parent text is missing. Fill it in and try again.", ); assert.equal( explainInputIssues([{ code: "too_small", origin: "string", minimum: 1, path: ["prompt"] }], { input: { prompt: "" }, }), "Prompt is empty. Fill it in and try again.", ); assert.equal( explainInputIssues([{ code: "invalid_value", values: ["post", "article"], path: ["mode"] }]), "Mode must be one of: post, article. Pick one and try again.", ); assert.equal( explainInputIssues([{ code: "invalid_type", expected: "number", path: ["count"] }], { input: { count: "x" }, }), "Count must be a number. Correct it and try again.", ); }); test("accepts a ZodError-like object and never returns a bare phrase", () => { assert.match(explainInputIssues({ issues: [] }), /reload the page and try again/); assert.match(explainInputIssues(null), /reload the page and try again/); assert.match( explainInputIssues({ issues: [{ code: "custom", path: ["x"], message: "Pick a date in the future." }], }), /^X: Pick a date in the future\. Correct it/, ); }); test("duplicate problems are said once, and at most three are listed", () => { const same = { code: "too_big", origin: "array", maximum: 2, path: ["tags"] }; const many = ["a", "b", "c", "d", "e"].map((k) => ({ code: "invalid_type", expected: "string", path: [k], })); assert.equal(explainInputIssues([same, same]).split(". ").length, 2); assert.equal((explainInputIssues(many, { input: {} }).match(/is missing/g) ?? []).length, 3); });