From b64a678fb8e2b10b088fbd9c84a1e7b1232956c9 Mon Sep 17 00:00:00 2001 From: Dharmesh Patel Date: Thu, 19 Dec 2024 17:09:44 +0530 Subject: [PATCH] Add E2E tests. --- .../image-processing-openai-chatgpt.test.js | 444 +++++++++--------- .../excerpt-generation-xai-grok.test.js | 124 +++++ .../resize_content-xai-grok.test.js | 101 ++++ .../title-generation-xai-grok.test.js | 148 ++++++ tests/test-plugin/e2e-test-plugin.php | 5 +- 5 files changed, 604 insertions(+), 218 deletions(-) create mode 100644 tests/cypress/integration/language-processing/excerpt-generation-xai-grok.test.js create mode 100644 tests/cypress/integration/language-processing/resize_content-xai-grok.test.js create mode 100644 tests/cypress/integration/language-processing/title-generation-xai-grok.test.js diff --git a/tests/cypress/integration/image-processing/image-processing-openai-chatgpt.test.js b/tests/cypress/integration/image-processing/image-processing-openai-chatgpt.test.js index 10b560105..45cf90d6c 100644 --- a/tests/cypress/integration/image-processing/image-processing-openai-chatgpt.test.js +++ b/tests/cypress/integration/image-processing/image-processing-openai-chatgpt.test.js @@ -1,235 +1,247 @@ import { getChatGPTData } from '../../plugins/functions'; -describe( 'OpenAI Image Processing Tests', () => { - let imageEditLink = ''; - let mediaModelLink = ''; - - before( () => { - cy.login(); - - const imageProcessingFeatures = [ - 'feature_descriptive_text_generator', - ]; +[ 'openai_chatgpt', 'xai_grok' ].forEach( ( provider ) => { + const providerName = + 'openai_chatgpt' === provider ? 'OpenAI ChatGPT' : 'xAI Grok'; + describe( `[${ providerName }] Image Processing Tests`, () => { + let imageEditLink = ''; + let mediaModelLink = ''; + + before( () => { + cy.login(); + + const imageProcessingFeatures = [ + 'feature_descriptive_text_generator', + ]; + + imageProcessingFeatures.forEach( ( feature ) => { + cy.visitFeatureSettings( `image_processing/${ feature }` ); + cy.enableFeature(); + cy.selectProvider( provider ); + cy.get( `#${ provider }_api_key` ).clear().type( 'password' ); + cy.allowFeatureToAdmin(); + cy.get( + '.classifai-settings__user-based-opt-out input' + ).uncheck(); + + // Disable access for all users. + cy.disableFeatureForUsers(); + + cy.saveFeatureSettings(); + } ); - imageProcessingFeatures.forEach( ( feature ) => { - cy.visitFeatureSettings( `image_processing/${ feature }` ); - cy.enableFeature(); - cy.selectProvider( 'openai_chatgpt' ); - cy.get( '#openai_chatgpt_api_key' ).clear().type( 'password' ); - cy.allowFeatureToAdmin(); - cy.get( '.classifai-settings__user-based-opt-out input' ).uncheck(); + cy.optInAllFeatures(); + } ); - // Disable access for all users. - cy.disableFeatureForUsers(); + beforeEach( () => { + cy.login(); + } ); + it( 'Can see Image Processing actions on edit media page and verify generated data.', () => { + cy.visitFeatureSettings( + 'image_processing/feature_descriptive_text_generator' + ); + cy.get( '.classifai-descriptive-text-fields input#alt' ).check(); cy.saveFeatureSettings(); + cy.visit( '/wp-admin/upload.php?mode=grid' ); // Ensure grid mode is enabled. + cy.visit( '/wp-admin/media-new.php' ); + cy.get( '#plupload-upload-ui' ).should( 'exist' ); + cy.get( '#plupload-upload-ui input[type=file]' ).attachFile( + '../../../assets/img/onboarding-1.png' + ); + + cy.get( '#media-items .media-item a.edit-attachment', { + timeout: 20000, + } ).should( 'exist' ); + cy.get( '#media-items .media-item a.edit-attachment' ) + .invoke( 'attr', 'href' ) + .then( ( editLink ) => { + imageEditLink = editLink; + cy.visit( editLink ); + } ); + + // Verify Metabox with Image processing actions. + cy.get( '.postbox-header h2, #classifai_image_processing h2' ) + .first() + .contains( 'ClassifAI Image Processing' ); + cy.get( + '#classifai_image_processing label[for=rescan-captions]' + ).contains( 'No descriptive text? Rescan image' ); + + // Verify generated Data. + const imageData = getChatGPTData(); + cy.get( '#attachment_alt' ).should( 'have.value', imageData ); } ); - cy.optInAllFeatures(); - } ); + it( 'Can see Image Processing actions on media modal', () => { + const imageId = imageEditLink + .split( 'post=' )[ 1 ] + ?.split( '&' )[ 0 ]; + mediaModelLink = `wp-admin/upload.php?item=${ imageId }`; + cy.visit( mediaModelLink ); + cy.get( '.media-modal' ).should( 'exist' ); - beforeEach( () => { - cy.login(); - } ); + // Verify Image processing actions. + cy.get( '#classifai-rescan-alt-tags' ).contains( 'Rescan' ); + } ); - it( 'Can see Image Processing actions on edit media page and verify generated data.', () => { - cy.visitFeatureSettings( - 'image_processing/feature_descriptive_text_generator' - ); - cy.get( '.classifai-descriptive-text-fields input#alt' ).check(); - cy.saveFeatureSettings(); - cy.visit( '/wp-admin/upload.php?mode=grid' ); // Ensure grid mode is enabled. - cy.visit( '/wp-admin/media-new.php' ); - cy.get( '#plupload-upload-ui' ).should( 'exist' ); - cy.get( '#plupload-upload-ui input[type=file]' ).attachFile( - '../../../assets/img/onboarding-1.png' - ); - - cy.get( '#media-items .media-item a.edit-attachment', { - timeout: 20000, - } ).should( 'exist' ); - cy.get( '#media-items .media-item a.edit-attachment' ) - .invoke( 'attr', 'href' ) - .then( ( editLink ) => { - imageEditLink = editLink; - cy.visit( editLink ); - } ); + it( 'Can disable Image Processing features', () => { + const options = { + imageEditLink, + mediaModelLink, + }; + + // Disable features + cy.visitFeatureSettings( + 'image_processing/feature_descriptive_text_generator' + ); + cy.wait( 1000 ); + cy.get( '.classifai-descriptive-text-fields input#alt' ).uncheck(); + cy.get( + '.classifai-descriptive-text-fields input#caption' + ).uncheck(); + cy.get( + '.classifai-descriptive-text-fields input#description' + ).uncheck(); + cy.saveFeatureSettings(); - // Verify Metabox with Image processing actions. - cy.get( '.postbox-header h2, #classifai_image_processing h2' ) - .first() - .contains( 'ClassifAI Image Processing' ); - cy.get( - '#classifai_image_processing label[for=rescan-captions]' - ).contains( 'No descriptive text? Rescan image' ); - - // Verify generated Data. - const imageData = getChatGPTData(); - cy.get( '#attachment_alt' ).should( 'have.value', imageData ); - } ); + // Verify that the feature is not available. + cy.wait( 1000 ); + cy.visit( options.imageEditLink ); + cy.get( + '#classifai_image_processing label[for=rescan-captions]' + ).should( 'not.exist' ); + cy.visit( options.mediaModelLink ); + cy.get( '.media-modal' ).should( 'exist' ); + cy.get( '#classifai-rescan-alt-tags' ).should( 'not.exist' ); + + // Enable features. + cy.visitFeatureSettings( + 'image_processing/feature_descriptive_text_generator' + ); + cy.get( '.classifai-descriptive-text-fields input#alt' ).check(); + cy.get( + '.classifai-descriptive-text-fields input#caption' + ).check(); + cy.get( + '.classifai-descriptive-text-fields input#description' + ).check(); + cy.wait( 1500 ); + cy.enableFeature(); + cy.saveFeatureSettings(); - it( 'Can see Image Processing actions on media modal', () => { - const imageId = imageEditLink.split( 'post=' )[ 1 ]?.split( '&' )[ 0 ]; - mediaModelLink = `wp-admin/upload.php?item=${ imageId }`; - cy.visit( mediaModelLink ); - cy.get( '.media-modal' ).should( 'exist' ); + // Verify that the feature is available. + cy.wait( 1000 ); + cy.visit( options.imageEditLink ); + cy.get( + '#classifai_image_processing label[for=rescan-captions]' + ).should( 'exist' ); + cy.visit( options.mediaModelLink ); + cy.get( '.media-modal' ).should( 'exist' ); + cy.get( '#classifai-rescan-alt-tags' ).should( 'exist' ); + } ); - // Verify Image processing actions. - cy.get( '#classifai-rescan-alt-tags' ).contains( 'Rescan' ); - } ); + it( 'Can enable/disable Image Processing features by roles', () => { + const options = { + imageEditLink, + mediaModelLink, + }; - it( 'Can disable Image Processing features', () => { - const options = { - imageEditLink, - mediaModelLink, - }; - - // Disable features - cy.visitFeatureSettings( - 'image_processing/feature_descriptive_text_generator' - ); - cy.wait( 1000 ); - cy.get( '.classifai-descriptive-text-fields input#alt' ).uncheck(); - cy.get( '.classifai-descriptive-text-fields input#caption' ).uncheck(); - cy.get( - '.classifai-descriptive-text-fields input#description' - ).uncheck(); - cy.saveFeatureSettings(); - - // Verify that the feature is not available. - cy.wait( 1000 ); - cy.visit( options.imageEditLink ); - cy.get( - '#classifai_image_processing label[for=rescan-captions]' - ).should( 'not.exist' ); - cy.visit( options.mediaModelLink ); - cy.get( '.media-modal' ).should( 'exist' ); - cy.get( '#classifai-rescan-alt-tags' ).should( 'not.exist' ); - - // Enable features. - cy.visitFeatureSettings( - 'image_processing/feature_descriptive_text_generator' - ); - cy.get( '.classifai-descriptive-text-fields input#alt' ).check(); - cy.get( '.classifai-descriptive-text-fields input#caption' ).check(); - cy.get( - '.classifai-descriptive-text-fields input#description' - ).check(); - cy.wait( 1500 ); - cy.enableFeature(); - cy.saveFeatureSettings(); - - // Verify that the feature is available. - cy.wait( 1000 ); - cy.visit( options.imageEditLink ); - cy.get( - '#classifai_image_processing label[for=rescan-captions]' - ).should( 'exist' ); - cy.visit( options.mediaModelLink ); - cy.get( '.media-modal' ).should( 'exist' ); - cy.get( '#classifai-rescan-alt-tags' ).should( 'exist' ); - } ); + // Enable features. + cy.visitFeatureSettings( + 'image_processing/feature_descriptive_text_generator' + ); + cy.enableFeature(); + cy.get( '.classifai-descriptive-text-fields input#alt' ).check(); + cy.wait( 500 ); + cy.saveFeatureSettings(); - it( 'Can enable/disable Image Processing features by roles', () => { - const options = { - imageEditLink, - mediaModelLink, - }; - - // Enable features. - cy.visitFeatureSettings( - 'image_processing/feature_descriptive_text_generator' - ); - cy.enableFeature(); - cy.get( '.classifai-descriptive-text-fields input#alt' ).check(); - cy.wait( 500 ); - cy.saveFeatureSettings(); - - // Disable access to admin role. - cy.disableFeatureForRoles( 'feature_descriptive_text_generator', [ - 'administrator', - ] ); - - // Verify that the feature is not available. - cy.wait( 1000 ); - cy.visit( options.imageEditLink ); - cy.get( - '#classifai_image_processing label[for=rescan-captions]' - ).should( 'not.exist' ); - cy.visit( options.mediaModelLink ); - cy.get( '.media-modal' ).should( 'exist' ); - cy.get( '#classifai-rescan-alt-tags' ).should( 'not.exist' ); - } ); + // Disable access to admin role. + cy.disableFeatureForRoles( 'feature_descriptive_text_generator', [ + 'administrator', + ] ); + + // Verify that the feature is not available. + cy.wait( 1000 ); + cy.visit( options.imageEditLink ); + cy.get( + '#classifai_image_processing label[for=rescan-captions]' + ).should( 'not.exist' ); + cy.visit( options.mediaModelLink ); + cy.get( '.media-modal' ).should( 'exist' ); + cy.get( '#classifai-rescan-alt-tags' ).should( 'not.exist' ); + } ); - it( 'Can enable/disable Image Processing features by user', () => { - const options = { - imageEditLink, - mediaModelLink, - }; - - // Disable access to admin role. - cy.disableFeatureForRoles( 'feature_descriptive_text_generator', [ - 'administrator', - ] ); - - // Verify that the feature is not available. - cy.wait( 1000 ); - cy.visit( options.imageEditLink ); - cy.get( - '#classifai_image_processing label[for=rescan-captions]' - ).should( 'not.exist' ); - cy.visit( options.mediaModelLink ); - cy.get( '.media-modal' ).should( 'exist' ); - cy.get( '#classifai-rescan-alt-tags' ).should( 'not.exist' ); - - cy.enableFeatureForUsers( 'feature_descriptive_text_generator', [ - 'admin', - ] ); - - // Verify that the feature is available. - cy.wait( 1000 ); - cy.visit( options.imageEditLink ); - cy.get( - '#classifai_image_processing label[for=rescan-captions]' - ).should( 'exist' ); - cy.visit( options.mediaModelLink ); - cy.get( '.media-modal' ).should( 'exist' ); - cy.get( '#classifai-rescan-alt-tags' ).should( 'exist' ); - } ); + it( 'Can enable/disable Image Processing features by user', () => { + const options = { + imageEditLink, + mediaModelLink, + }; + + // Disable access to admin role. + cy.disableFeatureForRoles( 'feature_descriptive_text_generator', [ + 'administrator', + ] ); + + // Verify that the feature is not available. + cy.wait( 1000 ); + cy.visit( options.imageEditLink ); + cy.get( + '#classifai_image_processing label[for=rescan-captions]' + ).should( 'not.exist' ); + cy.visit( options.mediaModelLink ); + cy.get( '.media-modal' ).should( 'exist' ); + cy.get( '#classifai-rescan-alt-tags' ).should( 'not.exist' ); + + cy.enableFeatureForUsers( 'feature_descriptive_text_generator', [ + 'admin', + ] ); + + // Verify that the feature is available. + cy.wait( 1000 ); + cy.visit( options.imageEditLink ); + cy.get( + '#classifai_image_processing label[for=rescan-captions]' + ).should( 'exist' ); + cy.visit( options.mediaModelLink ); + cy.get( '.media-modal' ).should( 'exist' ); + cy.get( '#classifai-rescan-alt-tags' ).should( 'exist' ); + } ); - it( 'User can opt-out of Image Processing features', () => { - const options = { - imageEditLink, - mediaModelLink, - }; - - // Enable user based opt-out. - cy.enableFeatureOptOut( 'feature_descriptive_text_generator' ); - - // opt-out - cy.optOutFeature( 'feature_descriptive_text_generator' ); - - // Verify that the feature is not available. - cy.wait( 1000 ); - cy.visit( options.imageEditLink ); - cy.get( - '#classifai_image_processing label[for=rescan-captions]' - ).should( 'not.exist' ); - cy.visit( options.mediaModelLink ); - cy.get( '.media-modal' ).should( 'exist' ); - cy.get( '#classifai-rescan-alt-tags' ).should( 'not.exist' ); - - // opt-in - cy.optInFeature( 'feature_descriptive_text_generator' ); - - // Verify that the feature is available. - cy.wait( 1000 ); - cy.visit( options.imageEditLink ); - cy.get( - '#classifai_image_processing label[for=rescan-captions]' - ).should( 'exist' ); - cy.visit( options.mediaModelLink ); - cy.get( '.media-modal' ).should( 'exist' ); - cy.get( '#classifai-rescan-alt-tags' ).should( 'exist' ); + it( 'User can opt-out of Image Processing features', () => { + const options = { + imageEditLink, + mediaModelLink, + }; + + // Enable user based opt-out. + cy.enableFeatureOptOut( 'feature_descriptive_text_generator' ); + + // opt-out + cy.optOutFeature( 'feature_descriptive_text_generator' ); + + // Verify that the feature is not available. + cy.wait( 1000 ); + cy.visit( options.imageEditLink ); + cy.get( + '#classifai_image_processing label[for=rescan-captions]' + ).should( 'not.exist' ); + cy.visit( options.mediaModelLink ); + cy.get( '.media-modal' ).should( 'exist' ); + cy.get( '#classifai-rescan-alt-tags' ).should( 'not.exist' ); + + // opt-in + cy.optInFeature( 'feature_descriptive_text_generator' ); + + // Verify that the feature is available. + cy.wait( 1000 ); + cy.visit( options.imageEditLink ); + cy.get( + '#classifai_image_processing label[for=rescan-captions]' + ).should( 'exist' ); + cy.visit( options.mediaModelLink ); + cy.get( '.media-modal' ).should( 'exist' ); + cy.get( '#classifai-rescan-alt-tags' ).should( 'exist' ); + } ); } ); } ); diff --git a/tests/cypress/integration/language-processing/excerpt-generation-xai-grok.test.js b/tests/cypress/integration/language-processing/excerpt-generation-xai-grok.test.js new file mode 100644 index 000000000..26910575c --- /dev/null +++ b/tests/cypress/integration/language-processing/excerpt-generation-xai-grok.test.js @@ -0,0 +1,124 @@ +import { getChatGPTData } from '../../plugins/functions'; + +describe( '[Language processing] Excerpt Generation Tests', () => { + before( () => { + cy.login(); + cy.visitFeatureSettings( + 'language_processing/feature_excerpt_generation' + ); + cy.get( '#classifai-logo' ).should( 'exist' ); + cy.enableFeature(); + cy.get( '.settings-allowed-post-types input#post' ).check(); + cy.get( '#classifai-prompt-setting-0' ).then( ( $prompt ) => { + if ( + $prompt + .find( '.actions-rows button.action__set_default' ) + .text() === 'Set as default prompt' + ) { + cy.get( + '#classifai-prompt-setting-0 .actions-rows button.action__set_default' + ).click(); + } + } ); + cy.saveFeatureSettings(); + cy.optInAllFeatures(); + cy.disableClassicEditor(); + } ); + + beforeEach( () => { + cy.login(); + } ); + + it( 'Can save Azure OpenAI "Language Processing" settings', () => { + cy.visitFeatureSettings( + 'language_processing/feature_excerpt_generation' + ); + cy.get( '#classifai-logo' ).should( 'exist' ); + cy.selectProvider( 'xai_grok' ); + cy.get( '#xai_grok_api_key' ).clear().type( 'password' ); + + cy.enableFeature(); + cy.allowFeatureToAdmin(); + cy.get( '#excerpt_length' ).clear().type( 35 ); + cy.saveFeatureSettings(); + } ); + + it( 'Can see the generate excerpt button in a post', () => { + cy.visit( '/wp-admin/plugins.php' ); + cy.disableClassicEditor(); + + const data = getChatGPTData(); + + // Create test post. + cy.createPost( { + title: 'Test xAI Grok post', + content: 'Test content', + } ); + + // Close post publish panel. + const closePanelSelector = 'button[aria-label="Close panel"]'; + cy.get( 'body' ).then( ( $body ) => { + if ( $body.find( closePanelSelector ).length > 0 ) { + cy.get( closePanelSelector ).click(); + } + } ); + + // Open post settings sidebar. + cy.openDocumentSettingsSidebar(); + + // Find and open the excerpt panel. + const panelButtonSelector = `.components-panel__body .components-panel__body-title button:contains("Excerpt")`; + + cy.get( panelButtonSelector ).then( ( $panelButton ) => { + // Find the panel container. + const $panel = $panelButton.parents( '.components-panel__body' ); + + // Open panel. + if ( ! $panel.hasClass( 'is-opened' ) ) { + cy.wrap( $panelButton ).click(); + } + + // Verify button exists. + cy.wrap( $panel ) + .find( '.editor-post-excerpt button' ) + .should( 'exist' ); + + // Click on button and verify data loads in. + cy.wrap( $panel ).find( '.editor-post-excerpt button' ).click(); + cy.wrap( $panel ).find( 'textarea' ).should( 'have.value', data ); + } ); + } ); + + it( 'Can see the generate excerpt button in a post (Classic Editor)', () => { + cy.enableClassicEditor(); + + cy.visitFeatureSettings( + 'language_processing/feature_excerpt_generation' + ); + cy.enableFeature(); + cy.saveFeatureSettings(); + + const data = getChatGPTData(); + + cy.classicCreatePost( { + title: 'Excerpt test classic', + content: 'Test GPT content.', + postType: 'post', + } ); + + // Ensure excerpt metabox is shown. + cy.get( '#show-settings-link' ).click(); + cy.get( '#postexcerpt-hide' ).check( { force: true } ); + + // Verify button exists. + cy.get( '#classifai-excerpt-generation__excerpt-generate-btn' ).should( + 'exist' + ); + + // Click on button and verify data loads in. + cy.get( '#classifai-excerpt-generation__excerpt-generate-btn' ).click(); + cy.get( '#excerpt' ).should( 'have.value', data ); + + cy.disableClassicEditor(); + } ); +} ); diff --git a/tests/cypress/integration/language-processing/resize_content-xai-grok.test.js b/tests/cypress/integration/language-processing/resize_content-xai-grok.test.js new file mode 100644 index 000000000..dbeaa0e30 --- /dev/null +++ b/tests/cypress/integration/language-processing/resize_content-xai-grok.test.js @@ -0,0 +1,101 @@ +describe( '[Language processing] Resize Content Tests', () => { + before( () => { + cy.login(); + cy.visitFeatureSettings( + 'language_processing/feature_content_resizing' + ); + cy.enableFeature(); + cy.selectProvider( 'xai_grok' ); + cy.get( '#xai_grok_api_key' ).type( 'abc123' ); + cy.get( + '.settings-condense-text-prompt #classifai-prompt-setting-0' + ).then( ( $prompt ) => { + if ( + $prompt + .find( '.actions-rows button.action__set_default' ) + .text() === 'Set as default prompt' + ) { + cy.get( + '.settings-condense-text-prompt #classifai-prompt-setting-0 .actions-rows button.action__set_default' + ).click(); + } + } ); + cy.get( + '.settings-expand-text-prompt #classifai-prompt-setting-0' + ).then( ( $prompt ) => { + if ( + $prompt + .find( '.actions-rows button.action__set_default' ) + .text() === 'Set as default prompt' + ) { + cy.get( + '.settings-expand-text-prompt #classifai-prompt-setting-0 .actions-rows button.action__set_default' + ).click(); + } + } ); + cy.saveFeatureSettings(); + cy.optInAllFeatures(); + cy.disableClassicEditor(); + } ); + + beforeEach( () => { + cy.login(); + } ); + + it( 'Resize content feature can grow and shrink content', () => { + cy.visitFeatureSettings( + 'language_processing/feature_content_resizing' + ); + + cy.enableFeature(); + cy.allowFeatureToAdmin(); + cy.saveFeatureSettings(); + + cy.createPost( { + title: 'Resize content', + content: 'Hello, world.', + } ); + + cy.get( '.classifai-resize-content-btn' ).click(); + cy.get( '.components-button' ).contains( 'Expand this text' ).click(); + cy.get( + '.classifai-content-resize__result-table tbody tr:first .classifai-content-resize__grow-stat' + ).should( 'contain.text', '+7 words' ); + cy.get( + '.classifai-content-resize__result-table tbody tr:first .classifai-content-resize__grow-stat' + ).should( 'contain.text', '+40 characters' ); + cy.get( + '.classifai-content-resize__result-table tbody tr:first button' + ).click(); + cy.getBlockEditor() + .find( '[data-type="core/paragraph"]' ) + .should( + 'contain.text', + 'Start with the basic building block of one narrative.' + ); + + cy.createPost( { + title: 'Resize content', + content: + 'Start with the basic building block of one narrative to begin with the editorial process.', + } ); + + cy.get( '.classifai-resize-content-btn' ).click(); + cy.get( '.components-button' ).contains( 'Condense this text' ).click(); + cy.get( + '.classifai-content-resize__result-table tbody tr:first .classifai-content-resize__shrink-stat' + ).should( 'contain.text', '-6 words' ); + cy.get( + '.classifai-content-resize__result-table tbody tr:first .classifai-content-resize__shrink-stat' + ).should( 'contain.text', '-36 characters' ); + cy.get( + '.classifai-content-resize__result-table tbody tr:first button' + ).click(); + cy.getBlockEditor() + .find( '[data-type="core/paragraph"]' ) + .should( + 'contain.text', + 'Start with the basic building block of one narrative.' + ); + } ); +} ); diff --git a/tests/cypress/integration/language-processing/title-generation-xai-grok.test.js b/tests/cypress/integration/language-processing/title-generation-xai-grok.test.js new file mode 100644 index 000000000..88b39eec4 --- /dev/null +++ b/tests/cypress/integration/language-processing/title-generation-xai-grok.test.js @@ -0,0 +1,148 @@ +import { getChatGPTData } from '../../plugins/functions'; + +describe( '[Language processing] Title Generation Tests', () => { + before( () => { + cy.login(); + cy.optInAllFeatures(); + cy.disableClassicEditor(); + } ); + + beforeEach( () => { + cy.login(); + } ); + + it( 'Can save OpenAI ChatGPT "Language Processing" title settings', () => { + cy.visitFeatureSettings( + 'language_processing/feature_title_generation' + ); + cy.get( '#classifai-logo' ).should( 'exist' ); + cy.selectProvider( 'xai_grok' ); + cy.get( '#xai_grok_api_key' ).clear().type( 'password' ); + cy.enableFeature(); + cy.allowFeatureToAdmin(); + cy.get( '#xai_grok_number_of_suggestions' ).type( 1 ); + cy.get( '#classifai-prompt-setting-0' ).then( ( $prompt ) => { + if ( + $prompt + .find( '.actions-rows button.action__set_default' ) + .text() === 'Set as default prompt' + ) { + cy.get( + '#classifai-prompt-setting-0 .actions-rows button.action__set_default' + ).click(); + } + } ); + cy.saveFeatureSettings(); + } ); + + it( 'Can see the generate titles button in a post', () => { + const data = getChatGPTData(); + + // Create test post. + cy.createPost( { + title: 'Test Grok generate titles', + content: 'Test content', + } ); + + // Close post publish panel. + const closePanelSelector = 'button[aria-label="Close panel"]'; + cy.get( 'body' ).then( ( $body ) => { + if ( $body.find( closePanelSelector ).length > 0 ) { + cy.get( closePanelSelector ).click(); + } + } ); + + // Open post settings sidebar. + cy.openDocumentSettingsSidebar(); + + // Find and open the summary panel. + const panelButtonSelector = `.components-panel__body.edit-post-post-status .components-panel__body-title button,.editor-sidebar__panel .editor-post-panel__section .editor-post-card-panel`; + + cy.get( panelButtonSelector ).then( ( $panelButton ) => { + // Support pre WP 6.6+. + const $newPanel = $panelButton.parents( + '.editor-post-panel__section' + ); + + if ( $newPanel.length === 0 ) { + // Find the panel container. + const $panel = $panelButton.parents( + '.components-panel__body' + ); + + // Open panel. + if ( ! $panel.hasClass( 'is-opened' ) ) { + cy.wrap( $panelButton ).click(); + } + + // Verify button exists. + cy.wrap( $panel ) + .find( '.classifai-post-status button.title' ) + .should( 'exist' ); + + // Click on button and verify modal shows. + cy.wrap( $panel ) + .find( '.classifai-post-status button.title' ) + .click(); + } else { + // Verify button exists. + cy.wrap( $newPanel ) + .find( '.classifai-post-status button.title' ) + .should( 'exist' ); + + // Click on button and verify modal shows. + cy.wrap( $newPanel ) + .find( '.classifai-post-status button.title' ) + .click(); + } + } ); + + cy.get( '.title-modal' ).should( 'exist' ); + + // Click on button and verify data loads in. + cy.get( '.title-modal .classifai-title' ) + .first() + .find( 'textarea' ) + .should( 'have.value', data ); + cy.get( '.title-modal .classifai-title' ) + .first() + .find( 'button' ) + .click(); + + cy.get( '.title-modal' ).should( 'not.exist' ); + cy.getBlockEditor() + .find( '.editor-post-title__input' ) + .should( ( $el ) => { + expect( $el.first() ).to.contain( data ); + } ); + } ); + + it( 'Can see the generate titles button in a post (Classic Editor)', () => { + cy.enableClassicEditor(); + + cy.visitFeatureSettings( + 'language_processing/feature_title_generation' + ); + cy.enableFeature(); + cy.saveFeatureSettings(); + + const data = getChatGPTData(); + + cy.visit( '/wp-admin/post-new.php' ); + + cy.get( '#classifai-title-generation__title-generate-btn' ).click(); + cy.get( '#classifai-title-generation__modal' ).should( 'be.visible' ); + cy.get( '.classifai-title-generation__result-item' ) + .first() + .find( 'textarea' ) + .should( 'have.value', data ); + + cy.get( '.classifai-title-generation__select-title' ).first().click(); + cy.get( '#classifai-title-generation__modal' ).should( + 'not.be.visible' + ); + cy.get( '#title' ).should( 'have.value', data ); + + cy.disableClassicEditor(); + } ); +} ); diff --git a/tests/test-plugin/e2e-test-plugin.php b/tests/test-plugin/e2e-test-plugin.php index 3659f1ffd..90052981d 100644 --- a/tests/test-plugin/e2e-test-plugin.php +++ b/tests/test-plugin/e2e-test-plugin.php @@ -23,12 +23,13 @@ function classifai_test_mock_http_requests( $preempt, $parsed_args, $url ) { if ( strpos( $url, 'http://e2e-test-nlu-server.test/v1/analyze' ) !== false ) { $response = file_get_contents( __DIR__ . '/nlu.json' ); - } elseif ( strpos( $url, 'https://api.openai.com/v1/models' ) !== false ) { + } elseif ( strpos( $url, 'https://api.openai.com/v1/models' ) !== false || strpos( $url, 'https://api.x.ai/v1/models' ) !== false ) { $response = file_get_contents( __DIR__ . '/models.json' ); - } elseif ( strpos( $url, 'https://api.openai.com/v1/completions' ) !== false ) { + } elseif ( strpos( $url, 'https://api.openai.com/v1/completions' ) !== false || strpos( $url, 'https://api.x.ai/v1/completions' ) !== false ) { $response = file_get_contents( __DIR__ . '/chatgpt.json' ); } elseif ( strpos( $url, 'https://api.openai.com/v1/chat/completions' ) !== false || + strpos( $url, 'https://api.x.ai/v1/chat/completions' ) !== false || strpos( $url, 'https://e2e-test-azure-openai.test/openai/deployments' ) !== false ) { $response = file_get_contents( __DIR__ . '/chatgpt.json' );