Files
moltbot/src/agents/pi-embedded-helpers/errors.test.ts
2026-04-29 21:59:46 +08:00

39 lines
1.7 KiB
TypeScript

import type { AssistantMessage } from "@mariozechner/pi-ai";
import { describe, expect, it } from "vitest";
import { MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE } from "../../shared/assistant-error-format.js";
import { makeAssistantMessageFixture } from "../test-helpers/assistant-message-fixtures.js";
import { formatAssistantErrorText } from "./errors.js";
describe("formatAssistantErrorText streaming JSON parse classification", () => {
const makeAssistantError = (errorMessage: string): AssistantMessage =>
makeAssistantMessageFixture({
errorMessage,
content: [{ type: "text", text: errorMessage }],
});
it("suppresses transport-classified malformed streaming fragments", () => {
const msg = makeAssistantError(MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE);
expect(formatAssistantErrorText(msg)).toBe(
"LLM streaming response contained a malformed fragment. Please try again.",
);
});
it("does not suppress unclassified JSON.parse text", () => {
const msg = makeAssistantError(
"Expected ',' or '}' after property value in JSON at position 334 (line 1 column 335)",
);
expect(formatAssistantErrorText(msg)).toBe(
"Expected ',' or '}' after property value in JSON at position 334 (line 1 column 335)",
);
});
it("keeps non-streaming provider request-validation syntax diagnostics", () => {
const msg = makeAssistantError(
'{"type":"error","error":{"type":"invalid_request_error","message":"Expected value in JSON at position 12 for messages.0.content"}}',
);
expect(formatAssistantErrorText(msg)).toBe(
"LLM request rejected: Expected value in JSON at position 12 for messages.0.content",
);
});
});