-
Notifications
You must be signed in to change notification settings - Fork 368
/
VariableSharingTests.cs
429 lines (344 loc) · 14.1 KB
/
VariableSharingTests.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// 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.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Formatting;
using Microsoft.DotNet.Interactive.FSharp;
using Microsoft.DotNet.Interactive.PowerShell;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Microsoft.DotNet.Interactive.ValueSharing;
using Pocket;
using Pocket.For.Xunit;
using Xunit;
namespace Microsoft.DotNet.Interactive.Tests
{
[LogToPocketLogger(FileNameEnvironmentVariable = "POCKETLOGGER_LOG_PATH")]
public class VariableSharingTests : IDisposable
{
private readonly CompositeDisposable _disposables = new();
[Theory]
[InlineData(
"#!fsharp",
"let x = 123",
"#!share --from fsharp x\nx")]
[InlineData(
"#!pwsh",
"$x = 123",
"#!share --from pwsh x\nx")]
public async Task csharp_kernel_can_read_variables_from_other_kernels(
string from,
string codeToWrite,
string codeToRead)
{
using var kernel = CreateKernel();
using var events = kernel.KernelEvents.ToSubscribedList();
await kernel.SubmitCodeAsync($"{from}\n{codeToWrite}");
await kernel.SubmitCodeAsync($"#!csharp\n{codeToRead}");
events.Should()
.ContainSingle<ReturnValueProduced>()
.Which
.Value
.Should()
.Be(123);
}
[Theory]
[InlineData(
"#!csharp",
"var x = 123;",
"#!share --from csharp x\nx")]
[InlineData(
"#!pwsh",
"$x = 123",
"#!share --from pwsh x\nx")]
public async Task fsharp_kernel_can_read_variables_from_other_kernels(
string from,
string codeToWrite,
string codeToRead)
{
using var kernel = CreateKernel();
using var events = kernel.KernelEvents.ToSubscribedList();
await kernel.SubmitCodeAsync($"{from}\n{codeToWrite}");
await kernel.SubmitCodeAsync($"#!fsharp\n{codeToRead}");
events.Should()
.ContainSingle<ReturnValueProduced>()
.Which
.Value
.Should()
.Be(123);
}
[Theory]
[InlineData(
"#!csharp",
"var x = 123;",
"#!share --from csharp x\n\"$($x):$($x.GetType().ToString())\"")]
[InlineData(
"#!fsharp",
"let x = 123",
"#!share --from fsharp x\n\"$($x):$($x.GetType().ToString())\"")]
public async Task pwsh_kernel_can_read_variables_from_other_kernels(
string from,
string codeToWrite,
string codeToRead)
{
using var kernel = CreateKernel();
await kernel.SubmitCodeAsync($"{from}\n{codeToWrite}");
var results = await kernel.SubmitCodeAsync($"#!pwsh\n{codeToRead} | Out-Display -MimeType text/plain");
results.KernelEvents.ToSubscribedList().Should()
.ContainSingle<DisplayedValueProduced>()
.Which
.FormattedValues
.Should()
.ContainSingle(v => v.MimeType == PlainTextFormatter.MimeType)
.Which
.Value
.Trim()
.Should()
.Be("123:System.Int32");
}
[Theory]
[InlineData(
"#!fsharp",
"let x = 1",
"#!share --from fsharp x")]
[InlineData(
"#!pwsh",
"$x = 1",
"#!share --from pwsh x")]
public async Task csharp_kernel_variables_shared_from_other_kernels_resolve_to_the_correct_runtime_type(string from, string codeToWrite, string codeToRead)
{
using var kernel = CreateKernel();
using var events = kernel.KernelEvents.ToSubscribedList();
await kernel.SubmitCodeAsync($"{from}\n{codeToWrite}");
await kernel.SubmitCodeAsync($"#!csharp\n{codeToRead}\nx + 1");
events.Should()
.ContainSingle<ReturnValueProduced>()
.Which
.Value
.Should()
.Be(2);
}
[Theory]
[InlineData(
"#!csharp",
"var x = 1;",
"#!share --from csharp x")]
[InlineData(
"#!pwsh",
"$x = 1",
"#!share --from pwsh x")]
public async Task fsharp_kernel_variables_shared_from_other_kernels_resolve_to_the_correct_runtime_types(string from, string codeToWrite, string codeToRead)
{
using var kernel = CreateKernel();
using var events = kernel.KernelEvents.ToSubscribedList();
await kernel.SubmitCodeAsync($"{from}\n{codeToWrite}");
await kernel.SubmitCodeAsync($"#!fsharp\n{codeToRead}\nx + 1");
events.Should()
.ContainSingle<ReturnValueProduced>()
.Which
.Value
.Should()
.Be(2);
}
[Theory]
[InlineData(
"#!csharp",
"var x = 1;",
"#!share --from csharp x")]
[InlineData(
"#!fsharp",
"let x = 1",
"#!share --from fsharp x")]
public async Task pwsh_kernel_variables_shared_from_other_kernels_resolve_to_the_correct_runtime_type(string from, string codeToWrite, string codeToRead)
{
using var kernel = CreateKernel();
using var events = kernel.KernelEvents.ToSubscribedList();
await kernel.SubmitCodeAsync($"{from}\n{codeToWrite}");
await kernel.SubmitCodeAsync($"#!pwsh\n{codeToRead}\n$x + 1 | Out-Display -MimeType text/plain");
events.Should()
.ContainSingle<DisplayedValueProduced>()
.Which
.FormattedValues
.Should()
.ContainSingle(v => v.MimeType == PlainTextFormatter.MimeType)
.Which
.Value
.Trim()
.Should()
.Be("2");
}
[Fact]
public async Task JavaScript_ProxyKernel_can_share_a_value_from_csharp()
{
var (compositeKernel, remoteKernel) = await CreateCompositeKernelWithJavaScriptProxyKernel();
var remoteCommands = new List<KernelCommand>();
remoteKernel.AddMiddleware(async (command, context, next) =>
{
remoteCommands.Add(command);
await next(command, context);
});
await compositeKernel.SubmitCodeAsync("int csharpVariable = 123;");
var submitCode = new SubmitCode(@"
#!javascript
#!share --from csharp csharpVariable");
await compositeKernel.SendAsync(submitCode);
remoteCommands.Should()
.ContainSingle<SubmitCode>()
.Which
.Code
.Should()
.Be("csharpVariable = 123;");
}
[Fact]
public async Task CSharpKernel_can_share_variable_from_JavaScript_via_a_ProxyKernel()
{
var (compositeKernel, jsKernel) = await CreateCompositeKernelWithJavaScriptProxyKernel();
var jsVariableName = "jsVariable";
jsKernel.RegisterCommandHandler<RequestValue>((cmd, context) =>
{
context.Publish(new ValueProduced(null, jsVariableName, new FormattedValue(JsonFormatter.MimeType, "123"), cmd));
return Task.CompletedTask;
});
var result = await compositeKernel.SendAsync(new RequestKernelInfo("javascript"));
var events = result.KernelEvents.ToSubscribedList();
events.Should().NotContainErrors();
var submitCode = new SubmitCode($@"
#!csharp
#!share --from javascript {jsVariableName}");
await compositeKernel.SendAsync(submitCode);
var csharpKernel = (CSharpKernel)compositeKernel.FindKernelByName("csharp");
var (success, valueInfosProduced) = await csharpKernel.TryRequestValueInfosAsync();
valueInfosProduced.ValueInfos
.Should()
.ContainSingle(v => v.Name == jsVariableName);
csharpKernel.TryGetValue<double>(jsVariableName, out var jsVariable)
.Should().BeTrue();
jsVariable.Should().Be(123);
}
[Fact]
public async Task CSharpKernel_can_prompt_for_input_from_JavaScript_via_a_ProxyKernel()
{
var (compositeKernel, jsKernel) = await CreateCompositeKernelWithJavaScriptProxyKernel();
var valueKernel = new KeyValueStoreKernel().UseValueSharing();
compositeKernel.Add(valueKernel);
jsKernel.RegisterCommandHandler<RequestInput>((cmd, context) =>
{
context.Publish(new InputProduced("hello!", cmd));
return Task.CompletedTask;
});
compositeKernel.SetDefaultTargetKernelNameForCommand(typeof(RequestInput), "javascript");
var valueName = "input";
var submitCode = new SubmitCode($@"
#!value --name {valueName} --from-value @input:input-please
");
var result = await compositeKernel.SendAsync(submitCode);
var events = result.KernelEvents.ToSubscribedList();
events.Should().NotContainErrors();
var (success, valueProduced) = await valueKernel.TryRequestValueAsync(valueName);
success
.Should()
.BeTrue();
valueProduced.Value.Should().Be("hello!");
}
[Fact]
public async Task Values_can_be_shared_using_a_specified_MIME_type()
{
using var kernel = CreateKernel();
using var events = kernel.KernelEvents.ToSubscribedList();
await kernel.SubmitCodeAsync("#!csharp\nvar x = 123;");
await kernel.SubmitCodeAsync(@"
#!fsharp
#!share --from csharp x --mime-type text/html
x");
events.Should().NotContainErrors();
events.Should()
.ContainSingle<ValueProduced>()
.Which
.FormattedValue
.Should()
.BeEquivalentTo(new FormattedValue("text/html", 123.ToDisplayString("text/html")));
}
[Fact]
public async Task When_a_MIME_type_is_specified_then_a_string_is_declared_instead_of_a_reference()
{
using var kernel = CreateKernel();
using var events = kernel.KernelEvents.ToSubscribedList();
await kernel.SubmitCodeAsync("#!csharp\nvar stringType = typeof(string);");
await kernel.SubmitCodeAsync(@"
#!fsharp
#!share --from csharp stringType --mime-type text/plain
stringType");
events.Should().NotContainErrors();
events.Should()
.ContainSingle<ReturnValueProduced>()
.Which
.Value
.Should()
.Be("System.String");
}
[Fact]
public async Task A_name_can_be_specified_for_the_imported_value()
{
using var kernel = CreateKernel();
using var events = kernel.KernelEvents.ToSubscribedList();
await kernel.SubmitCodeAsync("#!csharp\nvar x = 123;");
await kernel.SubmitCodeAsync(@"
#!fsharp
#!share --from csharp x --mime-type text/plain --as y
y");
events.Should().NotContainErrors();
events.Should()
.ContainSingle<ValueProduced>()
.Which
.FormattedValue
.Should()
.BeEquivalentTo(new FormattedValue("text/plain", 123.ToDisplayString("text/plain")));
}
private async Task<(CompositeKernel, FakeKernel)> CreateCompositeKernelWithJavaScriptProxyKernel()
{
var localCompositeKernel = new CompositeKernel
{
new CSharpKernel().UseValueSharing()
};
localCompositeKernel.DefaultKernelName = "csharp";
var remoteCompositeKernel = new CompositeKernel();
var remoteKernel = new FakeKernel("remote-javascript");
remoteCompositeKernel.Add(remoteKernel);
ConnectHost.ConnectInProcessHost(
localCompositeKernel,
remoteCompositeKernel);
var remoteKernelUri = new Uri("kernel://remote/remote-javascript");
var javascriptKernel =
await localCompositeKernel
.Host
.ConnectProxyKernelOnDefaultConnectorAsync(
"javascript",
remoteKernelUri);
await localCompositeKernel.SendAsync(new RequestKernelInfo(remoteKernelUri));
javascriptKernel.UseValueSharing(new JavaScriptValueDeclarer());
_disposables.Add(remoteCompositeKernel);
return (localCompositeKernel, remoteKernel);
}
private static CompositeKernel CreateKernel()
{
return new CompositeKernel
{
new CSharpKernel()
.UseNugetDirective()
.UseKernelHelpers()
.UseValueSharing(),
new FSharpKernel()
.UseNugetDirective()
.UseKernelHelpers()
.UseValueSharing(),
new PowerShellKernel()
.UseValueSharing()
}.LogEventsToPocketLogger();
}
public void Dispose() => _disposables.Dispose();
}
}