|
| 1 | +import React from 'react'; |
| 2 | +import { test, expect } from '@playwright/experimental-ct-react'; |
| 3 | +import { AudioElement } from '../../../client/src/elements/AudioElement'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Component Tests for AudioElement |
| 7 | + * |
| 8 | + * AudioElement plays a CDN-hosted audio file when a stage element has type |
| 9 | + * "audio". The file URL is resolved via useFileURL (which reads Empirica |
| 10 | + * globals for the CDN base URL) and played via the Web Audio API. |
| 11 | + * |
| 12 | + * These tests verify: |
| 13 | + * AE-001 Audio plays when the file URL resolves from CDN config |
| 14 | + * AE-002 Audio is not recreated on re-render with the same file (regression |
| 15 | + * guard: old code called `new Audio()` in the render body, creating |
| 16 | + * a new instance—and triggering setHasPlayed—on every render) |
| 17 | + * AE-003 Audio is paused and src cleared on unmount (prevents the same |
| 18 | + * buffer-loop bug seen with DailyAudio on call end) |
| 19 | + * |
| 20 | + * Mock setup: |
| 21 | + * window.__mockGlobal – read by the useGlobal mock alias for |
| 22 | + * @empirica/core/player/react; provides cdnList and |
| 23 | + * recruitingBatchConfig so useFileURL can resolve URLs |
| 24 | + * window.Audio – replaced with a spy that records instantiations, |
| 25 | + * play/pause calls, and canplaythrough listeners |
| 26 | + */ |
| 27 | + |
| 28 | +// --------------------------------------------------------------------------- |
| 29 | +// Helpers |
| 30 | +// --------------------------------------------------------------------------- |
| 31 | + |
| 32 | +async function setupAudioMock(page) { |
| 33 | + await page.evaluate(() => { |
| 34 | + window.mockAudioInstances = []; |
| 35 | + window.Audio = function AudioMock(url) { |
| 36 | + const instance = { |
| 37 | + src: url, |
| 38 | + _played: false, |
| 39 | + _paused: false, |
| 40 | + _listeners: {}, |
| 41 | + play() { |
| 42 | + instance._played = true; |
| 43 | + return Promise.resolve(); |
| 44 | + }, |
| 45 | + pause() { |
| 46 | + instance._paused = true; |
| 47 | + }, |
| 48 | + addEventListener(evt, fn) { |
| 49 | + instance._listeners[evt] = fn; |
| 50 | + }, |
| 51 | + removeEventListener(evt, fn) { |
| 52 | + if (instance._listeners[evt] === fn) { |
| 53 | + delete instance._listeners[evt]; |
| 54 | + } |
| 55 | + }, |
| 56 | + }; |
| 57 | + window.mockAudioInstances.push(instance); |
| 58 | + return instance; |
| 59 | + }; |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +async function setupGlobalsMock(page) { |
| 64 | + await page.evaluate(() => { |
| 65 | + window.__mockGlobal = { |
| 66 | + get(key) { |
| 67 | + if (key === 'cdnList') return { prod: 'http://test-cdn.example.com' }; |
| 68 | + if (key === 'recruitingBatchConfig') return { cdn: 'prod' }; |
| 69 | + return null; |
| 70 | + }, |
| 71 | + }; |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +// --------------------------------------------------------------------------- |
| 76 | +// Tests |
| 77 | +// --------------------------------------------------------------------------- |
| 78 | + |
| 79 | +/** AE-001: Audio plays when file URL resolves from CDN config */ |
| 80 | +test('AE-001: audio plays when file URL resolves', async ({ mount, page }) => { |
| 81 | + await setupAudioMock(page); |
| 82 | + await setupGlobalsMock(page); |
| 83 | + |
| 84 | + await mount(<AudioElement file="test-audio.mp3" />); |
| 85 | + |
| 86 | + // useFileURL resolves asynchronously via useEffect; wait for Audio instantiation |
| 87 | + await expect.poll(() => page.evaluate(() => window.mockAudioInstances.length)) |
| 88 | + .toBeGreaterThan(0); |
| 89 | + |
| 90 | + // Correct CDN URL was used |
| 91 | + const audioSrc = await page.evaluate(() => window.mockAudioInstances[0]?.src); |
| 92 | + expect(audioSrc).toBe('http://test-cdn.example.com/test-audio.mp3'); |
| 93 | + |
| 94 | + // Simulate browser signalling audio is ready to play |
| 95 | + await page.evaluate(() => { |
| 96 | + window.mockAudioInstances[0]?._listeners.canplaythrough?.(); |
| 97 | + }); |
| 98 | + |
| 99 | + // play() was called |
| 100 | + const played = await page.evaluate(() => window.mockAudioInstances[0]?._played); |
| 101 | + expect(played).toBe(true); |
| 102 | +}); |
| 103 | + |
| 104 | +/** AE-002: Audio element is not recreated on re-render with the same file */ |
| 105 | +test('AE-002: audio is not recreated on re-render', async ({ mount, page }) => { |
| 106 | + await setupAudioMock(page); |
| 107 | + await setupGlobalsMock(page); |
| 108 | + |
| 109 | + const component = await mount(<AudioElement file="test-audio.mp3" />); |
| 110 | + |
| 111 | + // Wait for initial Audio creation |
| 112 | + await expect.poll(() => page.evaluate(() => window.mockAudioInstances.length)) |
| 113 | + .toBe(1); |
| 114 | + |
| 115 | + // Re-render with the same file prop — fileURL dep is unchanged, effect should not re-run |
| 116 | + await component.update(<AudioElement file="test-audio.mp3" />); |
| 117 | + |
| 118 | + const count = await page.evaluate(() => window.mockAudioInstances.length); |
| 119 | + expect(count).toBe(1); |
| 120 | +}); |
| 121 | + |
| 122 | +/** AE-003: Audio is paused and src cleared on unmount */ |
| 123 | +test('AE-003: audio is paused and src cleared on unmount', async ({ mount, page }) => { |
| 124 | + await setupAudioMock(page); |
| 125 | + await setupGlobalsMock(page); |
| 126 | + |
| 127 | + const component = await mount(<AudioElement file="test-audio.mp3" />); |
| 128 | + |
| 129 | + // Wait for Audio to be instantiated |
| 130 | + await expect.poll(() => page.evaluate(() => window.mockAudioInstances.length)) |
| 131 | + .toBeGreaterThan(0); |
| 132 | + |
| 133 | + await component.unmount(); |
| 134 | + |
| 135 | + const paused = await page.evaluate(() => window.mockAudioInstances[0]?._paused); |
| 136 | + const src = await page.evaluate(() => window.mockAudioInstances[0]?.src); |
| 137 | + expect(paused).toBe(true); |
| 138 | + expect(src).toBe(''); |
| 139 | +}); |
0 commit comments