|
| 1 | +// npx vitest run api/transform/__tests__/zai-format.spec.ts |
| 2 | + |
| 3 | +import { Anthropic } from "@anthropic-ai/sdk" |
| 4 | + |
| 5 | +import { convertToZAiFormat } from "../zai-format" |
| 6 | + |
| 7 | +describe("convertToZAiFormat", () => { |
| 8 | + it("should convert simple user text messages", () => { |
| 9 | + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hello" }] |
| 10 | + |
| 11 | + const result = convertToZAiFormat(messages) |
| 12 | + expect(result).toHaveLength(1) |
| 13 | + expect(result[0]).toEqual({ role: "user", content: "Hello" }) |
| 14 | + }) |
| 15 | + |
| 16 | + it("should handle base64 image in user message", () => { |
| 17 | + const messages: Anthropic.Messages.MessageParam[] = [ |
| 18 | + { |
| 19 | + role: "user", |
| 20 | + content: [ |
| 21 | + { type: "text", text: "Describe this:" }, |
| 22 | + { |
| 23 | + type: "image", |
| 24 | + source: { type: "base64", media_type: "image/png", data: "abc123" }, |
| 25 | + }, |
| 26 | + ], |
| 27 | + }, |
| 28 | + ] |
| 29 | + |
| 30 | + const result = convertToZAiFormat(messages) |
| 31 | + expect(result).toHaveLength(1) |
| 32 | + const content = (result[0] as any).content as Array<{ |
| 33 | + type: string |
| 34 | + image_url?: { url: string } |
| 35 | + text?: string |
| 36 | + }> |
| 37 | + expect(content[0]).toEqual({ type: "text", text: "Describe this:" }) |
| 38 | + expect(content[1]).toEqual({ type: "image_url", image_url: { url: "data:image/png;base64,abc123" } }) |
| 39 | + }) |
| 40 | + |
| 41 | + it("should skip non-base64 images in user messages", () => { |
| 42 | + const messages: Anthropic.Messages.MessageParam[] = [ |
| 43 | + { |
| 44 | + role: "user", |
| 45 | + content: [ |
| 46 | + { type: "text", text: "Hello" }, |
| 47 | + { |
| 48 | + type: "image", |
| 49 | + source: { type: "url", url: "https://example.com/img.png" } as any, |
| 50 | + }, |
| 51 | + ], |
| 52 | + }, |
| 53 | + ] |
| 54 | + |
| 55 | + const result = convertToZAiFormat(messages) |
| 56 | + expect(result).toHaveLength(1) |
| 57 | + // URL image is skipped — only text remains, so content is a plain string |
| 58 | + expect((result[0] as any).content).toBe("Hello") |
| 59 | + }) |
| 60 | + |
| 61 | + it("should convert tool_result content", () => { |
| 62 | + const messages: Anthropic.Messages.MessageParam[] = [ |
| 63 | + { |
| 64 | + role: "user", |
| 65 | + content: [{ type: "tool_result", tool_use_id: "tool-1", content: "Result text" }], |
| 66 | + }, |
| 67 | + ] |
| 68 | + |
| 69 | + const result = convertToZAiFormat(messages) |
| 70 | + expect(result[0]).toEqual({ role: "tool", tool_call_id: "tool-1", content: "Result text" }) |
| 71 | + }) |
| 72 | + |
| 73 | + it("should convert assistant messages with text", () => { |
| 74 | + const messages: Anthropic.Messages.MessageParam[] = [{ role: "assistant", content: "I can help with that." }] |
| 75 | + |
| 76 | + const result = convertToZAiFormat(messages) |
| 77 | + expect(result[0]).toEqual({ role: "assistant", content: "I can help with that." }) |
| 78 | + }) |
| 79 | +}) |
0 commit comments