From 7059e5058727ce6864853bbcd3d96c89987dd79d Mon Sep 17 00:00:00 2001 From: Kamran Abdul Aziz Date: Mon, 18 May 2026 00:40:00 +0530 Subject: [PATCH] Normalize generation error notices --- .../components/useExcerptGeneration.ts | 8 ++- .../functions/useSummaryGeneration.ts | 8 ++- .../experiments/content-summarization.spec.js | 56 +++++++++++++++++++ .../experiments/excerpt-generation.spec.js | 39 +++++++++++++ 4 files changed, 109 insertions(+), 2 deletions(-) diff --git a/src/experiments/excerpt-generation/components/useExcerptGeneration.ts b/src/experiments/excerpt-generation/components/useExcerptGeneration.ts index d98e747c0..8e3bed080 100644 --- a/src/experiments/excerpt-generation/components/useExcerptGeneration.ts +++ b/src/experiments/excerpt-generation/components/useExcerptGeneration.ts @@ -8,6 +8,7 @@ import { dispatch, useDispatch, useSelect } from '@wordpress/data'; import { store as editorStore } from '@wordpress/editor'; import { useState } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; import { store as noticesStore } from '@wordpress/notices'; /** @@ -115,7 +116,12 @@ export function useExcerptGeneration(): { excerptInput.dispatchEvent( changeEvent ); } } catch ( error: any ) { - ( dispatch( noticesStore ) as any ).createErrorNotice( error, { + const message = + typeof error === 'string' + ? error + : error?.message ?? + __( 'Failed to generate excerpt.', 'ai' ); + ( dispatch( noticesStore ) as any ).createErrorNotice( message, { id: NOTICE_ID, isDismissible: true, } ); diff --git a/src/experiments/summarization/functions/useSummaryGeneration.ts b/src/experiments/summarization/functions/useSummaryGeneration.ts index 71504b4f9..44583cb6b 100644 --- a/src/experiments/summarization/functions/useSummaryGeneration.ts +++ b/src/experiments/summarization/functions/useSummaryGeneration.ts @@ -10,6 +10,7 @@ import { store as blockEditorStore } from '@wordpress/block-editor'; import { dispatch, useDispatch, useSelect } from '@wordpress/data'; import { store as editorStore } from '@wordpress/editor'; import { useEffect, useState } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; import { store as noticesStore } from '@wordpress/notices'; /** @@ -114,7 +115,12 @@ export function useSummaryGeneration() { ); } } catch ( error: any ) { - ( dispatch( noticesStore ) as any ).createErrorNotice( error, { + const message = + typeof error === 'string' + ? error + : error?.message ?? + __( 'Failed to generate summary.', 'ai' ); + ( dispatch( noticesStore ) as any ).createErrorNotice( message, { id: NOTICE_ID, isDismissible: true, } ); diff --git a/tests/e2e/specs/experiments/content-summarization.spec.js b/tests/e2e/specs/experiments/content-summarization.spec.js index 51c466055..ee07f650d 100644 --- a/tests/e2e/specs/experiments/content-summarization.spec.js +++ b/tests/e2e/specs/experiments/content-summarization.spec.js @@ -11,6 +11,7 @@ const { disableExperiments, enableExperiment, enableExperiments, + seedCredentials, } = require( '../../utils/helpers' ); test.describe( 'Content Summarization Experiment', () => { @@ -90,6 +91,61 @@ test.describe( 'Content Summarization Experiment', () => { await editor.saveDraft(); } ); + test( 'Shows a clean error notice when summary generation fails', async ( { + admin, + editor, + page, + requestUtils, + } ) => { + await seedCredentials( requestUtils ); + + // Globally turn on Experiments. + await enableExperiments( admin, page ); + + // Enable the Content Summarization Experiment. + await enableExperiment( admin, page, 'Content Summarization' ); + + await admin.createNewPost( { + postType: 'post', + title: 'Test Content Summarization Error Notice', + content: + 'This post has enough content to meet the minimum character requirement for summarization and trigger the failing AI provider path.', + } ); + + // Save the post. + await editor.saveDraft(); + + // Ensure the sidebar is visible. + await editor.openDocumentSettingsSidebar(); + + // Make the browser-side ability client fail so the notice path is exercised. + await page.evaluate( () => { + window.wp = window.wp || {}; + window.wp.abilities = window.wp.abilities || {}; + window.wp.abilities.executeAbility = async function () { + throw new Error( + 'Summarization failed. Please ensure you have a connected provider that supports text generation.' + ); + }; + } ); + + const generateButton = page.locator( + '.ai-summarization-plugin-container button' + ); + await expect( generateButton ).toBeVisible(); + await expect( generateButton ).toBeEnabled(); + await generateButton.click(); + + const notice = page.locator( '.components-notice', { + hasText: + 'Summarization failed. Please ensure you have a connected provider that supports text generation.', + } ); + await expect( notice ).toBeVisible( { timeout: 5000 } ); + await expect( notice ).not.toContainText( + 'Error: Summarization failed. Please ensure you have a connected provider that supports text generation.' + ); + } ); + test( 'Ensure the Content Summarization Experiment UI is not visible when Experiments are globally disabled', async ( { admin, editor, diff --git a/tests/e2e/specs/experiments/excerpt-generation.spec.js b/tests/e2e/specs/experiments/excerpt-generation.spec.js index c1d23708f..9d158af77 100644 --- a/tests/e2e/specs/experiments/excerpt-generation.spec.js +++ b/tests/e2e/specs/experiments/excerpt-generation.spec.js @@ -11,6 +11,7 @@ const { disableExperiments, enableExperiment, enableExperiments, + seedCredentials, } = require( '../../utils/helpers' ); test.describe( 'Excerpt Generation Experiment', () => { @@ -126,6 +127,44 @@ test.describe( 'Excerpt Generation Experiment', () => { await editor.saveDraft(); } ); + test( 'Shows a clean error notice when excerpt generation fails', async ( { + admin, + editor, + page, + requestUtils, + } ) => { + await seedCredentials( requestUtils ); + + // Globally turn on Experiments. + await enableExperiments( admin, page ); + + // Enable the Excerpt Generation Experiment. + await enableExperiment( admin, page, 'Excerpt Generation' ); + + // Create a new post without content so excerpt generation returns an error. + await admin.createNewPost( { + postType: 'post', + title: 'Test Excerpt Generation Error Notice', + } ); + + // Ensure the sidebar is visible. + await editor.openDocumentSettingsSidebar(); + + const inlineButton = page.locator( + '.editor-post-excerpt__dropdown .ai-excerpt-inline-wrapper .ai-excerpt-inline-button' + ); + await expect( inlineButton ).toBeVisible( { timeout: 5000 } ); + await inlineButton.click(); + + const notice = page.locator( '.components-notice', { + hasText: 'Content is required to generate an excerpt suggestion.', + } ); + await expect( notice ).toBeVisible( { timeout: 5000 } ); + await expect( notice ).not.toContainText( + 'Error: Content is required to generate an excerpt suggestion.' + ); + } ); + test( 'Ensure the Excerpt Generation Experiment UI is not visible when Experiments are globally disabled', async ( { admin, editor,