From 987601033d78ed7cd2d5cbf0119379904bc9429d Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 30 Aug 2026 19:07:07 -0400 Subject: [PATCH] fix(browser): wait for resolved editor post --- .../src/editor-command-runners.ts | 18 ++++++++------ tests/browser-routed-command-security.test.ts | 24 ++++++++++++++++++- tests/editor-actions.test.ts | 9 +++---- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/packages/runtime-playground/src/editor-command-runners.ts b/packages/runtime-playground/src/editor-command-runners.ts index 32311461..024e139c 100644 --- a/packages/runtime-playground/src/editor-command-runners.ts +++ b/packages/runtime-playground/src/editor-command-runners.ts @@ -630,7 +630,7 @@ export async function runEditorOpenCommand({ const waitStartedAt = now() const waitStartedAtMs = Date.now() try { - const readiness = await waitForEditorOpenReadiness(page, target.waitSelector, waitTimeoutMs) + const readiness = await waitForEditorOpenReadiness(page, target, target.waitSelector, waitTimeoutMs) editorReadiness = readiness.editorReadiness editorCanvasReadiness = readiness.editorCanvasReadiness finalUrl = page.url() @@ -812,8 +812,8 @@ export function editorOpenArtifactFilesForCapture(capture: ReadonlySet, } } -export async function waitForEditorOpenReadiness(page: import("playwright").Page, waitSelector: string | undefined, timeoutMs: number): Promise<{ editorReadiness: BrowserEditorReadinessSummary; editorCanvasReadiness?: BrowserEditorCanvasProbeSummary }> { - const editorReadiness = await waitForEditorSemanticReadiness(page, timeoutMs) +export async function waitForEditorOpenReadiness(page: import("playwright").Page, target: EditorOpenTarget, waitSelector: string | undefined, timeoutMs: number): Promise<{ editorReadiness: BrowserEditorReadinessSummary; editorCanvasReadiness?: BrowserEditorCanvasProbeSummary }> { + const editorReadiness = await waitForEditorSemanticReadiness(page, target, timeoutMs) if (!waitSelector) { return { editorReadiness } } @@ -1777,8 +1777,8 @@ async function waitForEditorReadiness(page: import("playwright").Page, timeoutMs // Opening and validating an editor require the block-editor data store. Global // block APIs and save availability are stricter, separate capabilities. -async function waitForEditorSemanticReadiness(page: import("playwright").Page, timeoutMs: number): Promise { - return page.waitForFunction(() => { +async function waitForEditorSemanticReadiness(page: import("playwright").Page, target: EditorOpenTarget, timeoutMs: number): Promise { + return page.waitForFunction((expectedPostId) => { const win = window as unknown as { wp?: { blocks?: { parse?: unknown; getBlockTypes?: () => unknown[] } @@ -1800,16 +1800,20 @@ async function waitForEditorSemanticReadiness(page: import("playwright").Page, t const dispatch = win.wp?.data?.dispatch const editor = select("core/editor") const editorDispatch = typeof dispatch === "function" ? dispatch("core/editor") : undefined + const postId = typeof editor?.getCurrentPostId === "function" ? editor.getCurrentPostId() : undefined + if (expectedPostId !== null && Number(postId) !== expectedPostId) { + return false + } return { schema: "wp-codebox/editor-readiness/v1", status: "ready", storesAvailable: Boolean(editor && blockEditor), canSave: typeof editorDispatch?.savePost === "function", ...(Array.isArray(blockTypes) ? { blockTypesRegistered: blockTypes.length } : {}), - postId: typeof editor?.getCurrentPostId === "function" ? editor.getCurrentPostId() : undefined, + postId, postType: typeof editor?.getCurrentPostType === "function" ? editor.getCurrentPostType() : undefined, } - }, undefined, { timeout: timeoutMs }).then(async (handle) => { + }, target.kind === "post" && target.postId ? target.postId : null, { timeout: timeoutMs }).then(async (handle) => { const readiness = await handle.jsonValue() as BrowserEditorReadinessSummary | false if (!readiness) { throw new Error("wp-codebox-editor-readiness-timeout: Gutenberg block runtime did not become available") diff --git a/tests/browser-routed-command-security.test.ts b/tests/browser-routed-command-security.test.ts index 7ade09a8..c6900f1e 100644 --- a/tests/browser-routed-command-security.test.ts +++ b/tests/browser-routed-command-security.test.ts @@ -23,6 +23,7 @@ const DELAYED_CANVAS_PRESENTATION_IDENTITY = "e".repeat(64) const PARENT_CANVAS_PRESENTATION_IDENTITY = "f".repeat(64) const SLOW_PRESENTATION_IDENTITY = "1".repeat(64) const PENDING_STYLES_PRESENTATION_IDENTITY = "2".repeat(64) +const DELAYED_POST_PRESENTATION_IDENTITY = "3".repeat(64) const matchedPresentationMarkup = `
Matched presentation
` const editorShell = `` const slowPresentationEditorHtml = `${editorShell}` const pendingStylesEditorHtml = `${editorShell}` +const delayedPostEditorHtml = `${editorShell}