Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

test: enable chat block e2e test (#10111)

donteatfriedricee350ba4a9c67
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"
></textarea>
<div class="chat-panel-input-actions">
<div
diff --git a/tests/affine-cloud-copilot/e2e/copilot.spec.ts b/tests/affine-cloud-copilot/e2e/copilot.spec.ts
index 9c12c9db17..504f86f63d 100644
--- a/tests/affine-cloud-copilot/e2e/copilot.spec.ts
+++ b/tests/affine-cloud-copilot/e2e/copilot.spec.ts
@@ -4,6 +4,7 @@ import {
loginUser,
loginUserDirectly,
} from '@affine-test/kit/utils/cloud';
+import { getPageMode } from '@affine-test/kit/utils/editor';
import { openHomePage, setCoreUrl } from '@affine-test/kit/utils/load-page';
import {
clickNewPageButton,
@@ -299,23 +300,115 @@ test.describe('chat panel', () => {
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 }) => {