|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------------------------------------------*/ |
| 4 | + |
| 5 | +package com.github.copilot; |
| 6 | + |
| 7 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 9 | + |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.AfterAll; |
| 14 | +import org.junit.jupiter.api.BeforeAll; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import com.github.copilot.generated.rpc.SessionCommandsListResult; |
| 18 | +import com.github.copilot.generated.rpc.SlashCommandInfo; |
| 19 | +import com.github.copilot.rpc.CopilotClientOptions; |
| 20 | +import com.github.copilot.rpc.PermissionHandler; |
| 21 | +import com.github.copilot.rpc.SessionConfig; |
| 22 | + |
| 23 | +/** |
| 24 | + * Failsafe integration test that exercises slash commands against the live |
| 25 | + * Copilot CLI (not the replay proxy). |
| 26 | + * <p> |
| 27 | + * Requires the CLI to be installed and the user to be signed in. |
| 28 | + */ |
| 29 | +class SlashCommandsIT { |
| 30 | + |
| 31 | + private static CopilotClient client; |
| 32 | + private static CopilotSession session; |
| 33 | + |
| 34 | + @BeforeAll |
| 35 | + static void setup() throws Exception { |
| 36 | + CopilotClientOptions options = new CopilotClientOptions() |
| 37 | + .setUseLoggedInUser(true); |
| 38 | + client = new CopilotClient(options); |
| 39 | + client.start().get(30, TimeUnit.SECONDS); |
| 40 | + session = client.createSession(new SessionConfig() |
| 41 | + .setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get(30, TimeUnit.SECONDS); |
| 42 | + } |
| 43 | + |
| 44 | + @AfterAll |
| 45 | + static void teardown() throws Exception { |
| 46 | + if (session != null) { |
| 47 | + session.close(); |
| 48 | + } |
| 49 | + if (client != null) { |
| 50 | + client.close(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void listCommandsReturnsAtLeast20() throws Exception { |
| 56 | + SessionCommandsListResult result = |
| 57 | + session.getRpc().commands.list().get(15, TimeUnit.SECONDS); |
| 58 | + |
| 59 | + assertNotNull(result, "commands.list result must not be null"); |
| 60 | + assertNotNull(result.commands(), "commands list must not be null"); |
| 61 | + assertTrue(result.commands().size() >= 20, |
| 62 | + "Expected at least 20 commands but got " + result.commands().size()); |
| 63 | + |
| 64 | + Pattern namePattern = Pattern.compile("^[a-z].*$"); |
| 65 | + |
| 66 | + // Print every command so we can pick one for the next iteration |
| 67 | + System.out.println("=== Available slash commands ==="); |
| 68 | + for (SlashCommandInfo cmd : result.commands()) { |
| 69 | + System.out.printf(" /%s kind=%s desc=%s aliases=%s%n", |
| 70 | + cmd.name(), cmd.kind(), cmd.description(), cmd.aliases()); |
| 71 | + assertTrue(namePattern.matcher(cmd.name()).matches(), |
| 72 | + "Command name should match /^[a-z].*$/ but was: " + cmd.name()); |
| 73 | + } |
| 74 | + System.out.println("=== Total: " + result.commands().size() + " commands ==="); |
| 75 | + } |
| 76 | +} |
0 commit comments