Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 186 additions & 0 deletions dotnet/test/E2E/CommandsE2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/

using GitHub.Copilot.Rpc;
using GitHub.Copilot.Test.Harness;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -10,6 +12,174 @@ namespace GitHub.Copilot.Test.E2E;
public class CommandsE2ETests(E2ETestFixture fixture, ITestOutputHelper output)
: E2ETestBase(fixture, "commands", output)
{
private static readonly string[] KnownBuiltinCommands = ["help", "model", "compact"];

[Fact]
public async Task Session_Commands_List_Returns_Builtins_And_Respects_Client_Command_Filter()
{
var session = await CreateSessionAsync(new SessionConfig
{
Commands =
[
new CommandDefinition { Name = "deploy", Description = "Deploy the app", Handler = _ => Task.CompletedTask },
new CommandDefinition { Name = "rollback", Description = "Rollback the app", Handler = _ => Task.CompletedTask },
],
});

CommandList? clientCommands = null;
await TestHelper.WaitForConditionAsync(
async () =>
{
clientCommands = await session.Rpc.Commands.ListAsync(new CommandsListRequest
{
IncludeBuiltins = false,
IncludeClientCommands = true,
IncludeSkills = false,
});
return clientCommands.Commands.Any(c => IsCommand(c, "deploy", SlashCommandKind.Client)) &&
clientCommands.Commands.Any(c => IsCommand(c, "rollback", SlashCommandKind.Client));
},
timeout: TimeSpan.FromSeconds(30),
timeoutMessage: "Timed out waiting for client commands to be listed.");
Assert.Contains(clientCommands!.Commands, c => IsCommand(c, "deploy", SlashCommandKind.Client));
Assert.Contains(clientCommands.Commands, c => IsCommand(c, "rollback", SlashCommandKind.Client));
Assert.DoesNotContain(clientCommands.Commands, c => c.Kind == SlashCommandKind.Builtin);

var builtinCommands = await session.Rpc.Commands.ListAsync(new CommandsListRequest
{
IncludeBuiltins = true,
IncludeClientCommands = false,
IncludeSkills = false,
});
Assert.True(
builtinCommands.Commands.Any(IsKnownBuiltin),
$"Expected a known built-in command. Actual commands: {FormatCommands(builtinCommands.Commands)}");
Assert.DoesNotContain(builtinCommands.Commands, c => string.Equals(c.Name, "deploy", StringComparison.OrdinalIgnoreCase));

await session.DisposeAsync();
}

[Fact]
public async Task Session_Commands_Invoke_Known_Builtin_Returns_Expected_Result()
{
var session = await CreateSessionAsync();

var builtinCommands = await session.Rpc.Commands.ListAsync(new CommandsListRequest
{
IncludeBuiltins = true,
IncludeClientCommands = false,
IncludeSkills = false,
});
var commandName = KnownBuiltinCommands.FirstOrDefault(name =>
builtinCommands.Commands.Any(c => IsCommand(c, name, SlashCommandKind.Builtin)));
Assert.NotNull(commandName);

var result = await session.Rpc.Commands.InvokeAsync(commandName);

switch (result)
{
case SlashCommandInvocationResultText text:
Assert.False(string.IsNullOrWhiteSpace(text.Text));
break;

case SlashCommandInvocationResultSelectSubcommand select:
Assert.False(string.IsNullOrWhiteSpace(select.Title));
Assert.NotEmpty(select.Options);
break;

case SlashCommandInvocationResultAgentPrompt prompt:
Assert.False(string.IsNullOrWhiteSpace(prompt.DisplayPrompt));
Assert.False(string.IsNullOrWhiteSpace(prompt.Prompt));
break;

case SlashCommandInvocationResultCompleted completed:
Assert.True(completed.Message is null || !string.IsNullOrWhiteSpace(completed.Message));
break;

default:
Assert.Fail($"Unexpected invocation result: {result.GetType().Name}");
break;
}

await session.DisposeAsync();
}

[Fact]
public async Task Session_Commands_Execute_Runs_Registered_Command_Handler()
{
CommandContext? capturedContext = null;
var session = await CreateSessionAsync(new SessionConfig
{
Commands =
[
new CommandDefinition
{
Name = "deploy",
Description = "Deploy the app",
Handler = ctx =>
{
capturedContext = ctx;
return Task.CompletedTask;
},
},
],
});

await TestHelper.WaitForConditionAsync(
async () =>
{
var commands = await session.Rpc.Commands.ListAsync(new CommandsListRequest
{
IncludeBuiltins = false,
IncludeClientCommands = true,
IncludeSkills = false,
});
return commands.Commands.Any(c => IsCommand(c, "deploy", SlashCommandKind.Client));
},
timeout: TimeSpan.FromSeconds(30),
timeoutMessage: "Timed out waiting for registered command to be listed.");

var result = await session.Rpc.Commands.ExecuteAsync("deploy", "production");

Assert.Null(result.Error);
await TestHelper.WaitForConditionAsync(
() => capturedContext is not null,
timeout: TimeSpan.FromSeconds(10),
timeoutMessage: "Timed out waiting for command handler execution.");
Assert.Equal(session.SessionId, capturedContext!.SessionId);
Assert.Equal("/deploy production", capturedContext.Command);
Assert.Equal("deploy", capturedContext.CommandName);
Assert.Equal("production", capturedContext.Args);

await session.DisposeAsync();
}

[Fact]
public async Task Session_Commands_Enqueue_Accepts_Deterministic_Command()
{
var session = await CreateSessionAsync();

var result = await session.Rpc.Commands.EnqueueAsync("/help");

Assert.True(result.Queued);

await session.DisposeAsync();
}

[Fact]
public async Task Session_Commands_RespondToQueuedCommand_Returns_False_For_Unknown_RequestId()
{
var session = await CreateSessionAsync();

var result = await session.Rpc.Commands.RespondToQueuedCommandAsync(
"missing-queued-command-request",
new QueuedCommandResult { Handled = false });

Assert.False(result.Success);

await session.DisposeAsync();
}

[Fact]
public async Task Session_With_Commands_Creates_Successfully()
{
Expand Down Expand Up @@ -134,4 +304,20 @@ public void Resume_Config_Commands_Are_Cloned()
Assert.Single(clone.Commands!);
Assert.Equal("deploy", clone.Commands![0].Name);
}

private static bool IsCommand(SlashCommandInfo command, string name, SlashCommandKind kind)
{
return string.Equals(command.Name, name, StringComparison.OrdinalIgnoreCase) && command.Kind == kind;
}

private static bool IsKnownBuiltin(SlashCommandInfo command)
{
return command.Kind == SlashCommandKind.Builtin &&
KnownBuiltinCommands.Contains(command.Name, StringComparer.OrdinalIgnoreCase);
}

private static string FormatCommands(IEnumerable<SlashCommandInfo> commands)
{
return string.Join(", ", commands.Select(c => $"{c.Name}:{c.Kind.Value}"));
}
}
39 changes: 39 additions & 0 deletions dotnet/test/E2E/CompactionE2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,43 @@ public async Task Should_Not_Emit_Compaction_Events_When_Infinite_Sessions_Disab
// Should not have any compaction events when disabled
Assert.Empty(compactionEvents);
}

[Fact]
public async Task Should_Return_Empty_Handoff_Summary_For_Fresh_Session()
{
await using var session = await CreateSessionAsync();

var result = await session.Rpc.History.SummarizeForHandoffAsync();

Assert.NotNull(result);
Assert.NotNull(result.Summary);
Assert.Equal(string.Empty, result.Summary);
}

[Fact]
public async Task Should_Summarize_For_Handoff_After_NonEphemeral_Log_Event()
{
await using var session = await CreateSessionAsync();

await session.LogAsync("handoff summary log coverage");

var result = await session.Rpc.History.SummarizeForHandoffAsync();

Assert.NotNull(result);
Assert.NotNull(result.Summary);
}

[Fact]
public async Task Should_Report_No_Op_When_Cancelling_Compaction_Without_In_Flight_Work()
{
await using var session = await CreateSessionAsync();

var backgroundResult = await session.Rpc.History.CancelBackgroundCompactionAsync();
var manualResult = await session.Rpc.History.AbortManualCompactionAsync();

Assert.NotNull(backgroundResult);
Assert.False(backgroundResult.Cancelled);
Assert.NotNull(manualResult);
Assert.False(manualResult.Aborted);
}
}
Loading
Loading