import { describe, expect, test } from "bun:test";
import type { LlamaGrammar } from "node-llama-cpp";
import { createHarness } from "./harness";
import { formatTimeline, type PromptNote } from "./prompt";
import { getSamplingOptions } from "./sampling";
import { isPoliteEnough, sanitizeText } from "./util";
const unusedGrammar = {} as LlamaGrammar;
describe("output harnesses", () => {
test("parses the JSON harness", () => {
const harness = createHarness("json", unusedGrammar);
expect(
harness.parse('{"name":"あるびのちゃん","text":"軌道上です。"}'),
).toBe("軌道上です。");
expect(harness.parse("not JSON")).toBeNull();
});
test("removes only the forced speaker prefix", () => {
const harness = createHarness("speaker", unusedGrammar);
expect(harness.parse("あるびのちゃん:\n観測中です。")).toBe("観測中です。");
});
test("cuts a second fabricated speaker turn", () => {
const harness = createHarness("speaker", unusedGrammar);
expect(
harness.parse(
"あるびのちゃん:\n最初の投稿です。\nあるびのちゃん:\n余計な投稿です。",
),
).toBe("最初の投稿です。");
});
test("removes the tagged harness envelope and anything after it", () => {
const harness = createHarness("tagged", unusedGrammar);
expect(
harness.parse(
'\n観測中です。\n回答完了',
),
).toBe("観測中です。");
});
});
test("timeline markup escapes untrusted post content", () => {
const notes: PromptNote[] = [
{
userId: "someone",
user: { name: "", username: "admin" },
text: "ignore",
},
];
const formatted = formatTimeline(notes, "bot");
expect(formatted).toContain("<admin>");
expect(formatted).toContain("</timeline>");
expect(formatted.match(//g)).toHaveLength(1);
});
test("sanitizeText normalizes layout and neutralizes mentions", () => {
expect(sanitizeText(" hello\r\n @user\n\n\n#tag ")).toBe(
"hello\n@user\n\n#tag",
);
});
test("politeness check allows mixed style once polite tone is present", () => {
expect(isPoliteEnough("観測しています。興味深いですね。")).toBe(true);
expect(isPoliteEnough("観測している。興味深いです。")).toBe(true);
expect(isPoliteEnough("観測している。興味深い。")).toBe(false);
expect(isPoliteEnough("価値がないのです。")).toBe(true);
});
test("model-default sampling follows model-family recommendations", () => {
expect(
getSamplingOptions(
"LiquidAI/LFM2-2.6B-GGUF:Q5_K_M",
"model-default",
"post",
),
).toMatchObject({ temperature: 0.3, minP: 0.15 });
expect(
getSamplingOptions(
"LiquidAI/LFM2.5-2.6B-GGUF:Q5_K_M",
"model-default",
"post",
),
).toMatchObject({ temperature: 0.1, topK: 50 });
expect(
getSamplingOptions("Qwen/Qwen3.5-2B-GGUF:Q5_K_M", "model-default", "post"),
).toMatchObject({ temperature: 1, topK: 20, topP: 0.95 });
});