public async Task The_extend_directive_can_be_used_to_load_a_kernel_extension() { var extensionDir = DirectoryUtility.CreateDirectory(); var extensionDllPath = (await KernelExtensionTestHelper.CreateExtension(extensionDir, @"await kernel.SendAsync(new SubmitCode(""using System.Reflection;""));")) .FullName; var kernel = CreateKernel(); using var events = kernel.KernelEvents.ToSubscribedList(); var submitCode = new SubmitCode($"#extend \"{extensionDllPath}\""); await kernel.SendAsync(submitCode); events.Should() .ContainSingle(e => e is ExtensionLoaded && e.As <ExtensionLoaded>().ExtensionPath.FullName.Equals(extensionDllPath)); events.Should() .ContainSingle(e => e is DisplayedValueProduced && e.As <DisplayedValueProduced>() .Value .ToString() .Contains($"Loaded kernel extension TestKernelExtension from assembly {extensionDllPath}")); }
public async Task It_loads_extensions_in_specified_directory_via_a_command(Language language) { var projectDir = DirectoryUtility.CreateDirectory(); var dllDir = projectDir.CreateSubdirectory("extension"); var code = language switch { Language.CSharp => $@"await kernel.SendAsync(new SubmitCode(""display(\""{language} extension installed\"");""));", Language.FSharp => $@"await kernel.SendAsync(new SubmitCode(""display(\""{language} extension installed\"");""));", _ => throw new NotSupportedException("This test does not support the specified language.") }; await KernelExtensionTestHelper.CreateExtensionAssembly( projectDir, code, dllDir); var kernel = CreateKernel(language); await using var context = KernelInvocationContext.Establish(new SubmitCode("")); using var events = context.KernelEvents.ToSubscribedList(); await kernel.LoadExtensionsFromDirectoryAsync( dllDir, context); events.Should() .NotContain(e => e is CommandFailed) .And .ContainSingle <DisplayedValueProduced>(v => v.FormattedValues.Single().Value == $"{language} extension installed"); }
public async Task Should_load_extension_in_directory() { var directory = DirectoryUtility.CreateDirectory(); const string nugetPackageName = "myNugetPackage"; var nugetPackageDirectory = new DirectoryInfo( Path.Combine( directory.FullName, nugetPackageName, "2.0.0")); var extensionsDir = new DirectoryInfo( Path.Combine( nugetPackageDirectory.FullName, "interactive-extensions", "dotnet", "cs")); var extensionDll = await KernelExtensionTestHelper.CreateExtensionInDirectory( directory, @"await kernel.SendAsync(new SubmitCode(""using System.Reflection;""));", extensionsDir); var kernel = CreateKernel(); await kernel.SendAsync(new LoadExtensionsInDirectory(nugetPackageDirectory, kernel.Name)); KernelEvents.Should() .ContainSingle <DisplayedValueUpdated>(e => e.Value.ToString() == $"Loaded kernel extension TestKernelExtension from assembly {extensionDll.FullName}"); }
public async Task Gives_kernel_extension_load_exception_event_when_extension_throws_exception_during_load() { var extensionDir = DirectoryUtility.CreateDirectory(); var extensionDllPath = (await KernelExtensionTestHelper.CreateExtension(extensionDir, @"throw new Exception();")).FullName; var kernel = CreateKernel(); using var events = kernel.KernelEvents.ToSubscribedList(); await kernel.SendAsync(new SubmitCode($"#extend \"{extensionDllPath}\"")); events.Should() .ContainSingle(e => e is KernelExtensionLoadException); }
public async Task It_loads_extensions_found_in_nuget_packages(Language language) { var extensionPackage = KernelExtensionTestHelper.GetOrCreateSimpleExtension(); var kernel = CreateKernel(language); await kernel.SubmitCodeAsync($@" #i ""nuget:{extensionPackage.PackageLocation}"" #r ""nuget:{extensionPackage.Name},{extensionPackage.Version}"""); KernelEvents.Should() .ContainSingle <ReturnValueProduced>() .Which .Value .As <string>() .Should() .Contain("SimpleExtension"); }
public async Task it_loads_script_extension_found_in_nuget_package(Language defaultLanguage) { var extensionPackage = KernelExtensionTestHelper.GetOrCreateScriptBasedExtensionPackage(); var kernel = CreateCompositeKernel(defaultLanguage); await kernel.SubmitCodeAsync($@" #i ""nuget:{extensionPackage.PackageLocation}"" #r ""nuget:{extensionPackage.Name},{extensionPackage.Version}"""); KernelEvents.Should() .ContainSingle <ReturnValueProduced>() .Which .Value .As <string>() .Should() .Contain("ScriptExtension"); }
public async Task Gives_kernel_extension_load_exception_event_when_extension_throws_exception_during_load(string extensionPath) { var extensionDir = DirectoryUtility.CreateDirectory(); await KernelExtensionTestHelper.CreateExtensionInDirectory( extensionDir, @"throw new Exception();", extensionDir.CreateSubdirectory(extensionPath) ); var kernel = CreateKernel(); using var events = kernel.KernelEvents.ToSubscribedList(); await kernel.SendAsync(new LoadExtensionsInDirectory(extensionDir, kernel.Name)); events.Should() .ContainSingle <CommandFailed>(cf => cf.Exception is KernelExtensionLoadException); }
public async Task can_load_kernel_extensions(string extensionPath, string code) { var extensionDir = DirectoryUtility.CreateDirectory(); var extensionFile = await KernelExtensionTestHelper.CreateExtensionInDirectory(extensionDir, code, extensionDir.CreateSubdirectory(extensionPath) ); var kernel = CreateKernel(); using var events = kernel.KernelEvents.ToSubscribedList(); await kernel.SendAsync(new LoadExtensionsInDirectory(extensionDir, kernel.Name)); events.Should() .NotContain(e => e is CommandFailed) .And .ContainSingle <DisplayedValueUpdated>(dv => dv.Value.ToString().Contains(extensionFile.FullName)); }
public async Task It_throws_when_extension_throws_during_load(Language language) { var projectDir = DirectoryUtility.CreateDirectory(); var dllDir = projectDir.CreateSubdirectory("extension"); await KernelExtensionTestHelper.CreateExtensionAssembly( projectDir, "throw new Exception();", dllDir); var kernel = CreateKernel(language); await using var context = KernelInvocationContext.Establish(new SubmitCode("")); using var events = context.KernelEvents.ToSubscribedList(); await kernel.LoadExtensionsFromDirectoryAsync( dllDir, context); events.Should() .ContainSingle <CommandFailed>(cf => cf.Exception is KernelExtensionLoadException); }