@@ -8,10 +8,13 @@ process.env.DATA_DIR = mkdtempSync(join(tmpdir(), 'playground-test-'))
88const {
99 createSession,
1010 getSession,
11+ waitForBrowserSyncResult,
12+ waitForErrorReport,
1113 updateShader,
1214 setScreenshot,
1315 setErrors,
1416 setStructuredErrors,
17+ recordErrorReport,
1518 setUniformValues,
1619 updateMetadata,
1720} = await import ( './playground-db.ts' )
@@ -172,6 +175,130 @@ runTest('setStructuredErrors stores structured errors', () => {
172175 assert . deepEqual ( session . structuredErrors , errors )
173176} )
174177
178+ runTest ( 'waitForErrorReport resolves when browser posts errors' , async ( ) => {
179+ const { id } = createSession ( )
180+ const wait = waitForErrorReport ( id , 1_000 )
181+
182+ setTimeout ( ( ) => {
183+ recordErrorReport ( id , {
184+ errors : [ 'shader failed' ] ,
185+ structuredErrors : [ ] ,
186+ } )
187+ } , 20 )
188+
189+ const report = await wait
190+ assert . deepEqual ( report , {
191+ errors : [ 'shader failed' ] ,
192+ structuredErrors : [ ] ,
193+ } )
194+ } )
195+
196+ runTest ( 'waitForBrowserSyncResult resolves early on compilation errors' , async ( ) => {
197+ const { id } = createSession ( )
198+ const startedAt = Date . now ( )
199+ const wait = waitForBrowserSyncResult ( id , 1_000 )
200+
201+ setTimeout ( ( ) => {
202+ recordErrorReport ( id , {
203+ errors : [ 'compile failed' ] ,
204+ structuredErrors : [ ] ,
205+ } )
206+ } , 20 )
207+
208+ const result = await wait
209+ assert . equal ( Date . now ( ) - startedAt < 500 , true )
210+ assert . equal ( result . screenshotBase64 , null )
211+ assert . deepEqual ( result . errorReport , {
212+ errors : [ 'compile failed' ] ,
213+ structuredErrors : [ ] ,
214+ } )
215+ } )
216+
217+ runTest ( 'waitForBrowserSyncResult waits for screenshot after a successful empty error report' , async ( ) => {
218+ const { id } = createSession ( )
219+ const startedAt = Date . now ( )
220+ const wait = waitForBrowserSyncResult ( id , 1_000 )
221+
222+ setTimeout ( ( ) => {
223+ recordErrorReport ( id , {
224+ errors : [ ] ,
225+ structuredErrors : [ ] ,
226+ } )
227+ } , 20 )
228+
229+ setTimeout ( ( ) => {
230+ setScreenshot ( id , 'data:image/png;base64,ok' )
231+ } , 60 )
232+
233+ const result = await wait
234+ const elapsedMs = Date . now ( ) - startedAt
235+
236+ assert . equal ( elapsedMs >= 40 , true )
237+ assert . equal ( elapsedMs < 500 , true )
238+ assert . equal ( result . screenshotBase64 , 'data:image/png;base64,ok' )
239+ assert . deepEqual ( result . errorReport , {
240+ errors : [ ] ,
241+ structuredErrors : [ ] ,
242+ } )
243+ } )
244+
245+ runTest ( 'waitForBrowserSyncResult waits for the error-clear report after screenshot success' , async ( ) => {
246+ const { id } = createSession ( )
247+ const startedAt = Date . now ( )
248+ const wait = waitForBrowserSyncResult ( id , 1_000 )
249+
250+ setTimeout ( ( ) => {
251+ setScreenshot ( id , 'data:image/png;base64,ok' )
252+ } , 20 )
253+
254+ setTimeout ( ( ) => {
255+ recordErrorReport ( id , {
256+ errors : [ ] ,
257+ structuredErrors : [ ] ,
258+ } )
259+ } , 60 )
260+
261+ const result = await wait
262+ const elapsedMs = Date . now ( ) - startedAt
263+
264+ assert . equal ( elapsedMs >= 40 , true )
265+ assert . equal ( elapsedMs < 500 , true )
266+ assert . equal ( result . screenshotBase64 , 'data:image/png;base64,ok' )
267+ assert . deepEqual ( result . errorReport , {
268+ errors : [ ] ,
269+ structuredErrors : [ ] ,
270+ } )
271+ } )
272+
273+ runTest ( 'waitForBrowserSyncResult returns nulls when the browser never responds' , async ( ) => {
274+ const { id } = createSession ( )
275+ const result = await waitForBrowserSyncResult ( id , 25 )
276+
277+ assert . deepEqual ( result , {
278+ screenshotBase64 : null ,
279+ errorReport : null ,
280+ } )
281+ } )
282+
283+ runTest ( 'waitForBrowserSyncResult treats structured errors as compilation failures' , async ( ) => {
284+ const { id } = createSession ( { language : 'tsl' , tslSource : 'export function createMaterial() {}' } )
285+ const wait = waitForBrowserSyncResult ( id , 1_000 )
286+
287+ setTimeout ( ( ) => {
288+ recordErrorReport ( id , {
289+ errors : [ ] ,
290+ structuredErrors : [ { kind : 'tsl-runtime' , message : 'material build failed' } ] ,
291+ } )
292+ } , 20 )
293+
294+ const result = await wait
295+ assert . equal ( result . screenshotBase64 , null )
296+ assert . deepEqual ( result . errorReport , {
297+ errors : [ ] ,
298+ structuredErrors : [ { kind : 'tsl-runtime' , message : 'material build failed' } ] ,
299+ } )
300+ } )
301+
175302runTest ( 'setUniformValues stores values' , ( ) => {
176303 const { id } = createSession ( )
177304 const values = { uTime : 1.5 , uColor : [ 1 , 0 , 0 ] }
0 commit comments