|
1 | | -import { EnvironmentId } from "@t3tools/contracts"; |
| 1 | +import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection"; |
| 2 | +import { EnvironmentId, type EnvironmentSpeechModel } from "@t3tools/contracts"; |
2 | 3 | import { expect, it, vi } from "vite-plus/test"; |
3 | 4 | import { userEvent } from "vite-plus/test/browser"; |
4 | 5 | import { render } from "../../../test/render"; |
5 | 6 | import { __setClientSettingsForTests, getClientSettings } from "../../hooks/useSettings"; |
| 7 | +import { useVoiceTranscriptionDestination } from "../../speech/useEnvironmentSpeechInput"; |
6 | 8 | import { chooseAudioDestination, chooseTranscriptCleanup } from "../../speech/voiceConsent"; |
| 9 | +import { toastManager } from "../ui/toast"; |
7 | 10 | import { SettingsScopeProvider } from "./SettingsScopeContext"; |
8 | 11 | import { SpeechLanguageSelect } from "./SpeechLanguageSelect"; |
9 | 12 | import { VoiceSettingsPanel } from "./VoiceSettingsPanel"; |
10 | 13 |
|
| 14 | +vi.mock(import("../../speech/useEnvironmentSpeechInput"), { spy: true }); |
| 15 | + |
| 16 | +it("recovers from a failed download progress poll without notifying, but reports a failed initial load", async () => { |
| 17 | + const target = new PrimaryConnectionTarget({ |
| 18 | + environmentId: EnvironmentId.make("voice-progress"), |
| 19 | + label: "Test environment", |
| 20 | + httpBaseUrl: window.location.origin, |
| 21 | + wsBaseUrl: "ws://localhost", |
| 22 | + }); |
| 23 | + const prepared = { ...target, target, socketUrl: target.wsBaseUrl, httpAuthorization: null }; |
| 24 | + const model: EnvironmentSpeechModel = { |
| 25 | + id: "tiny", |
| 26 | + name: "Tiny", |
| 27 | + description: "", |
| 28 | + size: 100, |
| 29 | + languages: ["en"], |
| 30 | + accuracy: 1, |
| 31 | + speed: 1, |
| 32 | + recommended: false, |
| 33 | + supportsStreaming: false, |
| 34 | + supportsTranslation: false, |
| 35 | + supportsLanguageDetection: false, |
| 36 | + active: false, |
| 37 | + state: "downloading", |
| 38 | + }; |
| 39 | + const status = { |
| 40 | + supported: true, |
| 41 | + state: "missing-model", |
| 42 | + modelId: model.id, |
| 43 | + model: model.name, |
| 44 | + size: model.size, |
| 45 | + supportsStreaming: false, |
| 46 | + supportsTranslation: false, |
| 47 | + acceleration: "cpu", |
| 48 | + modelUnloadTimeout: "never", |
| 49 | + gpuDevices: [], |
| 50 | + }; |
| 51 | + const unavailable = () => new Response(null, { status: 503 }); |
| 52 | + const readModels = vi |
| 53 | + .fn(unavailable) |
| 54 | + .mockImplementationOnce(() => Response.json({ models: [model] })) |
| 55 | + .mockImplementationOnce(unavailable) |
| 56 | + .mockImplementationOnce(() => Response.json({ models: [{ ...model, state: "installed" }] })); |
| 57 | + const fetch = globalThis.fetch; |
| 58 | + using _network = vi.spyOn(globalThis, "fetch").mockImplementation((input, init) => { |
| 59 | + const url = new URL(input instanceof Request ? input.url : input, window.location.href); |
| 60 | + if (url.pathname === "/api/voice/status") return Promise.resolve(Response.json(status)); |
| 61 | + if (url.pathname === "/api/voice/models") return Promise.resolve(readModels()); |
| 62 | + return fetch(input, init); |
| 63 | + }); |
| 64 | + using notify = vi.spyOn(toastManager, "add").mockReturnValue("test"); |
| 65 | + await vi.mocked(useVoiceTranscriptionDestination).withImplementation( |
| 66 | + () => ({ environmentId: target.environmentId, label: target.label, grantId: "test", prepared }), |
| 67 | + async () => { |
| 68 | + const panel = (key: string) => ( |
| 69 | + <SettingsScopeProvider search={{}} onChange={() => {}}> |
| 70 | + <VoiceSettingsPanel key={key} /> |
| 71 | + </SettingsScopeProvider> |
| 72 | + ); |
| 73 | + await using screen = await render(panel("first")); |
| 74 | + await expect.element(screen.getByRole("button", { name: "Use model" })).toBeVisible(); |
| 75 | + expect(notify).not.toHaveBeenCalled(); |
| 76 | + await screen.rerender(panel("reopened")); |
| 77 | + await vi.waitFor(() => |
| 78 | + expect(notify).toHaveBeenCalledWith( |
| 79 | + expect.objectContaining({ title: "Could not load transcription models" }), |
| 80 | + ), |
| 81 | + ); |
| 82 | + }, |
| 83 | + ); |
| 84 | +}); |
| 85 | + |
11 | 86 | it.each([ |
12 | 87 | ["speech-models", "voice-transcription-destination"], |
13 | 88 | ["transcription-test", "voice-transcription-destination"], |
|
0 commit comments