@@ -87,3 +87,110 @@ describe("ReplexicaEngine", () => {
8787 } ) ;
8888 } ) ;
8989} ) ;
90+
91+
92+
93+ describe ( "LingoDotDevEngine Abort Handling" , ( ) => {
94+ describe ( "Abort Signal Propagation" , ( ) => {
95+ it ( "should throw an error when abort signal is triggered before localization starts" , async ( ) => {
96+ const engine = new LingoDotDevEngine ( { apiKey : "test" } ) ;
97+ const abortController = new AbortController ( ) ;
98+
99+ // Abort immediately
100+ abortController . abort ( ) ;
101+
102+ await expect (
103+ engine . localizeText ( "Test text" , {
104+ sourceLocale : "en" ,
105+ targetLocale : "es"
106+ } , undefined , abortController . signal )
107+ ) . rejects . toThrow ( "Operation was aborted" ) ;
108+ } ) ;
109+
110+ it ( "should throw an error when abort signal is triggered during HTML localization" , async ( ) => {
111+ const engine = new LingoDotDevEngine ( { apiKey : "test" } ) ;
112+ const abortController = new AbortController ( ) ;
113+
114+ // Mock _localizeRaw to simulate a long-running operation
115+ const mockLocalizeRaw = vi . spyOn ( engine as any , "_localizeRaw" ) ;
116+ mockLocalizeRaw . mockImplementation ( async ( ) => {
117+ // Simulate an asynchronous operation
118+ await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
119+ // Abort during the operation
120+ abortController . abort ( ) ;
121+ return { } ;
122+ } ) ;
123+
124+ await expect (
125+ engine . localizeHtml ( "<html><body>Test</body></html>" , {
126+ sourceLocale : "en" ,
127+ targetLocale : "es"
128+ } , undefined , abortController . signal )
129+ ) . rejects . toThrow ( "Operation was aborted" ) ;
130+ } ) ;
131+
132+ it ( "should propagate abort signal through nested method calls" , async ( ) => {
133+ const engine = new LingoDotDevEngine ( { apiKey : "test" } ) ;
134+ const abortController = new AbortController ( ) ;
135+
136+ // Spy on internal methods to ensure abort signal is passed
137+ const spyLocalizeChunk = vi . spyOn ( engine as any , "localizeChunk" ) ;
138+ const spyCheckAbortSignal = vi . spyOn ( engine as any , "checkAbortSignal" ) ;
139+
140+ // Abort during object localization
141+ abortController . abort ( ) ;
142+
143+ await expect (
144+ engine . localizeObject ( { text : "Test" } , {
145+ sourceLocale : "en" ,
146+ targetLocale : "es"
147+ } , undefined , abortController . signal )
148+ ) . rejects . toThrow ( "Operation was aborted" ) ;
149+
150+ // Verify abort signal was checked in multiple places
151+ expect ( spyCheckAbortSignal ) . toHaveBeenCalledWith ( abortController . signal ) ;
152+ } ) ;
153+
154+ it ( "should handle multiple concurrent abort scenarios" , async ( ) => {
155+ const engine = new LingoDotDevEngine ( { apiKey : "test" } ) ;
156+
157+ const abortControllers = [
158+ new AbortController ( ) ,
159+ new AbortController ( ) ,
160+ new AbortController ( )
161+ ] ;
162+
163+ const localizationPromises = abortControllers . map ( ( controller , index ) => {
164+ // Stagger abort times
165+ setTimeout ( ( ) => controller . abort ( ) , index * 50 ) ;
166+
167+ return engine . localizeText ( `Test ${ index } ` , {
168+ sourceLocale : "en" ,
169+ targetLocale : "es"
170+ } , undefined , controller . signal ) . catch ( err => err ) ;
171+ } ) ;
172+
173+ const results = await Promise . all ( localizationPromises ) ;
174+
175+ // All promises should result in abort errors
176+ results . forEach ( result => {
177+ expect ( result . message ) . toBe ( "Operation was aborted" ) ;
178+ } ) ;
179+ } ) ;
180+
181+ it ( "should allow localization when no abort signal is provided" , async ( ) => {
182+ const engine = new LingoDotDevEngine ( { apiKey : "test" } ) ;
183+
184+ // Mock _localizeRaw to return a predictable result
185+ const mockLocalizeRaw = vi . spyOn ( engine as any , "_localizeRaw" ) ;
186+ mockLocalizeRaw . mockResolvedValue ( { text : "Localized Text" } ) ;
187+
188+ const result = await engine . localizeText ( "Test text" , {
189+ sourceLocale : "en" ,
190+ targetLocale : "es"
191+ } ) ;
192+
193+ expect ( result ) . toBe ( "Localized Text" ) ;
194+ } ) ;
195+ } ) ;
196+ } ) ;
0 commit comments