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, ); } );