Files
openclaw/src/auto-reply/tokens.test.ts
Ayaan Zaidi 3d998828b9 fix: stabilize Telegram draft boundaries and suppress NO_REPLY lead leaks (#33169)
* fix: stabilize telegram draft stream message boundaries

* fix: suppress NO_REPLY lead-fragment leaks

* fix: keep underscore guard for non-NO_REPLY prefixes

* fix: skip assistant-start rotation only after real lane rotation

* fix: preserve finalized state when pre-rotation does not force

* fix: reset finalized preview state on message-start boundary

* fix: document Telegram draft boundary + NO_REPLY reliability updates (#33169) (thanks @obviyus)
2026-03-03 22:49:33 +05:30

105 lines
3.9 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { isSilentReplyPrefixText, isSilentReplyText, stripSilentToken } from "./tokens.js";
describe("isSilentReplyText", () => {
it("returns true for exact token", () => {
expect(isSilentReplyText("NO_REPLY")).toBe(true);
});
it("returns true for token with surrounding whitespace", () => {
expect(isSilentReplyText(" NO_REPLY ")).toBe(true);
expect(isSilentReplyText("\nNO_REPLY\n")).toBe(true);
});
it("returns false for undefined/empty", () => {
expect(isSilentReplyText(undefined)).toBe(false);
expect(isSilentReplyText("")).toBe(false);
});
it("returns false for substantive text ending with token (#19537)", () => {
const text = "Here is a helpful response.\n\nNO_REPLY";
expect(isSilentReplyText(text)).toBe(false);
});
it("returns false for substantive text starting with token", () => {
const text = "NO_REPLY but here is more content";
expect(isSilentReplyText(text)).toBe(false);
});
it("returns false for token embedded in text", () => {
expect(isSilentReplyText("Please NO_REPLY to this")).toBe(false);
});
it("works with custom token", () => {
expect(isSilentReplyText("HEARTBEAT_OK", "HEARTBEAT_OK")).toBe(true);
expect(isSilentReplyText("Checked inbox. HEARTBEAT_OK", "HEARTBEAT_OK")).toBe(false);
});
});
describe("stripSilentToken", () => {
it("strips token from end of text", () => {
expect(stripSilentToken("Done.\n\nNO_REPLY")).toBe("Done.");
});
it("does not strip token from start of text", () => {
expect(stripSilentToken("NO_REPLY 👍")).toBe("NO_REPLY 👍");
});
it("strips token with emoji (#30916)", () => {
expect(stripSilentToken("😄 NO_REPLY")).toBe("😄");
});
it("does not strip embedded token suffix without whitespace delimiter", () => {
expect(stripSilentToken("interject.NO_REPLY")).toBe("interject.NO_REPLY");
});
it("strips only trailing occurrence", () => {
expect(stripSilentToken("NO_REPLY ok NO_REPLY")).toBe("NO_REPLY ok");
});
it("returns empty string when only token remains", () => {
expect(stripSilentToken("NO_REPLY")).toBe("");
expect(stripSilentToken(" NO_REPLY ")).toBe("");
});
it("strips token preceded by bold markdown formatting", () => {
expect(stripSilentToken("**NO_REPLY")).toBe("");
expect(stripSilentToken("some text **NO_REPLY")).toBe("some text");
expect(stripSilentToken("reasoning**NO_REPLY")).toBe("reasoning");
});
it("works with custom token", () => {
expect(stripSilentToken("done HEARTBEAT_OK", "HEARTBEAT_OK")).toBe("done");
});
});
describe("isSilentReplyPrefixText", () => {
it("matches uppercase token lead fragments", () => {
expect(isSilentReplyPrefixText("NO")).toBe(true);
expect(isSilentReplyPrefixText("NO_")).toBe(true);
expect(isSilentReplyPrefixText("NO_RE")).toBe(true);
expect(isSilentReplyPrefixText("NO_REPLY")).toBe(true);
expect(isSilentReplyPrefixText(" HEARTBEAT_", "HEARTBEAT_OK")).toBe(true);
});
it("rejects ambiguous natural-language prefixes", () => {
expect(isSilentReplyPrefixText("N")).toBe(false);
expect(isSilentReplyPrefixText("No")).toBe(false);
expect(isSilentReplyPrefixText("no")).toBe(false);
expect(isSilentReplyPrefixText("Hello")).toBe(false);
});
it("keeps underscore guard for non-NO_REPLY tokens", () => {
expect(isSilentReplyPrefixText("HE", "HEARTBEAT_OK")).toBe(false);
expect(isSilentReplyPrefixText("HEART", "HEARTBEAT_OK")).toBe(false);
expect(isSilentReplyPrefixText("HEARTBEAT", "HEARTBEAT_OK")).toBe(false);
expect(isSilentReplyPrefixText("HEARTBEAT_", "HEARTBEAT_OK")).toBe(true);
});
it("rejects non-prefixes and mixed characters", () => {
expect(isSilentReplyPrefixText("NO_X")).toBe(false);
expect(isSilentReplyPrefixText("NO_REPLY more")).toBe(false);
expect(isSilentReplyPrefixText("NO-")).toBe(false);
});
});