|
| 1 | +const path = require( 'node:path' ); |
| 2 | +const { test, expect } = require( '@playwright/test' ); |
| 3 | +const { |
| 4 | + BOOMERANG_URL, |
| 5 | + CONSENT_LOADERS, |
| 6 | + createLoaderHarness, |
| 7 | +} = require( './fixtures/browser' ); |
| 8 | + |
| 9 | +const BEACON_URL = 'https://collector.basicrum.test/beacon'; |
| 10 | + |
| 11 | +// Boomerang stub that mimics the bundled boomerang tail's init glue contract: |
| 12 | +// window.basicRumInitConfig=BOOMR.window.basicRumBoomerangConfig; |
| 13 | +// window.basicRumInitConfig&&BOOMR.init(window.basicRumInitConfig) |
| 14 | +// On execution it only "initializes" (records the init, recreates the RT |
| 15 | +// cookie, and fires a beacon) when the inline config object is still truthy, |
| 16 | +// exactly like the real bundle in assets/js/boomr/. |
| 17 | +const INIT_GLUE_BOOMERANG_STUB = ` |
| 18 | +window.__boomrExecutionCount = ( window.__boomrExecutionCount || 0 ) + 1; |
| 19 | +window.__boomrInitCalls = window.__boomrInitCalls || 0; |
| 20 | +window.BOOMR = window.BOOMR || {}; |
| 21 | +window.BOOMR.version = 'test'; |
| 22 | +window.BOOMR.window = window; |
| 23 | +window.BOOMR.init = function( config ) { |
| 24 | + window.__boomrInitCalls += 1; |
| 25 | + document.cookie = 'RT=recreated-by-late-script; path=/; SameSite=Strict'; |
| 26 | + fetch( '${ BEACON_URL }', { method: 'POST', body: 'beacon' } ).catch( function() {} ); |
| 27 | + return window.BOOMR; |
| 28 | +}; |
| 29 | +window.basicRumInitConfig = window.BOOMR.window.basicRumBoomerangConfig; |
| 30 | +window.basicRumInitConfig && window.BOOMR.init( window.basicRumInitConfig ); |
| 31 | +`; |
| 32 | + |
| 33 | +/** |
| 34 | + * Install routes that shadow the harness defaults for this spec: the beacon |
| 35 | + * endpoint is fulfilled (and counted) instead of blocked, and the Boomerang |
| 36 | + * script response is gated on an explicit promise so tests control exactly |
| 37 | + * when the "download" finishes. Routes registered after the harness route |
| 38 | + * take precedence, so the harness never sees these requests. |
| 39 | + * |
| 40 | + * @param {import('@playwright/test').Page} page Playwright page. |
| 41 | + * @return {Promise<object>} Gate controls and request counters. |
| 42 | + */ |
| 43 | +async function installGatedBoomerang( page ) { |
| 44 | + let releaseBoomerang; |
| 45 | + const boomerangReleased = new Promise( ( resolve ) => { |
| 46 | + releaseBoomerang = resolve; |
| 47 | + } ); |
| 48 | + let boomerangRequested; |
| 49 | + const boomerangRequestInFlight = new Promise( ( resolve ) => { |
| 50 | + boomerangRequested = resolve; |
| 51 | + } ); |
| 52 | + let boomerangRequests = 0; |
| 53 | + let beaconRequests = 0; |
| 54 | + |
| 55 | + await page.route( BEACON_URL, async ( route ) => { |
| 56 | + beaconRequests += 1; |
| 57 | + await route.fulfill( { |
| 58 | + status: 204, |
| 59 | + headers: { 'access-control-allow-origin': '*' }, |
| 60 | + body: '', |
| 61 | + } ); |
| 62 | + } ); |
| 63 | + |
| 64 | + await page.route( BOOMERANG_URL, async ( route ) => { |
| 65 | + boomerangRequests += 1; |
| 66 | + boomerangRequested(); |
| 67 | + await boomerangReleased; |
| 68 | + await route.fulfill( { |
| 69 | + status: 200, |
| 70 | + contentType: 'application/javascript; charset=utf-8', |
| 71 | + body: INIT_GLUE_BOOMERANG_STUB, |
| 72 | + } ); |
| 73 | + } ); |
| 74 | + |
| 75 | + return { |
| 76 | + boomerangRequestInFlight, |
| 77 | + releaseBoomerang, |
| 78 | + boomerangRequests: () => boomerangRequests, |
| 79 | + beaconRequests: () => beaconRequests, |
| 80 | + }; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Mirror the inline configuration that Assets.php prints before the loader. |
| 85 | + * |
| 86 | + * @param {import('@playwright/test').Page} page Playwright page. |
| 87 | + */ |
| 88 | +async function injectInlineConfig( page ) { |
| 89 | + await page.evaluate( ( beaconUrl ) => { |
| 90 | + window.basicRumBoomerangConfig = { |
| 91 | + beacon_url: beaconUrl, |
| 92 | + instrument_xhr: false, |
| 93 | + secure_cookie: false, |
| 94 | + same_site_cookie: 'Strict', |
| 95 | + }; |
| 96 | + window.__boomrInitCalls = 0; |
| 97 | + }, BEACON_URL ); |
| 98 | +} |
| 99 | + |
| 100 | +for ( const loaderPath of CONSENT_LOADERS ) { |
| 101 | + test.describe( path.basename( loaderPath ), () => { |
| 102 | + test( 'initializes from the pending config when consent stays granted', async ( { context, page } ) => { |
| 103 | + const harness = await createLoaderHarness( page ); |
| 104 | + const gate = await installGatedBoomerang( page ); |
| 105 | + |
| 106 | + await harness.load( loaderPath ); |
| 107 | + await injectInlineConfig( page ); |
| 108 | + |
| 109 | + await page.evaluate( () => window.OPT_IN_BASICRUM_LOADER_WRAPPER() ); |
| 110 | + await gate.boomerangRequestInFlight; |
| 111 | + gate.releaseBoomerang(); |
| 112 | + await harness.waitForExecutions( 1 ); |
| 113 | + |
| 114 | + // Sanity check for the stub's init-glue contract: with the config |
| 115 | + // still present the late script initializes, sets its cookie, and |
| 116 | + // beacons - proving the race assertions below are meaningful. |
| 117 | + expect( await page.evaluate( () => window.__boomrInitCalls ) ).toBe( 1 ); |
| 118 | + const cookies = await context.cookies( harness.pageUrl ); |
| 119 | + expect( cookies.some( ( cookie ) => 'RT' === cookie.name ) ).toBe( true ); |
| 120 | + await expect.poll( () => gate.beaconRequests() ).toBe( 1 ); |
| 121 | + expect( harness.unexpectedRequests() ).toEqual( [] ); |
| 122 | + } ); |
| 123 | + |
| 124 | + test( 'a deny before any opt-in must not block a later allow on the same page', async ( { page } ) => { |
| 125 | + const harness = await createLoaderHarness( page ); |
| 126 | + const gate = await installGatedBoomerang( page ); |
| 127 | + |
| 128 | + await harness.load( loaderPath ); |
| 129 | + await injectInlineConfig( page ); |
| 130 | + |
| 131 | + // 1. The consent tool reports deny or no-decision first: every |
| 132 | + // packaged adapter fails closed and calls OPT_OUT before the |
| 133 | + // visitor has made a choice. |
| 134 | + await page.evaluate( () => window.OPT_OUT_BASICRUM_LOADER_WRAPPER() ); |
| 135 | + |
| 136 | + // 2. The visitor accepts moments later on the same page. |
| 137 | + await page.evaluate( () => window.OPT_IN_BASICRUM_LOADER_WRAPPER() ); |
| 138 | + await gate.boomerangRequestInFlight; |
| 139 | + gate.releaseBoomerang(); |
| 140 | + await harness.waitForExecutions( 1 ); |
| 141 | + |
| 142 | + // The late grant must fully initialize monitoring: this is the |
| 143 | + // standard first-visit journey for every automatic adapter. |
| 144 | + expect( await page.evaluate( () => window.__boomrInitCalls ) ).toBe( 1 ); |
| 145 | + await expect.poll( () => gate.beaconRequests() ).toBe( 1 ); |
| 146 | + expect( harness.unexpectedRequests() ).toEqual( [] ); |
| 147 | + } ); |
| 148 | + |
| 149 | + test( 'a Boomerang script that arrives after opt-out must not initialize, set cookies, or beacon', async ( { context, page } ) => { |
| 150 | + const harness = await createLoaderHarness( page ); |
| 151 | + const gate = await installGatedBoomerang( page ); |
| 152 | + |
| 153 | + await harness.load( loaderPath ); |
| 154 | + await injectInlineConfig( page ); |
| 155 | + |
| 156 | + // 1. Consent granted: the loader starts the Boomerang download. |
| 157 | + await page.evaluate( () => window.OPT_IN_BASICRUM_LOADER_WRAPPER() ); |
| 158 | + await gate.boomerangRequestInFlight; |
| 159 | + |
| 160 | + // 2. Consent withdrawn while boomerang.js is still downloading. |
| 161 | + await page.evaluate( () => window.OPT_OUT_BASICRUM_LOADER_WRAPPER() ); |
| 162 | + |
| 163 | + // 3. Only now does the script "download" finish and execute. |
| 164 | + gate.releaseBoomerang(); |
| 165 | + await harness.waitForExecutions( 1 ); |
| 166 | + |
| 167 | + // Desired behavior: after the user opted out, the late-arriving |
| 168 | + // script must not initialize, recreate cookies, or send beacons. |
| 169 | + expect( await page.evaluate( () => window.__boomrInitCalls ) ).toBe( 0 ); |
| 170 | + const cookies = await context.cookies( harness.pageUrl ); |
| 171 | + expect( cookies.some( ( cookie ) => [ 'RT', 'BA' ].includes( cookie.name ) ) ).toBe( false ); |
| 172 | + expect( gate.beaconRequests() ).toBe( 0 ); |
| 173 | + expect( gate.boomerangRequests() ).toBe( 1 ); |
| 174 | + expect( harness.unexpectedRequests() ).toEqual( [] ); |
| 175 | + } ); |
| 176 | + } ); |
| 177 | +} |
0 commit comments