|
| 1 | +import React from 'react'; |
| 2 | +import { test, expect } from '@playwright/experimental-ct-react'; |
| 3 | +import { VideoCall } from '../../../../client/src/call/VideoCall'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Component Tests for Orphaned Daily Join Race Condition (Issue #1226) |
| 7 | + * |
| 8 | + * Tests: ORPHAN-001 to ORPHAN-003 |
| 9 | + * |
| 10 | + * The race condition: During Empirica stage transitions, a ~70ms desync window |
| 11 | + * can cause VideoCall to mount spuriously on a non-video stage. It calls |
| 12 | + * callObject.join(), then unmounts when the stage data resolves. But cleanup |
| 13 | + * skips leave() when meetingState is "joining", so the join completes orphaned |
| 14 | + * and the callObject is stuck in "joined-meeting" on the wrong room. |
| 15 | + * |
| 16 | + * Infrastructure: |
| 17 | + * - window.__mockInitialMeetingState: set BEFORE mount to override initial state |
| 18 | + * - window.__mockJoinBehavior: 'delayed' makes join() return a manually-resolvable promise |
| 19 | + * - window.mockCallObject._resolveJoin(): resolve the pending delayed join |
| 20 | + * - window.mockCallObject._leaveCalls: array of leave() call logs |
| 21 | + */ |
| 22 | + |
| 23 | +const baseConfig = { |
| 24 | + empirica: { |
| 25 | + currentPlayerId: 'p0', |
| 26 | + players: [{ id: 'p0', attrs: { position: '0', dailyId: 'daily-p0', name: 'Test User' } }], |
| 27 | + game: { attrs: { dailyUrl: 'https://test.daily.co/room-a' } }, |
| 28 | + stage: { attrs: {}, id: 'stage-1' }, |
| 29 | + stageTimer: { elapsed: 0 }, |
| 30 | + }, |
| 31 | + daily: { |
| 32 | + localSessionId: 'daily-p0', |
| 33 | + participantIds: ['daily-p0'], |
| 34 | + videoTracks: { 'daily-p0': { isOff: false, subscribed: true } }, |
| 35 | + audioTracks: { 'daily-p0': { isOff: false, subscribed: true } }, |
| 36 | + }, |
| 37 | +}; |
| 38 | + |
| 39 | +test.describe('Orphaned Join Cleanup (useCallLifecycle)', () => { |
| 40 | + /** |
| 41 | + * ORPHAN-001: When component unmounts during "joining" state, leave() must be |
| 42 | + * called after the join completes. |
| 43 | + * |
| 44 | + * This is the core bug from issue #1226: |
| 45 | + * 1. VideoCall mounts spuriously during stage desync window |
| 46 | + * 2. callObject.join() starts (meetingState → "joining") |
| 47 | + * 3. Component unmounts when stage data resolves (not a video stage) |
| 48 | + * 4. Cleanup sees "joining" and skips leave() ← BUG |
| 49 | + * 5. Join completes → callObject stuck in "joined-meeting" on wrong room |
| 50 | + * |
| 51 | + * Expected: cleanup arranges for leave() after the pending join resolves. |
| 52 | + */ |
| 53 | + test('ORPHAN-001: leave() called after orphaned join completes on unmount', async ({ mount, page }) => { |
| 54 | + // Setup: delayed join so we can unmount while "joining" |
| 55 | + await page.evaluate(() => { |
| 56 | + window.__mockInitialMeetingState = 'new'; |
| 57 | + window.__mockJoinBehavior = 'delayed'; |
| 58 | + }); |
| 59 | + |
| 60 | + const component = await mount(<VideoCall showSelfView />, { |
| 61 | + hooksConfig: baseConfig, |
| 62 | + }); |
| 63 | + |
| 64 | + // Wait for join to start (state should transition to 'joining') |
| 65 | + await expect(async () => { |
| 66 | + const state = await page.evaluate(() => window.mockCallObject?.meetingState()); |
| 67 | + expect(state).toBe('joining'); |
| 68 | + }).toPass({ timeout: 3000 }); |
| 69 | + |
| 70 | + // Save reference to mock before unmount (provider cleanup deletes window.mockCallObject) |
| 71 | + await page.evaluate(() => { |
| 72 | + window._savedMock = window.mockCallObject; |
| 73 | + }); |
| 74 | + |
| 75 | + // Verify no leave() calls yet |
| 76 | + const leavesBefore = await page.evaluate(() => window._savedMock._leaveCalls.length); |
| 77 | + expect(leavesBefore).toBe(0); |
| 78 | + |
| 79 | + // Unmount while join is in progress — this is the critical moment. |
| 80 | + // Current behavior: cleanup sees "joining", skips leave() → BUG |
| 81 | + // Fixed behavior: cleanup arranges for leave() after join resolves |
| 82 | + await component.unmount(); |
| 83 | + |
| 84 | + // Resolve the pending join (simulates Daily SDK completing the join) |
| 85 | + await page.evaluate(() => window._savedMock._resolveJoin()); |
| 86 | + |
| 87 | + // Give the post-join cleanup a tick to run |
| 88 | + await page.waitForTimeout(100); |
| 89 | + |
| 90 | + // Verify leave() was called after the orphaned join completed |
| 91 | + const leaveCalls = await page.evaluate(() => window._savedMock._leaveCalls); |
| 92 | + expect(leaveCalls.length).toBeGreaterThanOrEqual(1); |
| 93 | + }); |
| 94 | + |
| 95 | + /** |
| 96 | + * ORPHAN-002: Normal unmount from "joined-meeting" calls leave() immediately. |
| 97 | + * |
| 98 | + * Regression test: existing cleanup behavior must not break. |
| 99 | + */ |
| 100 | + test('ORPHAN-002: normal cleanup calls leave() when joined', async ({ mount, page }) => { |
| 101 | + const component = await mount(<VideoCall showSelfView />, { |
| 102 | + hooksConfig: baseConfig, |
| 103 | + }); |
| 104 | + await expect(component).toBeVisible({ timeout: 15000 }); |
| 105 | + |
| 106 | + // Save reference before unmount |
| 107 | + await page.evaluate(() => { |
| 108 | + window._savedMock = window.mockCallObject; |
| 109 | + }); |
| 110 | + |
| 111 | + // Unmount — cleanup should call leave() since state is "joined-meeting" |
| 112 | + await component.unmount(); |
| 113 | + |
| 114 | + const leaveCalls = await page.evaluate(() => window._savedMock._leaveCalls); |
| 115 | + expect(leaveCalls.length).toBeGreaterThanOrEqual(1); |
| 116 | + expect(leaveCalls[0].fromState).toBe('joined-meeting'); |
| 117 | + }); |
| 118 | + |
| 119 | + /** |
| 120 | + * ORPHAN-003: joinRoom skips when callObject is already in "joined-meeting". |
| 121 | + * |
| 122 | + * Existing guard behavior — join() should NOT be called redundantly. |
| 123 | + */ |
| 124 | + test('ORPHAN-003: joinRoom skips when already joined', async ({ mount, page }) => { |
| 125 | + const component = await mount(<VideoCall showSelfView />, { |
| 126 | + hooksConfig: baseConfig, |
| 127 | + }); |
| 128 | + await expect(component).toBeVisible({ timeout: 15000 }); |
| 129 | + |
| 130 | + // Mock starts in "joined-meeting" — joinRoom() should have skipped join() |
| 131 | + const joinCalled = await page.evaluate(() => window.mockCallObject._joinCalled); |
| 132 | + expect(joinCalled).toBe(false); |
| 133 | + }); |
| 134 | +}); |
0 commit comments