Files
ai-kit/test/answer-prefix.test.js

32 lines
1.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test } from "node:test";
import assert from "node:assert/strict";
import { classifyAnswerPrefix } from "@bitbaum/ai-kit";
test("a narrated XML call never becomes visible prose, across every chunk boundary", () => {
const text =
"<tool_call> <function=search_records> <parameter=query> room </parameter> </function> </tool_call>";
for (let boundary = 0; boundary < text.length; boundary++) {
const first = classifyAnswerPrefix(text.slice(0, boundary));
assert.notEqual(first, "answer", `protocol leaked at boundary ${boundary}`);
}
assert.equal(classifyAnswerPrefix(text), "protocol");
});
test("recognises the function form, plural wrapper and whitespace without executing anything", () => {
for (const text of ["<function=search_records>", "<tool_calls>", " \n<TOOL_CALL >"])
assert.equal(classifyAnswerPrefix(text), "protocol");
for (const text of ["", " \n", "<", "<to", "<tool_call"])
assert.equal(classifyAnswerPrefix(text), "pending");
});
test("ordinary answers, HTML and quoted code stay visible", () => {
for (const text of [
"Open your project’s room.",
"<div>Hello</div>",
"<tool_calling> is a name",
"```xml\n<tool_call>\n```",
"Tool: a hammer",
])
assert.equal(classifyAnswerPrefix(text), "answer");
});