|
| 1 | +import { test, expect, type Page } from '@playwright/test'; |
| 2 | + |
| 3 | +// The embed bundles @vapi-ai/web, which asks for the microphone before it |
| 4 | +// creates the call. Until the user allows it, no call may exist: the server |
| 5 | +// would end one as assistant-did-not-receive-customer-audio. |
| 6 | + |
| 7 | +// Full Chromium, because the headless shell rejects every getUserMedia call |
| 8 | +// with NotSupportedError, so the allowed case could not pass there. |
| 9 | +test.use({ |
| 10 | + channel: 'chromium', |
| 11 | + launchOptions: { args: ['--use-fake-device-for-media-stream'] }, |
| 12 | +}); |
| 13 | + |
| 14 | +const WIDGET_PAGE = `<!DOCTYPE html> |
| 15 | +<html> |
| 16 | + <body> |
| 17 | + <vapi-widget |
| 18 | + public-key="pk_test" |
| 19 | + assistant-id="asst_test" |
| 20 | + api-url="http://localhost:4200/stub-api" |
| 21 | + mode="voice" |
| 22 | + cta-title="Open test widget" |
| 23 | + start-button-text="Start test call" |
| 24 | + ></vapi-widget> |
| 25 | + <script src="/dist/embed/widget.umd.js"></script> |
| 26 | + </body> |
| 27 | +</html>`; |
| 28 | + |
| 29 | +const START_BUTTON = { name: 'Start test call' }; |
| 30 | + |
| 31 | +/** Opens the widget, clicks start, and returns a live count of POST /call/web. */ |
| 32 | +async function widgetCallStart(page: Page): Promise<() => number> { |
| 33 | + let webCallRequests = 0; |
| 34 | + await page.route('**/stub-api/call/web', async (route) => { |
| 35 | + webCallRequests++; |
| 36 | + await route.fulfill({ |
| 37 | + status: 400, |
| 38 | + contentType: 'application/json', |
| 39 | + body: JSON.stringify({ message: 'stub: no real call' }), |
| 40 | + }); |
| 41 | + }); |
| 42 | + await page.route('**/mic-test', (route) => |
| 43 | + route.fulfill({ contentType: 'text/html', body: WIDGET_PAGE }) |
| 44 | + ); |
| 45 | + |
| 46 | + await page.goto('/mic-test'); |
| 47 | + // The collapsed launcher is a clickable div, not a button; its label is |
| 48 | + // set by this page, so the text is stable. |
| 49 | + await page.getByText('Open test widget').click(); |
| 50 | + await page.getByRole('button', START_BUTTON).click(); |
| 51 | + return () => webCallRequests; |
| 52 | +} |
| 53 | + |
| 54 | +test.describe('VapiWidget microphone preflight', () => { |
| 55 | + test('creates no call when the microphone is blocked', async ({ page }) => { |
| 56 | + // Playwright can only grant permissions; denying needs the DevTools protocol. |
| 57 | + // Without the context id it applies to the default context, not this test's. |
| 58 | + const cdp = await page.context().newCDPSession(page); |
| 59 | + const { targetInfo } = await cdp.send('Target.getTargetInfo'); |
| 60 | + await cdp.send('Browser.setPermission', { |
| 61 | + permission: { name: 'microphone' }, |
| 62 | + setting: 'denied', |
| 63 | + origin: 'http://localhost:4200', |
| 64 | + browserContextId: targetInfo.browserContextId, |
| 65 | + }); |
| 66 | + |
| 67 | + const webCallRequests = await widgetCallStart(page); |
| 68 | + |
| 69 | + // The widget returns to its start state once start() fails. |
| 70 | + await expect(page.getByRole('button', START_BUTTON)).toBeEnabled(); |
| 71 | + expect(webCallRequests()).toBe(0); |
| 72 | + }); |
| 73 | + |
| 74 | + test('creates no call while the permission prompt is unanswered', async ({ |
| 75 | + page, |
| 76 | + }) => { |
| 77 | + // A click is a user gesture, so Chromium shows the prompt and waits. |
| 78 | + const webCallRequests = await widgetCallStart(page); |
| 79 | + |
| 80 | + await expect( |
| 81 | + page.getByRole('button', { name: 'Connecting...' }) |
| 82 | + ).toBeVisible(); |
| 83 | + await page.waitForTimeout(2000); |
| 84 | + expect(webCallRequests()).toBe(0); |
| 85 | + }); |
| 86 | + |
| 87 | + test('creates the call when the microphone is allowed', async ({ |
| 88 | + page, |
| 89 | + context, |
| 90 | + }) => { |
| 91 | + await context.grantPermissions(['microphone']); |
| 92 | + |
| 93 | + const webCallRequests = await widgetCallStart(page); |
| 94 | + |
| 95 | + await expect.poll(webCallRequests).toBe(1); |
| 96 | + }); |
| 97 | +}); |
0 commit comments