diff --git a/src/Bicep.LangServer.IntegrationTests/TelemetryTests.cs b/src/Bicep.LangServer.IntegrationTests/TelemetryTests.cs index 64faf7fb839..bae03445148 100644 --- a/src/Bicep.LangServer.IntegrationTests/TelemetryTests.cs +++ b/src/Bicep.LangServer.IntegrationTests/TelemetryTests.cs @@ -2,18 +2,22 @@ // Licensed under the MIT License. using System; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading.Tasks; -using Bicep.Core.Syntax; -using Bicep.Core.Text; +using Bicep.Core.FileSystem; using Bicep.Core.UnitTests.Assertions; +using Bicep.LangServer.IntegrationTests.Helpers; using Bicep.LanguageServer.Telemetry; using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json.Linq; +using OmniSharp.Extensions.LanguageServer.Protocol; using OmniSharp.Extensions.LanguageServer.Protocol.Document; using OmniSharp.Extensions.LanguageServer.Protocol.Models; +using OmniSharp.Extensions.LanguageServer.Protocol.Window; +using OmniSharp.Extensions.LanguageServer.Protocol.Workspace; namespace Bicep.LangServer.IntegrationTests { @@ -25,30 +29,51 @@ public class TelemetryTests public TestContext? TestContext { get; set; } [TestMethod] - public async Task ValidateDeclarationSnippetCompletionItemContainsCommandWithTelemetryInformation() + public async Task VerifyTopLevelDeclarationSnippetInsertionFiresTelemetryEvent() { - var syntaxTree = SyntaxTree.Create(new Uri("file:///main.bicep"), string.Empty); - var client = await IntegrationTestHelper.StartServerWithTextAsync(string.Empty, syntaxTree.FileUri); + var fileSystemDict = new Dictionary(); + var telemetryReceived = new TaskCompletionSource(); + + var client = await IntegrationTestHelper.StartServerWithClientConnectionAsync( + options => options.OnTelemetryEvent(telemetry => { + telemetryReceived.SetResult(telemetry); + }), + fileResolver: new InMemoryFileResolver(fileSystemDict)); + + var mainUri = DocumentUri.FromFileSystemPath("/main.bicep"); + fileSystemDict[mainUri.ToUri()] = string.Empty; + + client.TextDocument.DidOpenTextDocument(TextDocumentParamHelper.CreateDidOpenDocumentParams(mainUri, fileSystemDict[mainUri.ToUri()], 1)); var completions = await client.RequestCompletion(new CompletionParams { - TextDocument = new TextDocumentIdentifier(syntaxTree.FileUri), - Position = TextCoordinateConverter.GetPosition(syntaxTree.LineStarts, 0), + TextDocument = new TextDocumentIdentifier(mainUri), + Position = new Position(0, 0), }); CompletionItem completionItem = completions.Where(x => x.Kind == CompletionItemKind.Snippet && x.Label == "res-aks-cluster").First(); - Command? command = completionItem.Command; - Assert.IsNotNull(command); - Assert.AreEqual(TelemetryConstants.CommandName, command!.Name); + + command!.Name.Should().Be(TelemetryConstants.CommandName); JArray? arguments = command!.Arguments; - Assert.IsNotNull(arguments); + BicepTelemetryEvent? telemetryEvent = arguments!.First().ToObject(); + + telemetryEvent!.EventName.Should().Be(TelemetryConstants.EventNames.TopLevelDeclarationSnippetInsertion); + telemetryEvent!.Properties!.Should().ContainKey("name"); + + await client.ResolveCompletion(completionItem); + await client.Workspace.ExecuteCommand(command); + + TelemetryEventParams telemetryEventParams = await IntegrationTestHelper.WithTimeoutAsync(telemetryReceived.Task); - TelemetryEvent? telemetryEvent = arguments!.First().ToObject(); - Assert.IsNotNull(telemetryEvent); - Assert.AreEqual(TelemetryConstants.EventNames.TopLevelDeclarationSnippetInsertion, telemetryEvent!.EventName); - Assert.IsTrue(telemetryEvent!.Properties?.ContainsKey("name")); + telemetryEventParams.Data.Keys.Count.Should().Be(2); + telemetryEventParams.Data.Keys.Should().Contain("eventName"); + telemetryEventParams.Data.Keys.Should().Contain("properties"); + telemetryEventParams.Data["eventName"].ToString().Should().Be(TelemetryConstants.EventNames.TopLevelDeclarationSnippetInsertion); + telemetryEventParams.Data["properties"].ToString().Should().BeEquivalentToIgnoringNewlines(@"{ + ""name"": ""res-aks-cluster"" +}"); } } }