test: enable chat block e2e test (#10111)

This commit is contained in:
donteatfriedrice
2025-02-12 09:26:52 +00:00
parent e3691850ad
commit e350ba4a9c
2 changed files with 111 additions and 17 deletions

View File

@@ -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 }) => {