diff --git a/tests/e2e/pattern-editor-validity.spec.mjs b/tests/e2e/pattern-editor-validity.spec.mjs index 33fdff2..48dec37 100644 --- a/tests/e2e/pattern-editor-validity.spec.mjs +++ b/tests/e2e/pattern-editor-validity.spec.mjs @@ -3,6 +3,7 @@ import { expect, test } from '@playwright/test'; const patterns = [ { label: 'Music', + patternTitle: 'Music landing', pageSlug: 'music', patternSlug: 'chrnx/music-landing', atmosphereAsset: 'the-no-names-collage.jpeg', @@ -12,6 +13,7 @@ const patterns = [ }, { label: 'Podcasts', + patternTitle: 'Podcast landing', pageSlug: 'podcasts', patternSlug: 'chrnx/podcast-landing', atmosphereAsset: 'podcast-frequency-grunge.png', @@ -105,6 +107,7 @@ async function editorSnapshot( page ) { block.attributes?.className?.split( /\s+/ ).includes( 'chrnx-character-stage' ), ); const serialized = window.wp.blocks.serialize( roots ); + const stageBlocks = stage ? flatten( [ stage ] ) : []; const layerName = ( block ) => { const source = window.wp.blocks.serialize( block ); return [ 'atmosphere', 'content', 'art' ].find( ( layer ) => @@ -113,7 +116,15 @@ async function editorSnapshot( page ) { }; return { - invalid: all.filter( ( block ) => block.isValid === false ).map( ( block ) => block.name ), + invalid: all + .filter( ( block ) => block.isValid === false ) + .map( ( block ) => ( { + clientId: block.clientId, + name: block.name, + } ) ), + stageInvalid: stageBlocks + .filter( ( block ) => block.isValid === false ) + .map( ( block ) => block.name ), hasPatternReference: all.some( ( block ) => block.name === 'core/pattern' ), layerOrder: stage ? stage.innerBlocks.map( layerName ) : [], layerMarkup: stage @@ -141,8 +152,21 @@ async function expectNoRecoveryUi( page ) { ).toHaveCount( 0 ); } -function expectPatternContract( snapshot, pattern ) { - expect( snapshot.invalid ).toEqual( [] ); +async function recoveryUiCount( page ) { + const canvas = page.locator( 'iframe[name="editor-canvas"]' ).contentFrame(); + return ( + ( await page.getByText( /unexpected or invalid content/i ).count() ) + + ( await page.getByRole( 'button', { name: /attempt (?:block )?recovery/i } ).count() ) + + ( await canvas.getByText( /unexpected or invalid content/i ).count() ) + + ( await canvas.getByRole( 'button', { name: /attempt (?:block )?recovery/i } ).count() ) + ); +} + +function expectPatternContract( snapshot, pattern, { requireDocumentValidity = true } = {} ) { + if ( requireDocumentValidity ) { + expect( snapshot.invalid ).toEqual( [] ); + } + expect( snapshot.stageInvalid ).toEqual( [] ); expect( snapshot.hasPatternReference ).toBeFalsy(); expect( snapshot.layerOrder ).toEqual( [ 'atmosphere', 'content', 'art' ] ); expect( snapshot.layerMarkup.atmosphere ).toContain( pattern.atmosphereAsset ); @@ -152,15 +176,81 @@ function expectPatternContract( snapshot, pattern ) { expect( snapshot.serialized ).toContain( pattern.supportingCopy ); } -async function getTemplateRowCount( page ) { +async function getTemplateSnapshot( page ) { return page.evaluate( async () => { - const templates = await window.wp.apiFetch( { - path: '/wp/v2/templates?context=edit&per_page=100&_fields=wp_id', - } ); - return templates.filter( ( template ) => Number( template.wp_id ) > 0 ).length; + const [ template, templates ] = await Promise.all( [ + window.wp.apiFetch( { + path: '/wp/v2/templates/chrnx//page?context=edit&_fields=id,wp_id,source,modified,content', + } ), + window.wp.apiFetch( { + path: '/wp/v2/templates?context=edit&per_page=100&_fields=id,wp_id,source,modified,content', + } ), + ] ); + return { + template, + persistedRows: templates + .filter( ( candidate ) => Number( candidate.wp_id ) > 0 ) + .sort( ( left, right ) => left.id.localeCompare( right.id ) ), + }; } ); } +async function getPageSnapshot( page, pageId ) { + return page.evaluate( + ( id ) => + window.wp.apiFetch( { + path: `/wp/v2/pages/${ id }?context=edit&_fields=id,slug,status,modified,content`, + } ), + pageId, + ); +} + +function watchPersistenceRequests( page ) { + const persistenceRequests = []; + const writeMethods = [ 'POST', 'PUT', 'PATCH', 'DELETE' ]; + const listener = ( request ) => { + if ( writeMethods.includes( request.method() ) ) { + persistenceRequests.push( `${ request.method() } ${ request.url() }` ); + } + }; + page.on( 'request', listener ); + return { + persistenceRequests, + stop: () => page.off( 'request', listener ), + }; +} + +async function waitForEditorToSettle( page, { expectDirty } ) { + await expect + .poll( () => + page.evaluate( () => { + const editor = window.wp.data.select( 'core/editor' ); + return { + autosaving: editor.isAutosavingPost(), + saving: editor.isSavingPost(), + }; + } ), + ) + .toEqual( { autosaving: false, saving: false } ); + await expect + .poll( () => page.evaluate( () => window.wp.data.select( 'core/editor' ).isEditedPostDirty() ) ) + .toBe( expectDirty ); +} + +async function insertPatternThroughUi( page, pattern ) { + await page.getByRole( 'button', { name: 'Block Inserter' } ).click(); + // Searching from Blocks avoids the Site Editor's pattern-tab zoom mode, which + // intentionally scopes insertion to the existing
section. + await page.getByRole( 'tab', { name: 'Blocks', exact: true } ).click(); + await page.locator( 'input[type="search"]:visible' ).fill( pattern.patternTitle ); + const patternOption = page.getByRole( 'option', { + name: pattern.patternTitle, + exact: true, + } ); + await expect( patternOption ).toBeVisible(); + await patternOption.click(); +} + for ( const pattern of patterns ) { test( `${ pattern.label } Post Editor keeps the seeded pattern valid without saving`, async ( { page, @@ -172,71 +262,50 @@ for ( const pattern of patterns ) { expect( pagesResponse.ok() ).toBeTruthy(); const [ seededPage ] = await pagesResponse.json(); expect( seededPage?.id ).toBeTruthy(); - - const persistenceRequests = []; - page.on( 'request', ( request ) => { - if ( - ! [ 'GET', 'HEAD', 'OPTIONS' ].includes( request.method() ) && - /\/wp-json\/wp\/v2\/(?:pages|templates)|\/wp-admin\/post\.php/.test( request.url() ) - ) { - persistenceRequests.push( `${ request.method() } ${ request.url() }` ); - } - } ); await page.goto( `/wp-admin/post.php?post=${ seededPage.id }&action=edit` ); await closeWelcomeGuide( page ); await waitForPatternEditorState( page ); + const pageBefore = await getPageSnapshot( page, seededPage.id ); + const guard = watchPersistenceRequests( page ); expectPatternContract( await editorSnapshot( page ), pattern ); await expectNoRecoveryUi( page ); - expect( persistenceRequests ).toEqual( [] ); + await waitForEditorToSettle( page, { expectDirty: false } ); + const pageAfter = await getPageSnapshot( page, seededPage.id ); + expect( pageAfter ).toEqual( pageBefore ); + await page.close( { runBeforeUnload: false } ); + guard.stop(); + expect( guard.persistenceRequests ).toEqual( [] ); } ); test( `${ pattern.label } Site Editor inserts the registered pattern validly without saving`, async ( { page, } ) => { await logIn( page ); - const persistenceRequests = []; - page.on( 'request', ( request ) => { - if ( - ! [ 'GET', 'HEAD', 'OPTIONS' ].includes( request.method() ) && - /\/wp-json\/wp\/v2\/(?:pages|templates)|\/wp-admin\/post\.php/.test( request.url() ) - ) { - persistenceRequests.push( `${ request.method() } ${ request.url() }` ); - } - } ); await page.goto( '/wp-admin/site-editor.php?postType=wp_template&postId=chrnx%2F%2Fpage&canvas=edit', ); await closeWelcomeGuide( page ); await waitForEditor( page ); - const templatesBefore = await getTemplateRowCount( page ); - await expect - .poll( - () => - page.evaluate( ( slug ) => { - const selectedPattern = window.wp.data - .select( 'core/block-editor' ) - .__experimentalGetParsedPattern( slug ); - return selectedPattern?.blocks?.length ?? 0; - }, pattern.patternSlug ), - { timeout: 10_000 }, - ) - .toBeGreaterThan( 0 ); + const templateBefore = await getTemplateSnapshot( page ); + const initialEditor = await editorSnapshot( page ); + const initialRecoveryCount = await recoveryUiCount( page ); + const guard = watchPersistenceRequests( page ); - await page.evaluate( ( slug ) => { - const store = window.wp.data.select( 'core/block-editor' ); - const selectedPattern = store.__experimentalGetParsedPattern( slug ); - if ( ! selectedPattern?.blocks?.length ) { - throw new Error( `Parsed editor pattern not found: ${ slug }` ); - } - window.wp.data.dispatch( 'core/block-editor' ).resetBlocks( [] ); - window.wp.data.dispatch( 'core/block-editor' ).insertBlocks( selectedPattern.blocks ); - }, pattern.patternSlug ); + await insertPatternThroughUi( page, pattern ); await waitForPatternEditorState( page ); - expectPatternContract( await editorSnapshot( page ), pattern ); - await expectNoRecoveryUi( page ); - expect( await getTemplateRowCount( page ) ).toBe( templatesBefore ); - expect( persistenceRequests ).toEqual( [] ); + const insertedEditor = await editorSnapshot( page ); + expectPatternContract( insertedEditor, pattern, { + requireDocumentValidity: false, + } ); + expect( insertedEditor.invalid ).toEqual( initialEditor.invalid ); + expect( await recoveryUiCount( page ) ).toBe( initialRecoveryCount ); + await waitForEditorToSettle( page, { expectDirty: true } ); + const templateAfter = await getTemplateSnapshot( page ); + expect( templateAfter ).toEqual( templateBefore ); + await page.close( { runBeforeUnload: false } ); + guard.stop(); + expect( guard.persistenceRequests ).toEqual( [] ); } ); } diff --git a/tests/e2e/visual-states.spec.mjs b/tests/e2e/visual-states.spec.mjs index 597d867..0d0b1ed 100644 --- a/tests/e2e/visual-states.spec.mjs +++ b/tests/e2e/visual-states.spec.mjs @@ -1210,6 +1210,7 @@ const musicPodcastLandingCases = [ copy: 'The No Names es una banda activa', atmosphere: 'img[src*="the-no-names-collage.jpeg"]', art: 'img[src*="the-no-names-musician-cutout.png"]', + latestMedia: null, supporting: 'text=El proceso usa generative AI', }, { @@ -1219,10 +1220,58 @@ const musicPodcastLandingCases = [ copy: 'Conversations about the work behind software, research, music, and games.', atmosphere: 'img[src*="podcast-frequency-grunge.png"]', art: 'img[src*="alter-ego-podcasts.png"]', + latestMedia: '.chrnx-latest-podcast img', supporting: '.chrnx-latest-podcast', }, ]; +async function waitForDecodedLandingImages( page, landing, { awaitDecode = true } = {} ) { + const selectors = [ landing.atmosphere, landing.art ]; + if ( landing.latestMedia ) { + selectors.push( landing.latestMedia ); + } + const images = page.locator( landing.root ).locator( selectors.join( ', ' ) ); + + await expect + .poll( () => + images.evaluateAll( + ( elements ) => + elements.length >= 2 && + elements.every( + ( image ) => image.complete && image.naturalWidth > 0 && image.naturalHeight > 0, + ), + ), + ) + .toBe( true ); + if ( ! awaitDecode ) { + await images.evaluateAll( ( elements ) => { + elements.forEach( ( image ) => { + if ( typeof image.decode === 'function' ) { + image.decode().catch( () => {} ); + } + if ( ! image.complete || image.naturalWidth <= 0 || image.naturalHeight <= 0 ) { + throw new Error( `Landing image failed to decode: ${ image.currentSrc || image.src }` ); + } + } ); + } ); + return; + } + await images.evaluateAll( async ( elements ) => { + const decodePromises = elements.map( ( image ) => { + if ( typeof image.decode === 'function' ) { + return image.decode(); + } + return Promise.resolve(); + } ); + await Promise.all( decodePromises ); + elements.forEach( ( image ) => { + if ( ! image.complete || image.naturalWidth <= 0 || image.naturalHeight <= 0 ) { + throw new Error( `Landing image failed to decode: ${ image.currentSrc || image.src }` ); + } + } ); + } ); +} + async function readMusicPodcastLandingState( page, landing ) { return page.locator( landing.root ).evaluate( ( root ) => { const stage = root.querySelector( '.chrnx-character-stage' ); @@ -1303,6 +1352,9 @@ test( 'Music and Podcast without JavaScript preserve the complete landing compos await expect( root.locator( landing.atmosphere ) ).toBeVisible(); await expect( root.locator( landing.art ) ).toBeVisible(); await expect( root.locator( landing.supporting ) ).toBeVisible(); + await waitForDecodedLandingImages( page, landing, { + awaitDecode: false, + } ); const state = await readMusicPodcastLandingState( page, landing ); expect( state.layerOrder ).toEqual( [ 'atmosphere', 'content', 'art' ] ); expect( state.copyVisible ).toBeTruthy(); @@ -1335,15 +1387,14 @@ test( 'Music and Podcast reduced motion match settled layout without displacemen try { for ( const landing of musicPodcastLandingCases ) { await page.goto( landing.path ); - await page - .locator( `${ landing.root } .chrnx-character-stage__art img` ) - .evaluate( ( image ) => image.decode() ); + await waitForDecodedLandingImages( page, landing ); const settled = await readMusicPodcastLandingState( page, landing ); - await page.emulateMedia( { colorScheme, reducedMotion: 'reduce' } ); + await page.emulateMedia( { + colorScheme, + reducedMotion: 'reduce', + } ); await page.reload(); - await page - .locator( `${ landing.root } .chrnx-character-stage__art img` ) - .evaluate( ( image ) => image.decode() ); + await waitForDecodedLandingImages( page, landing ); const reduced = await readMusicPodcastLandingState( page, landing ); expect( reduced.layerOrder ).toEqual( [ 'atmosphere', 'content', 'art' ] ); diff --git a/tests/smoke/visual-integration.test.mjs b/tests/smoke/visual-integration.test.mjs index e94362a..894d3fa 100644 --- a/tests/smoke/visual-integration.test.mjs +++ b/tests/smoke/visual-integration.test.mjs @@ -516,6 +516,29 @@ test( 'editor pattern validity is derived only from the live block-editor store' /editorSnapshot[\s\S]*?wp\.apiFetch\(\s*\{\s*path:\s*'\/wp\/v2\/block-patterns\/patterns'/, ); assert.match( editorValidity, /const roots = store\.getBlocks\(\)/ ); + assert.doesNotMatch( editorValidity, /__experimentalGetParsedPattern/ ); + assert.doesNotMatch( editorValidity, /\.resetBlocks\s*\(/ ); + assert.doesNotMatch( editorValidity, /\.insertBlocks\s*\(/ ); + assert.match( editorValidity, /getByRole\( 'button', \{ name: 'Block Inserter' \} \)/ ); + assert.match( editorValidity, /getByRole\( 'tab', \{ name: 'Blocks'/ ); + assert.match( + editorValidity, + /getByRole\( 'option', \{[\s\S]*?name: pattern\.patternTitle/, + ); + assert.match( editorValidity, /\[ 'POST', 'PUT', 'PATCH', 'DELETE' \]/ ); + assert.match( editorValidity, /expect\( templateAfter \)\.toEqual\( templateBefore \)/ ); +} ); + +test( 'Music and Podcast visual states decode every relevant image before measuring', () => { + const visualStates = read( 'tests/e2e/visual-states.spec.mjs' ); + + assert.match( visualStates, /async function waitForDecodedLandingImages/ ); + assert.match( visualStates, /image\.complete/ ); + assert.match( visualStates, /image\.naturalWidth > 0/ ); + assert.match( visualStates, /image\.naturalHeight > 0/ ); + assert.match( visualStates, /typeof image\.decode === 'function'/ ); + assert.match( visualStates, /awaitDecode: false/ ); + assert.match( visualStates, /latestMedia/ ); } ); test( 'character stages define the shared mockup effect and responsive geometry', () => {