## CHANGES

- Eliminated mobile session ref initialization race with immediate saved defaults 🚀
- Made active tab ref mirror URL/saved tab instantly for reliable callbacks 🧭
- Simplified hook test by removing unnecessary async waiting on refs 🧪
This commit is contained in:
Pedram Amini
2026-01-18 13:14:42 -06:00
parent bcc15a486e
commit fe77591d42
2 changed files with 7 additions and 6 deletions

View File

@@ -3,7 +3,7 @@
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { renderHook, act, waitFor } from '@testing-library/react';
import { renderHook, act } from '@testing-library/react';
import { useMobileSessionManagement, type UseMobileSessionManagementDeps } from '../../../web/hooks/useMobileSessionManagement';
import type { Session } from '../../../web/hooks/useSessions';
@@ -82,9 +82,8 @@ describe('useMobileSessionManagement', () => {
savedActiveTabId: 'tab-1',
}));
await waitFor(() => {
expect(result.current.activeSessionIdRef.current).toBe('session-1');
});
// Refs should be initialized immediately with saved values (no race condition)
expect(result.current.activeSessionIdRef.current).toBe('session-1');
act(() => {
result.current.sessionsHandlers.onSessionOutput('session-1', 'hello', 'ai', 'tab-1');

View File

@@ -198,9 +198,11 @@ export function useMobileSessionManagement(
const previousSessionStatesRef = useRef<Map<string, string>>(new Map());
// Ref to track activeSessionId for use in callbacks (avoids stale closure issues)
const activeSessionIdRef = useRef<string | null>(null);
// Initialize with same value as state to avoid race condition where WebSocket
// messages arrive before useEffect syncs the ref
const activeSessionIdRef = useRef<string | null>(urlSessionId || savedActiveSessionId);
// Ref to track activeTabId for use in callbacks (avoids stale closure issues)
const activeTabIdRef = useRef<string | null>(null);
const activeTabIdRef = useRef<string | null>(urlTabId || savedActiveTabId);
// Keep activeSessionIdRef in sync with state
useEffect(() => {