Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

test(mobile): Stop rotation and partial-download tests timing out (#2330)

Chad Piha6ecfea8c36ea
diff --git a/openwhispr-mobile/src/screens/__tests__/ModelDownloadScreen.partialDownload.test.tsx b/openwhispr-mobile/src/screens/__tests__/ModelDownloadScreen.partialDownload.test.tsx
index fa243c4fc..97bbd563a 100644
--- a/openwhispr-mobile/src/screens/__tests__/ModelDownloadScreen.partialDownload.test.tsx
+++ b/openwhispr-mobile/src/screens/__tests__/ModelDownloadScreen.partialDownload.test.tsx
@@ -1,5 +1,5 @@
import React from 'react';
-import { act, fireEvent, render, screen, waitFor } from '@testing-library/react-native';
+import { act, fireEventAsync, renderAsync, screen } from '@testing-library/react-native';
jest.mock('expo-router', () => ({
useRouter: () => ({ canGoBack: () => true, back: jest.fn(), replace: jest.fn() }),
@@ -79,29 +79,32 @@ beforeEach(() => {
// delete button only exists for installed models, so without this action a user who gives up on
// Parakeet has no visible way to get that storage back.
describe('ModelDownloadScreen — partial Parakeet download', () => {
+ // Renders, presses and store updates all settle inside async act (renderAsync, fireEventAsync,
+ // act(async)), which keeps flushing until the mocked promise chains stop scheduling updates, so
+ // every assertion sees the settled screen. findByText/waitFor would instead poll on real timers
+ // against a 1 s budget, and under CPU load the event loop can stall past it before the render
+ // that shows or removes the link commits.
+
it('offers to clear a staged partial download and reclaims it on confirm', async () => {
mockParakeet.stagedDownloadBytes.mockImplementation(async (version) =>
version === 'v2' ? 445 * MB : 0,
);
- render(<ModelDownloadScreen />);
+ await renderAsync(<ModelDownloadScreen />);
- const action = await screen.findByText(/Clear partial download \(445\.0 MB\)/);
+ const action = screen.getByText(/Clear partial download \(445\.0 MB\)/);
expect(screen.getAllByText(/Clear partial download/)).toHaveLength(1);
mockParakeet.stagedDownloadBytes.mockResolvedValue(0);
- fireEvent.press(action);
+ await fireEventAsync.press(action);
- await waitFor(() => expect(mockParakeet.cancelModelDownload).toHaveBeenCalledWith('v2'));
- // Not `toBeNull()`: its failure message pretty-prints the element's whole fiber graph, which
- // blocks the event loop for ~250 ms per poll, so under CPU load the render that removes the
- // link never runs before waitFor times out.
- await waitFor(() => expect(screen.queryByText(/Clear partial download/)).not.toBeOnTheScreen());
+ expect(mockParakeet.cancelModelDownload).toHaveBeenCalledWith('v2');
+ expect(screen.queryByText(/Clear partial download/)).not.toBeOnTheScreen();
});
it('shows nothing when no partial download is staged', async () => {
- render(<ModelDownloadScreen />);
+ await renderAsync(<ModelDownloadScreen />);
- await screen.findByText('Parakeet v2');
+ expect(screen.getByText('Parakeet v2')).toBeTruthy();
expect(screen.queryByText(/Clear partial download/)).toBeNull();
});
@@ -111,14 +114,14 @@ describe('ModelDownloadScreen — partial Parakeet download', () => {
mockParakeet.stagedDownloadBytes.mockImplementation(async (version) =>
version === 'v2' ? 445 * MB : 0,
);
- render(<ModelDownloadScreen />);
- await screen.findByText(/Clear partial download \(445\.0 MB\)/);
+ await renderAsync(<ModelDownloadScreen />);
+ expect(screen.getByText(/Clear partial download \(445\.0 MB\)/)).toBeTruthy();
- act(() => setDownloadStatus('parakeet-v3', 'downloading'));
+ await act(async () => setDownloadStatus('parakeet-v3', 'downloading'));
expect(screen.queryByText(/Clear partial download/)).toBeNull();
- act(() => setDownloadStatus('parakeet-v3', 'idle'));
- await screen.findByText(/Clear partial download \(445\.0 MB\)/);
+ await act(async () => setDownloadStatus('parakeet-v3', 'idle'));
+ expect(screen.getByText(/Clear partial download \(445\.0 MB\)/)).toBeTruthy();
});
// The red link alone reads as "something went wrong"; the row should also say the kept bytes
@@ -127,16 +130,16 @@ describe('ModelDownloadScreen — partial Parakeet download', () => {
mockParakeet.stagedDownloadBytes.mockImplementation(async (version) =>
version === 'v2' ? 445 * MB : 0,
);
- render(<ModelDownloadScreen />);
+ await renderAsync(<ModelDownloadScreen />);
- await screen.findByText('445.0 MB saved from an earlier attempt will be reused.');
+ expect(screen.getByText('445.0 MB saved from an earlier attempt will be reused.')).toBeTruthy();
expect(screen.getAllByText(/saved from an earlier attempt/)).toHaveLength(1);
});
it('does not mention reuse when nothing is staged', async () => {
- render(<ModelDownloadScreen />);
+ await renderAsync(<ModelDownloadScreen />);
- await screen.findByText('Parakeet v2');
+ expect(screen.getByText('Parakeet v2')).toBeTruthy();
expect(screen.queryByText(/saved from an earlier attempt/)).toBeNull();
});
});
diff --git a/openwhispr-mobile/src/services/transcription/__tests__/RealtimeMeetingWsService.test.ts b/openwhispr-mobile/src/services/transcription/__tests__/RealtimeMeetingWsService.test.ts
index 19d2684f2..000689525 100644
--- a/openwhispr-mobile/src/services/transcription/__tests__/RealtimeMeetingWsService.test.ts
+++ b/openwhispr-mobile/src/services/transcription/__tests__/RealtimeMeetingWsService.test.ts
@@ -113,6 +113,18 @@ async function flushMicro(): Promise<void> {
}
}
+/**
+ * Advance fake time onto the rotation seam. advanceTimersByTimeAsync yields a real event-loop
+ * turn after every timer it fires, and the flush interval fires 20,000 times before the seam, so
+ * walking all of ROTATION_MS that way tied these tests' wall time to CPU load until they hit
+ * Jest's timeout. The flush ticks queue no async work, so walk them synchronously and cross only
+ * the seam itself (where the rotation's token fetch and leg open run) asynchronously.
+ */
+async function advanceToRotation(): Promise<void> {
+ jest.advanceTimersByTime(ROTATION_MS - 1);
+ await jest.advanceTimersByTimeAsync(1);
+}
+
function emitFrame(bytes: number[]): void {
const calls = (addPcmFrameListener as jest.Mock).mock.calls;
const cb = calls[calls.length - 1]?.[0] as ((frame: { audio: string }) => void) | undefined;
@@ -298,7 +310,7 @@ describe('startRealtimeMeetingWs — rotation', () => {
const { session, leg1 } = await boot();
leg1.emit(completed('a', 'leg one'));
- await jest.advanceTimersByTimeAsync(ROTATION_MS);
+ await advanceToRotation();
await flushMicro();
const leg2 = lastLeg();
@@ -326,7 +338,7 @@ describe('startRealtimeMeetingWs — rotation', () => {
// The rotation's token fetch fails once; the follow-up reconnect then succeeds.
(api.post as jest.Mock).mockRejectedValueOnce(new Error('rotate token failed'));
- await jest.advanceTimersByTimeAsync(ROTATION_MS);
+ await advanceToRotation();
await flushMicro();
// Rotation failed → fell back to a reconnect (old leg still live meanwhile).
@@ -357,7 +369,7 @@ describe('startRealtimeMeetingWs — rotation', () => {
});
expect(onPartial).toHaveBeenLastCalledWith('half a senten', 'L1:x');
- await jest.advanceTimersByTimeAsync(ROTATION_MS);
+ await advanceToRotation();
await flushMicro();
lastLeg().open();
await flushMicro();
@@ -369,7 +381,7 @@ describe('startRealtimeMeetingWs — rotation', () => {
it('routes PCM to exactly one leg across the seam', async () => {
await boot();
- await jest.advanceTimersByTimeAsync(ROTATION_MS);
+ await advanceToRotation();
await flushMicro();
const leg2 = lastLeg();
leg2.open();