|
1 | 1 | using System.Text.Json; |
2 | | - |
3 | 2 | using AzureOpenAIProxy.ApiApp.Models; |
4 | | - |
5 | 3 | using FluentAssertions; |
6 | 4 |
|
7 | 5 | namespace AzureOpenAIProxy.ApiApp.Tests.Models; |
| 6 | + |
8 | 7 | public class CreateChatCompletionResponseTests |
9 | 8 | { |
10 | | - [Fact] |
11 | | - public void Given_ExampleJson_When_DeserializedAndSerialized_Then_ShouldMatchExpectedJson() |
| 9 | + private readonly string exampleJson = @" |
12 | 10 | { |
13 | | - // Arrange |
14 | | - var exampleJson = @" |
15 | | - { |
16 | | - ""id"": ""string"", |
17 | | - ""object"": ""chat.completion"", |
18 | | - ""created"": 1620241923, |
19 | | - ""model"": ""string"", |
20 | | - ""usage"": { |
21 | | - ""prompt_tokens"": 0, |
22 | | - ""completion_tokens"": 0, |
23 | | - ""total_tokens"": 0 |
24 | | - }, |
25 | | - ""system_fingerprint"": ""string"", |
26 | | - ""prompt_filter_results"": [ |
27 | | - { |
28 | | - ""prompt_index"": 0, |
29 | | - ""content_filter_results"": { |
30 | | - ""sexual"": { |
31 | | - ""filtered"": true, |
32 | | - ""severity"": ""safe"" |
33 | | - }, |
34 | | - ""violence"": { |
35 | | - ""filtered"": true, |
36 | | - ""severity"": ""safe"" |
37 | | - }, |
38 | | - ""hate"": { |
39 | | - ""filtered"": true, |
40 | | - ""severity"": ""safe"" |
41 | | - }, |
42 | | - ""self_harm"": { |
43 | | - ""filtered"": true, |
44 | | - ""severity"": ""safe"" |
45 | | - }, |
46 | | - ""profanity"": { |
47 | | - ""filtered"": true, |
48 | | - ""detected"": true |
49 | | - }, |
50 | | - ""error"": { |
51 | | - ""code"": ""string"", |
52 | | - ""message"": ""string"" |
53 | | - }, |
54 | | - ""jailbreak"": { |
55 | | - ""filtered"": true, |
56 | | - ""detected"": true |
57 | | - } |
| 11 | + ""id"": ""string"", |
| 12 | + ""object"": ""chat.completion"", |
| 13 | + ""created"": 1620241923, |
| 14 | + ""model"": ""string"", |
| 15 | + ""usage"": { |
| 16 | + ""prompt_tokens"": 0, |
| 17 | + ""completion_tokens"": 0, |
| 18 | + ""total_tokens"": 0 |
| 19 | + }, |
| 20 | + ""system_fingerprint"": ""string"", |
| 21 | + ""prompt_filter_results"": [ |
| 22 | + { |
| 23 | + ""prompt_index"": 0, |
| 24 | + ""content_filter_results"": { |
| 25 | + ""sexual"": { |
| 26 | + ""filtered"": true, |
| 27 | + ""severity"": ""safe"" |
| 28 | + }, |
| 29 | + ""violence"": { |
| 30 | + ""filtered"": true, |
| 31 | + ""severity"": ""safe"" |
| 32 | + }, |
| 33 | + ""hate"": { |
| 34 | + ""filtered"": true, |
| 35 | + ""severity"": ""safe"" |
| 36 | + }, |
| 37 | + ""self_harm"": { |
| 38 | + ""filtered"": true, |
| 39 | + ""severity"": ""safe"" |
| 40 | + }, |
| 41 | + ""profanity"": { |
| 42 | + ""filtered"": true, |
| 43 | + ""detected"": true |
| 44 | + }, |
| 45 | + ""error"": { |
| 46 | + ""code"": ""string"", |
| 47 | + ""message"": ""string"" |
| 48 | + }, |
| 49 | + ""jailbreak"": { |
| 50 | + ""filtered"": true, |
| 51 | + ""detected"": true |
58 | 52 | } |
59 | 53 | } |
60 | | - ], |
61 | | - ""choices"": [ |
62 | | - { |
63 | | - ""index"": 0, |
64 | | - ""finish_reason"": ""string"", |
65 | | - ""message"": { |
66 | | - ""role"": ""assistant"", |
67 | | - ""content"": ""string"", |
68 | | - ""tool_calls"": [ |
69 | | - { |
70 | | - ""id"": ""string"", |
71 | | - ""type"": ""function"", |
72 | | - ""function"": { |
73 | | - ""name"": ""string"", |
74 | | - ""arguments"": ""string"" |
75 | | - } |
| 54 | + } |
| 55 | + ], |
| 56 | + ""choices"": [ |
| 57 | + { |
| 58 | + ""index"": 0, |
| 59 | + ""finish_reason"": ""string"", |
| 60 | + ""message"": { |
| 61 | + ""role"": ""assistant"", |
| 62 | + ""content"": ""string"", |
| 63 | + ""tool_calls"": [ |
| 64 | + { |
| 65 | + ""id"": ""string"", |
| 66 | + ""type"": ""function"", |
| 67 | + ""function"": { |
| 68 | + ""name"": ""string"", |
| 69 | + ""arguments"": ""string"" |
76 | 70 | } |
77 | | - ], |
78 | | - ""function_call"": { |
79 | | - ""name"": ""string"", |
80 | | - ""arguments"": ""string"" |
81 | | - }, |
82 | | - ""context"": { |
83 | | - ""citations"": [ |
84 | | - { |
85 | | - ""content"": ""string"", |
86 | | - ""title"": ""string"", |
87 | | - ""url"": ""string"", |
88 | | - ""filepath"": ""string"", |
89 | | - ""chunk_id"": ""string"" |
90 | | - } |
91 | | - ], |
92 | | - ""intent"": ""string"" |
93 | 71 | } |
| 72 | + ], |
| 73 | + ""function_call"": { |
| 74 | + ""name"": ""string"", |
| 75 | + ""arguments"": ""string"" |
94 | 76 | }, |
95 | | - ""content_filter_results"": { |
96 | | - ""sexual"": { |
97 | | - ""filtered"": true, |
98 | | - ""severity"": ""safe"" |
99 | | - }, |
100 | | - ""violence"": { |
101 | | - ""filtered"": true, |
102 | | - ""severity"": ""safe"" |
103 | | - }, |
104 | | - ""hate"": { |
105 | | - ""filtered"": true, |
106 | | - ""severity"": ""safe"" |
107 | | - }, |
108 | | - ""self_harm"": { |
109 | | - ""filtered"": true, |
110 | | - ""severity"": ""safe"" |
111 | | - }, |
112 | | - ""profanity"": { |
113 | | - ""filtered"": true, |
114 | | - ""detected"": true |
115 | | - }, |
116 | | - ""error"": { |
117 | | - ""code"": ""string"", |
118 | | - ""message"": ""string"" |
119 | | - }, |
120 | | - ""protected_material_text"": { |
121 | | - ""filtered"": true, |
122 | | - ""detected"": true |
123 | | - }, |
124 | | - ""protected_material_code"": { |
125 | | - ""filtered"": true, |
126 | | - ""detected"": true, |
127 | | - ""citation"": { |
128 | | - ""URL"": ""string"", |
129 | | - ""license"": ""string"" |
130 | | - } |
131 | | - } |
132 | | - }, |
133 | | - ""logprobs"": { |
134 | | - ""content"": [ |
| 77 | + ""context"": { |
| 78 | + ""citations"": [ |
135 | 79 | { |
136 | | - ""token"": ""string"", |
137 | | - ""logprob"": 0, |
138 | | - ""bytes"": [ |
139 | | - 0 |
140 | | - ], |
141 | | - ""top_logprobs"": [ |
142 | | - { |
143 | | - ""token"": ""string"", |
144 | | - ""logprob"": 0, |
145 | | - ""bytes"": [ |
146 | | - 0 |
147 | | - ] |
148 | | - } |
149 | | - ] |
| 80 | + ""content"": ""string"", |
| 81 | + ""title"": ""string"", |
| 82 | + ""url"": ""string"", |
| 83 | + ""filepath"": ""string"", |
| 84 | + ""chunk_id"": ""string"" |
150 | 85 | } |
151 | | - ] |
| 86 | + ], |
| 87 | + ""intent"": ""string"" |
152 | 88 | } |
| 89 | + }, |
| 90 | + ""content_filter_results"": { |
| 91 | + ""sexual"": { |
| 92 | + ""filtered"": true, |
| 93 | + ""severity"": ""safe"" |
| 94 | + }, |
| 95 | + ""violence"": { |
| 96 | + ""filtered"": true, |
| 97 | + ""severity"": ""safe"" |
| 98 | + }, |
| 99 | + ""hate"": { |
| 100 | + ""filtered"": true, |
| 101 | + ""severity"": ""safe"" |
| 102 | + }, |
| 103 | + ""self_harm"": { |
| 104 | + ""filtered"": true, |
| 105 | + ""severity"": ""safe"" |
| 106 | + }, |
| 107 | + ""profanity"": { |
| 108 | + ""filtered"": true, |
| 109 | + ""detected"": true |
| 110 | + }, |
| 111 | + ""error"": { |
| 112 | + ""code"": ""string"", |
| 113 | + ""message"": ""string"" |
| 114 | + }, |
| 115 | + ""protected_material_text"": { |
| 116 | + ""filtered"": true, |
| 117 | + ""detected"": true |
| 118 | + }, |
| 119 | + ""protected_material_code"": { |
| 120 | + ""filtered"": true, |
| 121 | + ""detected"": true, |
| 122 | + ""citation"": { |
| 123 | + ""URL"": ""string"", |
| 124 | + ""license"": ""string"" |
| 125 | + } |
| 126 | + } |
| 127 | + }, |
| 128 | + ""logprobs"": { |
| 129 | + ""content"": [ |
| 130 | + { |
| 131 | + ""token"": ""string"", |
| 132 | + ""logprob"": 0, |
| 133 | + ""bytes"": [ |
| 134 | + 0 |
| 135 | + ], |
| 136 | + ""top_logprobs"": [ |
| 137 | + { |
| 138 | + ""token"": ""string"", |
| 139 | + ""logprob"": 0, |
| 140 | + ""bytes"": [ |
| 141 | + 0 |
| 142 | + ] |
| 143 | + } |
| 144 | + ] |
| 145 | + } |
| 146 | + ] |
153 | 147 | } |
154 | | - ] |
155 | | - }"; |
| 148 | + } |
| 149 | + ] |
| 150 | + }"; |
156 | 151 |
|
157 | | - // Act |
| 152 | + [Fact] |
| 153 | + public void Given_ExampleJson_When_Deserialized_Then_ShouldReturnValidObject() |
| 154 | + { |
| 155 | + // Arrange & Act |
158 | 156 | var deserializedResponse = JsonSerializer.Deserialize<CreateChatCompletionResponse>(exampleJson); |
159 | | - var serializedJson = JsonSerializer.Serialize(deserializedResponse, new JsonSerializerOptions { WriteIndented = false }); |
160 | 157 |
|
161 | 158 | // Assert |
162 | 159 | deserializedResponse.Should().NotBeNull(); |
163 | | - serializedJson.Should().Be(exampleJson.Replace("\r", "").Replace("\n", "").Replace(" ", "")); |
| 160 | + } |
| 161 | + |
| 162 | + [Fact] |
| 163 | + public void Given_DeserializedObject_When_Serialized_Then_ShouldMatchOriginalJson() |
| 164 | + { |
| 165 | + // Arrange |
| 166 | + var deserializedResponse = JsonSerializer.Deserialize<CreateChatCompletionResponse>(exampleJson); |
| 167 | + |
| 168 | + // Act |
| 169 | + var serializedJson = JsonSerializer.Serialize(deserializedResponse, new JsonSerializerOptions { WriteIndented = false }); |
164 | 170 |
|
| 171 | + // Assert |
| 172 | + serializedJson.Should().Be(exampleJson.Replace("\r", "").Replace("\n", "").Replace(" ", "")); |
165 | 173 | } |
166 | 174 | } |
0 commit comments