-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathOpenApiAiCapabilitiesExtensionTests.cs
More file actions
279 lines (256 loc) · 10.2 KB
/
Copy pathOpenApiAiCapabilitiesExtensionTests.cs
File metadata and controls
279 lines (256 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Kiota.Builder.Configuration;
using Kiota.Builder.OpenApiExtensions;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi;
using Microsoft.OpenApi.Writers;
using Moq;
using Xunit;
namespace Kiota.Builder.Tests.OpenApiExtensions;
public sealed class OpenApiAiCapabilitiesExtensionTest : IDisposable
{
private readonly HttpClient _httpClient = new();
private readonly string TempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
public void Dispose()
{
_httpClient.Dispose();
if (Directory.Exists(TempDirectory))
{
Directory.Delete(TempDirectory, true);
}
}
[Fact]
public void Parses()
{
var oaiValueRepresentation =
"""
{
"response_semantics": {
"data_path": "$.items",
"static_template": {
"title": "Search for items",
"body": "Here are the items I found for you."
},
"properties": {
"title": "Some title",
"subtitle": "Some subtitle",
"url": "https://example.com",
"thumbnail_url": "https://example.com/thumbnail.jpg",
"information_protection_label": "confidential"
},
"oauth_card_path": "oauthCard.json"
},
"confirmation": {
"type": "modal",
"title": "Confirm action",
"body": "Do you want to proceed?"
},
"security_info": {
"data_handling": ["some data handling"]
}
}
""";
using var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(oaiValueRepresentation));
var oaiValue = JsonNode.Parse(stream);
var value = OpenApiAiCapabilitiesExtension.Parse(oaiValue);
Assert.NotNull(value);
var responseSemantics = value.ResponseSemantics;
var confirmation = value.Confirmation;
var securityInfo = value.SecurityInfo;
Assert.NotNull(responseSemantics);
Assert.NotNull(confirmation);
Assert.NotNull(securityInfo);
Assert.Equal("$.items", responseSemantics.DataPath);
Assert.Equal("oauthCard.json", responseSemantics.OauthCardPath);
var staticTemplate = responseSemantics.StaticTemplate as JsonObject;
Assert.NotNull(staticTemplate);
Assert.Equal("Search for items", staticTemplate["title"]?.ToString());
Assert.Equal("Here are the items I found for you.", staticTemplate["body"]?.ToString());
var properties = responseSemantics.Properties;
Assert.NotNull(properties);
Assert.Equal("Some title", properties.Title);
Assert.Equal("Some subtitle", properties.Subtitle);
Assert.Equal("https://example.com", properties.Url);
Assert.Equal("https://example.com/thumbnail.jpg", properties.ThumbnailUrl);
Assert.Equal("confidential", properties.InformationProtectionLabel);
Assert.Equal("modal", confirmation.Type);
Assert.Equal("Confirm action", confirmation.Title);
Assert.Equal("Do you want to proceed?", confirmation.Body);
Assert.Equal("some data handling", securityInfo.DataHandling[0]);
}
[Fact]
public async Task ParsesInDocumentAsync()
{
var documentContent = @"openapi: 3.0.0
info:
title: Test API
version: 0.0.0
servers:
- url: https://api.example.com/v1
description: Example API
paths:
/items:
get:
operationId: getItems
parameters: []
responses:
'200':
description: The request has succeeded.
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Item'
x-ai-capabilities:
response_semantics:
data_path: $.items
static_template:
title: Search for items
body: Here are the items I found for you.
properties:
title: Some title
subtitle: Some subtitle
url: https://example.com
thumbnail_url: https://example.com/thumbnail.jpg
information_protection_label: confidential
oauth_card_path: oauthCard.json
confirmation:
type: modal
title: Confirm action
body: Do you want to proceed?
security_info:
data_handling:
- some data handling
components:
schemas:
Item:
type: object
properties:
id:
type: string
name:
type: string";
Directory.CreateDirectory(TempDirectory);
var documentPath = Path.Combine(TempDirectory, "document.yaml");
await File.WriteAllTextAsync(documentPath, documentContent);
var mockLogger = new Mock<ILogger<OpenApiAiCapabilitiesExtension>>();
var documentDownloadService = new OpenApiDocumentDownloadService(_httpClient, mockLogger.Object);
var generationConfig = new GenerationConfiguration { OutputPath = TempDirectory, PluginTypes = [PluginType.APIPlugin] };
var (openApiDocumentStream, _) = await documentDownloadService.LoadStreamAsync(documentPath, generationConfig);
var document = await documentDownloadService.GetDocumentFromStreamAsync(openApiDocumentStream, generationConfig);
Assert.NotNull(document);
Assert.NotNull(document.Paths);
Assert.NotNull(document.Paths["/items"].Operations.FirstOrDefault().Value.Extensions);
Assert.True(document.Paths["/items"].Operations.FirstOrDefault().Value.Extensions.TryGetValue(OpenApiAiCapabilitiesExtension.Name, out var capabilitiesExtension));
Assert.NotNull(capabilitiesExtension);
}
[Fact]
public void Serializes()
{
var value = new OpenApiAiCapabilitiesExtension
{
ResponseSemantics = new ExtensionResponseSemantics
{
DataPath = "$.items",
StaticTemplate = new JsonObject
{
["title"] = "Search for items",
["body"] = "Here are the items I found for you."
},
Properties = new ExtensionResponseSemanticsProperties
{
Title = "Some title",
Subtitle = "Some subtitle",
Url = "https://example.com",
ThumbnailUrl = "https://example.com/thumbnail.jpg",
InformationProtectionLabel = "confidential"
},
OauthCardPath = "oauthCard.json"
},
Confirmation = new ExtensionConfirmation
{
Type = "modal",
Title = "Confirm action",
Body = "Do you want to proceed?"
},
SecurityInfo = new ExtensionSecurityInfo
{
DataHandling = ["some data handling"]
}
};
using var sWriter = new StringWriter();
OpenApiJsonWriter writer = new(sWriter, new OpenApiJsonWriterSettings { Terse = true });
value.Write(writer, OpenApiSpecVersion.OpenApi3_0);
var result = sWriter.ToString();
Assert.Contains("\"response_semantics\":", result);
Assert.Contains("data_path", result);
Assert.Contains("$.items", result);
Assert.Contains("static_template", result);
Assert.Contains("title", result);
Assert.Contains("Search for items", result);
Assert.Contains("body", result);
Assert.Contains("Here are the items I found for you", result);
Assert.Contains("properties", result);
Assert.Contains("title", result);
Assert.Contains("Some title", result);
Assert.Contains("subtitle", result);
Assert.Contains("Some subtitle", result);
Assert.Contains("url", result);
Assert.Contains("https://example.com", result);
Assert.Contains("thumbnail_url", result);
Assert.Contains("https://example.com/thumbnail.jpg", result);
Assert.Contains("information_protection_label", result);
Assert.Contains("confidential", result);
Assert.Contains("\"oauth_card_path", result);
Assert.Contains("oauthCard.json", result);
Assert.Contains("\"confirmation\":", result);
Assert.Contains("type", result);
Assert.Contains("modal", result);
Assert.Contains("title", result);
Assert.Contains("Confirm action", result);
Assert.Contains("body", result);
Assert.Contains("Do you want to proceed?", result);
Assert.Contains("\"security_info\":", result);
Assert.Contains("data_handling", result);
Assert.Contains("some data handling", result);
}
[Fact]
public void WritesNothingForEmptyCapabilities()
{
var value = new OpenApiAiCapabilitiesExtension
{
ResponseSemantics = null,
Confirmation = null,
SecurityInfo = null
};
using var sWriter = new StringWriter();
OpenApiJsonWriter writer = new(sWriter, new OpenApiJsonWriterSettings { Terse = true });
value.Write(writer, OpenApiSpecVersion.OpenApi3_0);
var result = sWriter.ToString();
// When all properties are null, nothing should be written
Assert.Equal(string.Empty, result);
}
[Fact]
public void WritesNothingForCapabilitiesWithEmptyNestedObjects()
{
var value = new OpenApiAiCapabilitiesExtension
{
ResponseSemantics = new ExtensionResponseSemantics { DataPath = string.Empty },
Confirmation = new ExtensionConfirmation(),
SecurityInfo = new ExtensionSecurityInfo()
};
using var sWriter = new StringWriter();
OpenApiJsonWriter writer = new(sWriter, new OpenApiJsonWriterSettings { Terse = true });
value.Write(writer, OpenApiSpecVersion.OpenApi3_0);
var result = sWriter.ToString();
// Should write the object structure even if nested objects are empty
// since the properties are not null
Assert.NotEqual(string.Empty, result);
}
}