diff --git a/src/renderer/src/components/dictation/dictation-setup-pill.test.tsx b/src/renderer/src/components/dictation/dictation-setup-pill.test.tsx new file mode 100644 index 00000000000..6c2f63ea866 --- /dev/null +++ b/src/renderer/src/components/dictation/dictation-setup-pill.test.tsx @@ -0,0 +1,86 @@ +// @vitest-environment happy-dom + +import { act } from 'react' +import { createRoot, type Root } from 'react-dom/client' +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import { TooltipProvider } from '@/components/ui/tooltip' +import { useAppStore } from '@/store' +import { DictationSetupPill } from './DictationSetupPill' + +globalThis.IS_REACT_ACT_ENVIRONMENT = true + +const cancelDownload = vi.fn(async () => {}) +let root: Root +let container: HTMLDivElement + +beforeEach(() => { + cancelDownload.mockClear() + Object.defineProperty(window, 'api', { + configurable: true, + value: { + speech: { + // Neither the download nor its first progress arrives before the pill is closed. + downloadModel: () => new Promise(() => {}), + cancelDownload, + getModelStates: () => new Promise(() => {}), + onDownloadProgress: () => () => {} + } + } + }) + useAppStore.setState(useAppStore.getInitialState(), true) + useAppStore.setState({ + modelStates: [{ id: 'parakeet', status: 'not-downloaded', needsUpdate: true }] + }) + container = document.createElement('div') + document.body.appendChild(container) + root = createRoot(container) +}) + +afterEach(() => { + act(() => root.unmount()) + container.remove() + useAppStore.setState(useAppStore.getInitialState(), true) +}) + +function button(label: string): HTMLButtonElement { + const match = Array.from(document.querySelectorAll('button')).find( + (candidate) => + candidate.textContent?.trim() === label || candidate.getAttribute('aria-label') === label + ) + if (!match) { + throw new Error(`missing ${label} button`) + } + return match +} + +it('cancels a download started from the pill when it is closed before any progress', async () => { + const onClose = vi.fn() + await act(async () => { + root.render( + + + + ) + }) + + await act(async () => button('Install').click()) + await act(async () => button('Cancel download').click()) + + expect(cancelDownload).toHaveBeenCalledWith('parakeet') + expect(onClose).toHaveBeenCalledOnce() +}) diff --git a/src/renderer/src/components/dictation/use-speech-model-update-download.ts b/src/renderer/src/components/dictation/use-speech-model-update-download.ts index 4241dc18d1d..0496c35d6e6 100644 --- a/src/renderer/src/components/dictation/use-speech-model-update-download.ts +++ b/src/renderer/src/components/dictation/use-speech-model-update-download.ts @@ -26,6 +26,8 @@ export function useSpeechModelUpdateDownload( const state = useAppStore((s) => s.modelStates.find((m) => m.id === model.id)) const refreshModelStates = useAppStore((s) => s.refreshModelStates) const [failed, setFailed] = useState(false) + // Set from the click, so ✕ cancels a download whose first progress hasn't arrived yet. + const [inFlight, setInFlight] = useState(false) const [deleting, setDeleting] = useState(false) useEffect( @@ -35,7 +37,10 @@ export function useSpeechModelUpdateDownload( const totalMb = Math.round((model.sizeBytes ?? 0) / 1_000_000) const progress = state?.progress ?? 0 - const downloading = state?.status === 'downloading' || state?.status === 'extracting' + const downloading = + state?.status === 'downloading' || + state?.status === 'extracting' || + (inFlight && state?.status !== 'ready') const phase: SpeechModelUpdatePhase = downloading ? 'downloading' : state?.status === 'ready' @@ -46,10 +51,14 @@ export function useSpeechModelUpdateDownload( const start = (): void => { setFailed(false) + setInFlight(true) void window.api.speech .downloadModel(model.id) .catch(() => setFailed(true)) - .finally(() => void refreshModelStates()) + .finally(() => { + setInFlight(false) + void refreshModelStates() + }) } const cancel = (): void => {