1- import { act , useLayoutEffect } from "react" ;
2- import { create , type ReactTestRenderer } from "react-test-renderer" ;
3- import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
1+ import { useLayoutEffect } from "react" ;
2+ import { describe , expect , it , vi } from "vite-plus/test" ;
43import {
54 EnvironmentId ,
65 ProjectId ,
76 ProviderInstanceId ,
87 VOICE_TEXT_STARTER_PROMPTS_FORK ,
98 type VoiceTextConfigFork ,
109} from "@t3tools/contracts" ;
10+ import * as Cause from "effect/Cause" ;
11+ import { AsyncResult } from "effect/reactivity" ;
12+ import { act , render , type Screen } from "../../test/render" ;
1113import { useVoiceTextFork , type VoiceTextInputFork } from "./useVoiceText.fork" ;
1214
1315const mocks = vi . hoisted ( ( ) => ( {
@@ -25,18 +27,26 @@ vi.mock("../state/session", () => ({
2527vi . mock ( "@t3tools/client-runtime/rpc" , ( ) => ( {
2628 transformVoiceTextFork : ( _prepared : unknown , input : unknown ) => input ,
2729} ) ) ;
28- vi . mock ( "./client.fork" , ( ) => ( {
29- runVoiceInputRequestFork : ( input : unknown , signal : AbortSignal ) => mocks . request ( input , signal ) ,
30+ vi . mock ( "@t3tools/client-runtime/state/runtime" , async ( importOriginal ) => ( {
31+ ...( await importOriginal < object > ( ) ) ,
32+ executeAtomQuery : async (
33+ _registry : unknown ,
34+ input : unknown ,
35+ options : { signal ?: AbortSignal } ,
36+ ) => {
37+ try {
38+ return AsyncResult . success ( await mocks . request ( input , options . signal ) ) ;
39+ } catch ( error ) {
40+ return AsyncResult . failure ( Cause . fail ( error ) ) ;
41+ }
42+ } ,
3043} ) ) ;
31- vi . mock ( "../components/ui/button" , ( ) => ( { Button : "button" } ) ) ;
32- vi . mock ( "../components/ui/spinner" , ( ) => ( { Spinner : "span" } ) ) ;
33- vi . mock ( "../components/ui/tooltip" , ( ) => ( {
34- Tooltip : "div" ,
35- TooltipTrigger : "span" ,
36- TooltipPopup : "span" ,
44+ vi . mock ( "../connection/runtime" , ( ) => ( {
45+ connectionAtomRuntime : { atom : ( input : unknown ) => input } ,
3746} ) ) ;
47+ vi . mock ( "../rpc/atomRegistry" , ( ) => ( { appAtomRegistry : { } } ) ) ;
3848
39- let renderer : ReactTestRenderer ;
49+ let screen : Screen ;
4050let actions : ReturnType < typeof useVoiceTextFork > ;
4151let draft : {
4252 text : string ;
@@ -52,15 +62,17 @@ function Probe() {
5262 } ) ;
5363 return value . controls ;
5464}
65+ function errorText ( ) {
66+ return screen . container . textContent ?? "" ;
67+ }
5568async function click ( label : string ) {
56- await act ( async ( ) => {
57- await renderer . root . findByProps ( { "aria- label" : label } ) . props . onClick ( ) ;
58- } ) ;
69+ const button = screen . container . querySelector < HTMLButtonElement > ( `button[aria-label=" ${ label } "]` ) ;
70+ if ( ! button ) throw new Error ( `No ${ label } button.` ) ;
71+ await act ( async ( ) => button . click ( ) ) ;
5972}
60- beforeEach ( async ( ) => {
61- vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
62- vi . stubGlobal ( "navigator" , { language : "en-US" } ) ;
63- vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
73+ async function setup ( ) {
74+ await using stack = new AsyncDisposableStack ( ) ;
75+ stack . use ( vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ) ;
6476 mocks . canOperate = true ;
6577 mocks . config = {
6678 modelSelection : { instanceId : ProviderInstanceId . make ( "codex" ) , model : "test" } ,
@@ -88,17 +100,12 @@ beforeEach(async () => {
88100 draft = { ...draft , text, selection : { start : cursor , end : cursor } } ;
89101 } ,
90102 } ;
91- await act ( ( ) => {
92- renderer = create ( < Probe /> ) ;
93- } ) ;
94- } ) ;
95- afterEach ( async ( ) => {
96- await act ( ( ) => renderer . unmount ( ) ) ;
97- vi . restoreAllMocks ( ) ;
98- vi . unstubAllGlobals ( ) ;
99- } ) ;
103+ screen = stack . use ( await render ( < Probe /> , { router : false } ) ) ;
104+ return stack . move ( ) ;
105+ }
100106describe ( "dictation text lifecycle" , ( ) => {
101107 it ( "cleans only the transcript, formats the committed draft, and undoes both to raw transcription" , async ( ) => {
108+ await using _fixture = await setup ( ) ;
102109 let transcript = "" ;
103110 await act ( async ( ) => {
104111 transcript = await actions . processTranscript (
@@ -120,15 +127,17 @@ describe("dictation text lifecycle", () => {
120127 expect ( draft . text ) . toBe ( "react use affect" ) ;
121128 } ) ;
122129 it ( "does not automatically process typed edits" , async ( ) => {
123- await act ( ( ) => {
130+ await using _fixture = await setup ( ) ;
131+ await act ( async ( ) => {
124132 draft . text = "typed use affect words" ;
125133 draft . revision ++ ;
126134 actions . markDraftChanged ( ) ;
127- renderer . update ( < Probe /> ) ;
135+ await screen . rerender ( < Probe /> ) ;
128136 } ) ;
129137 expect ( mocks . request ) . not . toHaveBeenCalled ( ) ;
130138 } ) ;
131139 it ( "uses the recording snapshot for Undo when editor and session revisions differ" , async ( ) => {
140+ await using _fixture = await setup ( ) ;
132141 draft = { ...draft , text : "prefix suffix" , revision : 8 , selection : { start : 13 , end : 13 } } ;
133142 let transcript = "" ;
134143 await act ( async ( ) => {
@@ -151,6 +160,7 @@ describe("dictation text lifecycle", () => {
151160 expect ( draft . text ) . toBe ( "prefix react use affect" ) ;
152161 } ) ;
153162 it ( "ignores a manual result after the draft was edited and reverted" , async ( ) => {
163+ await using _fixture = await setup ( ) ;
154164 const response = Promise . withResolvers < { text : string } > ( ) ;
155165 mocks . request . mockReturnValueOnce ( response . promise ) ;
156166 draft . text = "use affect" ;
@@ -166,6 +176,7 @@ describe("dictation text lifecycle", () => {
166176 expect ( draft . text ) . toBe ( "use affect" ) ;
167177 } ) ;
168178 it ( "cancels cleanup without formatting or discarding the raw transcript" , async ( ) => {
179+ await using _fixture = await setup ( ) ;
169180 mocks . request . mockImplementationOnce (
170181 ( _input , signal : AbortSignal ) =>
171182 new Promise ( ( _resolve , reject ) =>
@@ -185,24 +196,57 @@ describe("dictation text lifecycle", () => {
185196 expect ( draft . text ) . toBe ( "react use affect" ) ;
186197 expect ( mocks . request ) . toHaveBeenCalledTimes ( 1 ) ;
187198 } ) ;
188- it ( "preserves the transcript on cleanup failure" , async ( ) => {
189- mocks . request . mockRejectedValueOnce ( new Error ( "model failed" ) ) ;
199+ it . each ( [
200+ { detail : "This model is unavailable." , label : "Clean up draft" } ,
201+ { detail : "Provider refused: Bearer [REDACTED] api_key=[REDACTED]" , label : "Format draft" } ,
202+ ] ) ( "preserves the draft and shows provider detail for $label" , async ( { detail, label } ) => {
203+ await using _fixture = await setup ( ) ;
204+ mocks . request . mockRejectedValueOnce ( {
205+ error : "Text generation failed." ,
206+ code : "model" ,
207+ detail,
208+ } ) ;
209+ draft . text = "raw use affect" ;
210+ const original = structuredClone ( draft ) ;
211+ await click ( label ) ;
212+ expect ( draft ) . toEqual ( original ) ;
213+ expect ( errorText ( ) ) . toContain ( `${ detail } Your draft was preserved.` ) ;
214+ } ) ;
215+ it ( "keeps the current text when provider detail is absent" , async ( ) => {
216+ await using _fixture = await setup ( ) ;
217+ mocks . request . mockRejectedValueOnce ( { error : "Text generation failed." , code : "model" } ) ;
218+ draft . text = "raw use affect" ;
219+ await click ( "Clean up draft" ) ;
220+ expect ( draft . text ) . toBe ( "raw use affect" ) ;
221+ expect ( errorText ( ) ) . toContain ( "Text processing failed. Your draft was preserved." ) ;
222+ } ) ;
223+ it ( "preserves the transcript and shows detail on automatic cleanup failure" , async ( ) => {
224+ await using _fixture = await setup ( ) ;
225+ mocks . request . mockRejectedValueOnce ( {
226+ error : "Text generation failed." ,
227+ code : "model" ,
228+ detail : "This model is unavailable." ,
229+ } ) ;
190230 await act ( async ( ) => {
191231 expect ( await actions . processTranscript ( "raw use affect" , new AbortController ( ) . signal ) ) . toBe (
192232 "raw use affect" ,
193233 ) ;
194234 } ) ;
235+ expect ( errorText ( ) ) . toContain (
236+ "This model is unavailable. The original transcript was preserved." ,
237+ ) ;
195238 } ) ;
196239 it ( "does not submit cleanup without operate permission or for empty speech" , async ( ) => {
240+ await using _fixture = await setup ( ) ;
197241 mocks . canOperate = false ;
198- await act ( ( ) => renderer . update ( < Probe /> ) ) ;
242+ await screen . rerender ( < Probe /> ) ;
199243 await act ( async ( ) => {
200244 expect ( await actions . processTranscript ( "raw use affect" , new AbortController ( ) . signal ) ) . toBe (
201245 "raw use affect" ,
202246 ) ;
203247 } ) ;
204248 mocks . canOperate = true ;
205- await act ( ( ) => renderer . update ( < Probe /> ) ) ;
249+ await screen . rerender ( < Probe /> ) ;
206250 await act ( async ( ) => {
207251 expect ( await actions . processTranscript ( " " , new AbortController ( ) . signal ) ) . toBe ( " " ) ;
208252 } ) ;
0 commit comments