|
| 1 | +import { convertToR1Format } from "../r1-format" |
| 2 | +import { Anthropic } from "@anthropic-ai/sdk" |
| 3 | +import OpenAI from "openai" |
| 4 | + |
| 5 | +describe("convertToR1Format", () => { |
| 6 | + it("should convert basic text messages", () => { |
| 7 | + const input: Anthropic.Messages.MessageParam[] = [ |
| 8 | + { role: "user", content: "Hello" }, |
| 9 | + { role: "assistant", content: "Hi there" }, |
| 10 | + ] |
| 11 | + |
| 12 | + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ |
| 13 | + { role: "user", content: "Hello" }, |
| 14 | + { role: "assistant", content: "Hi there" }, |
| 15 | + ] |
| 16 | + |
| 17 | + expect(convertToR1Format(input)).toEqual(expected) |
| 18 | + }) |
| 19 | + |
| 20 | + it("should merge consecutive messages with same role", () => { |
| 21 | + const input: Anthropic.Messages.MessageParam[] = [ |
| 22 | + { role: "user", content: "Hello" }, |
| 23 | + { role: "user", content: "How are you?" }, |
| 24 | + { role: "assistant", content: "Hi!" }, |
| 25 | + { role: "assistant", content: "I'm doing well" }, |
| 26 | + ] |
| 27 | + |
| 28 | + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ |
| 29 | + { role: "user", content: "Hello\nHow are you?" }, |
| 30 | + { role: "assistant", content: "Hi!\nI'm doing well" }, |
| 31 | + ] |
| 32 | + |
| 33 | + expect(convertToR1Format(input)).toEqual(expected) |
| 34 | + }) |
| 35 | + |
| 36 | + it("should handle image content", () => { |
| 37 | + const input: Anthropic.Messages.MessageParam[] = [ |
| 38 | + { |
| 39 | + role: "user", |
| 40 | + content: [ |
| 41 | + { |
| 42 | + type: "image", |
| 43 | + source: { |
| 44 | + type: "base64", |
| 45 | + media_type: "image/jpeg", |
| 46 | + data: "base64data", |
| 47 | + }, |
| 48 | + }, |
| 49 | + ], |
| 50 | + }, |
| 51 | + ] |
| 52 | + |
| 53 | + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ |
| 54 | + { |
| 55 | + role: "user", |
| 56 | + content: [ |
| 57 | + { |
| 58 | + type: "image_url", |
| 59 | + image_url: { |
| 60 | + url: "data:image/jpeg;base64,base64data", |
| 61 | + }, |
| 62 | + }, |
| 63 | + ], |
| 64 | + }, |
| 65 | + ] |
| 66 | + |
| 67 | + expect(convertToR1Format(input)).toEqual(expected) |
| 68 | + }) |
| 69 | + |
| 70 | + it("should handle mixed text and image content", () => { |
| 71 | + const input: Anthropic.Messages.MessageParam[] = [ |
| 72 | + { |
| 73 | + role: "user", |
| 74 | + content: [ |
| 75 | + { type: "text", text: "Check this image:" }, |
| 76 | + { |
| 77 | + type: "image", |
| 78 | + source: { |
| 79 | + type: "base64", |
| 80 | + media_type: "image/jpeg", |
| 81 | + data: "base64data", |
| 82 | + }, |
| 83 | + }, |
| 84 | + ], |
| 85 | + }, |
| 86 | + ] |
| 87 | + |
| 88 | + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ |
| 89 | + { |
| 90 | + role: "user", |
| 91 | + content: [ |
| 92 | + { type: "text", text: "Check this image:" }, |
| 93 | + { |
| 94 | + type: "image_url", |
| 95 | + image_url: { |
| 96 | + url: "data:image/jpeg;base64,base64data", |
| 97 | + }, |
| 98 | + }, |
| 99 | + ], |
| 100 | + }, |
| 101 | + ] |
| 102 | + |
| 103 | + expect(convertToR1Format(input)).toEqual(expected) |
| 104 | + }) |
| 105 | + |
| 106 | + it("should merge mixed content messages with same role", () => { |
| 107 | + const input: Anthropic.Messages.MessageParam[] = [ |
| 108 | + { |
| 109 | + role: "user", |
| 110 | + content: [ |
| 111 | + { type: "text", text: "First image:" }, |
| 112 | + { |
| 113 | + type: "image", |
| 114 | + source: { |
| 115 | + type: "base64", |
| 116 | + media_type: "image/jpeg", |
| 117 | + data: "image1", |
| 118 | + }, |
| 119 | + }, |
| 120 | + ], |
| 121 | + }, |
| 122 | + { |
| 123 | + role: "user", |
| 124 | + content: [ |
| 125 | + { type: "text", text: "Second image:" }, |
| 126 | + { |
| 127 | + type: "image", |
| 128 | + source: { |
| 129 | + type: "base64", |
| 130 | + media_type: "image/png", |
| 131 | + data: "image2", |
| 132 | + }, |
| 133 | + }, |
| 134 | + ], |
| 135 | + }, |
| 136 | + ] |
| 137 | + |
| 138 | + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ |
| 139 | + { |
| 140 | + role: "user", |
| 141 | + content: [ |
| 142 | + { type: "text", text: "First image:" }, |
| 143 | + { |
| 144 | + type: "image_url", |
| 145 | + image_url: { |
| 146 | + url: "data:image/jpeg;base64,image1", |
| 147 | + }, |
| 148 | + }, |
| 149 | + { type: "text", text: "Second image:" }, |
| 150 | + { |
| 151 | + type: "image_url", |
| 152 | + image_url: { |
| 153 | + url: "data:image/png;base64,image2", |
| 154 | + }, |
| 155 | + }, |
| 156 | + ], |
| 157 | + }, |
| 158 | + ] |
| 159 | + |
| 160 | + expect(convertToR1Format(input)).toEqual(expected) |
| 161 | + }) |
| 162 | + |
| 163 | + it("should handle empty messages array", () => { |
| 164 | + expect(convertToR1Format([])).toEqual([]) |
| 165 | + }) |
| 166 | + |
| 167 | + it("should handle messages with empty content", () => { |
| 168 | + const input: Anthropic.Messages.MessageParam[] = [ |
| 169 | + { role: "user", content: "" }, |
| 170 | + { role: "assistant", content: "" }, |
| 171 | + ] |
| 172 | + |
| 173 | + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ |
| 174 | + { role: "user", content: "" }, |
| 175 | + { role: "assistant", content: "" }, |
| 176 | + ] |
| 177 | + |
| 178 | + expect(convertToR1Format(input)).toEqual(expected) |
| 179 | + }) |
| 180 | +}) |
0 commit comments