Skip to content

Commit

Permalink
Updated integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
bhsubra committed May 26, 2021
1 parent 18f351b commit e4dd5cb
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions src/Bicep.LangServer.IntegrationTests/TelemetryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<Uri, string>();
var telemetryReceived = new TaskCompletionSource<TelemetryEventParams>();

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<BicepTelemetryEvent>();

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<TelemetryEvent>();
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""
}");
}
}
}

0 comments on commit e4dd5cb

Please sign in to comment.