Open History to see the last commit
1import { test, expect } from '../fixtures'
2import {
3 selectScenario,
4 runTest,
5 waitForTestComplete,
6 waitForExecutionComplete,
7 waitForApproval,
8 getMetadata,
9 getEventLog,
10} from './helpers'
11
12/**
13 * Approval Flow E2E Tests
14 *
15 * These tests verify that the approval flow works correctly,
16 * including:
17 * - Approval requests appearing in the UI
18 * - Approve/Deny buttons working correctly
19 * - Flow continuing after approval
20 * - Sequential approvals not blocking each other
21 * - Parallel approvals being handled correctly
22 */
23
24test.describe('Approval Flow E2E Tests', () => {
25 test('single approval flow - approve', async ({
26 page,
27 testId,
28 aimockPort,
29 }) => {
30 await selectScenario(page, 'approval-tool', testId, aimockPort)
31 await runTest(page)
32
33 // Wait for approval request to appear
34 await waitForApproval(page)
35
36 // Verify approval section is visible
37 await expect(page.locator('#approval-section')).toBeVisible()
38 await expect(page.locator('.approve-button').first()).toBeVisible()
39
40 // Click approve
41 await page.click('.approve-button')
42
43 // Wait for the test to complete
44 await waitForTestComplete(page)
45
46 // Verify results
47 const metadata = await getMetadata(page)
48 expect(metadata.testComplete).toBe('true')
49 expect(parseInt(metadata.approvalGrantedCount)).toBe(1)
50 expect(parseInt(metadata.approvalDeniedCount)).toBe(0)
51 })
52
53 test('single approval flow - deny', async ({ page, testId, aimockPort }) => {
54 await selectScenario(page, 'approval-tool', testId, aimockPort)
55 await runTest(page)
56
57 // Wait for approval request to appear
58 await waitForApproval(page)
59
60 // Click deny
61 await page.click('.deny-button')
62
63 // Wait a bit for the flow to process the denial
64 await page.waitForTimeout(500)
65
66 // Verify denial was recorded
67 const metadata = await getMetadata(page)
68 expect(parseInt(metadata.approvalDeniedCount)).toBe(1)
69 expect(parseInt(metadata.approvalGrantedCount)).toBe(0)
70 })
71
72 test('sequential approvals - both approved', async ({
73 page,
74 testId,
75 aimockPort,
76 }) => {
77 // This tests the specific issue: two approvals in sequence
78 // The second approval shouldn't be blocked by the first
79 await selectScenario(page, 'sequential-approvals', testId, aimockPort)
80 await runTest(page)
81
82 // Wait for first approval
83 await waitForApproval(page)
84
85 // Approve the first one
86 await page.click('.approve-button')
87
88 // Wait for second approval to appear
89 await page.waitForFunction(
90 () => {
91 const section = document.getElementById('approval-section')
92 const buttons = document.querySelectorAll('.approve-button')
93 // Either new approval appeared or section is visible
94 return section !== null && buttons.length > 0
95 },
96 { timeout: 10000 },
97 )
98
99 // Approve the second one if present
100 const approveButton = page.locator('.approve-button').first()
101 if (await approveButton.isVisible()) {
102 await approveButton.click()
103 }
104
105 // Wait for the test to complete
106 await waitForTestComplete(page)
107
108 // Verify results
109 const metadata = await getMetadata(page)
110 expect(metadata.testComplete).toBe('true')
111 expect(parseInt(metadata.approvalGrantedCount)).toBe(2)
112 })
113
114 test('parallel approvals - both approved', async ({
115 page,
116 testId,
117 aimockPort,
118 }) => {
119 // Two approvals requested at the same time
120 await selectScenario(page, 'parallel-approvals', testId, aimockPort)
121 await runTest(page)
122
123 // Wait for approvals to appear
124 await waitForApproval(page)
125
126 // There should be 2 approve buttons initially
127 const approveButtons = page.locator('.approve-button')
128 const count = await approveButtons.count()
129 expect(count).toBe(2)
130
131 // Get the IDs of the approve buttons before clicking (they have id="approve-{toolCallId}")
132 const button1Id = await approveButtons.nth(0).getAttribute('id')
133 const button2Id = await approveButtons.nth(1).getAttribute('id')
134
135 // Approve both using their specific IDs (since clicking one removes it from the list)
136 await page.click(`#${button1Id}`)
137 // Wait a moment for React to re-render
138 await page.waitForTimeout(100)
139 // The second button should still exist (may be at position 0 now)
140 await page.click(`#${button2Id}`)
141
142 // Wait for the test to complete
143 await waitForTestComplete(page)
144
145 // Verify results
146 const metadata = await getMetadata(page)
147 expect(metadata.testComplete).toBe('true')
148 expect(parseInt(metadata.approvalGrantedCount)).toBe(2)
149 })
150
151 test('client tool then approval', async ({ page, testId, aimockPort }) => {
152 // Tests that a client tool doesn't block subsequent approval
153 await selectScenario(page, 'client-then-approval', testId, aimockPort)
154 await runTest(page)
155
156 // Wait for approval request (after client tool completes)
157 await waitForApproval(page)
158
159 // Approve
160 await page.click('.approve-button')
161
162 // Wait for the test to complete
163 await waitForTestComplete(page)
164 await waitForExecutionComplete(page, 'show_notification')
165
166 // Verify results
167 const metadata = await getMetadata(page)
168 expect(metadata.testComplete).toBe('true')
169
170 // Check that client tool executed (via event log)
171 const events = await getEventLog(page)
172 const clientExecution = events.find(
173 (e) =>
174 e.toolName === 'show_notification' && e.type === 'execution-complete',
175 )
176 expect(clientExecution).toBeTruthy()
177 })
178
179 test('approval then client tool', async ({ page, testId, aimockPort }) => {
180 // Tests that approval doesn't block subsequent client tool
181 await selectScenario(page, 'approval-then-client', testId, aimockPort)
182 await runTest(page)
183
184 // Wait for approval request
185 await waitForApproval(page)
186
187 // Approve
188 await page.click('.approve-button')
189
190 // Wait for the test to complete
191 await waitForTestComplete(page)
192 await waitForExecutionComplete(page, 'show_notification')
193
194 // Verify results
195 const metadata = await getMetadata(page)
196 expect(metadata.testComplete).toBe('true')
197
198 // Check that both approval and client tool executed
199 const events = await getEventLog(page)
200 const approvalEvent = events.find((e) => e.type === 'approval-granted')
201 const clientExecution = events.find(
202 (e) =>
203 e.toolName === 'show_notification' && e.type === 'execution-complete',
204 )
205
206 expect(approvalEvent).toBeTruthy()
207 expect(clientExecution).toBeTruthy()
208 })
209
210 // Screenshot on failure
211 test.afterEach(async ({ page }, testInfo) => {
212 if (testInfo.status !== testInfo.expectedStatus) {
213 await page.screenshot({
214 path: `test-results/approval-failure-${testInfo.title.replace(/\s+/g, '-')}.png`,
215 fullPage: true,
216 })
217
218 const events = await getEventLog(page)
219 const metadata = await getMetadata(page)
220
221 console.log('Test failed. Debug info:')
222 console.log('Metadata:', metadata)
223 console.log('Events:', events)
224 }
225 })
226})