|
| 1 | +import { test, expect } from '../fixtures' |
| 2 | +import { |
| 3 | + selectScenario, |
| 4 | + runTest, |
| 5 | + waitForTestComplete, |
| 6 | + getMetadata, |
| 7 | + getEventLog, |
| 8 | + getToolCalls, |
| 9 | +} from './helpers' |
| 10 | + |
| 11 | +/** |
| 12 | + * Drain Re-Entrancy Guard E2E Tests |
| 13 | + * |
| 14 | + * Regression test for GitHub issue #302 / PR #429. |
| 15 | + * |
| 16 | + * When multiple client tools complete in the same round, each addToolResult() |
| 17 | + * queues a checkForContinuation action. Without a re-entrancy guard on |
| 18 | + * drainPostStreamActions(), the first action's streamResponse() → finally → |
| 19 | + * drainPostStreamActions() (nested) steals the remaining actions from the |
| 20 | + * queue, permanently stalling the conversation. The user sees tool results |
| 21 | + * but the model never produces its follow-up text response. |
| 22 | + * |
| 23 | + * The fix adds a re-entrancy guard to drainPostStreamActions() and a |
| 24 | + * shouldAutoSend() check requiring tool-call parts before triggering |
| 25 | + * continuation. |
| 26 | + * |
| 27 | + * This test uses the parallel-client-tools scenario (2 client tools in the |
| 28 | + * same turn) and verifies not just that both tools execute, but critically |
| 29 | + * that the **continuation fires and the follow-up text response arrives**. |
| 30 | + * Without the fix, the test would time out waiting for the follow-up text. |
| 31 | + */ |
| 32 | + |
| 33 | +test.describe('Drain Re-Entrancy Guard (Regression #302)', () => { |
| 34 | + test('parallel client tools complete and continuation fires with follow-up text', async ({ |
| 35 | + page, |
| 36 | + testId, |
| 37 | + aimockPort, |
| 38 | + }) => { |
| 39 | + await selectScenario(page, 'parallel-client-tools', testId, aimockPort) |
| 40 | + await runTest(page) |
| 41 | + |
| 42 | + // Wait for the test to fully complete — this includes the continuation |
| 43 | + // round producing the follow-up text. Without the fix, this would |
| 44 | + // time out because the continuation never fires. |
| 45 | + await waitForTestComplete(page, 20000, 2) |
| 46 | + |
| 47 | + // Verify both client tools executed |
| 48 | + const metadata = await getMetadata(page) |
| 49 | + expect(metadata.testComplete).toBe('true') |
| 50 | + expect(metadata.isLoading).toBe('false') |
| 51 | + |
| 52 | + const events = await getEventLog(page) |
| 53 | + const toolNames = [...new Set(events.map((e) => e.toolName))] |
| 54 | + expect(toolNames).toContain('show_notification') |
| 55 | + expect(toolNames).toContain('display_chart') |
| 56 | + |
| 57 | + // Verify both tools reached execution-complete state |
| 58 | + const completionEvents = events.filter( |
| 59 | + (e) => e.type === 'execution-complete', |
| 60 | + ) |
| 61 | + expect(completionEvents.length).toBe(2) |
| 62 | + |
| 63 | + // CRITICAL ASSERTION: Verify the follow-up text from round 2 was received. |
| 64 | + // Without the re-entrancy fix, the conversation stalls after both tools |
| 65 | + // complete — the continuation request is never sent, so this text never |
| 66 | + // arrives. |
| 67 | + const messages = await page.evaluate(() => { |
| 68 | + const el = document.getElementById('messages-json-content') |
| 69 | + if (!el) return [] |
| 70 | + try { |
| 71 | + return JSON.parse(el.textContent || '[]') |
| 72 | + } catch { |
| 73 | + return [] |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + const assistantMessages = messages.filter( |
| 78 | + (m: any) => m.role === 'assistant', |
| 79 | + ) |
| 80 | + |
| 81 | + // There should be at least 2 assistant messages: |
| 82 | + // 1. The tool-call round (with both tool calls + results) |
| 83 | + // 2. The continuation round (with the follow-up text) |
| 84 | + expect(assistantMessages.length).toBeGreaterThanOrEqual(2) |
| 85 | + |
| 86 | + // The follow-up text from the continuation round should be present |
| 87 | + const allTextParts = assistantMessages.flatMap((m: any) => |
| 88 | + m.parts.filter((p: any) => p.type === 'text').map((p: any) => p.content), |
| 89 | + ) |
| 90 | + const allText = allTextParts.join(' ') |
| 91 | + expect(allText).toContain('All displayed') |
| 92 | + }) |
| 93 | + |
| 94 | + test.afterEach(async ({ page }, testInfo) => { |
| 95 | + if (testInfo.status !== testInfo.expectedStatus) { |
| 96 | + await page.screenshot({ |
| 97 | + path: `test-results/drain-reentrance-failure-${testInfo.title.replace(/\s+/g, '-')}.png`, |
| 98 | + fullPage: true, |
| 99 | + }) |
| 100 | + |
| 101 | + const toolCalls = await getToolCalls(page) |
| 102 | + const metadata = await getMetadata(page) |
| 103 | + const events = await getEventLog(page) |
| 104 | + |
| 105 | + console.log('Test failed. Debug info:') |
| 106 | + console.log('Metadata:', metadata) |
| 107 | + console.log('Tool calls:', toolCalls) |
| 108 | + console.log('Events:', events) |
| 109 | + } |
| 110 | + }) |
| 111 | +}) |
0 commit comments