fix: sanitize user-facing errors and strip final tags

Co-authored-by: Drake Thomsen <drake.thomsen@example.com>
This commit is contained in:
Peter Steinberger
2026-01-16 03:00:40 +00:00
parent d9f9e93dee
commit 23e4ba845c
13 changed files with 239 additions and 31 deletions

View File

@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import { sanitizeUserFacingText } from "./pi-embedded-helpers.js";
describe("sanitizeUserFacingText", () => {
it("strips final tags", () => {
expect(sanitizeUserFacingText("<final>Hello</final>")).toBe("Hello");
expect(sanitizeUserFacingText("Hi <final>there</final>!")).toBe("Hi there!");
});
it("does not clobber normal numeric prefixes", () => {
expect(sanitizeUserFacingText("202 results found")).toBe("202 results found");
expect(sanitizeUserFacingText("400 days left")).toBe("400 days left");
});
it("sanitizes role ordering errors", () => {
const result = sanitizeUserFacingText("400 Incorrect role information");
expect(result).toContain("Message ordering conflict");
});
it("sanitizes HTTP status errors with error hints", () => {
expect(sanitizeUserFacingText("500 Internal Server Error")).toBe(
"The AI service returned an error. Please try again.",
);
});
it("sanitizes raw API error payloads", () => {
const raw = '{"type":"error","error":{"message":"Something exploded","type":"server_error"}}';
expect(sanitizeUserFacingText(raw)).toBe(
"The AI service returned an error. Please try again.",
);
});
});