The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | /// <reference lib="dom" /> |
| 2 | import type { EnvironmentSpeechModel, EnvironmentSpeechStatus } from "@t3tools/contracts"; |
| 3 | import { expect } from "e2e"; |
| 4 | |
| 5 | import { test, WEB } from "./t3.ts"; |
| 6 | |
| 7 | declare global { |
| 8 | interface Window { |
| 9 | voiceSetupDevices: { |
| 10 | holdNext: () => void; |
| 11 | held: Promise<void>; |
| 12 | replaceMicrophone: () => void; |
| 13 | releaseOld: () => Promise<void>; |
| 14 | }; |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | test( |
| 19 | "Voice setup keeps the current microphone after an older refresh finishes", |
| 20 | WEB, |
| 21 | async ({ t3, app, browser, screen }) => { |
| 22 | const project = await t3.project("demo"); |
| 23 | const thread = await t3.thread({ project, title: "Voice notes", messages: ["first"] }); |
| 24 | |
| 25 | const model = { |
| 26 | id: "scenario-speech", |
| 27 | name: "Scenario speech model", |
| 28 | description: "", |
| 29 | size: 1_048_576, |
| 30 | languages: ["en", "fr", "de"], |
| 31 | accuracy: 1, |
| 32 | speed: 1, |
| 33 | recommended: true, |
| 34 | supportsStreaming: false, |
| 35 | supportsLanguageDetection: true, |
| 36 | supportsTranslation: false, |
| 37 | active: false, |
| 38 | state: "downloadable", |
| 39 | } satisfies EnvironmentSpeechModel; |
| 40 | let installed = false; |
| 41 | await browser.route(/\/api\/voice\/status$/, (route) => { |
| 42 | const status = { |
| 43 | supported: true, |
| 44 | state: installed ? "ready" : "missing-model", |
| 45 | modelId: model.id, |
| 46 | model: model.name, |
| 47 | size: model.size, |
| 48 | supportsStreaming: false, |
| 49 | supportsTranslation: false, |
| 50 | acceleration: "cpu", |
| 51 | benchmarkTier: "cpu", |
| 52 | modelUnloadTimeout: "never", |
| 53 | gpuDevices: [], |
| 54 | } satisfies EnvironmentSpeechStatus; |
| 55 | return route.fulfill({ json: status }); |
| 56 | }); |
| 57 | await browser.route(/\/api\/voice\/models(?:\?|$)/, (route) => |
| 58 | route.fulfill({ |
| 59 | json: { models: [installed ? { ...model, active: true, state: "installed" } : model] }, |
| 60 | }), |
| 61 | ); |
| 62 | await browser.route(/\/api\/voice\/models\/download$/, (route) => { |
| 63 | installed = true; |
| 64 | return route.fulfill({ |
| 65 | json: { models: [{ ...model, active: true, state: "installed" }] }, |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | await browser.addInitScript(() => { |
| 70 | const oldMicrophone = { kind: "audioinput", deviceId: "desk", label: "Desk microphone" }; |
| 71 | const newMicrophone = { kind: "audioinput", deviceId: "usb", label: "USB microphone" }; |
| 72 | const { promise: held, resolve: markHeld } = Promise.withResolvers<void>(); |
| 73 | const { promise: old, resolve: release } = |
| 74 | Promise.withResolvers<Array<typeof oldMicrophone>>(); |
| 75 | let holdNext = false; |
| 76 | let current = oldMicrophone; |
| 77 | const media = navigator.mediaDevices; |
| 78 | Object.defineProperty(media, "enumerateDevices", { |
| 79 | configurable: true, |
| 80 | value: () => { |
| 81 | if (!holdNext) return Promise.resolve([current]); |
| 82 | holdNext = false; |
| 83 | markHeld(); |
| 84 | return old; |
| 85 | }, |
| 86 | }); |
| 87 | window.voiceSetupDevices = { |
| 88 | holdNext: () => { |
| 89 | holdNext = true; |
| 90 | }, |
| 91 | held, |
| 92 | replaceMicrophone: () => { |
| 93 | current = newMicrophone; |
| 94 | media.dispatchEvent(new Event("devicechange")); |
| 95 | }, |
| 96 | releaseOld: async () => { |
| 97 | release([oldMicrophone]); |
| 98 | // Let enumeration and its React update settle before inspecting the picker. |
| 99 | await new Promise<void>((resolve) => |
| 100 | requestAnimationFrame(() => requestAnimationFrame(() => resolve())), |
| 101 | ); |
| 102 | }, |
| 103 | }; |
| 104 | }); |
| 105 | |
| 106 | await t3.open(thread); |
| 107 | const threadPath = new URL(await browser.url()).pathname; |
| 108 | const voice = screen.getByRole("button", "Start voice input"); |
| 109 | await voice.tap(); |
| 110 | const destination = screen.getByRole("combobox", "Transcribe with"); |
| 111 | await destination.tap(); |
| 112 | // The only paired environment is the first choice after Off. |
| 113 | await screen.getByRole("option").nth(1).tap(); |
| 114 | await app.open(threadPath); |
| 115 | await voice.tap(); |
| 116 | |
| 117 | const download = screen.getByRole("button", "Download model", { exact: true }); |
| 118 | await download.waitFor(); |
| 119 | await browser.evaluate(() => { |
| 120 | window.voiceSetupDevices.holdNext(); |
| 121 | return null; |
| 122 | }); |
| 123 | await download.tap(); |
| 124 | await screen.getByText("Voice input is ready", { exact: true }).waitFor(); |
| 125 | await browser.evaluate(async () => { |
| 126 | await window.voiceSetupDevices.held; |
| 127 | window.voiceSetupDevices.replaceMicrophone(); |
| 128 | return null; |
| 129 | }); |
| 130 | await screen.getByRole("combobox", "Microphone", { exact: true }).tap(); |
| 131 | const connected = screen.getByRole("option", "USB microphone", { exact: true }); |
| 132 | await connected.waitFor(); |
| 133 | |
| 134 | await browser.evaluate(async () => { |
| 135 | await window.voiceSetupDevices.releaseOld(); |
| 136 | return null; |
| 137 | }); |
| 138 | await app.screenshot("microphones-after-both-refreshes"); |
| 139 | const labels = await browser.evaluate(() => |
| 140 | Array.from( |
| 141 | document.querySelectorAll('[role="listbox"] [role="option"]'), |
| 142 | (option) => option.textContent?.trim() ?? "", |
| 143 | ), |
| 144 | ); |
| 145 | expect(labels, "The microphone picker lists only the current devices").toEqual([ |
| 146 | "System default", |
| 147 | "USB microphone", |
| 148 | ]); |
| 149 | }, |
| 150 | ); |