mirror of
https://github.com/stablyai/orca.git
synced 2026-11-18 03:45:08 +00:00
fix(dictation): cancel a pill download closed before its first progress
Co-Authored-By: Claude <noreply@anthropic.com>
(cherry picked from commit 5d8acbcde8)
This commit is contained in:
@@ -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(
|
||||
<TooltipProvider>
|
||||
<DictationSetupPill
|
||||
target={{
|
||||
model: {
|
||||
id: 'parakeet',
|
||||
label: 'Parakeet',
|
||||
type: 'transducer',
|
||||
provider: 'local',
|
||||
language: 'en',
|
||||
languages: ['en'],
|
||||
sampleRate: 16000,
|
||||
streaming: false
|
||||
}
|
||||
}}
|
||||
anchor={null}
|
||||
onClose={onClose}
|
||||
/>
|
||||
</TooltipProvider>
|
||||
)
|
||||
})
|
||||
|
||||
await act(async () => button('Install').click())
|
||||
await act(async () => button('Cancel download').click())
|
||||
|
||||
expect(cancelDownload).toHaveBeenCalledWith('parakeet')
|
||||
expect(onClose).toHaveBeenCalledOnce()
|
||||
})
|
||||
@@ -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 => {
|
||||
|
||||
Reference in New Issue
Block a user