Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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,
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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,
} );
Expand Down
56 changes: 56 additions & 0 deletions tests/e2e/specs/experiments/content-summarization.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
disableExperiments,
enableExperiment,
enableExperiments,
seedCredentials,
} = require( '../../utils/helpers' );

test.describe( 'Content Summarization Experiment', () => {
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 39 additions & 0 deletions tests/e2e/specs/experiments/excerpt-generation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
disableExperiments,
enableExperiment,
enableExperiments,
seedCredentials,
} = require( '../../utils/helpers' );

test.describe( 'Excerpt Generation Experiment', () => {
Expand Down Expand Up @@ -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,
Expand Down
Loading