77 * @position Docsite integration tests; run against both latest and canary builds
88 */
99
10- import { expect , test , type Page } from '@playwright/test' ;
10+ import { expect , test , type Page , type Route } from '@playwright/test' ;
1111
1212const deployments = [
1313 { name : 'production' , origin : 'https://astryx.atmeta.com' } ,
@@ -68,18 +68,31 @@ async function servePublicOrigin(
6868
6969async function serveReachability ( page : Page , origin : string ) {
7070 let requests = 0 ;
71- const sandboxes : ( string | null ) [ ] = [ ] ;
71+ // The page reads each probe frame's sandbox as the frame is inserted. The
72+ // probe sets it before it inserts the frame.
73+ await page . addInitScript ( ( ) => {
74+ const sandboxes : ( string | null ) [ ] = [ ] ;
75+ Object . assign ( window , { probeSandboxes : sandboxes } ) ;
76+ new MutationObserver ( records => {
77+ for ( const record of records ) {
78+ for ( const node of record . addedNodes ) {
79+ if (
80+ node instanceof HTMLIFrameElement &&
81+ node . title === 'Astryx documentation reachability check'
82+ ) {
83+ sandboxes . push ( node . getAttribute ( 'sandbox' ) ) ;
84+ }
85+ }
86+ }
87+ } ) . observe ( document , { childList : true , subtree : true } ) ;
88+ } ) ;
7289 await page . route ( `${ probeOrigin } /embed/access-check?**` , async route => {
7390 requests ++ ;
7491 expect (
7592 new URL ( route . request ( ) . url ( ) ) . searchParams . get ( 'parent_origin' ) ,
7693 ) . toBe ( origin ) ;
77- // The probe's frame exists while its request waits here: the probe removes
78- // it once it has an answer, or when its four-second timeout fires. Reading
79- // it now cannot race that timeout.
80- const frame = await route . request ( ) . frame ( ) . frameElement ( ) ;
81- sandboxes . push ( await frame . getAttribute ( 'sandbox' ) ) ;
82- await frame . dispose ( ) ;
94+ // The probe gives up four seconds after it inserts its frame. Answer at
95+ // once: a call back into a busy page before the answer spends that time.
8396 await route . fulfill ( {
8497 contentType : 'text/html; charset=utf-8' ,
8598 headers : {
@@ -92,7 +105,27 @@ async function serveReachability(page: Page, origin: string) {
92105 body : `<!doctype html><html><body><script>window.parent.postMessage({type:'codebunny:access-check:v1',reachable:true},${ JSON . stringify ( origin ) } );</script></body></html>` ,
93106 } ) ;
94107 } ) ;
95- return { requests : ( ) => requests , sandboxes : ( ) => [ ...sandboxes ] } ;
108+ return {
109+ requests : ( ) => requests ,
110+ sandboxes : ( ) =>
111+ page . evaluate ( ( ) => [
112+ ...( window as unknown as { probeSandboxes : ( string | null ) [ ] } )
113+ . probeSandboxes ,
114+ ] ) ,
115+ } ;
116+ }
117+
118+ // Past the probe's four-second deadline.
119+ const PAST_DEADLINE_MS = 5000 ;
120+
121+ /**
122+ * Holds the page's clock still from now on: no timer fires until the test
123+ * advances the clock or resumes it.
124+ */
125+ async function holdClock ( page : Page ) {
126+ await page . clock . install ( ) ;
127+ const now = await page . evaluate ( ( ) => Date . now ( ) ) ;
128+ await page . clock . pauseAt ( now + 1000 ) ;
96129}
97130
98131for ( const { name, origin} of deployments ) {
@@ -106,6 +139,10 @@ for (const {name, origin} of deployments) {
106139 await page . emulateMedia ( { colorScheme} ) ;
107140 await servePublicOrigin ( page , baseURL , origin ) ;
108141 const probe = await serveReachability ( page , origin ) ;
142+ // The probe gives up four seconds after it inserts its frame. The
143+ // page's clock holds still until the answer is in, so a slow page
144+ // cannot reach that deadline first.
145+ await holdClock ( page ) ;
109146 const documentResponse = await page . goto (
110147 `${ origin } /docs/getting-started?source=test` ,
111148 {
@@ -138,7 +175,13 @@ for (const {name, origin} of deployments) {
138175 // failed probe leaves, checked by the failed-reachability case.
139176 await expect ( notice ) . toHaveCount ( 1 ) ;
140177 await expect ( notice ) . toContainText ( 'Working on Meta internal tools?' ) ;
141- expect ( probe . sandboxes ( ) ) . toEqual ( [ 'allow-scripts allow-same-origin' ] ) ;
178+ // Past the deadline, the answer still stands.
179+ await page . clock . runFor ( PAST_DEADLINE_MS ) ;
180+ await expect ( notice ) . toContainText ( 'Working on Meta internal tools?' ) ;
181+ await page . clock . resume ( ) ;
182+ expect ( await probe . sandboxes ( ) ) . toEqual ( [
183+ 'allow-scripts allow-same-origin' ,
184+ ] ) ;
142185 await expect ( notice ) . not . toContainText ( "You're viewing Canary docs" ) ;
143186 if ( isCanary ) {
144187 await expect ( notice ) . toContainText (
@@ -235,9 +278,13 @@ for (const {name, origin} of deployments) {
235278 }
236279 expect ( probe . requests ( ) ) . toBe ( 1 ) ;
237280 await dismiss . click ( ) ;
281+ await holdClock ( page ) ;
238282 await page . reload ( ) ;
239283 await expect ( heading ) . toBeVisible ( ) ;
240284 await expect ( internal ) . toBeVisible ( ) ;
285+ await page . clock . runFor ( PAST_DEADLINE_MS ) ;
286+ await expect ( internal ) . toBeVisible ( ) ;
287+ await page . clock . resume ( ) ;
241288 expect ( probe . requests ( ) ) . toBe ( 2 ) ;
242289 await page . route ( `${ internalOrigin } /**` , route =>
243290 route . fulfill ( {
@@ -258,6 +305,46 @@ for (const {name, origin} of deployments) {
258305 }
259306}
260307
308+ test ( `${ target } : a probe with no answer by its deadline exposes no internal wording` , async ( {
309+ page,
310+ baseURL,
311+ } ) => {
312+ await page . setViewportSize ( { width : 1440 , height : 900 } ) ;
313+ const origin = deployments [ 0 ] . origin ;
314+ await servePublicOrigin ( page , baseURL , origin ) ;
315+ // The probe's request is held and never answered.
316+ const held : Route [ ] = [ ] ;
317+ await page . route ( `${ probeOrigin } /**` , route => {
318+ held . push ( route ) ;
319+ } ) ;
320+ await holdClock ( page ) ;
321+ // The held probe frame keeps the page from firing `load`.
322+ await page . goto ( `${ origin } /docs/getting-started?source=test` , {
323+ waitUntil : 'domcontentloaded' ,
324+ } ) ;
325+ await expect (
326+ page . getByRole ( 'heading' , { name : 'Getting Started' , exact : true } ) ,
327+ ) . toBeVisible ( ) ;
328+ const frame = page . locator (
329+ 'iframe[title="Astryx documentation reachability check"]' ,
330+ ) ;
331+ // The probe inserts its frame as it starts its four seconds.
332+ await expect ( frame ) . toHaveCount ( 1 ) ;
333+ await page . clock . runFor ( 3990 ) ;
334+ await expect ( frame ) . toHaveCount ( 1 ) ;
335+ await page . clock . runFor ( 20 ) ;
336+ await expect ( frame ) . toHaveCount ( 0 ) ;
337+ const notice = page . getByRole ( 'region' , { name : noticeName } ) ;
338+ await expect ( notice ) . toHaveCount ( isCanary ? 1 : 0 ) ;
339+ await expect (
340+ page . getByRole ( 'link' , { name : 'Meta internal docs' , exact : true } ) ,
341+ ) . toHaveCount ( 0 ) ;
342+ if ( isCanary ) {
343+ await expect ( notice ) . toContainText ( "You're viewing Canary docs" ) ;
344+ }
345+ await Promise . all ( held . map ( route => route . abort ( ) . catch ( ( ) => { } ) ) ) ;
346+ } ) ;
347+
261348for ( const width of [ 1440 , 390 ] ) {
262349 for ( const colorScheme of [ 'light' , 'dark' ] as const ) {
263350 test ( `${ target } : failed reachability exposes no internal wording at ${ width } px in ${ colorScheme } ` , async ( {
0 commit comments