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 Current_differs_per_async_context() { var barrier = new Barrier(2); IKernelCommand commandInTask1 = null; IKernelCommand commandInTask2 = null; await Task.Run(() => { using (var x = KernelInvocationContext.Establish(new SubmitCode(""))) { barrier.SignalAndWait(1000); commandInTask1 = KernelInvocationContext.Current.Command; } }); await Task.Run(() => { using (KernelInvocationContext.Establish(new SubmitCode(""))) { barrier.SignalAndWait(1000); commandInTask2 = KernelInvocationContext.Current.Command; } }); commandInTask1.Should() .NotBe(commandInTask2) .And .NotBeNull(); }
public async Task It_loads_extensions_in_specified_directory_via_a_command() { var projectDir = DirectoryUtility.CreateDirectory(); var dllDir = projectDir.CreateSubdirectory("extension"); var extensionDll = await CreateExtensionAssembly( projectDir, @"await kernel.SendAsync(new SubmitCode(""display(\""csharp extension installed\"");""));", dllDir); var kernel = (IExtensibleKernel)CreateKernel(Language.CSharp); 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 <DisplayedValueUpdated>(dv => dv.Value.ToString().Contains(extensionDll.FullName)); }
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(\""C# extension installed\"");""));", Language.FSharp => @"await kernel.SendAsync(new SubmitCode(""display(\""F# extension installed\"");""));", _ => throw new NotSupportedException("This test does not support the specified language.") }; var extensionDll = await CreateExtensionAssembly( projectDir, code, dllDir); var kernel = (IExtensibleKernel)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 <DisplayedValueUpdated>(dv => dv.Value.ToString().Contains(extensionDll.FullName)); }
public void HttpNotebookFrontendEnvironment_can_complete_if_the_uri_is_supplied_after_execution_is_invoked() { var testCommand = new TestCommand(); testCommand.SetToken("token-abcd"); var context = KernelInvocationContext.Establish(testCommand); var events = context.KernelEvents.ToSubscribedList(); var httpFrontend = new HtmlNotebookFrontendEnvironment(TimeSpan.FromSeconds(2)); _ = httpFrontend.ExecuteClientScript("console.log('test');", context); httpFrontend.SetApiUri(new Uri("http://test-uri")); events .Should() .ContainSingle <DisplayedValueProduced>() .Which .FormattedValues .Should() .ContainSingle() .Which .Value .Should() .ContainAll( "http://test-uri", @"console.log(\u0027test\u0027);" ); }
public void After_disposal_Current_is_null() { var context = KernelInvocationContext.Establish(new SubmitCode("123")); ((IDisposable)context).Dispose(); KernelInvocationContext.Current.Should().BeNull(); }
private (KernelInvocationContext, HtmlNotebookFrontendEnvironment) CreateTestHtmlFrontendAndContext() { var testCommand = new TestCommand(); testCommand.SetToken("token-abcd"); var context = KernelInvocationContext.Establish(testCommand); var httpFrontend = new HtmlNotebookFrontendEnvironment(new Uri("http://12.12.12.12:4242")); return(context, httpFrontend); }
public async Task When_Fail_is_called_CommandFailed_is_published() { var command = new SubmitCode("123"); await using var context = KernelInvocationContext.Establish(command); var events = context.KernelEvents.ToSubscribedList(); context.Fail(message: "oops!"); events.Should() .ContainSingle <CommandFailed>(); }
public async Task After_disposal_Current_is_null() { var context = KernelInvocationContext.Establish(new SubmitCode("123")); context.OnComplete(async invocationContext => { await Task.Delay(10); }); await context.DisposeAsync(); KernelInvocationContext.Current.Should().BeNull(); }
public async Task When_Complete_is_called_then_CommandHandled_is_published() { var command = new SubmitCode("123"); await using var context = KernelInvocationContext.Establish(command); var events = context.KernelEvents.ToSubscribedList(); context.Complete(command); events.Should() .ContainSingle <CommandHandled>(); }
public void When_Complete_is_called_then_CommandFailed_is_not_published() { var command = new SubmitCode("123"); using var context = KernelInvocationContext.Establish(command); var events = context.KernelEvents.ToSubscribedList(); context.Complete(command); events.Should() .NotContain(e => e is CommandFailed); }
public void When_Fail_is_called_CommandHandled_is_not_published() { var command = new SubmitCode("123"); using var context = KernelInvocationContext.Establish(command); var events = context.KernelEvents.ToSubscribedList(); context.Fail(message: "oops!"); events.Should() .NotContain(e => e is CommandHandled); }
public void When_outer_context_is_completed_then_inner_commands_can_no_longer_be_used_to_publish_events() { using var outer = KernelInvocationContext.Establish(new SubmitCode("abc")); var events = outer.KernelEvents.ToSubscribedList(); using var inner = KernelInvocationContext.Establish(new SubmitCode("def")); outer.Complete(outer.Command); inner.Publish(new ErrorProduced("oops!")); events.Should().NotContain(e => e is ErrorProduced); }
public async Task HttpNotebookFrontendEnvironment_does_not_complete_if_uri_is_not_specified() { var testCommand = new TestCommand(); testCommand.SetToken("token-abcd"); var context = KernelInvocationContext.Establish(testCommand); var httpFrontend = new HtmlNotebookFrontendEnvironment(TimeSpan.FromSeconds(2)); Func <Task> executeTask = () => httpFrontend.ExecuteClientScript("console.log('test');", context); await executeTask .Should() .ThrowAsync <TimeoutException>(); }
public void When_Fail_is_called_then_no_further_events_are_published() { var command = new SubmitCode("123"); using var context = KernelInvocationContext.Establish(command); var events = context.KernelEvents.ToSubscribedList(); context.Fail(message: "oops"); context.Publish(new DisplayedValueProduced("oops", command)); events.Should().NotContain(e => e is DisplayedValueProduced); }
public void When_Complete_is_called_then_no_further_events_are_published() { var command = new SubmitCode("123"); using var context = KernelInvocationContext.Establish(command); var events = context.KernelEvents.ToSubscribedList(); context.Complete(command); context.Publish(new ErrorProduced("oops", command)); events.Should().NotContain(e => e is ErrorProduced); }
public void When_inner_context_fails_then_no_further_events_can_be_published() { using var outer = KernelInvocationContext.Establish(new SubmitCode("abc")); var events = outer.KernelEvents.ToSubscribedList(); var innerCommand = new SubmitCode("def"); using var inner = KernelInvocationContext.Establish(innerCommand); inner.Fail(); inner.Publish(new ErrorProduced("oops!")); events.Should().NotContain(e => e is ErrorProduced); }
public void When_created_in_the_same_context_then_child_commands_having_the_same_parent_also_have_the_same_token() { var parentCommand = new SubmitCode("123"); string token1 = null; string token2 = null; using (KernelInvocationContext.Establish(parentCommand)) { token1 = new SubmitCode("456").GetToken(); token2 = new SubmitCode("456").GetToken(); } token1.Should().Be(token2); }
public async Task When_inner_context_is_completed_then_no_further_events_can_be_published_for_it() { await using var outer = KernelInvocationContext.Establish(new SubmitCode("abc")); var events = outer.KernelEvents.ToSubscribedList(); var innerSubmitCode = new SubmitCode("def"); await using var inner = KernelInvocationContext.Establish(innerSubmitCode); inner.Complete(innerSubmitCode); inner.Publish(new ErrorProduced("oops!", innerSubmitCode)); events.Should().NotContain(e => e is ErrorProduced); }
public void When_multiple_commands_are_active_then_context_does_not_publish_CommandHandled_until_all_are_complete() { var outerSubmitCode = new SubmitCode("abc"); using var outer = KernelInvocationContext.Establish(outerSubmitCode); var events = outer.KernelEvents.ToSubscribedList(); var innerSubmitCode = new SubmitCode("def"); using var inner = KernelInvocationContext.Establish(innerSubmitCode); inner.Complete(innerSubmitCode); events.Should().NotContain(e => e is CommandHandled); }
public async Task When_inner_context_fails_then_no_further_events_can_be_published() { var command = new SubmitCode("abc"); await using var outer = KernelInvocationContext.Establish(command); var events = outer.KernelEvents.ToSubscribedList(); var innerCommand = new SubmitCode("def"); await using var inner = KernelInvocationContext.Establish(innerCommand); inner.Fail(); inner.Publish(new DisplayedValueProduced("oops!", command)); events.Should().NotContain(e => e is DisplayedValueProduced); }
public void When_inner_context_fails_then_CommandFailed_is_published_for_outer_command() { using var outer = KernelInvocationContext.Establish(new SubmitCode("abc")); var events = outer.KernelEvents.ToSubscribedList(); var innerCommand = new SubmitCode("def"); using var inner = KernelInvocationContext.Establish(innerCommand); inner.Fail(); events.Should() .ContainSingle <CommandFailed>() .Which .Command .Should() .Be(outer.Command); }
public void When_resent_then_child_commands_having_the_same_parent_have_repeatable_tokens() { var parentCommand = new SubmitCode("123"); parentCommand.SetToken("the-token"); string token1 = null; string token2 = null; using (KernelInvocationContext.Establish(parentCommand)) { token1 = new SubmitCode("456").GetToken(); } using (KernelInvocationContext.Establish(parentCommand)) { token2 = new SubmitCode("456").GetToken(); } token1.Should().Be(token2); }
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); }