|
| 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.*; |
| 8 | + |
| 9 | +import java.util.concurrent.CompletableFuture; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.AfterAll; |
| 13 | +import org.junit.jupiter.api.BeforeAll; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import com.github.copilot.generated.SessionTodosChangedEvent; |
| 17 | +import com.github.copilot.generated.rpc.PlanSqlTodoDependency; |
| 18 | +import com.github.copilot.rpc.MessageOptions; |
| 19 | +import com.github.copilot.rpc.PermissionHandler; |
| 20 | +import com.github.copilot.rpc.SessionConfig; |
| 21 | + |
| 22 | +public class SessionTodosChangedTest { |
| 23 | + |
| 24 | + private static E2ETestContext ctx; |
| 25 | + |
| 26 | + @BeforeAll |
| 27 | + static void setup() throws Exception { |
| 28 | + ctx = E2ETestContext.create(); |
| 29 | + } |
| 30 | + |
| 31 | + @AfterAll |
| 32 | + static void teardown() throws Exception { |
| 33 | + if (ctx != null) { |
| 34 | + ctx.close(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void firesSessionTodosChangedAndExposesRowsAndDependencies() throws Exception { |
| 40 | + ctx.configureForTest("session_todos_changed", "fires_session_todos_changed_and_exposes_rows_and_dependencies"); |
| 41 | + |
| 42 | + try (CopilotClient client = ctx.createClient()) { |
| 43 | + CopilotSession session = client |
| 44 | + .createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get(); |
| 45 | + |
| 46 | + CompletableFuture<SessionTodosChangedEvent> todosChanged = new CompletableFuture<>(); |
| 47 | + session.on(event -> { |
| 48 | + if (event instanceof SessionTodosChangedEvent todosEvent && !todosChanged.isDone()) { |
| 49 | + todosChanged.complete(todosEvent); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + session.sendAndWait(new MessageOptions() |
| 54 | + .setPrompt("Use the sql tool to execute exactly these statements, in order, with no extra rows:\n" |
| 55 | + + "1. INSERT INTO todos (id, title, status) VALUES ('alpha', 'First todo', 'pending');\n" |
| 56 | + + "2. INSERT INTO todos (id, title, status) VALUES ('beta', 'Second todo', 'done');\n" |
| 57 | + + "3. INSERT INTO todo_deps (todo_id, depends_on) VALUES ('beta', 'alpha');\n" |
| 58 | + + "Then stop. Do not insert any other rows or create any other tables.")) |
| 59 | + .get(120, TimeUnit.SECONDS); |
| 60 | + |
| 61 | + assertNotNull(todosChanged.get(15, TimeUnit.SECONDS), |
| 62 | + "Should have received at least one session.todos_changed event"); |
| 63 | + |
| 64 | + var result = session.getRpc().plan.readSqlTodosWithDependencies().get(15, TimeUnit.SECONDS); |
| 65 | + assertEquals(2, result.rows().size()); |
| 66 | + var ids = result.rows().stream().map(row -> row.id()).filter(id -> id != null).sorted().toList(); |
| 67 | + |
| 68 | + assertEquals(java.util.List.of("alpha", "beta"), ids); |
| 69 | + assertTrue(result.dependencies().stream().anyMatch(SessionTodosChangedTest::isBetaDependsOnAlpha), |
| 70 | + "Should contain beta -> alpha dependency"); |
| 71 | + |
| 72 | + session.close(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static boolean isBetaDependsOnAlpha(PlanSqlTodoDependency dependency) { |
| 77 | + return "beta".equals(dependency.todoId()) && "alpha".equals(dependency.dependsOn()); |
| 78 | + } |
| 79 | +} |
0 commit comments