File size: 4,513 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
import { Locator } from 'playwright';
import { envVariables } from '../../..';
import { BlockFlow, EditorContext, PublishedPostContext } from '.';
interface ConfigurationData {
query: string;
tone?:
| 'Formal'
| 'Informal'
| 'Optimistic'
| 'Humorous'
| 'Serious'
| 'Skeptical'
| 'Empathetic'
| 'Confident'
| 'Passionate'
| 'Provocative';
improve?: 'Summarize' | 'Make longer' | 'Make shorter';
}
interface ValidationData {
keywords: string[];
}
const TIMEOUT = envVariables.TEST_ON_ATOMIC ? 30 * 1000 : 15 * 1000;
/**it com
* Represents the flow of using the AI Assistant block.
*/
export class AIAssistantFlow implements BlockFlow {
private configurationData: ConfigurationData;
private validationData: ValidationData;
/**
* Constructs an instance of this block flow with data to be used when
* configuring and validating the block.
*
* @param {ConfigurationData} configurationData Configuration data for the block.
*/
constructor( configurationData: ConfigurationData, validationData: ValidationData ) {
this.configurationData = configurationData;
this.validationData = validationData;
}
blockSidebarName = 'AI Assistant';
blockEditorSelector = 'div[aria-label="Block: AI Assistant"]';
/**
* Configure the block in the editor with the configuration data from the constructor
*
* @param {EditorContext} context The current context for the editor at the point of
* test execution.
*/
async configure( context: EditorContext ): Promise< void > {
const editorCanvas = await context.editorPage.getEditorCanvas();
const block = editorCanvas.getByRole( 'document', {
name: `Block: ${ this.blockSidebarName }`,
} );
await this.enterInput( block );
await this.clickButtonOnBlock( block, 'Send' );
await this.waitForQuery( block );
if ( this.configurationData.tone ) {
await context.editorPage.clickBlockToolbarButton( { name: 'Change tone' } );
await context.editorPage.selectFromToolbarPopover( this.configurationData.tone );
await this.waitForQuery( block );
}
if ( this.configurationData.improve ) {
await context.editorPage.clickBlockToolbarButton( { name: 'Improve' } );
await context.editorPage.selectFromToolbarPopover( this.configurationData.improve );
await this.waitForQuery( block );
}
await this.clickButtonOnBlock( block, 'Accept' );
await block.waitFor( { state: 'detached' } );
}
/**
* Clicks on a button on the block.
*
* @param {Locator} block Locator to the block.
* @param {string}name Name of the button to click.
*/
private async clickButtonOnBlock( block: Locator, name: string ) {
await block.getByRole( 'button', { name: name } ).click();
}
/**
* Enters the query into the input on the block.
*
* @param {Locator} block Locator to the block.
*/
private async enterInput( block: Locator ) {
const input = block.getByRole( 'textbox', { name: 'Write about… Make a table for…' } );
await input.waitFor();
await input.fill( this.configurationData.query );
}
/**
* Helper method to wait for the query to complete.
*
* @param {Locator} block Locator to the block.
*/
private async waitForQuery( block: Locator ) {
const stopButton = block.getByRole( 'button', { name: 'Stop request' } );
try {
await stopButton.waitFor( { state: 'detached', timeout: TIMEOUT } );
} catch {
// Stop the generation request after the timeout is met.
// AI Assistant block will retain any generated
// text up to this point.
await stopButton.click();
}
}
/**
* Validate the block in the published post
*
* @param {PublishedPostContext} context The current context for the published post at
* the point of test execution.
*/
async validateAfterPublish( context: PublishedPostContext ): Promise< void > {
for ( const keyword of this.validationData.keywords ) {
try {
await context.page.getByRole( 'main' ).filter( { hasText: keyword } ).waitFor();
} catch {
// Sometimes, the AI block fails to generate anything meaningful and isntead returns
// an error text. We can wait on that, but that would be testing implementation details.
// Since the AI block ends up creating paragraph blocks which are indistinguishable
// from what a user would have typed manually, the fallback is to merely verify that some
// form of paragraph block is present.
// @see: p1694596605435499-slack-CDLH4C1UZ
await context.page.getByRole( 'main' ).getByRole( 'paragraph' ).first().waitFor();
}
}
}
}
|