Skip to content

Commit

Permalink
autocomplete test update
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Gołębiowski committed Dec 31, 2024
1 parent a1d22a2 commit 082db9c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 11 deletions.
60 changes: 55 additions & 5 deletions src/Cody.VisualStudio.Tests/AutocompleteTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Cody.VisualStudio.Services;
using EnvDTE;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -36,18 +37,67 @@ public async Task Autocomplete_Is_Working()
var sim = new InputSimulator();
sim.Keyboard
.KeyPress(VirtualKeyCode.RETURN)
.TextEntry("if (str")
.Sleep(5000)
.KeyPress(VirtualKeyCode.TAB)
.Sleep(200);
.TextEntry("if (str");

var suggestion = await GetCodyAutocompleteSuggestion();

sim.Keyboard.KeyPress(VirtualKeyCode.TAB).Sleep(200);

var newText = doc.CreateEditPoint(doc.StartPoint).GetText(doc.EndPoint);

Assert.Contains(suggestion, newText);

Dte.ActiveDocument.Close(vsSaveChanges.vsSaveChangesNo);
}

[VsFact(Version = VsVersion.VS2022)]
public async Task Explicit_Invocation_Is_Working()
{
await GetPackageAsync();
await WaitForChat();
await OpenSolution(SolutionsPaths.GetConsoleApp1File("ConsoleApp1.sln"));
await OpenDocument(SolutionsPaths.GetConsoleApp1File(@"ConsoleApp1\Manager.cs"));
await WaitForAsync(() => CodyPackage.TestingSupportService.InProgressBackgroundTasksCount == 0);

var doc = Dte.ActiveDocument.Object() as TextDocument;
var oldText = doc.CreateEditPoint(doc.StartPoint).GetText(doc.EndPoint);
var lineOfCode = " if (repeat < 0) throw new ArgumentException(\"repeat must be greater than 0\");";
var position = FindPositionAfterText(oldText, lineOfCode);
Assert.NotNull(position);

doc.Selection.MoveToLineAndOffset(position.Value.Line + 1, 9);

var sim = new InputSimulator();
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.LMENU, VirtualKeyCode.OEM_COMMA);

var suggestion = await GetCodyAutocompleteSuggestion();
sim.Keyboard.KeyPress(VirtualKeyCode.TAB).Sleep(200);

var newText = doc.CreateEditPoint(doc.StartPoint).GetText(doc.EndPoint);

Assert.Contains(CodyPackage.TestingSupportService.LastDisplayedAutocompleteSuggestion, newText);
Assert.Contains(suggestion, newText);

Dte.ActiveDocument.Close(vsSaveChanges.vsSaveChangesNo);
}

private async Task<string> GetCodyAutocompleteSuggestion()
{
var set = new HashSet<string>();
var sim = new InputSimulator();
TestingSupportService.AutocompleteSuggestion suggestion;
while(true)
{
suggestion = await CodyPackage.TestingSupportService.GetAutocompleteSuggestion();

if (suggestion == null) throw new Exception("No sugesstions");
if (suggestion.IsCodySuggestion) return suggestion.SuggestionText;
if (set.Contains(suggestion.SuggestionId)) throw new Exception("No Cody suggestions");

set.Add(suggestion.SuggestionId);
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.LMENU, VirtualKeyCode.OEM_COMMA);
}
}

private (int Line, int Column)? FindPositionAfterText(string document, string textToFind)
{
var lines = document.Split(new[] { "\r\n" }, StringSplitOptions.None);
Expand Down
1 change: 1 addition & 0 deletions src/Cody.VisualStudio.Tests/Cody.VisualStudio.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SolutionsPaths.cs" />
<Compile Include="TestsBase.cs" />
<Compile Include="VisualStudioSolutionFixture.cs" />
<Compile Include="VsVersion.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
5 changes: 0 additions & 5 deletions src/Cody.VisualStudio/Completions/CodyProposalSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,6 @@ private CodyProposalCollection CreateProposals(AutocompleteResult autocomplete,

}

if(CodyPackage.TestingSupportService != null && proposalList.Any())
{
CodyPackage.TestingSupportService.LastDisplayedAutocompleteSuggestion = proposalList.First().Edits.First().ReplacementText;
}

var collection = new CodyProposalCollection(proposalList);
return collection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ private void OnProposalDisplayed(object sender, ProposalDisplayedEventArgs e)
trace.TraceEvent("ProposalDisplayed", completionId);
CodyPackage.AgentService.CompletionSuggested(completionItem);
}

if(CodyPackage.TestingSupportService != null)
{
CodyPackage.TestingSupportService.SetAutocompleteSuggestion(
e.OriginalProposal.ProposalId,
e.OriginalProposal.ProposalId.StartsWith(ProposalIdPrefix),
e.OriginalProposal.Edits.First().ReplacementText);
}
}

private void OnSuggestionAccepted(object sender, SuggestionAcceptedEventArgs e)
Expand Down
31 changes: 30 additions & 1 deletion src/Cody.VisualStudio/Services/TestingSupportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Cody.VisualStudio.Services
Expand All @@ -14,6 +15,8 @@ public class TestingSupportService
private PropertyInfo inProgressCountProp;
private IVsTaskStatusCenterService statusCenterService;

private SemaphoreSlim suggestionSemaphore = new SemaphoreSlim(0, 1);
private AutocompleteSuggestion autocompleteSuggestion;

public TestingSupportService(IVsTaskStatusCenterService statusCenterService)
{
Expand All @@ -23,7 +26,33 @@ public TestingSupportService(IVsTaskStatusCenterService statusCenterService)

public int InProgressBackgroundTasksCount => (int)inProgressCountProp.GetValue(statusCenterService);

public string LastDisplayedAutocompleteSuggestion { get; set; }
public void SetAutocompleteSuggestion(string suggestionId, bool isCodySuggestion, string suggestionText)
{
autocompleteSuggestion = new AutocompleteSuggestion(suggestionId, isCodySuggestion, suggestionText);
suggestionSemaphore.Release();
}

public async Task<AutocompleteSuggestion> GetAutocompleteSuggestion(int milisecondsTimeout = 8000)
{
if(await suggestionSemaphore.WaitAsync(milisecondsTimeout))
return autocompleteSuggestion;
else
return null;
}


public class AutocompleteSuggestion
{
public AutocompleteSuggestion(string suggestionId, bool isCodySuggestion, string suggestionText)
{
SuggestionId = suggestionId;
IsCodySuggestion = isCodySuggestion;
SuggestionText = suggestionText;
}

public string SuggestionId { get; }
public bool IsCodySuggestion { get; }
public string SuggestionText { get; }
}
}
}

0 comments on commit 082db9c

Please sign in to comment.