import assert from 'node:assert/strict'; import { readFile, readdir } from 'node:fs/promises'; import test from 'node:test'; test( 'Playwright selects an environment-specific base URL with a root default', async () => { const config = await readFile( 'playwright.config.mjs', 'utf8' ); assert.match( config, /baseURL:\s*process\.env\.CHRNX_E2E_BASE_URL\s*\|\|\s*['"]http:\/\/localhost:8888['"]/, ); } ); test( 'Playwright seeds deterministic local fixtures once outside browser workers', async () => { const config = await readFile( 'playwright.config.mjs', 'utf8' ); const setup = await readFile( 'tests/e2e/global-setup.mjs', 'utf8' ); const seed = await readFile( 'scripts/seed-test-content.php', 'utf8' ); const spec = await readFile( 'tests/e2e/editorial-editor.spec.mjs', 'utf8' ); assert.match( config, /globalSetup:\s*['"]\.\/tests\/e2e\/global-setup\.mjs['"]/ ); assert.match( setup, /execFileSync\(\s*['"]\.\/scripts\/seed-test-content\.sh['"]/s ); assert.equal( setup.match( /seed-test-content\.sh/g )?.length, 1 ); assert.equal( seed.match( /chrnx_seed_editorial_terms\(\);/g )?.length, 1 ); assert.doesNotMatch( spec, /wp-env|execFileSync|beforeAll/ ); } ); test( 'Playwright caps read-only browser concurrency for the local WordPress stack', async () => { const { default: config } = await import( '../../playwright.config.mjs' ); assert.equal( config.workers, 2 ); } ); test( 'exactly one serial mutation project runs after every read-only browser', async () => { const config = await readFile( 'playwright.config.mjs', 'utf8' ); const files = await readdir( 'tests/e2e' ); const mutationSpecs = [ 'visual-scenarios.spec.mjs' ]; const mutationSources = await Promise.all( mutationSpecs.map( ( file ) => readFile( `tests/e2e/${ file }`, 'utf8' ) ), ); const detectedMutations = []; for ( const file of files.filter( ( name ) => name.endsWith( '.spec.mjs' ) ) ) { const source = await readFile( `tests/e2e/${ file }`, 'utf8' ); if ( /seed-test-content|wp-env|execFileSync/.test( source ) ) { detectedMutations.push( file ); } else { assert.doesNotMatch( source, /seed-test-content|wp-env|execFileSync/, `${ file } must be DB read-only`, ); } } assert.deepEqual( detectedMutations.sort(), mutationSpecs ); assert.equal( config.match( /testIgnore:\s*['"]visual-scenarios\.spec\.mjs['"]/g )?.length, 3 ); assert.match( config, /name:\s*['"]scenarios['"]/ ); assert.match( config, /testMatch:\s*['"]visual-scenarios\.spec\.mjs['"]/ ); assert.match( config, /dependencies:\s*\[\s*['"]chromium['"],\s*['"]firefox['"],\s*['"]webkit['"]\s*\]/s, ); assert.doesNotMatch( config, /related-scenarios|related-stories-scenarios/ ); assert.equal( config.match( /fullyParallel:\s*false/g )?.length, 1 ); assert.equal( config.match( /workers:\s*1/g )?.length, 1 ); for ( const source of mutationSources ) { assert.match( source, /test\.describe\.configure\(\s*\{\s*mode:\s*['"]serial['"]/s ); assert.match( source, /test\.afterAll[\s\S]*seedScenario\(\s*['"]default['"]\s*\)/ ); } } ); test( 'Related Stories partial-count verification never mutates unowned browser content', async () => { const seed = await readFile( 'scripts/seed-test-content.php', 'utf8' ); const shell = await readFile( 'scripts/seed-test-content.sh', 'utf8' ); const phpTest = await readFile( 'tests/php/BlockRenderingTest.php', 'utf8' ); const files = await readdir( 'tests/e2e' ); assert.doesNotMatch( seed, /related-[124]|related_unowned|unowned_status_snapshot/ ); assert.doesNotMatch( shell, /related-[124]/ ); assert.equal( files.includes( 'related-stories-scenarios.spec.mjs' ), false ); assert.match( phpTest, /related_partial_count_provider[\s\S]*array\(\s*1\s*\)[\s\S]*array\(\s*2\s*\)[\s\S]*array\(\s*4\s*\)/, ); } ); test( 'the npm integration chain finishes isolated PHP before browser workers', async () => { const manifest = JSON.parse( await readFile( 'package.json', 'utf8' ) ); assert.equal( manifest.scripts[ 'test:integration' ], undefined ); assert.match( manifest.scripts.test, /npm run test:php && npm run test:e2e/ ); assert.doesNotMatch( JSON.stringify( manifest.scripts ), /seed-state\.sh/ ); } ); test( 'restore lifecycle runs only in isolated PHPUnit and never in shared DB scripts', async () => { const manifest = JSON.parse( await readFile( 'package.json', 'utf8' ) ); const phpTest = await readFile( 'tests/php/FixtureEnvironmentTest.php', 'utf8' ); const library = await readFile( 'scripts/lib/fixture-environment.php', 'utf8' ); const files = await readdir( 'tests/integration' ).catch( () => [] ); assert.deepEqual( files.filter( ( name ) => /seed-state|harness-preservation/.test( name ) ), [] ); assert.doesNotMatch( JSON.stringify( manifest.scripts ), /tests\/integration|seed-state/ ); assert.match( phpTest, /WP_UnitTestCase/ ); assert.match( phpTest, /chrnx_fixture_environment_snapshot/ ); assert.match( phpTest, /chrnx_fixture_environment_restore/ ); assert.match( library, /raw_base64/ ); } ); test( 'PHPUnit is pinned to the isolated tests-cli database', async () => { const manifest = JSON.parse( await readFile( 'package.json', 'utf8' ) ); assert.match( manifest.scripts[ 'test:php' ], /wp-env run tests-cli/ ); assert.doesNotMatch( manifest.scripts[ 'test:php' ], /wp-env run cli/ ); assert.doesNotMatch( JSON.stringify( manifest.scripts ), /wp-env run cli[^\n"]*phpunit/ ); } );