11import { expect } from "chai" ;
22import { RefObject } from "react" ;
33import { RuntimeLang } from "./languages" ;
4- import { emptyMutex , ReplOutput , RuntimeContext , UpdatedFile } from "./interface" ;
4+ import { ReplOutput , RuntimeContext , UpdatedFile } from "./interface" ;
55
66export function defineTests (
77 lang : RuntimeLang ,
88 runtimeRef : RefObject < Record < RuntimeLang , RuntimeContext > >
99) {
1010 describe ( `${ lang } Runtime` , function ( ) {
11- this . timeout (
12- (
13- {
14- python : 2000 ,
15- ruby : 5000 ,
16- cpp : 10000 ,
17- javascript : 2000 ,
18- } as Record < RuntimeLang , number >
19- ) [ lang ]
20- ) ;
11+ const timeout = (
12+ {
13+ python : 2000 ,
14+ ruby : 5000 ,
15+ javascript : 2000 ,
16+ typescript : 2000 ,
17+ cpp : 10000 ,
18+ rust : 10000 ,
19+ } as Record < RuntimeLang , number >
20+ ) [ lang ] ;
21+
22+ if ( typeof this !== "undefined" && "timeout" in this ) {
23+ ( this as any ) . timeout ( timeout ) ;
24+ }
2125
2226 beforeEach ( async function ( ) {
23- this . timeout ( 60000 ) ;
24- while ( ! runtimeRef . current [ lang ] . ready ) {
27+ if ( typeof this !== "undefined" && "timeout" in this ) {
28+ ( this as any ) . timeout ( 60000 ) ;
29+ }
30+
31+ while ( true ) {
32+ const runtime = runtimeRef . current [ lang ] ;
33+ if ( runtime ?. ready ) {
34+ const isLocked = runtime . mutex ?. isLocked ( ) ;
35+ if ( ! isLocked ) {
36+ break ;
37+ }
38+ }
2539 await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
2640 }
41+ await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) ) ;
2742 } ) ;
2843
2944 describe ( "REPL" , function ( ) {
@@ -40,7 +55,10 @@ export function defineTests(
4055 } satisfies Record < RuntimeLang , string | null >
4156 ) [ lang ] ;
4257 if ( ! printCode ) {
43- this . skip ( ) ;
58+ if ( typeof this !== "undefined" && "skip" in this ) {
59+ return ( this as any ) . skip ( ) ;
60+ }
61+ return ;
4462 }
4563 const outputs : ReplOutput [ ] = [ ] ;
4664 await ( runtimeRef . current [ lang ] . mutex || emptyMutex ) . runExclusive ( ( ) =>
@@ -69,7 +87,10 @@ export function defineTests(
6987 } satisfies Record < RuntimeLang , string [ ] | null [ ] >
7088 ) [ lang ] ;
7189 if ( ! setIntVarCode || ! printIntVarCode ) {
72- this . skip ( ) ;
90+ if ( typeof this !== "undefined" && "skip" in this ) {
91+ return ( this as any ) . skip ( ) ;
92+ }
93+ return ;
7394 }
7495 const outputs : ReplOutput [ ] = [ ] ;
7596 await ( runtimeRef . current [ lang ] . mutex || emptyMutex ) . runExclusive (
@@ -103,7 +124,10 @@ export function defineTests(
103124 } satisfies Record < RuntimeLang , string | null >
104125 ) [ lang ] ;
105126 if ( ! errorCode ) {
106- this . skip ( ) ;
127+ if ( typeof this !== "undefined" && "skip" in this ) {
128+ return ( this as any ) . skip ( ) ;
129+ }
130+ return ;
107131 }
108132 const outputs : ReplOutput [ ] = [ ] ;
109133 await ( runtimeRef . current [ lang ] . mutex || emptyMutex ) . runExclusive ( ( ) =>
@@ -133,7 +157,10 @@ export function defineTests(
133157 } satisfies Record < RuntimeLang , ( string | null ) [ ] >
134158 ) [ lang ] ;
135159 if ( ! setIntVarCode || ! infLoopCode || ! printIntVarCode ) {
136- this . skip ( ) ;
160+ if ( typeof this !== "undefined" && "skip" in this ) {
161+ return ( this as any ) . skip ( ) ;
162+ }
163+ return ;
137164 }
138165 const runPromise = (
139166 runtimeRef . current [ lang ] . mutex || emptyMutex
@@ -146,9 +173,18 @@ export function defineTests(
146173 runtimeRef . current [ lang ] . interrupt ! ( ) ;
147174 await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
148175 await runPromise ;
149- while ( ! runtimeRef . current [ lang ] . ready ) {
176+
177+ // Wait for it to become ready again
178+ const start = Date . now ( ) ;
179+ while ( true ) {
180+ const runtime = runtimeRef . current [ lang ] ;
181+ if ( runtime ?. ready && ! runtime . mutex ?. isLocked ( ) ) {
182+ break ;
183+ }
184+ if ( Date . now ( ) - start > 30000 ) break ;
150185 await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
151186 }
187+
152188 const outputs : ReplOutput [ ] = [ ] ;
153189 await ( runtimeRef . current [ lang ] . mutex || emptyMutex ) . runExclusive ( ( ) =>
154190 runtimeRef . current [ lang ] . runCommand ! ( printIntVarCode , ( output ) => {
@@ -173,7 +209,10 @@ export function defineTests(
173209 } satisfies Record < RuntimeLang , string | null >
174210 ) [ lang ] ;
175211 if ( ! writeCode ) {
176- this . skip ( ) ;
212+ if ( typeof this !== "undefined" && "skip" in this ) {
213+ return ( this as any ) . skip ( ) ;
214+ }
215+ return ;
177216 }
178217 const updatedFiles : UpdatedFile [ ] = [ ] ;
179218 await ( runtimeRef . current [ lang ] . mutex || emptyMutex ) . runExclusive ( ( ) =>
@@ -206,7 +245,10 @@ export function defineTests(
206245 } satisfies Record < RuntimeLang , [ string , string ] | [ null , null ] >
207246 ) [ lang ] ;
208247 if ( ! filename || ! code ) {
209- this . skip ( ) ;
248+ if ( typeof this !== "undefined" && "skip" in this ) {
249+ return ( this as any ) . skip ( ) ;
250+ }
251+ return ;
210252 }
211253 const outputs : ReplOutput [ ] = [ ] ;
212254 await runtimeRef . current [ lang ] . runFiles (
@@ -242,7 +284,10 @@ export function defineTests(
242284 } satisfies Record < RuntimeLang , [ string , string ] | [ null , null ] >
243285 ) [ lang ] ;
244286 if ( ! filename || ! code ) {
245- this . skip ( ) ;
287+ if ( typeof this !== "undefined" && "skip" in this ) {
288+ return ( this as any ) . skip ( ) ;
289+ }
290+ return ;
246291 }
247292 const outputs : ReplOutput [ ] = [ ] ;
248293 await runtimeRef . current [ lang ] . runFiles (
@@ -305,7 +350,10 @@ export function defineTests(
305350 >
306351 ) [ lang ] ;
307352 if ( ! codes || ! execFiles ) {
308- this . skip ( ) ;
353+ if ( typeof this !== "undefined" && "skip" in this ) {
354+ return ( this as any ) . skip ( ) ;
355+ }
356+ return ;
309357 }
310358 const outputs : ReplOutput [ ] = [ ] ;
311359 await runtimeRef . current [ lang ] . runFiles ( execFiles , codes , ( output ) => {
@@ -335,7 +383,10 @@ export function defineTests(
335383 } satisfies Record < RuntimeLang , [ string , string ] | [ null , null ] >
336384 ) [ lang ] ;
337385 if ( ! filename || ! code ) {
338- this . skip ( ) ;
386+ if ( typeof this !== "undefined" && "skip" in this ) {
387+ return ( this as any ) . skip ( ) ;
388+ }
389+ return ;
339390 }
340391 const updatedFiles : UpdatedFile [ ] = [ ] ;
341392 await runtimeRef . current [ lang ] . runFiles (
0 commit comments