public async Task Completions_do_not_include_duplicates() { var cSharpKernel = new CSharpKernel(); using var compositeKernel = new CompositeKernel { cSharpKernel }; compositeKernel.DefaultKernelName = cSharpKernel.Name; var commandName = "#!hello"; compositeKernel.AddDirective(new Command(commandName) { Handler = CommandHandler.Create(() => { }) }); cSharpKernel.AddDirective(new Command(commandName) { Handler = CommandHandler.Create(() => { }) }); var result = await compositeKernel.SendAsync(new RequestCompletions("#!", new LinePosition(0, 2))); var events = result.KernelEvents.ToSubscribedList(); events .Should() .ContainSingle <CompletionsProduced>() .Which .Completions .Should() .ContainSingle(e => e.DisplayText == commandName); }
public async Task Directive_handlers_are_invoked_in_the_order_in_which_they_occur_in_the_code_submission() { using var kernel = new CSharpKernel(); var events = kernel.KernelEvents.ToSubscribedList(); kernel.AddDirective(new Command("#increment") { Handler = CommandHandler.Create(async(KernelInvocationContext context) => { await context.HandlingKernel.SubmitCodeAsync("i++;"); }) }); await kernel.SubmitCodeAsync(@" var i = 0; #increment i"); events .Should() .ContainSingle <ReturnValueProduced>() .Which .Value .Should() .Be(1); }
public async Task magic_command_parse_errors_prevent_code_submission_from_being_run() { var command = new Command("%oops") { new Argument <string>() }; using var kernel = new CSharpKernel(); kernel.AddDirective(command); var events = kernel.KernelEvents.ToSubscribedList(); await kernel.SubmitCodeAsync("%oops\n123"); events.Should().NotContain(e => e is ReturnValueProduced); }
public async Task lsmagic_lists_registered_magic_commands_in_subkernels() { var subkernel1 = new CSharpKernel(); subkernel1.AddDirective(new Command("#!from-subkernel-1")); var subkernel2 = new FSharpKernel(); subkernel2.AddDirective(new Command("#!from-subkernel-2")); using var compositeKernel = new CompositeKernel { subkernel1, subkernel2 } .UseDefaultMagicCommands() .LogEventsToPocketLogger(); compositeKernel.DefaultKernelName = "csharp"; compositeKernel.AddDirective(new Command("#!from-compositekernel")); using var events = compositeKernel.KernelEvents.ToSubscribedList(); await compositeKernel.SendAsync(new SubmitCode("#!lsmagic")); var valueProduceds = events.OfType <DisplayedValueProduced>().ToArray(); valueProduceds[0].Value .ToDisplayString("text/html") .Should() .ContainAll("#!lsmagic", "#!csharp", "#!fsharp", "#!from-compositekernel"); valueProduceds[1].Value .ToDisplayString("text/html") .Should() .ContainAll("#!lsmagic", "#!from-subkernel-1"); valueProduceds[2].Value .ToDisplayString("text/html") .Should() .ContainAll("#!lsmagic", "#!from-subkernel-2"); }
public async Task magic_command_parse_errors_are_displayed() { var command = new Command("%oops") { new Argument <string>() }; using var kernel = new CSharpKernel(); kernel.AddDirective(command); var events = kernel.KernelEvents.ToSubscribedList(); await kernel.SubmitCodeAsync("%oops"); events.Should() .ContainSingle <ErrorProduced>() .Which .Message .Should() .Be("Required argument missing for command: %oops"); }
private static async Task ConfigureRover(CSharpKernel csharpKernel) { Microsoft.DotNet.Interactive.Formatting.Formatter.ListExpansionLimit = 42; using var _ = Log.OnEnterAndExit(); await LoadAssemblyAndAddNamespace <RoverRobot>(csharpKernel); await LoadAssemblyAndAddNamespace <ResourceScanner>(csharpKernel); await AddNamespace(csharpKernel, typeof(ImageProcessing.ImageExtensions)); var RoverBody = new RoverRobot(PiTop4Board.Instance.GetOrCreateExpansionPlate(), PiTop4Board.Instance.GetOrCreateCamera <StreamingCamera>(0), RoverRobotConfiguration.Default); var RoverBrain = new RoverRobotAgent(); var ResourceScanner = new ResourceScanner(); RoverBody.BlinkAllLights(); await csharpKernel.SetVariableAsync(nameof(RoverBody), RoverBody); await csharpKernel.SetVariableAsync(nameof(RoverBrain), RoverBrain); await csharpKernel.SetVariableAsync(nameof(ResourceScanner), ResourceScanner); var command = new Command("#!reset", "Reset RoverBody, RoverBrain and BrainState") { new Option <bool>("--body", description: "Resets the rover body"), new Option <bool>("--brain", description: "Resets the rover brain"), new Option <bool>("--state", description: "Resets the rover brain state"), new Option <bool>("--all", description: "Resets the entire rover"), }; command.Handler = CommandHandler.Create <bool, bool, bool, bool, KernelInvocationContext>(async(body, brain, state, all, context) => { if (body || brain || state || all) { var code = new StringBuilder(); var resetTarget = new List <string>(); if (brain || all) { code.AppendLine($"{nameof(RoverBrain)}.Reset();"); resetTarget.Add("brain"); } if (state || all) { code.AppendLine($"{nameof(RoverBrain)}.ClearState();"); resetTarget.Add("state"); } if (body || all) { code.AppendLine($"{nameof(RoverBody)}.Reset();"); resetTarget.Add("body"); } var value = context.Display($"Reset for {string.Join(", ", resetTarget)} in progress", PlainTextFormatter.MimeType); await csharpKernel.SendAsync(new SubmitCode(code.ToString())); value.Update($"Reset for {string.Join(", ", resetTarget)} done!"); } }); csharpKernel.AddDirective(command); var source = new CancellationTokenSource(); var robotLoop = Task.Run(() => { using var operation = Log.OnEnterAndExit("roverBrainLoop"); while (!source.IsCancellationRequested) { if (!source.IsCancellationRequested) { using var __ = operation.OnEnterAndExit("Perceive"); try { RoverBrain.Perceive(); } catch (Exception e) { __.Error(e); } } if (!source.IsCancellationRequested) { var planResult = PlanningResult.NoPlan; using var ___ = operation.OnEnterAndExit("Plan"); try { planResult = RoverBrain.Plan(); } catch (Exception e) { ___.Error(e); planResult = PlanningResult.NoPlan; } if (!source.IsCancellationRequested && planResult != PlanningResult.NoPlan) { using var ____ = operation.OnEnterAndExit("Act"); RoverBrain.Act(); } } } RoverBody.MotionComponent.Stop(); }, source.Token); var reactLoop = Task.Run(() => { using var operation = Log.OnEnterAndExit("roverBrainReactLoop"); while (!source.IsCancellationRequested) { if (!source.IsCancellationRequested) { using var __ = operation.OnEnterAndExit("React"); try { RoverBrain.React(); } catch (Exception e) { __.Error(e); } } } RoverBody.MotionComponent.Stop(); }, source.Token); csharpKernel.RegisterForDisposal(() => { source.Cancel(false); Task.WaitAll(new[] { robotLoop, reactLoop }, TimeSpan.FromSeconds(10)); RoverBody.Dispose(); }); }