|
2 | 2 |
|
3 | 3 | import AgentRunKit |
4 | 4 | @testable import AgentRunKitFoundationModels |
| 5 | + import Foundation |
5 | 6 | import Testing |
6 | 7 |
|
7 | 8 | @Suite(.serialized) struct FMMessageMapperTests { |
8 | | - @Test func singleUserMessage() { |
| 9 | + @Test func singleUserMessage() throws { |
9 | 10 | guard #available(macOS 26, iOS 26, *) else { return } |
10 | | - let mapped = FMMessageMapper.map([.user("Hello")]) |
| 11 | + let mapped = try FMMessageMapper.map([.user("Hello")]) |
11 | 12 | #expect(mapped.prompt == "Hello") |
12 | 13 | #expect(mapped.instructions == nil) |
13 | 14 | } |
14 | 15 |
|
15 | | - @Test func systemMessageExtractedAsInstructions() { |
| 16 | + @Test func systemMessageExtractedAsInstructions() throws { |
16 | 17 | guard #available(macOS 26, iOS 26, *) else { return } |
17 | | - let mapped = FMMessageMapper.map([ |
| 18 | + let mapped = try FMMessageMapper.map([ |
18 | 19 | .system("You are helpful"), |
19 | 20 | .user("Hi"), |
20 | 21 | ]) |
21 | 22 | #expect(mapped.instructions == "You are helpful") |
22 | 23 | #expect(mapped.prompt == "Hi") |
23 | 24 | } |
24 | 25 |
|
25 | | - @Test func multipleSystemMessagesJoinedWithNewline() { |
| 26 | + @Test func multipleSystemMessagesJoinedWithNewline() throws { |
26 | 27 | guard #available(macOS 26, iOS 26, *) else { return } |
27 | | - let mapped = FMMessageMapper.map([ |
| 28 | + let mapped = try FMMessageMapper.map([ |
28 | 29 | .system("First instruction"), |
29 | 30 | .system("Second instruction"), |
30 | 31 | .user("Question"), |
31 | 32 | ]) |
32 | 33 | #expect(mapped.instructions == "First instruction\nSecond instruction") |
33 | 34 | } |
34 | 35 |
|
35 | | - @Test func multimodalUserExtractsTextOnly() { |
| 36 | + @Test func textOnlyMultimodalUserMessage() throws { |
36 | 37 | guard #available(macOS 26, iOS 26, *) else { return } |
37 | | - let mapped = FMMessageMapper.map([ |
| 38 | + let mapped = try FMMessageMapper.map([ |
38 | 39 | .userMultimodal([ |
39 | 40 | .text("Describe this"), |
40 | | - .imageURL("https://example.com/image.jpg"), |
41 | 41 | .text("in detail"), |
42 | 42 | ]), |
43 | 43 | ]) |
44 | 44 | #expect(mapped.prompt == "Describe this\nin detail") |
45 | 45 | } |
46 | 46 |
|
47 | | - @Test func noSystemMessageYieldsNilInstructions() { |
| 47 | + @Test func noSystemMessageYieldsNilInstructions() throws { |
48 | 48 | guard #available(macOS 26, iOS 26, *) else { return } |
49 | | - let mapped = FMMessageMapper.map([.user("Just a question")]) |
| 49 | + let mapped = try FMMessageMapper.map([.user("Just a question")]) |
50 | 50 | #expect(mapped.instructions == nil) |
51 | 51 | } |
52 | 52 |
|
53 | | - @Test func multipleUserMessagesUsesLast() { |
| 53 | + @Test func multipleUserMessagesThrows() { |
54 | 54 | guard #available(macOS 26, iOS 26, *) else { return } |
55 | | - let mapped = FMMessageMapper.map([ |
56 | | - .user("First question"), |
57 | | - .user("Second question"), |
58 | | - ]) |
59 | | - #expect(mapped.prompt == "Second question") |
| 55 | + #expect { |
| 56 | + _ = try FMMessageMapper.map([ |
| 57 | + .user("First question"), |
| 58 | + .user("Second question"), |
| 59 | + ]) |
| 60 | + } throws: { error in |
| 61 | + isUnsupportedFoundationModelsMappingError(error) |
| 62 | + } |
60 | 63 | } |
61 | 64 |
|
62 | | - @Test func assistantAndToolMessagesIgnored() { |
| 65 | + @Test func systemMessageAfterUserThrows() { |
63 | 66 | guard #available(macOS 26, iOS 26, *) else { return } |
64 | | - let mapped = FMMessageMapper.map([ |
65 | | - .system("System"), |
66 | | - .user("First"), |
67 | | - .assistant(AssistantMessage(content: "Response")), |
68 | | - .tool(id: "1", name: "test", content: "result"), |
69 | | - .user("Follow up"), |
70 | | - ]) |
71 | | - #expect(mapped.instructions == "System") |
72 | | - #expect(mapped.prompt == "Follow up") |
| 67 | + #expect { |
| 68 | + _ = try FMMessageMapper.map([ |
| 69 | + .user("Question"), |
| 70 | + .system("Late instruction"), |
| 71 | + ]) |
| 72 | + } throws: { error in |
| 73 | + isUnsupportedFoundationModelsMappingError(error) |
| 74 | + } |
73 | 75 | } |
74 | 76 |
|
75 | | - @Test func emptyMessagesYieldsEmptyPrompt() { |
| 77 | + @Test func assistantMessageThrows() { |
76 | 78 | guard #available(macOS 26, iOS 26, *) else { return } |
77 | | - let mapped = FMMessageMapper.map([]) |
78 | | - #expect(mapped.prompt == "") |
79 | | - #expect(mapped.instructions == nil) |
| 79 | + #expect { |
| 80 | + _ = try FMMessageMapper.map([ |
| 81 | + .user("First"), |
| 82 | + .assistant(AssistantMessage(content: "Response")), |
| 83 | + ]) |
| 84 | + } throws: { error in |
| 85 | + isUnsupportedFoundationModelsMappingError(error) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test func toolMessageThrows() { |
| 90 | + guard #available(macOS 26, iOS 26, *) else { return } |
| 91 | + #expect { |
| 92 | + _ = try FMMessageMapper.map([ |
| 93 | + .user("First"), |
| 94 | + .tool(id: "1", name: "test", content: "result"), |
| 95 | + ]) |
| 96 | + } throws: { error in |
| 97 | + isUnsupportedFoundationModelsMappingError(error) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test func emptyHistoryThrows() { |
| 102 | + guard #available(macOS 26, iOS 26, *) else { return } |
| 103 | + #expect { |
| 104 | + _ = try FMMessageMapper.map([]) |
| 105 | + } throws: { error in |
| 106 | + isUnsupportedFoundationModelsMappingError(error) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test func systemOnlyHistoryThrows() { |
| 111 | + guard #available(macOS 26, iOS 26, *) else { return } |
| 112 | + #expect { |
| 113 | + _ = try FMMessageMapper.map([.system("System")]) |
| 114 | + } throws: { error in |
| 115 | + isUnsupportedFoundationModelsMappingError(error) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test func emptyUserTextThrows() { |
| 120 | + guard #available(macOS 26, iOS 26, *) else { return } |
| 121 | + #expect { |
| 122 | + _ = try FMMessageMapper.map([.user(" \n\t ")]) |
| 123 | + } throws: { error in |
| 124 | + isUnsupportedFoundationModelsMappingError(error) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Test func nonTextMultimodalPartsThrow() { |
| 129 | + guard #available(macOS 26, iOS 26, *) else { return } |
| 130 | + let data = Data([0x01, 0x02]) |
| 131 | + let nonTextParts: [ContentPart] = [ |
| 132 | + .imageURL("https://example.com/image.jpg"), |
| 133 | + .imageBase64(data: data, mimeType: "image/png"), |
| 134 | + .videoBase64(data: data, mimeType: "video/mp4"), |
| 135 | + .pdfBase64(data: data), |
| 136 | + .audioBase64(data: data, format: .mp3), |
| 137 | + ] |
| 138 | + |
| 139 | + for part in nonTextParts { |
| 140 | + #expect { |
| 141 | + _ = try FMMessageMapper.map([ |
| 142 | + .userMultimodal([part]), |
| 143 | + ]) |
| 144 | + } throws: { error in |
| 145 | + isUnsupportedFoundationModelsMappingError(error) |
| 146 | + } |
| 147 | + |
| 148 | + #expect { |
| 149 | + _ = try FMMessageMapper.map([ |
| 150 | + .userMultimodal([.text("Describe this"), part]), |
| 151 | + ]) |
| 152 | + } throws: { error in |
| 153 | + isUnsupportedFoundationModelsMappingError(error) |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @Test func whitespaceOnlyMultimodalThrows() { |
| 159 | + guard #available(macOS 26, iOS 26, *) else { return } |
| 160 | + #expect { |
| 161 | + _ = try FMMessageMapper.map([ |
| 162 | + .userMultimodal([.text(" "), .text("\n")]), |
| 163 | + ]) |
| 164 | + } throws: { error in |
| 165 | + isUnsupportedFoundationModelsMappingError(error) |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Test func multimodalPlusUserThrows() { |
| 170 | + guard #available(macOS 26, iOS 26, *) else { return } |
| 171 | + #expect { |
| 172 | + _ = try FMMessageMapper.map([ |
| 173 | + .userMultimodal([.text("First question")]), |
| 174 | + .user("Second question"), |
| 175 | + ]) |
| 176 | + } throws: { error in |
| 177 | + isUnsupportedFoundationModelsMappingError(error) |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + @Test func assistantToolAndFollowUpThrows() { |
| 182 | + guard #available(macOS 26, iOS 26, *) else { return } |
| 183 | + #expect { |
| 184 | + _ = try FMMessageMapper.map([ |
| 185 | + .system("System"), |
| 186 | + .user("First"), |
| 187 | + .assistant(AssistantMessage(content: "Response")), |
| 188 | + .tool(id: "1", name: "test", content: "result"), |
| 189 | + .user("Follow up"), |
| 190 | + ]) |
| 191 | + } throws: { error in |
| 192 | + isUnsupportedFoundationModelsMappingError(error) |
| 193 | + } |
80 | 194 | } |
81 | 195 | } |
82 | 196 |
|
|
0 commit comments