1- import { act , useLayoutEffect , useSyncExternalStore } from "react" ;
2- import { createRoot , type Root } from "react-dom/client" ;
3- import { afterEach , describe , expect , it , vi } from "vite-plus/test" ;
1+ import { useLayoutEffect , useSyncExternalStore } from "react" ;
2+ import { describe , expect , it , vi } from "vite-plus/test" ;
43import * as Option from "effect/Option" ;
54import {
65 EnvironmentId ,
@@ -9,13 +8,15 @@ import {
98 ThreadId ,
109 VOICE_TEXT_STARTER_PROMPTS_FORK ,
1110} from "@t3tools/contracts" ;
11+ import { act , render , type Screen } from "../../test/render" ;
1212import { useComposerDraftStore } from "../composerDraftStore" ;
1313import { DesktopVoiceSessionFork } from "./session.fork" ;
1414import { useDesktopVoiceInputFork } from "./useVoiceInput.fork" ;
1515
1616const mocks = vi . hoisted ( ( ) => ( {
1717 request : vi . fn ( ) ,
1818 draft : "" ,
19+ stored : [ ] as string [ ] ,
1920 listeners : new Set < ( ) => void > ( ) ,
2021} ) ) ;
2122const environmentId = EnvironmentId . make ( "test" ) ;
@@ -63,6 +64,7 @@ vi.mock("./draft.fork", () => ({
6364 readVoiceDraftTextFork : ( ) => mocks . draft ,
6465 commitVoiceDraftFork : ( _target : unknown , text : string ) => {
6566 mocks . draft = text ;
67+ mocks . stored . push ( text ) ;
6668 mocks . listeners . forEach ( ( listener ) => listener ( ) ) ;
6769 } ,
6870} ) ) ;
@@ -74,8 +76,8 @@ vi.mock("./VoiceInputProvider.fork", () => ({
7476 } ) ) as never ,
7577} ) ) ;
7678
77- let root : Root | undefined ;
7879let actions : ReturnType < typeof useDesktopVoiceInputFork > ;
80+ let pendingAnswer : { key : string ; text : string ; commit : ( text : string ) => void } | undefined ;
7981function Probe ( ) {
8082 const value = useDesktopVoiceInputFork ( {
8183 environmentId,
@@ -90,87 +92,90 @@ function Probe() {
9092 mocks . draft = text ;
9193 actions . markDraftChanged ( ) ;
9294 } ,
95+ pendingAnswer,
9396 } ) ;
9497 useLayoutEffect ( ( ) => {
9598 actions = value ;
9699 } ) ;
97100 // The composer hides the normal controls when the dictation toolbar is presented.
98101 return value . presented ? value . toolbar : value . controls ;
99102}
100- let host : HTMLDivElement | undefined ;
103+ let screen : Screen | undefined ;
101104function container ( ) {
102- return host ! ;
105+ return screen ! . container ;
103106}
104107function button ( label : string ) {
105108 const match = container ( ) . querySelector < HTMLButtonElement > ( `button[aria-label="${ label } "]` ) ;
106109 if ( ! match ) throw new Error ( `No ${ label } button.` ) ;
107110 return match ;
108111}
109- afterEach ( async ( ) => {
110- await act ( ( ) => root ?. unmount ( ) ) ;
111- root = undefined ;
112- host ?. remove ( ) ;
113- host = undefined ;
114- runtime ?. session . dispose ( ) ;
115- vi . restoreAllMocks ( ) ;
116- vi . unstubAllGlobals ( ) ;
117- mocks . listeners . clear ( ) ;
118- } ) ;
112+ function mountRuntime ( transcript : string ) {
113+ const stack = new AsyncDisposableStack ( ) ;
114+ stack . defer ( ( ) => {
115+ mocks . listeners . clear ( ) ;
116+ pendingAnswer = undefined ;
117+ } ) ;
118+ stack . use ( vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ) ;
119+ mocks . draft = "" ;
120+ stack . use (
121+ vi . spyOn ( useComposerDraftStore , "subscribe" ) . mockImplementation ( ( listener ) => {
122+ const notify = ( ) =>
123+ listener ( useComposerDraftStore . getState ( ) , useComposerDraftStore . getState ( ) ) ;
124+ mocks . listeners . add ( notify ) ;
125+ return ( ) => mocks . listeners . delete ( notify ) ;
126+ } ) ,
127+ ) ;
128+ const recorded = Promise . withResolvers < void > ( ) ;
129+ const recorder = {
130+ uri : "memory:recording" ,
131+ prepareToRecordAsync : async ( ) => { } ,
132+ record : ( ) => recorded . resolve ( ) ,
133+ stop : async ( ) => { } ,
134+ readLevel : ( ) => 0 ,
135+ } ;
136+ runtime = {
137+ recorder,
138+ session : new DesktopVoiceSessionFork ( {
139+ recorder,
140+ requestPermission : async ( ) => ( { granted : true , canAskAgain : true } ) ,
141+ configureRecording : async ( ) => { } ,
142+ releaseRecording : async ( ) => { } ,
143+ deleteRecording : ( ) => { } ,
144+ getTranscriber : ( ) => ( {
145+ prepare : async ( ) => ( { locale : "en-US" , transcribe : async ( ) => transcript } ) ,
146+ } ) ,
147+ } ) ,
148+ } ;
149+ stack . defer ( ( ) => runtime . session . dispose ( ) ) ;
150+ return {
151+ recorded : recorded . promise ,
152+ async mount ( ) {
153+ screen = stack . use ( await render ( < Probe /> , { router : false } ) ) ;
154+ } ,
155+ async [ Symbol . asyncDispose ] ( ) {
156+ await stack . disposeAsync ( ) ;
157+ screen = undefined ;
158+ } ,
159+ } ;
160+ }
119161
120162describe ( "automatic cleanup in the active dictation toolbar" , ( ) => {
121163 it . each ( [ "button" , "Escape" ] ) (
122164 "cancels through %s and commits raw speech without formatting" ,
123165 async ( cancelWith ) => {
124- vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
125- vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
126- mocks . draft = "" ;
127- vi . spyOn ( useComposerDraftStore , "subscribe" ) . mockImplementation ( ( listener ) => {
128- const notify = ( ) =>
129- listener ( useComposerDraftStore . getState ( ) , useComposerDraftStore . getState ( ) ) ;
130- mocks . listeners . add ( notify ) ;
131- return ( ) => mocks . listeners . delete ( notify ) ;
132- } ) ;
133- const recorded = Promise . withResolvers < void > ( ) ;
166+ await using fixture = mountRuntime ( "raw use affect words" ) ;
134167 const cleanupStarted = Promise . withResolvers < AbortSignal > ( ) ;
135- const recorder = {
136- uri : "memory:recording" ,
137- prepareToRecordAsync : async ( ) => { } ,
138- record : ( ) => recorded . resolve ( ) ,
139- stop : async ( ) => { } ,
140- readLevel : ( ) => 0 ,
141- } ;
142- runtime = {
143- recorder,
144- session : new DesktopVoiceSessionFork ( {
145- recorder,
146- requestPermission : async ( ) => ( { granted : true , canAskAgain : true } ) ,
147- configureRecording : async ( ) => { } ,
148- releaseRecording : async ( ) => { } ,
149- deleteRecording : ( ) => { } ,
150- getTranscriber : ( ) => ( {
151- prepare : async ( ) => ( {
152- locale : "en-US" ,
153- transcribe : async ( ) => "raw use affect words" ,
154- } ) ,
155- } ) ,
156- } ) ,
157- } ;
158168 mocks . request . mockReset ( ) . mockImplementation (
159169 ( _input , signal : AbortSignal ) =>
160170 new Promise ( ( _resolve , reject ) => {
161171 cleanupStarted . resolve ( signal ) ;
162172 signal . addEventListener ( "abort" , ( ) => reject ( new Error ( "cancelled" ) ) , { once : true } ) ;
163173 } ) ,
164174 ) ;
165- await act ( ( ) => {
166- host = document . createElement ( "div" ) ;
167- document . body . append ( host ) ;
168- root = createRoot ( host ) ;
169- root . render ( < Probe /> ) ;
170- } ) ;
175+ await fixture . mount ( ) ;
171176 await act ( async ( ) => {
172177 button ( "Record dictation" ) . click ( ) ;
173- await recorded . promise ;
178+ await fixture . recorded ;
174179 } ) ;
175180 let finishing : Promise < void > ;
176181 await act ( async ( ) => {
@@ -183,7 +188,7 @@ describe("automatic cleanup in the active dictation toolbar", () => {
183188 status . textContent ?. includes ( "Cleaning up" ) ,
184189 ) ,
185190 ) . toBe ( true ) ;
186- const cancelDictation = vi . spyOn ( runtime . session . controller , "cancel" ) ;
191+ using cancelDictation = vi . spyOn ( runtime . session . controller , "cancel" ) ;
187192 await act ( async ( ) => {
188193 if ( cancelWith === "button" ) button ( "Cancel text processing" ) . click ( ) ;
189194 else
@@ -198,3 +203,23 @@ describe("automatic cleanup in the active dictation toolbar", () => {
198203 } ,
199204 ) ;
200205} ) ;
206+
207+ describe ( "dictating a pending question answer" , ( ) => {
208+ it ( "commits to the answer, never the hidden composer draft" , async ( ) => {
209+ await using fixture = mountRuntime ( "yes" ) ;
210+ mocks . stored = [ ] ;
211+ mocks . request . mockReset ( ) . mockRejectedValue ( new Error ( "offline" ) ) ;
212+ const commit = vi . fn ( ( text : string ) => {
213+ pendingAnswer = { key : "request:question" , text, commit } ;
214+ } ) ;
215+ pendingAnswer = { key : "request:question" , text : "" , commit } ;
216+ await fixture . mount ( ) ;
217+ await act ( async ( ) => {
218+ button ( "Record dictation" ) . click ( ) ;
219+ await fixture . recorded ;
220+ } ) ;
221+ await act ( ( ) => runtime . session . controller . stop ( ) ) ;
222+ expect ( commit ) . toHaveBeenCalledWith ( "yes" ) ;
223+ expect ( mocks . stored ) . toEqual ( [ ] ) ;
224+ } ) ;
225+ } ) ;
0 commit comments