diff --git a/mobile/src/hooks/use-mobile-dictation-source.test.ts b/mobile/src/hooks/use-mobile-dictation-source.test.ts new file mode 100644 index 000000000..7dc889341 --- /dev/null +++ b/mobile/src/hooks/use-mobile-dictation-source.test.ts @@ -0,0 +1,29 @@ +import { readFileSync } from 'node:fs' +import { describe, expect, it } from 'vitest' + +const source = readFileSync(new URL('./use-mobile-dictation.ts', import.meta.url), 'utf8') + +function sliceBetween(startPattern: string, endPattern: string): string { + const start = source.indexOf(startPattern) + expect(start).toBeGreaterThanOrEqual(0) + const end = source.indexOf(endPattern, start) + expect(end).toBeGreaterThan(start) + return source.slice(start, end) +} + +describe('useMobileDictation source invariants', () => { + it('publishes live option refs from committed renders before passive Effects flush', () => { + const refDeclarations = sliceBetween( + 'const clientRef = useRef(client)', + 'useLayoutEffect(() => {' + ) + expect(refDeclarations).not.toContain('.current =') + + const mirrorEffect = sliceBetween('useLayoutEffect(() => {', 'const reportError =') + expect(mirrorEffect).toContain('clientRef.current = client') + expect(mirrorEffect).toContain('enabledRef.current = enabled') + expect(mirrorEffect).toContain('onTranscriptRef.current = onTranscript') + expect(mirrorEffect).toContain('onErrorRef.current = onError') + expect(mirrorEffect).toContain('}, [client, enabled, onTranscript, onError])') + }) +}) diff --git a/mobile/src/hooks/use-mobile-dictation.ts b/mobile/src/hooks/use-mobile-dictation.ts index 29963ecec..55857b58e 100644 --- a/mobile/src/hooks/use-mobile-dictation.ts +++ b/mobile/src/hooks/use-mobile-dictation.ts @@ -1,7 +1,7 @@ /* oxlint-disable max-lines -- Why: mobile dictation keeps permission, recording, * chunk upload, completion, and cancellation in one hook so native audio state * cannot drift from the runtime RPC lifecycle. */ -import { useCallback, useEffect, useRef, useState } from 'react' +import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react' import { Buffer } from 'buffer' import { addExpoTwoWayAudioEventListener, @@ -57,7 +57,9 @@ export function useMobileDictation(options: UseMobileDictationOptions): UseMobil const generationRef = useRef(0) const finishingIdRef = useRef(null) - useEffect(() => { + useLayoutEffect(() => { + // Native audio events can arrive before passive Effects flush, but refs + // should only expose options from a committed render. clientRef.current = client enabledRef.current = enabled onTranscriptRef.current = onTranscript