-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Canvas SDK E2E tests across all 5 languages #1422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jmoseley
wants to merge
3
commits into
main
Choose a base branch
from
jmoseley/canvas-e2e-tests-all-sdks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| using GitHub.Copilot.Rpc; | ||
| using GitHub.Copilot.Test.Harness; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace GitHub.Copilot.Test.E2E; | ||
|
|
||
| public class CanvasE2ETests(E2ETestFixture fixture, ITestOutputHelper output) : E2ETestBase(fixture, "canvas", output) | ||
| { | ||
| [Fact] | ||
| public async Task DispatchesCanvasOpenToProviderHandler() | ||
| { | ||
| var opens = new List<CanvasOpenContext>(); | ||
| await using var session = await CreateSessionAsync(CreateCanvasSessionConfig(new RecordingCanvasHandler(opens: opens))); | ||
|
|
||
| var result = await session.Rpc.Canvas.OpenAsync( | ||
| canvasId: "counter", | ||
| instanceId: "counter-1", | ||
| input: new Dictionary<string, object> { ["seed"] = 7 }); | ||
|
|
||
| var open = Assert.Single(opens); | ||
| Assert.Equal("counter", open.CanvasId); | ||
| Assert.Equal("counter-1", open.InstanceId); | ||
| Assert.Equal(7, open.Input.GetProperty("seed").GetInt32()); | ||
| Assert.Equal("counter", result.CanvasId); | ||
| Assert.Equal("counter-1", result.InstanceId); | ||
| Assert.Equal("https://example.test/counter-1", result.Url); | ||
| Assert.Equal(CanvasInstanceAvailability.Ready, result.Availability); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DispatchesCanvasActionInvokeToHandler() | ||
| { | ||
| var actions = new List<CanvasActionContext>(); | ||
| await using var session = await CreateSessionAsync(CreateCanvasSessionConfig(new RecordingCanvasHandler(actions: actions))); | ||
|
|
||
| await session.Rpc.Canvas.OpenAsync(canvasId: "counter", instanceId: "counter-2"); | ||
| var result = await session.Rpc.Canvas.InvokeActionAsync( | ||
| instanceId: "counter-2", | ||
| actionName: "increment", | ||
| input: new Dictionary<string, object> { ["amount"] = 3 }); | ||
|
|
||
| var action = Assert.Single(actions); | ||
| Assert.Equal("counter", action.CanvasId); | ||
| Assert.Equal("counter-2", action.InstanceId); | ||
| Assert.Equal("increment", action.ActionName); | ||
| Assert.Equal(3, action.Input.GetProperty("amount").GetInt32()); | ||
|
|
||
| var actionResult = result.Result; | ||
| Assert.NotNull(actionResult); | ||
| var payload = actionResult!.Value; | ||
| Assert.True(payload.GetProperty("ok").GetBoolean()); | ||
| Assert.Equal("increment", payload.GetProperty("actionName").GetString()); | ||
| Assert.Equal(3, payload.GetProperty("input").GetProperty("amount").GetInt32()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DispatchesCanvasCloseToOnCloseHandler() | ||
| { | ||
| var closes = new List<CanvasLifecycleContext>(); | ||
| await using var session = await CreateSessionAsync(CreateCanvasSessionConfig(new RecordingCanvasHandler(closes: closes))); | ||
|
|
||
| await session.Rpc.Canvas.OpenAsync(canvasId: "counter", instanceId: "counter-3"); | ||
| await session.Rpc.Canvas.CloseAsync(instanceId: "counter-3"); | ||
| await Task.Delay(50); | ||
|
|
||
| var close = Assert.Single(closes); | ||
| Assert.Equal("counter", close.CanvasId); | ||
| Assert.Equal("counter-3", close.InstanceId); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReturnsCanvasActionNoHandlerForDeclaredActionWithoutHandler() | ||
| { | ||
| await using var session = await CreateSessionAsync(CreateCanvasSessionConfig(new OpenOnlyCanvasHandler())); | ||
|
|
||
| await session.Rpc.Canvas.OpenAsync(canvasId: "counter", instanceId: "counter-4"); | ||
| var ex = await Assert.ThrowsAsync<IOException>(() => session.Rpc.Canvas.InvokeActionAsync( | ||
| instanceId: "counter-4", | ||
| actionName: "increment", | ||
| input: new Dictionary<string, object>())); | ||
|
|
||
| Assert.Contains("canvas_action_no_handler", ex.Message, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task SeedsOpenCanvasesOnResumeFromRuntime() | ||
| { | ||
| await using var sessionA = await CreateSessionAsync(CreateCanvasSessionConfig(new OpenOnlyCanvasHandler())); | ||
|
|
||
| await sessionA.Rpc.Canvas.OpenAsync( | ||
| canvasId: "counter", | ||
| instanceId: "counter-resume", | ||
| input: new Dictionary<string, object> { ["initial"] = true }); | ||
|
|
||
| await using var resumed = await ResumeSessionAsync(sessionA.SessionId, CreateCanvasResumeConfig(new OpenOnlyCanvasHandler())); | ||
|
|
||
| Assert.NotEmpty(resumed.OpenCanvases); | ||
| var match = Assert.Single(resumed.OpenCanvases, canvas => canvas.InstanceId == "counter-resume"); | ||
| Assert.Equal("counter", match.CanvasId); | ||
| } | ||
|
|
||
| private static SessionConfig CreateCanvasSessionConfig(ICanvasHandler handler) => new() | ||
| { | ||
| Canvases = [CreateCounterCanvas()], | ||
| CanvasHandler = handler, | ||
| RequestCanvasRenderer = true, | ||
| ExtensionInfo = new ExtensionInfo { Source = "github-app", Name = "counter-provider" }, | ||
| OnPermissionRequest = PermissionHandler.ApproveAll, | ||
| }; | ||
|
|
||
| private static ResumeSessionConfig CreateCanvasResumeConfig(ICanvasHandler handler) => new() | ||
| { | ||
| Canvases = [CreateCounterCanvas()], | ||
| CanvasHandler = handler, | ||
| RequestCanvasRenderer = true, | ||
| ExtensionInfo = new ExtensionInfo { Source = "github-app", Name = "counter-provider" }, | ||
| OnPermissionRequest = PermissionHandler.ApproveAll, | ||
| }; | ||
|
|
||
| private static CanvasDeclaration CreateCounterCanvas() => new() | ||
| { | ||
| Id = "counter", | ||
| DisplayName = "Counter", | ||
| Description = "A test counter canvas", | ||
| Actions = | ||
| [ | ||
| new CanvasAction | ||
| { | ||
| Name = "increment", | ||
| Description = "Increment the counter", | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| private class OpenOnlyCanvasHandler : CanvasHandlerBase | ||
| { | ||
| public override Task<CanvasOpenResponse> OnOpenAsync(CanvasOpenContext context, CancellationToken cancellationToken) | ||
| => Task.FromResult(new CanvasOpenResponse { Url = $"https://example.test/{context.InstanceId}" }); | ||
| } | ||
|
|
||
| private sealed class RecordingCanvasHandler( | ||
| List<CanvasOpenContext>? opens = null, | ||
| List<CanvasLifecycleContext>? closes = null, | ||
| List<CanvasActionContext>? actions = null) : OpenOnlyCanvasHandler | ||
| { | ||
| public override Task<CanvasOpenResponse> OnOpenAsync(CanvasOpenContext context, CancellationToken cancellationToken) | ||
| { | ||
| opens?.Add(CloneOpenContext(context)); | ||
| return base.OnOpenAsync(context, cancellationToken); | ||
| } | ||
|
|
||
| public override Task OnCloseAsync(CanvasLifecycleContext context, CancellationToken cancellationToken) | ||
| { | ||
| closes?.Add(context); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| public override Task<object?> OnActionAsync(CanvasActionContext context, CancellationToken cancellationToken) | ||
| { | ||
| actions?.Add(CloneActionContext(context)); | ||
| return Task.FromResult<object?>(new Dictionary<string, object?> | ||
| { | ||
| ["ok"] = true, | ||
| ["actionName"] = context.ActionName, | ||
| ["input"] = context.Input.Clone(), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| private static CanvasOpenContext CloneOpenContext(CanvasOpenContext context) => new() | ||
| { | ||
| SessionId = context.SessionId, | ||
| ExtensionId = context.ExtensionId, | ||
| CanvasId = context.CanvasId, | ||
| InstanceId = context.InstanceId, | ||
| Input = context.Input.Clone(), | ||
| Host = context.Host, | ||
| }; | ||
|
|
||
| private static CanvasActionContext CloneActionContext(CanvasActionContext context) => new() | ||
| { | ||
| SessionId = context.SessionId, | ||
| ExtensionId = context.ExtensionId, | ||
| CanvasId = context.CanvasId, | ||
| InstanceId = context.InstanceId, | ||
| ActionName = context.ActionName, | ||
| Input = context.Input.Clone(), | ||
| Host = context.Host, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.