11import { expect } from "chai" ;
22import { RefObject } from "react" ;
3- import { emptyMutex , RuntimeContext , RuntimeLang } from "./runtime" ;
3+ import { emptyMutex , RuntimeContext , RuntimeLang , UpdatedFile } from "./runtime" ;
44import { ReplOutput } from "./repl" ;
55
66export function defineTests (
77 lang : RuntimeLang ,
8- runtimeRef : RefObject < Record < RuntimeLang , RuntimeContext > > ,
9- filesRef : RefObject < Readonly < Record < string , string > > >
8+ runtimeRef : RefObject < Record < RuntimeLang , RuntimeContext > >
109) {
1110 describe ( `${ lang } Runtime` , function ( ) {
1211 this . timeout (
@@ -46,7 +45,7 @@ export function defineTests(
4645 const outputs : ReplOutput [ ] = [ ] ;
4746 await ( runtimeRef . current [ lang ] . mutex || emptyMutex ) . runExclusive ( ( ) =>
4847 runtimeRef . current [ lang ] . runCommand ! ( printCode , ( output ) => {
49- outputs . push ( output ) ;
48+ if ( output . type !== "file" ) outputs . push ( output ) ;
5049 } )
5150 ) ;
5251 console . log ( `${ lang } REPL stdout test: ` , outputs ) ;
@@ -79,7 +78,7 @@ export function defineTests(
7978 await runtimeRef . current [ lang ] . runCommand ! (
8079 printIntVarCode ,
8180 ( output ) => {
82- outputs . push ( output ) ;
81+ if ( output . type !== "file" ) outputs . push ( output ) ;
8382 }
8483 ) ;
8584 }
@@ -109,7 +108,7 @@ export function defineTests(
109108 const outputs : ReplOutput [ ] = [ ] ;
110109 await ( runtimeRef . current [ lang ] . mutex || emptyMutex ) . runExclusive ( ( ) =>
111110 runtimeRef . current [ lang ] . runCommand ! ( errorCode , ( output ) => {
112- outputs . push ( output ) ;
111+ if ( output . type !== "file" ) outputs . push ( output ) ;
113112 } )
114113 ) ;
115114 console . log ( `${ lang } REPL error capture test: ` , outputs ) ;
@@ -153,7 +152,7 @@ export function defineTests(
153152 const outputs : ReplOutput [ ] = [ ] ;
154153 await ( runtimeRef . current [ lang ] . mutex || emptyMutex ) . runExclusive ( ( ) =>
155154 runtimeRef . current [ lang ] . runCommand ! ( printIntVarCode , ( output ) => {
156- outputs . push ( output ) ;
155+ if ( output . type !== "file" ) outputs . push ( output ) ;
157156 } )
158157 ) ;
159158 console . log ( `${ lang } REPL interrupt recovery test: ` , outputs ) ;
@@ -176,12 +175,17 @@ export function defineTests(
176175 if ( ! writeCode ) {
177176 this . skip ( ) ;
178177 }
178+ const updatedFiles : UpdatedFile [ ] = [ ] ;
179179 await ( runtimeRef . current [ lang ] . mutex || emptyMutex ) . runExclusive ( ( ) =>
180- runtimeRef . current [ lang ] . runCommand ! ( writeCode , ( ) => { } )
180+ runtimeRef . current [ lang ] . runCommand ! ( writeCode , ( output ) => {
181+ if ( output . type === "file" ) {
182+ updatedFiles . push ( output ) ;
183+ }
184+ } )
181185 ) ;
182- // wait for files to be updated
183- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
184- expect ( filesRef . current [ targetFile ] ) . to . equal ( msg ) ;
186+ expect (
187+ updatedFiles . find ( ( f ) => f . filename === targetFile ) ?. content
188+ ) . to . equal ( msg ) ;
185189 } ) ;
186190 } ) ;
187191
@@ -211,7 +215,7 @@ export function defineTests(
211215 [ filename ] : code ,
212216 } ,
213217 ( output ) => {
214- outputs . push ( output ) ;
218+ if ( output . type !== "file" ) outputs . push ( output ) ;
215219 }
216220 ) ;
217221 console . log ( `${ lang } single file stdout test: ` , outputs ) ;
@@ -247,7 +251,7 @@ export function defineTests(
247251 [ filename ] : code ,
248252 } ,
249253 ( output ) => {
250- outputs . push ( output ) ;
254+ if ( output . type !== "file" ) outputs . push ( output ) ;
251255 }
252256 ) ;
253257 console . log ( `${ lang } single file error capture test: ` , outputs ) ;
@@ -305,7 +309,7 @@ export function defineTests(
305309 }
306310 const outputs : ReplOutput [ ] = [ ] ;
307311 await runtimeRef . current [ lang ] . runFiles ( execFiles , codes , ( output ) => {
308- outputs . push ( output ) ;
312+ if ( output . type !== "file" ) outputs . push ( output ) ;
309313 } ) ;
310314 console . log ( `${ lang } multifile stdout test: ` , outputs ) ;
311315 expect ( outputs ) . to . be . deep . include ( { type : "stdout" , message : msg } ) ;
@@ -333,16 +337,21 @@ export function defineTests(
333337 if ( ! filename || ! code ) {
334338 this . skip ( ) ;
335339 }
340+ const updatedFiles : UpdatedFile [ ] = [ ] ;
336341 await runtimeRef . current [ lang ] . runFiles (
337342 [ filename ] ,
338343 {
339344 [ filename ] : code ,
340345 } ,
341- ( ) => { }
346+ ( output ) => {
347+ if ( output . type === "file" ) {
348+ updatedFiles . push ( output ) ;
349+ }
350+ }
342351 ) ;
343- // wait for files to be updated
344- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
345- expect ( filesRef . current [ targetFile ] ) . to . equal ( msg ) ;
352+ expect (
353+ updatedFiles . find ( ( f ) => f . filename === targetFile ) ?. content
354+ ) . to . equal ( msg ) ;
346355 } ) ;
347356 } ) ;
348357 } ) ;
0 commit comments