@@ -17,6 +17,7 @@ import { describe, expect, it, vi } from 'vitest'
1717import type {
1818 SessionCommand ,
1919 SessionEvent ,
20+ SessionSnapshot ,
2021} from '../../../forge/modules/shared/protocol'
2122import { createSite } from '../../init/create-site'
2223import * as managers from '../../init/package-manager'
@@ -29,10 +30,27 @@ class BrowserPeer implements DevWebSocketConnection {
2930 readonly events : SessionEvent [ ] = [ ]
3031 private readonly listeners = new Set < ( message : DevWebSocketMessage ) => void > ( )
3132 private readonly closed = new Set < ( ) => void > ( )
33+ private readonly snapshots = new Set < ( snapshot : SessionSnapshot ) => void > ( )
3234 send ( message : DevWebSocketMessage ) {
33- this . events . push ( JSON . parse ( String ( message ) ) )
35+ const event : SessionEvent = JSON . parse ( String ( message ) )
36+ this . events . push ( event )
37+ if ( event . type === 'snapshot' )
38+ for ( const listener of this . snapshots ) listener ( event . snapshot )
3439 return true
3540 }
41+ // Creation touches real files; await its event under the normal test timeout.
42+ waitForCreation ( ) {
43+ const completion = Promise . withResolvers < SessionSnapshot > ( )
44+ const inspect = ( snapshot : SessionSnapshot ) => {
45+ if ( snapshot . status !== 'ready' && snapshot . status !== 'failed' ) return
46+ this . snapshots . delete ( inspect )
47+ completion . resolve ( snapshot )
48+ }
49+ this . snapshots . add ( inspect )
50+ const latest = this . events . findLast ( ( event ) => event . type === 'snapshot' )
51+ if ( latest ?. type === 'snapshot' ) inspect ( latest . snapshot )
52+ return completion . promise
53+ }
3654 request ( command : SessionCommand ) {
3755 for ( const listener of this . listeners ) listener ( JSON . stringify ( command ) )
3856 }
@@ -138,7 +156,9 @@ describe('Forge session', () => {
138156 install : true ,
139157 } ,
140158 } )
141- await expect . poll ( ( ) => test . session . snapshot . status ) . toBe ( 'ready' )
159+ expect ( await test . peer . waitForCreation ( ) ) . toMatchObject ( {
160+ status : 'ready' ,
161+ } )
142162 const tasks = test . session . snapshot . tasks
143163 const labels = tasks . map ( ( task ) => task . label )
144164 expect ( labels . indexOf ( 'Format project files' ) ) . toBeGreaterThan (
@@ -181,7 +201,9 @@ describe('Forge session', () => {
181201 test . session . connect ( reconnected )
182202 expect ( reconnected . events [ 0 ] . type ) . toBe ( 'snapshot' )
183203 test . gate . resolve ( )
184- await expect . poll ( ( ) => test . session . snapshot . status ) . toBe ( 'ready' )
204+ expect ( await reconnected . waitForCreation ( ) ) . toMatchObject ( {
205+ status : 'ready' ,
206+ } )
185207 const directory = path . join ( test . cwd , 'my-site' )
186208 const config = JSON . parse (
187209 await readFile ( path . join ( directory , 'content/siteConfig.json' ) , 'utf8' ) ,
@@ -220,7 +242,9 @@ describe('Forge session', () => {
220242 type : 'create' ,
221243 configuration : structuredClone ( test . session . snapshot . configuration ) ,
222244 } )
223- await expect . poll ( ( ) => test . session . snapshot . status ) . toBe ( 'failed' )
245+ expect ( await test . peer . waitForCreation ( ) ) . toMatchObject ( {
246+ status : 'failed' ,
247+ } )
224248 expect ( test . session . snapshot . error ) . toContain ( 'empty directory' )
225249 expect (
226250 await readFile ( path . join ( test . cwd , 'my-site/keep.txt' ) , 'utf8' ) ,
@@ -233,7 +257,9 @@ describe('Forge session', () => {
233257 directory : 'another-site' ,
234258 } ,
235259 } )
236- await expect . poll ( ( ) => test . session . snapshot . status ) . toBe ( 'ready' )
260+ expect ( await test . peer . waitForCreation ( ) ) . toMatchObject ( {
261+ status : 'ready' ,
262+ } )
237263 expect ( test . session . snapshot . result ?. directory ) . toBe (
238264 path . join ( test . cwd , 'another-site' ) ,
239265 )
@@ -250,7 +276,9 @@ describe('Forge session', () => {
250276 type : 'create' ,
251277 configuration : structuredClone ( test . session . snapshot . configuration ) ,
252278 } )
253- await expect . poll ( ( ) => test . session . snapshot . status ) . toBe ( 'ready' )
279+ expect ( await test . peer . waitForCreation ( ) ) . toMatchObject ( {
280+ status : 'ready' ,
281+ } )
254282 expect ( test . session . snapshot . result ?. previewError ) . toContain (
255283 'Missing a site dependency' ,
256284 )
0 commit comments