From 18c7f57afbaaab60e8a5845243ed26bc60661d29 Mon Sep 17 00:00:00 2001 From: Matt Rubens Date: Tue, 28 Jan 2025 00:15:14 -0500 Subject: [PATCH] Add test --- src/api/transform/__tests__/r1-format.test.ts | 180 ++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 src/api/transform/__tests__/r1-format.test.ts diff --git a/src/api/transform/__tests__/r1-format.test.ts b/src/api/transform/__tests__/r1-format.test.ts new file mode 100644 index 0000000..fce1f99 --- /dev/null +++ b/src/api/transform/__tests__/r1-format.test.ts @@ -0,0 +1,180 @@ +import { convertToR1Format } from "../r1-format" +import { Anthropic } from "@anthropic-ai/sdk" +import OpenAI from "openai" + +describe("convertToR1Format", () => { + it("should convert basic text messages", () => { + const input: Anthropic.Messages.MessageParam[] = [ + { role: "user", content: "Hello" }, + { role: "assistant", content: "Hi there" }, + ] + + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { role: "user", content: "Hello" }, + { role: "assistant", content: "Hi there" }, + ] + + expect(convertToR1Format(input)).toEqual(expected) + }) + + it("should merge consecutive messages with same role", () => { + const input: Anthropic.Messages.MessageParam[] = [ + { role: "user", content: "Hello" }, + { role: "user", content: "How are you?" }, + { role: "assistant", content: "Hi!" }, + { role: "assistant", content: "I'm doing well" }, + ] + + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { role: "user", content: "Hello\nHow are you?" }, + { role: "assistant", content: "Hi!\nI'm doing well" }, + ] + + expect(convertToR1Format(input)).toEqual(expected) + }) + + it("should handle image content", () => { + const input: Anthropic.Messages.MessageParam[] = [ + { + role: "user", + content: [ + { + type: "image", + source: { + type: "base64", + media_type: "image/jpeg", + data: "base64data", + }, + }, + ], + }, + ] + + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { + role: "user", + content: [ + { + type: "image_url", + image_url: { + url: "data:image/jpeg;base64,base64data", + }, + }, + ], + }, + ] + + expect(convertToR1Format(input)).toEqual(expected) + }) + + it("should handle mixed text and image content", () => { + const input: Anthropic.Messages.MessageParam[] = [ + { + role: "user", + content: [ + { type: "text", text: "Check this image:" }, + { + type: "image", + source: { + type: "base64", + media_type: "image/jpeg", + data: "base64data", + }, + }, + ], + }, + ] + + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { + role: "user", + content: [ + { type: "text", text: "Check this image:" }, + { + type: "image_url", + image_url: { + url: "data:image/jpeg;base64,base64data", + }, + }, + ], + }, + ] + + expect(convertToR1Format(input)).toEqual(expected) + }) + + it("should merge mixed content messages with same role", () => { + const input: Anthropic.Messages.MessageParam[] = [ + { + role: "user", + content: [ + { type: "text", text: "First image:" }, + { + type: "image", + source: { + type: "base64", + media_type: "image/jpeg", + data: "image1", + }, + }, + ], + }, + { + role: "user", + content: [ + { type: "text", text: "Second image:" }, + { + type: "image", + source: { + type: "base64", + media_type: "image/png", + data: "image2", + }, + }, + ], + }, + ] + + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { + role: "user", + content: [ + { type: "text", text: "First image:" }, + { + type: "image_url", + image_url: { + url: "data:image/jpeg;base64,image1", + }, + }, + { type: "text", text: "Second image:" }, + { + type: "image_url", + image_url: { + url: "data:image/png;base64,image2", + }, + }, + ], + }, + ] + + expect(convertToR1Format(input)).toEqual(expected) + }) + + it("should handle empty messages array", () => { + expect(convertToR1Format([])).toEqual([]) + }) + + it("should handle messages with empty content", () => { + const input: Anthropic.Messages.MessageParam[] = [ + { role: "user", content: "" }, + { role: "assistant", content: "" }, + ] + + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { role: "user", content: "" }, + { role: "assistant", content: "" }, + ] + + expect(convertToR1Format(input)).toEqual(expected) + }) +})