test: isolate Related Stories partial counts

This commit is contained in:
chrnx committed 2026-08-12 22:51:08 -05:00
1 parent 38d26acabc
commit 12259f4837
7 files changed
+33 -283

No files matched your search

@@ -1,104 +0,0 @@
import { createHash } from 'node:crypto';
import { execFileSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { expect, test } from '@playwright/test';
const projectRoot = fileURLToPath( new URL( '../..', import.meta.url ) );
const commandEnvironment = {
...process.env,
PATH: `/opt/homebrew/opt/docker/bin:${ process.env.PATH ?? '' }`,
};
const seedScenario = ( scenario ) =>
execFileSync( './scripts/seed-test-content.sh', [ scenario ], {
cwd: projectRoot,
env: commandEnvironment,
stdio: 'pipe',
} );
function readFixtureState() {
const php = String.raw`
$owned = get_posts(array('post_type'=>array('page','post'),'post_status'=>'any','posts_per_page'=>-1,'orderby'=>'ID','order'=>'ASC','meta_key'=>'_chrnx_fixture_key'));
$records = array();
foreach ($owned as $post) {
$records[] = array('id'=>$post->ID,'key'=>get_post_meta($post->ID,'_chrnx_fixture_key',true),'type'=>$post->post_type,'status'=>$post->post_status,'slug'=>$post->post_name,'content'=>hash('sha256',$post->post_content));
}
$unrelated = get_posts(array('post_type'=>array('page','post'),'post_status'=>'any','posts_per_page'=>-1,'orderby'=>'ID','order'=>'ASC','meta_query'=>array(array('key'=>'_chrnx_fixture_key','compare'=>'NOT EXISTS'))));
$outside = array();
foreach ($unrelated as $post) {
$outside[] = array('id'=>$post->ID,'type'=>$post->post_type,'status'=>$post->post_status,'slug'=>$post->post_name,'content'=>hash('sha256',$post->post_content));
}
echo 'CHRNX_STATE=' . wp_json_encode(array('owned'=>$records,'unrelated'=>$outside,'relatedSnapshot'=>get_option('_chrnx_fixture_related_unowned_status_snapshot_v1',false)));
`;
const output = execFileSync( 'npm', [ 'exec', '--', 'wp-env', 'run', 'cli', 'wp', 'eval', php ], {
cwd: projectRoot,
env: commandEnvironment,
encoding: 'utf8',
} );
const encoded = output.match( /CHRNX_STATE=(\{.*\})/ )?.[ 1 ];
if ( ! encoded ) {
throw new Error( 'Could not read the local fixture state.' );
}
const state = JSON.parse( encoded );
return {
...state,
fingerprint: createHash( 'sha256' ).update( JSON.stringify( state ) ).digest( 'hex' ),
};
}
test.describe.configure( { mode: 'serial' } );
test.afterAll( () => {
seedScenario( 'default' );
} );
test( 'related scenarios render exact partial counts and restore the identical default dataset', async ( {
page,
} ) => {
seedScenario( 'default' );
const baseline = readFixtureState();
const unrelatedBaseline = baseline.unrelated.find( ( post ) => post.slug === 'hello-world' );
expect( unrelatedBaseline ).toMatchObject( { status: 'publish', slug: 'hello-world' } );
expect( baseline.owned.filter( ( post ) => post.type === 'page' ) ).toHaveLength( 7 );
expect( baseline.owned.filter( ( post ) => post.type === 'post' ) ).toHaveLength( 12 );
expect( baseline.owned ).toHaveLength( 19 );
seedScenario( 'default' );
expect( readFixtureState() ).toEqual( baseline );
try {
for ( const count of [ 1, 2, 4 ] ) {
seedScenario( `related-${ count }` );
const duringScenario = readFixtureState();
const unrelatedDuringScenario = duringScenario.unrelated.find(
( post ) => post.id === unrelatedBaseline.id,
);
expect( unrelatedDuringScenario ).toEqual( {
...unrelatedBaseline,
status: 'draft',
} );
expect( duringScenario.relatedSnapshot.posts ).toContainEqual( {
id: unrelatedBaseline.id,
status: 'publish',
} );
await page.setViewportSize( { width: 900, height: 900 } );
await page.goto( `/long-title-night-writing-fixture/?fixtureScenario=related-${ count }` );
const section = page.locator( '#related-stories-5-adaptive .chrnx-related-posts' );
const cards = section.locator( '.chrnx-related-card' );
const buttons = section.getByRole( 'button' );
await expect( cards ).toHaveCount( count );
await expect( buttons ).toHaveCount( 2 );
if ( count === 1 ) {
await expect( buttons.nth( 0 ) ).toBeDisabled();
await expect( buttons.nth( 1 ) ).toBeDisabled();
}
}
} finally {
seedScenario( 'default' );
}
const restored = readFixtureState();
expect( restored ).toEqual( baseline );
await page.goto( '/long-title-night-writing-fixture/?fixtureScenario=default' );
await expect( page.locator( '#related-stories-5-adaptive .chrnx-related-card' ) ).toHaveCount(
5,
);
} );
+21 -15
View File
@@ -30,10 +30,10 @@ test( 'Playwright caps read-only browser concurrency for the local WordPress sta
assert.equal( config.workers, 2 );
} );
test( 'exactly two serial mutation projects run after every read-only browser', async () => {
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 = [ 'related-stories-scenarios.spec.mjs', 'visual-scenarios.spec.mjs' ];
const mutationSpecs = [ 'visual-scenarios.spec.mjs' ];
const mutationSources = await Promise.all(
mutationSpecs.map( ( file ) => readFile( `tests/e2e/${ file }`, 'utf8' ) ),
);
@@ -53,31 +53,37 @@ test( 'exactly two serial mutation projects run after every read-only browser',
}
assert.deepEqual( detectedMutations.sort(), mutationSpecs );
assert.equal(
config.match(
/testIgnore:\s*\[\s*['"]visual-scenarios\.spec\.mjs['"],\s*['"]related-stories-scenarios\.spec\.mjs['"]\s*\]/gs,
)?.length,
3,
);
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, /testIgnore:\s*['"]related-stories-scenarios\.spec\.mjs['"]/ );
assert.match(
config,
/dependencies:\s*\[\s*['"]chromium['"],\s*['"]firefox['"],\s*['"]webkit['"]\s*\]/s,
);
assert.match( config, /name:\s*['"]related-scenarios['"]/ );
assert.match( config, /testMatch:\s*['"]related-stories-scenarios\.spec\.mjs['"]/ );
assert.match( config, /testIgnore:\s*['"]visual-scenarios\.spec\.mjs['"]/ );
assert.match( config, /dependencies:\s*\[\s*['"]scenarios['"]\s*\]/s );
assert.equal( config.match( /fullyParallel:\s*false/g )?.length, 2 );
assert.equal( config.match( /workers:\s*1/g )?.length, 2 );
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 );