|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System.IO; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.SemanticKernel; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace SemanticKernel.IntegrationTests.Plugins.OpenApiManifest; |
| 9 | + |
| 10 | +public sealed class ApiManifestKernelExtensionsTests |
| 11 | +{ |
| 12 | + private readonly string _testPluginsDir; |
| 13 | + private readonly Kernel _kernel; |
| 14 | + |
| 15 | + public ApiManifestKernelExtensionsTests() |
| 16 | + { |
| 17 | + this._testPluginsDir = Path.Combine(Directory.GetCurrentDirectory(), "Plugins", "OpenApiManifest"); |
| 18 | + this._kernel = new Kernel(); |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public async Task ItCanCreatePluginFromApiManifestAsync() |
| 23 | + { |
| 24 | + // Act |
| 25 | + var manifestFilePath = Path.Combine(this._testPluginsDir, "example-apimanifest.json"); |
| 26 | + |
| 27 | + // Arrange |
| 28 | + var plugin = await this._kernel.CreatePluginFromApiManifestAsync("ApiManifestPlugin", manifestFilePath); |
| 29 | + |
| 30 | + // Assert |
| 31 | + Assert.NotNull(plugin); |
| 32 | + Assert.Equal(3, plugin.FunctionCount); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public async Task ItCanCreatePluginFromApiManifestWithDescriptionParameterAsync() |
| 37 | + { |
| 38 | + // Act |
| 39 | + var manifestFilePath = Path.Combine(this._testPluginsDir, "example-apimanifest.json"); |
| 40 | + var description = "My plugin description"; |
| 41 | + |
| 42 | + // Arrange |
| 43 | + var plugin = await this._kernel.CreatePluginFromApiManifestAsync("ApiManifestPlugin", manifestFilePath, description); |
| 44 | + |
| 45 | + // Assert |
| 46 | + Assert.NotNull(plugin); |
| 47 | + Assert.Equal(description, plugin.Description); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public async Task ItCanCreatePluginFromApiManifestWithEmptyDescriptionParameterAsync() |
| 52 | + { |
| 53 | + // Act |
| 54 | + var manifestFilePath = Path.Combine(this._testPluginsDir, "example-apimanifest.json"); |
| 55 | + |
| 56 | + // Arrange |
| 57 | + var plugin = await this._kernel.CreatePluginFromApiManifestAsync("ApiManifestPlugin", manifestFilePath, description: null); |
| 58 | + |
| 59 | + // Assert |
| 60 | + Assert.NotNull(plugin); |
| 61 | + Assert.Empty(plugin.Description); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public async Task ItCanImportPluginFromApiManifestAsync() |
| 66 | + { |
| 67 | + // Act |
| 68 | + var manifestFilePath = Path.Combine(this._testPluginsDir, "example-apimanifest.json"); |
| 69 | + |
| 70 | + // Arrange |
| 71 | + var plugin = await this._kernel.ImportPluginFromApiManifestAsync("ApiManifestPlugin", manifestFilePath); |
| 72 | + |
| 73 | + // Assert |
| 74 | + Assert.NotNull(plugin); |
| 75 | + Assert.Equal(3, plugin.FunctionCount); |
| 76 | + Assert.Single(this._kernel.Plugins); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public async Task ItCanImportPluginFromApiManifestWithDescriptionParameterAsync() |
| 81 | + { |
| 82 | + // Act |
| 83 | + var manifestFilePath = Path.Combine(this._testPluginsDir, "example-apimanifest.json"); |
| 84 | + var description = "My plugin description"; |
| 85 | + |
| 86 | + // Arrange |
| 87 | + var plugin = await this._kernel.ImportPluginFromApiManifestAsync("ApiManifestPlugin", manifestFilePath, description); |
| 88 | + |
| 89 | + // Assert |
| 90 | + Assert.NotNull(plugin); |
| 91 | + Assert.Equal(description, plugin.Description); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public async Task ItCanImportPluginFromApiManifestWithLocalAndRemoteApiDescriptionUrlAsync() |
| 96 | + { |
| 97 | + // Act |
| 98 | + var manifestFilePath = Path.Combine(this._testPluginsDir, "example-apimanifest-local.json"); |
| 99 | + |
| 100 | + // Arrange |
| 101 | + var plugin = await this._kernel.ImportPluginFromApiManifestAsync("ApiManifestPlugin", manifestFilePath); |
| 102 | + |
| 103 | + // Assert |
| 104 | + Assert.NotNull(plugin); |
| 105 | + Assert.Equal(2, plugin.FunctionCount); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + // Verify that functions are correctly imported |
| 110 | + public async Task VerifyPluginFunctionsFromApiManifestAsync() |
| 111 | + { |
| 112 | + // Act |
| 113 | + var manifestFilePath = Path.Combine(this._testPluginsDir, "example-apimanifest-local.json"); |
| 114 | + |
| 115 | + // Arrange |
| 116 | + var plugin = await this._kernel.ImportPluginFromApiManifestAsync("ApiManifestPlugin", manifestFilePath); |
| 117 | + |
| 118 | + // Assert |
| 119 | + Assert.NotNull(plugin); |
| 120 | + Assert.Equal(2, plugin.FunctionCount); |
| 121 | + |
| 122 | + // Assert functions imported from local openapi.json |
| 123 | + Assert.True(plugin.Contains("listRepairs")); |
| 124 | + Assert.Contains(plugin["listRepairs"].Metadata.AdditionalProperties, static p => p.Key == "method" && p.Value?.ToString() == "GET"); |
| 125 | + |
| 126 | + // Assert functions imported from remote openapi.json |
| 127 | + Assert.True(plugin.Contains("directoryObject_GetDirectoryObject")); |
| 128 | + Assert.Contains(plugin["directoryObject_GetDirectoryObject"].Metadata.AdditionalProperties, static p => p.Key == "method" && p.Value?.ToString() == "GET"); |
| 129 | + } |
| 130 | +} |
0 commit comments