From e350ba4a9c672c93b2afe1ac343bc86adfe65351 Mon Sep 17 00:00:00 2001 From: donteatfriedrice Date: Wed, 12 Feb 2025 09:26:52 +0000 Subject: [PATCH] test: enable chat block e2e test (#10111) --- .../presets/ai/peek-view/chat-block-input.ts | 1 + .../affine-cloud-copilot/e2e/copilot.spec.ts | 127 +++++++++++++++--- 2 files changed, 111 insertions(+), 17 deletions(-) diff --git a/packages/frontend/core/src/blocksuite/presets/ai/peek-view/chat-block-input.ts b/packages/frontend/core/src/blocksuite/presets/ai/peek-view/chat-block-input.ts index b51a768bbe..f1b1055c52 100644 --- a/packages/frontend/core/src/blocksuite/presets/ai/peek-view/chat-block-input.ts +++ b/packages/frontend/core/src/blocksuite/presets/ai/peek-view/chat-block-input.ts @@ -202,6 +202,7 @@ export class ChatBlockInput extends LitElement { } } }} + data-testid="chat-block-input" >
{ expect(editorContent).toBe(content); }); - // feature not launched yet - test.skip('can be save chat to block', async ({ page }) => { - await page.reload(); - await clickSideBarAllPageButton(page); - await page.waitForTimeout(200); - await createLocalWorkspace({ name: 'test' }, page); - await clickNewPageButton(page); - await makeChat(page, 'hello'); - const contents = (await collectChat(page)).map(m => m.content); - await switchToEdgelessMode(page); - await page.getByTestId('action-save-chat-to-block').click(); - const chatBlock = await page.waitForSelector('affine-edgeless-ai-chat'); - expect( - await Promise.all( - (await chatBlock.$$('.ai-chat-user-message')).map(m => m.innerText()) - ) - ).toBe(contents); + test.describe('chat block', () => { + const collectNewMessages = async (page: Page) => { + // wait ai response + const newMessagesContainer = await page.waitForSelector( + '.new-chat-messages-container' + ); + await page.waitForSelector( + '.new-chat-messages-container .assistant-message-container chat-copy-more' + ); + const lastMessage = await newMessagesContainer + .$$('.assistant-message-container') + .then(m => m[m.length - 1]); + await lastMessage.waitForSelector('chat-copy-more'); + await page.waitForTimeout(ONE_SECOND); + return Promise.all( + Array.from(await newMessagesContainer.$$('.ai-chat-message')).map( + async m => ({ + name: await m.$('.user-name').then(i => i?.innerText()), + content: await m + .$('.ai-answer-text-editor') + .then(t => t?.$('editor-host')) + .then(e => e?.innerText()), + }) + ) + ); + }; + + // make chat before each test + test.beforeEach(async ({ page }) => { + await page.reload(); + await clickSideBarAllPageButton(page); + await page.waitForTimeout(200); + await createLocalWorkspace({ name: 'test' }, page); + await clickNewPageButton(page); + await makeChat(page, 'hello'); + }); + + test('can be save chat to block when page mode', async ({ page }) => { + const contents = (await collectChat(page)).map(m => m.content); + expect(await getPageMode(page)).toBe('page'); + await page.getByTestId('action-save-chat-to-block').click(); + const chatBlock = await page.waitForSelector('affine-edgeless-ai-chat'); + // should switch to edgeless mode + expect(await getPageMode(page)).toBe('edgeless'); + expect( + await Promise.all( + (await chatBlock.$$('.ai-chat-message .ai-answer-text-editor')).map( + m => m.innerText() + ) + ) + ).toStrictEqual(contents); + }); + + test('can be save chat to block when edgeless mode', async ({ page }) => { + const contents = (await collectChat(page)).map(m => m.content); + await switchToEdgelessMode(page); + expect(await getPageMode(page)).toBe('edgeless'); + await page.getByTestId('action-save-chat-to-block').click(); + const chatBlock = await page.waitForSelector('affine-edgeless-ai-chat'); + expect( + await Promise.all( + (await chatBlock.$$('.ai-chat-message .ai-answer-text-editor')).map( + m => m.innerText() + ) + ) + ).toStrictEqual(contents); + }); + + test('chat in center peek', async ({ page }) => { + const contents = (await collectChat(page)).map(m => m.content); + await page.getByTestId('action-save-chat-to-block').click(); + const chatBlock = await page.waitForSelector('affine-edgeless-ai-chat'); + // open chat in center peek + await chatBlock.dblclick(); + const chatBlockPeekView = await page.waitForSelector( + 'ai-chat-block-peek-view' + ); + expect(await chatBlockPeekView.isVisible()).toBe(true); + expect( + await Promise.all( + ( + await chatBlockPeekView.$$( + '.ai-chat-message .ai-answer-text-editor' + ) + ).map(m => m.innerText()) + ) + ).toStrictEqual(contents); + + // chat in center peek + await page.getByTestId('chat-block-input').focus(); + await page.keyboard.type('hi'); + await page.keyboard.press('Enter'); + + // collect new messages + const newMessages = await collectNewMessages(page); + expect(newMessages[0].content).toEqual('hi'); + expect(newMessages[1].name).toBe('AFFiNE AI'); + expect(newMessages[1].content?.length).toBeGreaterThan(0); + + // close peek view + await page.getByTestId('peek-view-control').click(); + await page.waitForSelector('ai-chat-block-peek-view', { + state: 'hidden', + }); + + // there should be two chat block + const chatBlocks = await page.$$('affine-edgeless-ai-chat'); + expect(chatBlocks.length).toBe(2); + }); }); test('can be chat and insert below in page mode', async ({ page }) => {