Files
openclaw/src/agents/deepseek-text-filter.test.ts
2026-05-09 05:03:10 -04:00

70 lines
2.1 KiB
TypeScript
Raw 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 { describe, expect, it } from "vitest";
import { createDeepSeekTextFilter } from "./deepseek-text-filter.js";
function filteredText(chunks: readonly string[]) {
const filter = createDeepSeekTextFilter();
return [...chunks.flatMap((chunk) => filter.push(chunk)), ...filter.flush()].join("");
}
describe("createDeepSeekTextFilter", () => {
it.each([
{
name: "tool_use_error in visible text",
chunks: [
"before <DSMLtool_use_error><tool_name>write</tool_name></DSMLtool_use_error> after",
],
expected: "before after",
},
{
name: "split open token",
chunks: ["before ", "<DS", "MLtool_calls>body</DSMLtool_calls>", " after"],
expected: "before after",
},
{
name: "singular tool_call close",
chunks: ["<|DSML|tool_call>read</|DSML|tool_call> visible"],
expected: " visible",
},
{
name: "singular open plural close",
chunks: ["<|DS", "ML|tool_call>read\n", "</|DSML|tool_calls>"],
expected: "",
},
{
name: "unterminated block",
chunks: ["visible <DSMLtool_calls>partial body, no close"],
expected: "visible ",
},
{
name: "multiple blocks",
chunks: [
"a<DSMLtool_use_error>x</DSMLtool_use_error>b<DSMLfunction_calls>y</DSMLfunction_calls>c",
],
expected: "abc",
},
])("drops DSML: $name", ({ chunks, expected }) => {
const text = filteredText(chunks);
expect(text).toBe(expected);
expect(text).not.toContain("DSML");
});
it("holds a partial open token until it can classify it", () => {
const filter = createDeepSeekTextFilter();
const mid = filter.push("safe text<DSM");
expect(mid.join("")).toBe("safe text");
const all = [
...mid,
...filter.push("Ltool_calls>body</DSMLtool_calls> done"),
...filter.flush(),
];
expect(all.join("")).toBe("safe text done");
});
it("emits normal short text immediately", () => {
const filter = createDeepSeekTextFilter();
expect(filter.push("hello")).toEqual(["hello"]);
expect(filter.flush()).toEqual([]);
});
});