Skip to content

Commit 8314ba0

Browse files
committed
Added E2E tests.
1 parent 41e5e33 commit 8314ba0

10 files changed

+459
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { getGeminiAPIData } from '../../plugins/functions';
2+
3+
describe( '[Language processing] Excerpt Generation Tests', () => {
4+
before( () => {
5+
cy.login();
6+
cy.visit(
7+
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_excerpt_generation'
8+
);
9+
cy.get( '#status' ).check();
10+
cy.get(
11+
'#classifai_feature_excerpt_generation_post_types_post'
12+
).check();
13+
cy.get( '#submit' ).click();
14+
cy.optInAllFeatures();
15+
cy.disableClassicEditor();
16+
} );
17+
18+
beforeEach( () => {
19+
cy.login();
20+
} );
21+
22+
it( 'Can save Google AI (Gemini API) "Language Processing" settings', () => {
23+
cy.visit(
24+
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_excerpt_generation'
25+
);
26+
cy.get( '#provider' ).select( 'googleai_gemini_api' );
27+
cy.get(
28+
'input[name="classifai_feature_excerpt_generation[googleai_gemini_api][api_key]"]'
29+
)
30+
.clear()
31+
.type( 'password' );
32+
33+
cy.get( '#status' ).check();
34+
cy.get(
35+
'#classifai_feature_excerpt_generation_roles_administrator'
36+
).check();
37+
cy.get( '#length' ).clear().type( 35 );
38+
cy.get( '#submit' ).click();
39+
} );
40+
41+
it( 'Can see the generate excerpt button in a post', () => {
42+
cy.visit( '/wp-admin/plugins.php' );
43+
cy.disableClassicEditor();
44+
45+
const data = getGeminiAPIData();
46+
47+
// Create test post.
48+
cy.createPost( {
49+
title: 'Test ChatGPT post',
50+
content: 'Test GPT content',
51+
} );
52+
53+
// Close post publish panel.
54+
const closePanelSelector = 'button[aria-label="Close panel"]';
55+
cy.get( 'body' ).then( ( $body ) => {
56+
if ( $body.find( closePanelSelector ).length > 0 ) {
57+
cy.get( closePanelSelector ).click();
58+
}
59+
} );
60+
61+
// Open post settings sidebar.
62+
cy.openDocumentSettingsSidebar();
63+
64+
// Find and open the excerpt panel.
65+
const panelButtonSelector = `.components-panel__body .components-panel__body-title button:contains("Excerpt")`;
66+
67+
cy.get( panelButtonSelector ).then( ( $panelButton ) => {
68+
// Find the panel container.
69+
const $panel = $panelButton.parents( '.components-panel__body' );
70+
71+
// Open panel.
72+
if ( ! $panel.hasClass( 'is-opened' ) ) {
73+
cy.wrap( $panelButton ).click();
74+
}
75+
76+
// Verify button exists.
77+
cy.wrap( $panel )
78+
.find( '.editor-post-excerpt button' )
79+
.should( 'exist' );
80+
81+
// Click on button and verify data loads in.
82+
cy.wrap( $panel ).find( '.editor-post-excerpt button' ).click();
83+
cy.wrap( $panel ).find( 'textarea' ).should( 'have.value', data );
84+
} );
85+
} );
86+
87+
it( 'Can see the generate excerpt button in a post (Classic Editor)', () => {
88+
cy.enableClassicEditor();
89+
90+
cy.visit(
91+
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_excerpt_generation'
92+
);
93+
cy.get( '#status' ).check();
94+
cy.get( '#submit' ).click();
95+
96+
const data = getGeminiAPIData();
97+
98+
cy.createClassicPost( {
99+
title: 'Excerpt test classic',
100+
content: 'Test GPT content.',
101+
postType: 'post',
102+
} );
103+
104+
// Ensure excerpt metabox is shown.
105+
cy.get( '#show-settings-link' ).click();
106+
cy.get( '#postexcerpt-hide' ).check( { force: true } );
107+
108+
// Verify button exists.
109+
cy.get( '#classifai-openai__excerpt-generate-btn' ).should( 'exist' );
110+
111+
// Click on button and verify data loads in.
112+
cy.get( '#classifai-openai__excerpt-generate-btn' ).click();
113+
cy.get( '#excerpt' ).should( 'have.value', data );
114+
115+
cy.disableClassicEditor();
116+
} );
117+
} );

tests/cypress/integration/language-processing/excerpt-generation-openapi-chatgpt.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe( '[Language processing] Excerpt Generation Tests', () => {
2424
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_excerpt_generation'
2525
);
2626

27+
cy.get( '#provider' ).select( 'openai_chatgpt' );
2728
cy.get( '#api_key' ).clear().type( 'password' );
2829

2930
cy.get( '#status' ).check();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
describe( '[Language processing] Speech to Text Tests', () => {
2+
before( () => {
3+
cy.login();
4+
cy.visit(
5+
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_content_resizing'
6+
);
7+
cy.get( '#status' ).check();
8+
cy.get( '#provider' ).select( 'googleai_gemini_api' );
9+
cy.get(
10+
'input[name="classifai_feature_content_resizing[googleai_gemini_api][api_key]"]'
11+
)
12+
.clear()
13+
.type( 'abc123' );
14+
cy.get( '#submit' ).click();
15+
cy.optInAllFeatures();
16+
cy.disableClassicEditor();
17+
} );
18+
19+
beforeEach( () => {
20+
cy.login();
21+
} );
22+
23+
it( 'Resize content feature can grow and shrink content', () => {
24+
cy.visit(
25+
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_content_resizing'
26+
);
27+
28+
cy.get( '#status' ).check();
29+
cy.get(
30+
'#classifai_feature_content_resizing_roles_administrator'
31+
).check();
32+
cy.get( '#submit' ).click();
33+
34+
cy.createPost( {
35+
title: 'Resize content',
36+
content: 'Hello, world.',
37+
} );
38+
39+
cy.get( '.classifai-resize-content-btn' ).click();
40+
cy.get( '.components-button' ).contains( 'Expand this text' ).click();
41+
cy.get(
42+
'.classifai-content-resize__result-table tbody tr:first .classifai-content-resize__grow-stat'
43+
).should( 'contain.text', '+8 words' );
44+
cy.get(
45+
'.classifai-content-resize__result-table tbody tr:first .classifai-content-resize__grow-stat'
46+
).should( 'contain.text', '+49 characters' );
47+
cy.get(
48+
'.classifai-content-resize__result-table tbody tr:first button'
49+
).click();
50+
cy.getBlockEditor()
51+
.find( '[data-type="core/paragraph"]' )
52+
.should(
53+
'contain.text',
54+
'Start with the basic building block of one narrative.'
55+
);
56+
57+
cy.createPost( {
58+
title: 'Resize content',
59+
content:
60+
'Start with the basic building block of one narrative to begin with the editorial process.',
61+
} );
62+
63+
cy.get( '.classifai-resize-content-btn' ).click();
64+
cy.get( '.components-button' ).contains( 'Condense this text' ).click();
65+
cy.get(
66+
'.classifai-content-resize__result-table tbody tr:first .classifai-content-resize__shrink-stat'
67+
).should( 'contain.text', '-5 words' );
68+
cy.get(
69+
'.classifai-content-resize__result-table tbody tr:first .classifai-content-resize__shrink-stat'
70+
).should( 'contain.text', '-27 characters' );
71+
cy.get(
72+
'.classifai-content-resize__result-table tbody tr:first button'
73+
).click();
74+
cy.getBlockEditor()
75+
.find( '[data-type="core/paragraph"]' )
76+
.should(
77+
'contain.text',
78+
'Start with the basic building block of one narrative.'
79+
);
80+
} );
81+
} );

tests/cypress/integration/language-processing/resize_content-openapi-chatgpt.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ describe( '[Language processing] Speech to Text Tests', () => {
55
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_content_resizing'
66
);
77
cy.get( '#status' ).check();
8+
cy.get( '#provider' ).select( 'openai_chatgpt' );
89
cy.get( '#api_key' ).type( 'abc123' );
910
cy.get( '#submit' ).click();
1011
cy.optInAllFeatures();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { getGeminiAPIData } from '../../plugins/functions';
2+
3+
describe( '[Language processing] Title Generation Tests', () => {
4+
before( () => {
5+
cy.login();
6+
cy.optInAllFeatures();
7+
cy.disableClassicEditor();
8+
} );
9+
10+
beforeEach( () => {
11+
cy.login();
12+
} );
13+
14+
it( 'Can save Google AI (Gemini API) "Language Processing" title settings', () => {
15+
cy.visit(
16+
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_title_generation'
17+
);
18+
19+
cy.get( '#provider' ).select( 'googleai_gemini_api' );
20+
cy.get(
21+
'input[name="classifai_feature_title_generation[googleai_gemini_api][api_key]"]'
22+
)
23+
.clear()
24+
.type( 'password' );
25+
cy.get( '#status' ).check();
26+
cy.get(
27+
'#classifai_feature_title_generation_roles_administrator'
28+
).check();
29+
cy.get( '#submit' ).click();
30+
} );
31+
32+
it( 'Can see the generate titles button in a post', () => {
33+
const data = getGeminiAPIData();
34+
35+
// Create test post.
36+
cy.createPost( {
37+
title: 'Test ChatGPT generate titles',
38+
content: 'Test content',
39+
} );
40+
41+
// Close post publish panel.
42+
const closePanelSelector = 'button[aria-label="Close panel"]';
43+
cy.get( 'body' ).then( ( $body ) => {
44+
if ( $body.find( closePanelSelector ).length > 0 ) {
45+
cy.get( closePanelSelector ).click();
46+
}
47+
} );
48+
49+
// Open post settings sidebar.
50+
cy.openDocumentSettingsSidebar();
51+
52+
// Find and open the summary panel.
53+
const panelButtonSelector = `.components-panel__body.edit-post-post-status .components-panel__body-title button`;
54+
55+
cy.get( panelButtonSelector ).then( ( $panelButton ) => {
56+
// Find the panel container.
57+
const $panel = $panelButton.parents( '.components-panel__body' );
58+
59+
// Open panel.
60+
if ( ! $panel.hasClass( 'is-opened' ) ) {
61+
cy.wrap( $panelButton ).click();
62+
}
63+
64+
// Verify button exists.
65+
cy.wrap( $panel )
66+
.find( '.classifai-post-status button.title' )
67+
.should( 'exist' );
68+
69+
// Click on button and verify modal shows.
70+
cy.wrap( $panel )
71+
.find( '.classifai-post-status button.title' )
72+
.click();
73+
} );
74+
75+
cy.get( '.title-modal' ).should( 'exist' );
76+
77+
// Click on button and verify data loads in.
78+
cy.get( '.title-modal .classifai-title' )
79+
.first()
80+
.find( 'textarea' )
81+
.should( 'have.value', data );
82+
cy.get( '.title-modal .classifai-title' )
83+
.first()
84+
.find( 'button' )
85+
.click();
86+
87+
cy.get( '.title-modal' ).should( 'not.exist' );
88+
cy.getBlockEditor()
89+
.find( '.editor-post-title__input' )
90+
.should( ( $el ) => {
91+
expect( $el.first() ).to.contain( data );
92+
} );
93+
} );
94+
95+
it( 'Can see the generate titles button in a post (Classic Editor)', () => {
96+
cy.enableClassicEditor();
97+
98+
cy.visit(
99+
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_title_generation'
100+
);
101+
cy.get( '#status' ).check();
102+
cy.get( '#submit' ).click();
103+
104+
const data = getGeminiAPIData();
105+
106+
cy.visit( '/wp-admin/post-new.php' );
107+
108+
cy.get( '#classifai-openai__title-generate-btn' ).click();
109+
cy.get( '#classifai-openai__modal' ).should( 'be.visible' );
110+
cy.get( '.classifai-openai__result-item' )
111+
.first()
112+
.find( 'textarea' )
113+
.should( 'have.value', data );
114+
115+
cy.get( '.classifai-openai__select-title' ).first().click();
116+
cy.get( '#classifai-openai__modal' ).should( 'not.be.visible' );
117+
cy.get( '#title' ).should( 'have.value', data );
118+
119+
cy.disableClassicEditor();
120+
} );
121+
} );

tests/cypress/integration/language-processing/title-generation-openapi-chatgpt.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe( '[Language processing] Title Generation Tests', () => {
1616
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_title_generation'
1717
);
1818

19+
cy.get( '#provider' ).select( 'openai_chatgpt' );
1920
cy.get( '#api_key' ).clear().type( 'password' );
2021
cy.get( '#status' ).check();
2122
cy.get(

tests/cypress/plugins/functions.js

+15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as ocrData from '../../test-plugin/ocr.json';
77
import * as whisperData from '../../test-plugin/whisper.json';
88
import * as imageData from '../../test-plugin/image_analyze.json';
99
import * as pdfData from '../../test-plugin/pdf.json';
10+
import * as geminiData from '../../test-plugin/geminiapi.json';
1011

1112
/**
1213
* Get Taxonomy data from test NLU json file.
@@ -59,6 +60,20 @@ export const getChatGPTData = ( type = 'default' ) => {
5960
return text.join( ' ' );
6061
};
6162

63+
/**
64+
* Get text data from test GeminiAPI json file.
65+
*
66+
* @return {string[]} GeminiAPI Data.
67+
*/
68+
export const getGeminiAPIData = () => {
69+
const text = [];
70+
geminiData.candidates.forEach( ( el ) => {
71+
text.push( el.content.parts[ 0 ].text );
72+
} );
73+
74+
return text.join( ' ' );
75+
};
76+
6277
/**
6378
* Get data from test DALL·E json file.
6479
*

tests/test-plugin/e2e-test-plugin.php

+14
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ function classifai_test_mock_http_requests( $preempt, $parsed_args, $url ) {
8484
'success' => 1,
8585
'body' => '',
8686
);
87+
} elseif ( strpos( $url, 'https://generativelanguage.googleapis.com/v1beta' ) !== false ) {
88+
$response = file_get_contents( __DIR__ . '/geminiapi.json' );
89+
$body_json = $parsed_args['body'] ?? false;
90+
91+
if ( $body_json ) {
92+
$body = json_decode( $body_json, JSON_OBJECT_AS_ARRAY );
93+
$contents = isset( $body['contents'] ) ? $body['contents'] : [];
94+
$parts = isset( $contents[0]['parts'] ) ? $contents[0]['parts'] : [];
95+
$prompt = $parts['text'] ?? '';
96+
97+
if ( str_contains( $prompt, 'Increase the content' ) || str_contains( $prompt, 'Decrease the content' ) ) {
98+
$response = file_get_contents( __DIR__ . '/geminiapi-resize-content.json' );
99+
}
100+
}
87101
}
88102

89103
if ( ! empty( $response ) ) {

0 commit comments

Comments
 (0)