-
Notifications
You must be signed in to change notification settings - Fork 367
/
ConnectDirectiveTests.cs
211 lines (170 loc) · 7.16 KB
/
ConnectDirectiveTests.cs
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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.CommandLine;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Connection;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Pocket;
using Pocket.For.Xunit;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.DotNet.Interactive.Tests
{
[LogToPocketLogger(FileNameEnvironmentVariable = "POCKETLOGGER_LOG_PATH")]
public class ConnectDirectiveTests : IDisposable
{
private readonly CompositeDisposable _disposables = new();
public ConnectDirectiveTests(ITestOutputHelper output)
{
_disposables.Add(output.SubscribeToPocketLogger());
}
public void Dispose()
{
_disposables.Dispose();
}
[Fact]
public void connect_command_is_not_available_by_default()
{
using var compositeKernel = new CompositeKernel();
compositeKernel.Directives
.Should()
.NotContain(c => c.Name == "#!connect");
}
[Fact]
public void connect_command_is_available_when_a_user_adds_a_kernel_connection_type()
{
using var compositeKernel = new CompositeKernel();
compositeKernel.UseKernelClientConnection(new ConnectNamedPipe());
compositeKernel.Directives
.Should()
.Contain(c => c.Name == "#!connect");
}
[Fact]
public async Task When_a_kernel_is_connected_then_information_about_it_is_displayed()
{
using var kernel = CreateKernelWithConnectableFakeKernel();
var result = await kernel.SubmitCodeAsync("#!connect --kernel-name my-fake-kernel fake --fakeness-level 9000");
result.KernelEvents
.ToSubscribedList()
.Should()
.ContainSingle<DisplayedValueProduced>()
.Which
.FormattedValues
.Should()
.ContainSingle()
.Which
.Value
.Should()
.Be("Kernel added: #!my-fake-kernel");
}
[Fact]
public async Task When_a_new_kernel_is_connected_then_it_becomes_addressable_by_name()
{
var wasCalled = false;
var fakeKernel = new FakeKernel
{
Handle = (command, context) =>
{
wasCalled = true;
return Task.CompletedTask;
}
};
using var kernel = CreateKernelWithConnectableFakeKernel(fakeKernel);
await kernel.SubmitCodeAsync("#!connect --kernel-name my-fake-kernel fake --fakeness-level 9000");
await kernel.SubmitCodeAsync(@"
#!my-fake-kernel
hello!
");
wasCalled.Should().BeTrue();
}
[Fact]
public async Task Connected_kernels_are_disposed_when_composite_kernel_is_disposed()
{
var disposed = false;
var fakeKernel = new FakeKernel();
fakeKernel.RegisterForDisposal(() => disposed = true);
using var compositeKernel = CreateKernelWithConnectableFakeKernel(fakeKernel);
await compositeKernel.SubmitCodeAsync("#!connect --kernel-name my-fake-kernel fake --fakeness-level 9000");
compositeKernel.Dispose();
disposed.Should().BeTrue();
}
[Fact]
public async Task Connected_kernel_help_description_provides_context_about_the_connection()
{
using var compositeKernel = new CompositeKernel();
compositeKernel.UseKernelClientConnection(
new ConnectFakeKernel("fake", "Connects the fake kernel")
{
CreateKernel = (options, context) => Task.FromResult<Kernel>(new FakeKernel(options.KernelName))
});
await compositeKernel.SubmitCodeAsync("#!connect fake --kernel-name fake-kernel");
var result = await compositeKernel.SubmitCodeAsync("#!fake-kernel -h");
using var events = result.KernelEvents.ToSubscribedList();
events.Should()
.ContainSingle<StandardOutputValueProduced>()
.Which
.FormattedValues
.Should()
.ContainSingle()
.Which
.Value
.Should()
.ContainAll("#!fake-kernel", "Doesn't really do anything at all. (Connected kernel)");
}
[Fact]
public async Task Multiple_connections_can_be_created_using_the_same_connection_type()
{
using var compositeKernel = new CompositeKernel();
compositeKernel.UseKernelClientConnection(
new ConnectFakeKernel("fake", "Connects the fake kernel")
{
CreateKernel = (options, context) => Task.FromResult<Kernel>(new FakeKernel(options.KernelName))
});
await compositeKernel.SubmitCodeAsync("#!connect fake --kernel-name fake1");
await compositeKernel.SubmitCodeAsync("#!connect fake --kernel-name fake2");
compositeKernel
.Should()
.ContainSingle(k => k.Name == "fake1")
.And
.ContainSingle(k => k.Name == "fake2");
}
private static Kernel CreateKernelWithConnectableFakeKernel(FakeKernel fakeKernel = null)
{
var compositeKernel = new CompositeKernel
{
new FakeKernel("x")
};
compositeKernel.UseKernelClientConnection(
new ConnectFakeKernel("fake", "Connects the fake kernel")
{
CreateKernel = (options, context) =>
{
var kernel = fakeKernel ?? new FakeKernel();
kernel.Name = options.KernelName;
return Task.FromResult<Kernel>(kernel);
}
});
return compositeKernel;
}
public class ConnectFakeKernel : ConnectKernelCommand<FakeKernelConnectionOptions>
{
public ConnectFakeKernel(string name, string description) : base(name, description)
{
AddOption(new Option<int>("--fakeness-level"));
ConnectedKernelDescription = "Doesn't really do anything at all.";
}
public Func<FakeKernelConnectionOptions, KernelInvocationContext, Task<Kernel>> CreateKernel { get; set; }
public override Task<Kernel> CreateKernelAsync(FakeKernelConnectionOptions options, KernelInvocationContext context)
{
return CreateKernel(options, context);
}
}
}
public class FakeKernelConnectionOptions : KernelConnectionOptions
{
public int FakenessLevel { get; set; }
}
}