-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathE2ETestBase.cs
More file actions
72 lines (60 loc) · 2.75 KB
/
Copy pathE2ETestBase.cs
File metadata and controls
72 lines (60 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using System.Data;
using System.Reflection;
using GitHub.Copilot.SDK.Test.Harness;
using Xunit;
using Xunit.Abstractions;
namespace GitHub.Copilot.SDK.Test;
public abstract class E2ETestBase : IClassFixture<E2ETestFixture>, IAsyncLifetime
{
private readonly E2ETestFixture _fixture;
private readonly string _snapshotCategory;
private readonly string _testName;
protected E2ETestContext Ctx => _fixture.Ctx;
protected CopilotClient Client => _fixture.Client;
protected E2ETestBase(E2ETestFixture fixture, string snapshotCategory, ITestOutputHelper output)
{
_fixture = fixture;
_snapshotCategory = snapshotCategory;
_testName = GetTestName(output);
}
private static string GetTestName(ITestOutputHelper output)
{
// xUnit doesn't provide a public API to get the current test name.
var type = output.GetType();
var testField = type.GetField("test", BindingFlags.Instance | BindingFlags.NonPublic);
var test = (ITest?)testField?.GetValue(output);
return test?.TestCase.TestMethod.Method.Name ?? throw new InvalidOperationException("Couldn't find test name");
}
public async Task InitializeAsync()
{
await Ctx.ConfigureForTestAsync(_snapshotCategory, _testName);
}
public Task DisposeAsync() => Task.CompletedTask;
/// <summary>
/// Creates a session with a default config that approves all permissions.
/// Convenience wrapper for E2E tests.
/// </summary>
protected Task<CopilotSession> CreateSessionAsync(SessionConfig? config = null)
{
config ??= new SessionConfig();
config.OnPermissionRequest ??= PermissionHandler.ApproveAll;
return Client.CreateSessionAsync(config);
}
/// <summary>
/// Resumes a session with a default config that approves all permissions.
/// Convenience wrapper for E2E tests.
/// </summary>
protected Task<CopilotSession> ResumeSessionAsync(string sessionId, ResumeSessionConfig? config = null)
{
config ??= new ResumeSessionConfig();
config.OnPermissionRequest ??= PermissionHandler.ApproveAll;
return Client.ResumeSessionAsync(sessionId, config);
}
protected static string GetSystemMessage(ParsedHttpExchange exchange) =>
exchange.Request.Messages.FirstOrDefault(m => m.Role == "system")?.Content ?? string.Empty;
protected static List<string> GetToolNames(ParsedHttpExchange exchange) =>
exchange.Request.Tools?.Select(t => t.Function.Name).ToList() ?? new();
}