|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { createAnthropicAdapter } from "../src/adapters/anthropic"; |
| 3 | +import type { OcxMessage, OcxParsedRequest, OcxProviderConfig } from "../src/types"; |
| 4 | + |
| 5 | +const provider = { adapter: "anthropic", baseUrl: "https://api.anthropic.com", apiKey: "sk-x", authMode: "apiKey" } as unknown as OcxProviderConfig; |
| 6 | + |
| 7 | +function parsed(messages: OcxMessage[]): OcxParsedRequest { |
| 8 | + return { |
| 9 | + modelId: "anthropic/claude-sonnet-4.5", |
| 10 | + stream: false, |
| 11 | + options: {}, |
| 12 | + context: { messages }, |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +function bodyOf(p: OcxParsedRequest): { messages: Array<{ role: string; content: unknown }> } { |
| 17 | + const { body } = createAnthropicAdapter(provider).buildRequest(p); |
| 18 | + return JSON.parse(typeof body === "string" ? body : JSON.stringify(body)) as { messages: Array<{ role: string; content: unknown }> }; |
| 19 | +} |
| 20 | + |
| 21 | +describe("anthropic empty content block guard", () => { |
| 22 | + test("user message with empty string content is replaced with placeholder", () => { |
| 23 | + const body = bodyOf(parsed([ |
| 24 | + { role: "user", content: "", timestamp: 0 }, |
| 25 | + ])); |
| 26 | + const msg = body.messages[0] as { role: string; content: string }; |
| 27 | + expect(msg.content).toBe("(empty)"); |
| 28 | + }); |
| 29 | + |
| 30 | + test("user message with empty text part is filtered out", () => { |
| 31 | + const body = bodyOf(parsed([ |
| 32 | + { role: "user", content: [{ type: "text", text: "" }], timestamp: 0 }, |
| 33 | + ])); |
| 34 | + const msg = body.messages[0] as { role: string; content: string }; |
| 35 | + // Empty part filtered -> falls back to placeholder string |
| 36 | + expect(msg.content).toBe("(empty)"); |
| 37 | + }); |
| 38 | + |
| 39 | + test("user message with mixed empty and non-empty parts keeps only non-empty", () => { |
| 40 | + const body = bodyOf(parsed([ |
| 41 | + { role: "user", content: [{ type: "text", text: "" }, { type: "text", text: "hello" }], timestamp: 0 }, |
| 42 | + ])); |
| 43 | + const msg = body.messages[0] as { role: string; content: Array<{ type: string; text: string }> }; |
| 44 | + expect(msg.content).toEqual([{ type: "text", text: "hello" }]); |
| 45 | + }); |
| 46 | + |
| 47 | + test("assistant message with empty text part is dropped silently", () => { |
| 48 | + const body = bodyOf(parsed([ |
| 49 | + { role: "user", content: "start", timestamp: 0 }, |
| 50 | + { role: "assistant", content: [{ type: "text", text: "" }, { type: "text", text: "visible" }], model: "claude", timestamp: 0 }, |
| 51 | + ])); |
| 52 | + const assistantMsg = body.messages.find(m => (m as { role: string }).role === "assistant") as { content: Array<{ type: string; text?: string }> }; |
| 53 | + expect(assistantMsg.content).toEqual([{ type: "text", text: "visible" }]); |
| 54 | + }); |
| 55 | + |
| 56 | + test("tool result with empty string content gets a placeholder", () => { |
| 57 | + const body = bodyOf(parsed([ |
| 58 | + { role: "user", content: "start", timestamp: 0 }, |
| 59 | + { role: "assistant", content: [{ type: "toolCall", id: "tc1", name: "run", arguments: {} }], model: "claude", timestamp: 0 }, |
| 60 | + { role: "toolResult", toolCallId: "tc1", toolName: "run", content: "", isError: false, timestamp: 0 }, |
| 61 | + ])); |
| 62 | + const toolResultMsg = body.messages.find(m => { |
| 63 | + const content = (m as { content?: unknown[] }).content; |
| 64 | + return Array.isArray(content) && content.some((c: { type?: string }) => c.type === "tool_result"); |
| 65 | + }) as { content: Array<{ type: string; content?: string }> }; |
| 66 | + const toolResult = toolResultMsg.content.find((c: { type: string }) => c.type === "tool_result") as { content: string }; |
| 67 | + expect(toolResult.content).toBe("(empty tool output)"); |
| 68 | + }); |
| 69 | +}); |
0 commit comments