Files
heidi/lib/domain/chat/answer.test.ts
CatoandClaude Opus 5.5 a3c8b86aeb Mark answers from the last-resort fallback model (#375)
Live 2026-11-27, with every stronger model busy, openrouter/free translated «Chunnsch au no verbi hüt Abig?» as "Das Kind arbeitet auch noch heute Abend?". The owner chose to keep it and warn: such answers carry fallback: true (set on the server, decoded strictly) and show a note asking the reader to check before sending. The model is still not named in the answer view.


Claude-Session: https://claude.ai/code/session_019MDtmtfseicWGK2CMC2U5r

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 19:34:20 +01:00

139 lines
5.9 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { decodeAnswer } from "./answer.ts";
import { titleFrom, MAX_TITLE } from "./title.ts";
/**
* These exist because an `Answer` comes back out of `jsonb`, and a row written
* weeks ago was produced by a different version of the prompt. `as Answer` on
* such a row is how `a.glosses.map(...)` throws in production.
*/
const full = {
text: "Sie fragen, ob jemand vorbeikommt.",
mode: "understand",
dialect: "Ja, chume gern.",
dialectClean: true,
dialectFlags: [],
tone: "warm",
toneNote: "Freundlich.",
glosses: [{ form: "Chunnsch", standard: "kommst du", english: "are you coming", rule: "k → ch" }],
suggestions: [{ label: "neutral", text: "Ja, chume.", english: "Yes, I'm coming.", clean: true, flags: [] }],
note: "Kurz gehalten.",
model: "openai/gpt-oss-120b",
};
test("a well-formed answer survives the round trip", () => {
const a = decodeAnswer(full);
assert.ok(a);
assert.equal(a!.text, full.text);
assert.equal(a!.glosses[0].form, "Chunnsch");
assert.equal(a!.suggestions[0].text, "Ja, chume.");
assert.equal(a!.tone, "warm");
assert.equal(a!.model, "openai/gpt-oss-120b");
});
test("all three modes survive the round trip, and an unknown one falls back", () => {
for (const mode of ["understand", "produce", "answer"] as const) {
assert.equal(decodeAnswer({ ...full, mode })!.mode, mode);
}
assert.equal(decodeAnswer({ ...full, mode: "chat" })!.mode, "understand");
});
test("anything that is not an answer decodes to null, not a crash", () => {
for (const junk of [null, undefined, "", "a string", 42, [], {}]) {
assert.equal(decodeAnswer(junk), null, `${JSON.stringify(junk)} should not decode`);
}
});
test("a row from before a field existed loses the field, not the answer", () => {
// The actual failure this guards: an old row with no `glosses` key at all.
const old = { text: "Etwas Altes.", model: "old-model" };
const a = decodeAnswer(old);
assert.ok(a, "an answer with text is still an answer");
assert.deepEqual(a!.glosses, [], "missing glosses becomes an empty list, never undefined");
assert.deepEqual(a!.suggestions, []);
});
test("an unrecognised tone is dropped, and the note it labelled is kept", () => {
const a = decodeAnswer({ ...full, tone: "cheerful" });
assert.equal(a!.tone, undefined, "a tone outside the union is not a tone");
assert.equal(a!.toneNote, "Freundlich.", "the sentence survives its missing heading");
});
test("a half-written gloss is dropped rather than rendered blank", () => {
const a = decodeAnswer({ ...full, glosses: [{ standard: "kommst du" }, full.glosses[0], null, "nonsense"] });
assert.deepEqual(a!.glosses.map((g) => g.form), ["Chunnsch"]);
});
test("a suggestion with no verdict is treated as clean", () => {
// An old row never met the gate. Showing an unexplained warning on a line
// nothing judged would invent a problem the learner cannot act on.
const a = decodeAnswer({ ...full, suggestions: [{ label: "x", text: "Hoi." }] });
assert.equal(a!.suggestions[0].clean, true);
assert.deepEqual(a!.suggestions[0].flags, []);
});
test("an answer with no model says so rather than borrowing today's", () => {
const a = decodeAnswer({ text: "Ohne Provenienz." });
assert.equal(a!.model, "unknown");
});
test("a title is the first line, in one line", () => {
assert.equal(titleFrom("Chunnsch au no verbi hüt Abig?"), "Chunnsch au no verbi hüt Abig?");
assert.equal(titleFrom(" zwei\nZeilen "), "zwei Zeilen");
assert.equal(titleFrom(" "), "");
});
test("a long title is cut at a word boundary, not mid-word", () => {
const long = "Chunnsch au no verbi hüt Abig oder hesch scho öppis anders abgmacht mit dene andere?";
const title = titleFrom(long);
assert.ok(title.length <= MAX_TITLE + 1, `too long: ${title.length}`);
assert.ok(title.endsWith("…"));
assert.ok(!/\s…$/.test(title), "no dangling space before the ellipsis");
// The cut lands between words, so the last visible word is a whole one.
const lastWord = title.slice(0, -1).trim().split(" ").pop()!;
assert.ok(long.split(/\s+/).includes(lastWord), `"${lastWord}" is not a whole word from the input`);
});
test("one enormous word is truncated rather than emptied", () => {
const title = titleFrom("x".repeat(200));
assert.ok(title.length <= MAX_TITLE + 1);
assert.ok(title.startsWith("xxx"));
});
test("next moves survive the round trip through jsonb", () => {
const a = decodeAnswer({
text: "Sie fragen, ob jemand vorbeikommt.",
next: [{ id: "reply" }, { id: "rephrase", axis: "firmer" }],
});
assert.deepEqual(a?.next, [{ id: "reply" }, { id: "rephrase", axis: "firmer" }]);
});
test("a move the model invented is dropped, not rendered", () => {
// Same rule as `tone`: a chip whose label cannot be looked up is a blank
// button that does nothing when pressed, which is worse than no button.
const a = decodeAnswer({ text: "x", next: [{ id: "book-a-flight" }, { id: "rephrase", axis: "sarcastic" }] });
assert.equal(a?.next, undefined, "no valid moves means no `next` at all, not an empty row of chips");
});
test("an answer written before moves existed simply has none", () => {
const a = decodeAnswer({ text: "x", glosses: [], suggestions: [] });
assert.equal(a?.next, undefined);
});
test("stored replies are re-bounded: strings only, short, distinct, at most four", () => {
const a = decodeAnswer({
text: "Gut.",
replies: ["Ja", 3, "ja", "x".repeat(80), "B", "C", "D", "E"],
})!;
assert.deepEqual(a.replies, ["Ja", "B", "C", "D"]);
assert.equal("replies" in decodeAnswer({ text: "Gut.", replies: "Ja" })!, false);
});
test("a fallback flag survives a round trip, and only true counts", async () => {
const { decodeAnswer } = await import("./answer.ts");
assert.equal(decodeAnswer({ text: "x", model: "m", fallback: true })?.fallback, true);
assert.equal(decodeAnswer({ text: "x", model: "m", fallback: "yes" })?.fallback, undefined);
});