-
Notifications
You must be signed in to change notification settings - Fork 367
/
RuntimeTelemetryTests.cs
165 lines (137 loc) · 5.94 KB
/
RuntimeTelemetryTests.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
// 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.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.Telemetry;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Xunit;
namespace Microsoft.DotNet.Interactive.App.Tests;
public class RuntimeTelemetryTests : IDisposable
{
private readonly FakeTelemetrySender _telemetrySender;
private readonly CompositeKernel _kernel;
public RuntimeTelemetryTests()
{
_telemetrySender = new FakeTelemetrySender();
_kernel = new CompositeKernel
{
new CSharpKernel().UseNugetDirective()
}
.UseTelemetrySender(_telemetrySender);
}
public void Dispose() => _kernel.Dispose();
[Fact]
public async Task Language_information_is_sent_on_successful_code_execution_when_target_kernel_is_specified()
{
var telemetrySender = new FakeTelemetrySender();
using var kernel = new CompositeKernel
{
new CSharpKernel(),
new FakeKernel("kql", "KQL"),
new FakeKernel("sql", "T-SQL")
}
.UseTelemetrySender(telemetrySender);
await kernel.SendAsync(new SubmitCode(@"
#!sql
select * from db
#!kql
telemetry
| take 1
#!csharp
1+1
"));
var properties = telemetrySender.TelemetryEvents.Where(e => e.EventName == "CodeSubmitted").SelectMany(e => e.Properties).Where(p => p.Key is "KernelName" or "KernelLanguageName").ToArray();
var expected = new[]
{
new KeyValuePair<string, string>("KernelName", "kql".ToSha256Hash()),
new KeyValuePair<string, string>("KernelLanguageName", "KQL".ToSha256Hash()),
new KeyValuePair<string, string>("KernelName", "sql".ToSha256Hash()),
new KeyValuePair<string, string>("KernelLanguageName", "T-SQL".ToSha256Hash()),
new KeyValuePair<string, string>("KernelName", "csharp".ToSha256Hash()),
new KeyValuePair<string, string>("KernelLanguageName", "C#".ToSha256Hash())
};
properties.Should().BeEquivalentTo(expected);
}
[Fact]
public async Task Language_information_is_sent_on_successful_code_execution_after_split()
{
await _kernel.SendAsync(new SubmitCode("123", "csharp"));
_telemetrySender.TelemetryEvents
.Should()
.ContainSingle(e => e.EventName == "CodeSubmitted")
.Which
.Properties
.Should()
.Contain(
new KeyValuePair<string, string>("KernelName", "csharp".ToSha256Hash()),
new KeyValuePair<string, string>("KernelLanguageName", "C#".ToSha256Hash()));
}
[Fact]
public async Task Language_information_is_sent_on_unsuccessful_code_execution_when_target_kernel_is_specified()
{
await _kernel.SendAsync(new SubmitCode("that doesn't compile", "csharp"));
_telemetrySender.TelemetryEvents
.Should()
.ContainSingle(e => e.EventName == "CodeSubmitted")
.Which
.Properties
.Should()
.Contain(new KeyValuePair<string, string>("KernelName", "csharp".ToSha256Hash()));
}
[Fact]
public async Task Language_information_is_sent_on_successful_code_execution_when_target_kernel_is_not_specified()
{
await _kernel.SendAsync(new SubmitCode("123"));
_telemetrySender.TelemetryEvents
.Should()
.ContainSingle(e => e.EventName == "CodeSubmitted")
.Which
.Properties
.Should()
.Contain(
new KeyValuePair<string, string>("KernelName", "csharp".ToSha256Hash()),
new KeyValuePair<string, string>("KernelLanguageName", "C#".ToSha256Hash()));
}
[Fact]
public async Task Language_information_is_sent_on_unsuccessful_code_execution_when_target_kernel_is_not_specified()
{
await _kernel.SendAsync(new SubmitCode("that doesn't compile"));
_telemetrySender.TelemetryEvents
.Should()
.ContainSingle(e => e.EventName == "CodeSubmitted")
.Which
.Properties
.Should()
.Contain(new KeyValuePair<string, string>("KernelName", "csharp".ToSha256Hash()));
}
[Fact]
public async Task Kernel_session_id_is_sent()
{
await _kernel.SendAsync(new SubmitCode("123"));
await _kernel.SendAsync(new SubmitCode("456"));
var sessionIdForFirstExecution = _telemetrySender.TelemetryEvents[0].Properties["KernelSessionId"];
var sessionIdForSecondExecution = _telemetrySender.TelemetryEvents[1].Properties["KernelSessionId"];
sessionIdForFirstExecution
.Should()
.Be(sessionIdForSecondExecution);
}
[Fact]
public async Task Package_and_version_number_are_sent_on_successful_package_load()
{
await _kernel.SendAsync(new SubmitCode("#r \"nuget:NodaTime,3.1.0\"", "csharp"));
_telemetrySender.TelemetryEvents
.Should()
.ContainSingle(e => e.EventName == "PackageAdded")
.Which
.Properties
.Should()
.Contain(
new KeyValuePair<string, string>("PackageName", "nodatime".ToSha256HashWithNormalizedCasing()),
new KeyValuePair<string, string>("PackageVersion", "3.1.0".ToSha256Hash()));
}
}