Files
AFFiNE-Mirror/tests/affine-cloud-copilot/e2e/utils/settings-panel-utils.ts
yoyoyohamapi 45ed9038b6 feat(core): workspace attachment uploading & error (#12330)
### TL;DR

feat: optimize workspace attachment uploading & error display

![截屏2025-05-16 15.29.43.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/MyktQ6Qwc7H6TiRCFoYN/2408fe5e-e54d-44a8-882c-91e1b26bb660.png)

### What Changes
####
Support for Workspace Attachment Uploading & Error Handling
* Added support for three attachment states: uploading (local), upload failed (local error), and uploaded (persisted). The frontend UI now displays real-time upload progress and error messages.
* Attachments that fail to upload can be deleted directly without confirmation.
* Merged display of uploading and uploaded attachments for a smoother user experience.

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Attachments now show real-time upload status including uploading, error, and uploaded states.
  - Users can remove failed (error) attachments instantly without confirmation.
  - Attachment list merges uploading and uploaded files, displaying up to 10 items.
- **Bug Fixes**
  - Improved error handling and messaging for failed attachment uploads.
- **Style**
  - Enhanced visual styling for error attachments with distinct colors and backgrounds.
- **Tests**
  - Added tests simulating slow network uploads, upload failures, and direct removal of error attachments.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-05-22 02:35:03 +00:00

186 lines
6.0 KiB
TypeScript

import { expect, type Page } from '@playwright/test';
const WORKSPACE_EMBEDDING_SWITCH_TEST_ID = 'workspace-embedding-setting-switch';
export class SettingsPanelUtils {
public static async openSettingsPanel(page: Page) {
if (await page.getByTestId('workspace-setting:embedding').isHidden()) {
await page.getByTestId('slider-bar-workspace-setting-button').click();
await page.getByTestId('workspace-setting:embedding').click();
await page.getByTestId('workspace-embedding-setting-wrapper').waitFor({
state: 'visible',
});
}
}
public static async closeSettingsPanel(page: Page) {
if (
await page.getByTestId('workspace-embedding-setting-wrapper').isVisible()
) {
await page.getByTestId('modal-close-button').click();
await page.getByTestId('workspace-embedding-setting-wrapper').waitFor({
state: 'hidden',
});
}
}
public static async isWorkspaceEmbeddingEnabled(page: Page) {
const input = await page
.getByTestId(WORKSPACE_EMBEDDING_SWITCH_TEST_ID)
.locator('input');
return (await input.getAttribute('value')) === 'on';
}
public static async waitForWorkspaceEmbeddingSwitchToBe(
page: Page,
enabled: boolean
) {
const input = await page
.getByTestId(WORKSPACE_EMBEDDING_SWITCH_TEST_ID)
.locator('input');
await expect(input).toHaveAttribute('value', enabled ? 'on' : 'off');
}
public static async toggleWorkspaceEmbedding(page: Page) {
const input = await page.getByTestId(WORKSPACE_EMBEDDING_SWITCH_TEST_ID);
await input.click();
}
public static async enableWorkspaceEmbedding(page: Page) {
const enabled = await this.isWorkspaceEmbeddingEnabled(page);
if (!enabled) {
await this.toggleWorkspaceEmbedding(page);
}
await this.waitForWorkspaceEmbeddingSwitchToBe(page, true);
}
public static async disableWorkspaceEmbedding(page: Page) {
const enabled = await this.isWorkspaceEmbeddingEnabled(page);
if (enabled) {
await this.toggleWorkspaceEmbedding(page);
}
await this.waitForWorkspaceEmbeddingSwitchToBe(page, false);
}
public static async uploadWorkspaceEmbedding(
page: Page,
attachments: { name: string; mimeType: string; buffer: Buffer }[]
) {
await page.evaluate(() => {
delete window.showOpenFilePicker;
});
for (const attachment of attachments) {
const fileChooserPromise = page.waitForEvent('filechooser');
await page
.getByTestId('workspace-embedding-setting-upload-button')
.click();
const fileChooser = await fileChooserPromise;
await fileChooser.setFiles(attachment);
}
}
public static async removeAllAttachments(page: Page) {
const itemId = 'workspace-embedding-setting-attachment-item';
let count = await page.getByTestId(itemId).count();
while (count > 0) {
const attachmentItem = await page.getByTestId(itemId).first();
const hasErrorItem = await attachmentItem
.getByTestId('workspace-embedding-setting-attachment-error-item')
.isVisible();
await attachmentItem
.getByTestId('workspace-embedding-setting-attachment-delete-button')
.click();
if (!hasErrorItem) {
await page.getByTestId('confirm-modal-confirm').click();
}
await page.waitForTimeout(1000);
count = await page.getByTestId(itemId).count();
}
}
public static async removeAttachment(
page: Page,
attachment: string,
shouldConfirm = true
) {
const attachmentItem = await page
.getByTestId('workspace-embedding-setting-attachment-item')
.filter({ hasText: attachment });
await attachmentItem
.getByTestId('workspace-embedding-setting-attachment-delete-button')
.click();
if (shouldConfirm) {
await page.getByTestId('confirm-modal-confirm').click();
}
await page
.getByTestId('workspace-embedding-setting-attachment-item')
.filter({ hasText: attachment })
.waitFor({
state: 'hidden',
});
}
public static async ignoreDocForEmbedding(page: Page, doc: string) {
// Open Dos Searcher
const ignoreDocsButton = await page.getByTestId(
'workspace-embedding-setting-ignore-docs-button'
);
await ignoreDocsButton.click();
// Search and select the doc
const searcher = await page.getByTestId('doc-selector-layout');
const searchInput = await page.getByTestId('doc-selector-search-input');
await searchInput.focus();
await page.keyboard.insertText(doc);
const pageListItem = searcher.getByTestId('page-list-item');
await expect(pageListItem).toHaveCount(1);
const pageListItemTitle = pageListItem.getByTestId(
'page-list-item-title-text'
);
await expect(pageListItemTitle).toHaveText(doc);
await pageListItem.getByTestId('affine-checkbox').check();
await searcher.getByTestId('doc-selector-confirm-button').click();
const ignoredDocs = await page.getByTestId(
'workspace-embedding-setting-ignore-docs-list'
);
await expect(
ignoredDocs
.getByTestId('workspace-embedding-setting-ignore-docs-list-item')
.filter({ hasText: doc })
).toBeVisible();
}
public static async clearAllIgnoredDocs(page: Page) {
const ignoredDocs = await page.getByTestId('ignore-doc-title').all();
for (const ignoredDoc of ignoredDocs) {
const doc = await ignoredDoc.innerText();
// Open Dos Searcher
const ignoreDocsButton = await page.getByTestId(
'workspace-embedding-setting-ignore-docs-button'
);
await ignoreDocsButton.click();
// Search and select the doc
const searcher = await page.getByTestId('doc-selector-layout');
const searchInput = await page.getByTestId('doc-selector-search-input');
await searchInput.focus();
await page.keyboard.insertText(doc);
const pageListItem = searcher.getByTestId('page-list-item');
await expect(pageListItem).toHaveCount(1);
await pageListItem.getByTestId('affine-checkbox').uncheck();
await searcher.getByTestId('doc-selector-confirm-button').click();
}
}
}