Matt Rubens 11 месяцев назад
Родитель
Сommit
18c7f57afb
1 измененных файлов с 180 добавлено и 0 удалено
  1. 180 0
      src/api/transform/__tests__/r1-format.test.ts

+ 180 - 0
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)
+	})
+})