Files
AFFiNE-Mirror/tests/blocksuite/e2e/image/load.spec.ts
fundon 93b1d6c729 fix(editor): improve image block upload and download states (#12017)
Related to: [BS-3143](https://linear.app/affine-design/issue/BS-3143/更新-loading-和错误样式)

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

- **New Features**
  - Introduced a unified resource controller for managing image and attachment resources, providing improved loading, error, and state handling.
  - Added a visual loading indicator overlay to image blocks for better feedback during image loading.

- **Improvements**
  - Simplified and centralized image and attachment state management, reducing redundant properties and manual state tracking.
  - Updated fallback UI for image blocks with clearer titles, descriptions, and improved layout.
  - Enhanced batch image block creation and download handling for improved efficiency.
  - Refined image block accessibility with improved alt text and streamlined rendering logic.
  - Centralized target model selection for image insertion in AI actions.
  - Reordered CSS declarations without affecting styling.
  - Improved reactive state tracking for blob upload/download operations in mock server.

- **Bug Fixes**
  - Improved cleanup of object URLs to prevent resource leaks.
  - Adjusted toolbar logic to more accurately reflect available actions based on image state.

- **Tests**
  - Updated end-to-end tests to match new UI text and behaviors for image loading and error states.

- **Chores**
  - Refactored internal logic and updated comments for clarity and maintainability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-05-07 05:15:57 +00:00

165 lines
4.0 KiB
TypeScript

import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import type { Page } from '@playwright/test';
import { expect } from '@playwright/test';
import {
enterPlaygroundRoom,
expectConsoleMessage,
} from '../utils/actions/index.js';
import { test } from '../utils/playwright.js';
const mockImageId = '_e2e_test_image_id_';
async function initMockImage(page: Page) {
await page.evaluate(sourceId => {
const { doc } = window;
doc.captureSync();
const rootId = doc.addBlock('affine:page');
const noteId = doc.addBlock('affine:note', {}, rootId);
doc.addBlock(
'affine:image',
{
sourceId,
width: 200,
height: 180,
},
noteId
);
doc.captureSync();
}, mockImageId);
}
test('image loading but failed', async ({ page }) => {
expectConsoleMessage(
page,
`Error: Failed to fetch blob ${mockImageId}`,
'warning'
);
expectConsoleMessage(
page,
'Failed to load resource: the server responded with a status of 404 (Not Found)'
);
expectConsoleMessage(
page,
'Error: Image blob is missing!, retrying',
'warning'
);
const room = await enterPlaygroundRoom(page, { blobSource: ['mock'] });
const timeout = 2000;
// block image data request, force wait 100ms for loading test,
// always return 404
await page.route(
`**/api/collection/${room}/blob/${mockImageId}`,
async route => {
await page.waitForTimeout(timeout);
// broken image
return route.fulfill({
status: 404,
});
}
);
await initMockImage(page);
const title = page.locator(
'.affine-image-fallback-card .affine-image-fallback-card-title-text'
);
await expect(title).toHaveText('Image');
await page.waitForTimeout(3 * timeout);
const desc = page.locator(
'.affine-image-fallback-card .affine-image-fallback-card-description'
);
await expect(desc).toContainText('Image not found');
});
test('image loading but success', async ({ page }) => {
expectConsoleMessage(
page,
`Error: Failed to fetch blob ${mockImageId}`,
'warning'
);
expectConsoleMessage(
page,
'Failed to load resource: the server responded with a status of 404 (Not Found)'
);
expectConsoleMessage(
page,
'Error: Image blob is missing!, retrying',
'warning'
);
const room = await enterPlaygroundRoom(page, { blobSource: ['mock'] });
const imageBuffer = await readFile(
fileURLToPath(new URL('../fixtures/smile.png', import.meta.url))
);
const timeout = 2000;
let count = 0;
// block image data request, force wait 100ms for loading test,
// always return 404
await page.route(
`**/api/collection/${room}/blob/${mockImageId}`,
async route => {
await page.waitForTimeout(timeout);
count++;
if (count === 3) {
return route.fulfill({
status: 200,
body: imageBuffer,
});
}
return route.continue();
}
);
await initMockImage(page);
const title = page.locator(
'.affine-image-fallback-card .affine-image-fallback-card-title-text'
);
await expect(title).toHaveText('Image');
await page.waitForTimeout(3 * timeout);
const img = page.locator('.affine-image-container img');
await expect(img).toBeVisible();
const src = await img.getAttribute('src');
expect(src).toBeDefined();
});
test('image loaded successfully', async ({ page }) => {
const room = await enterPlaygroundRoom(page, { blobSource: ['mock'] });
const imageBuffer = await readFile(
fileURLToPath(new URL('../fixtures/smile.png', import.meta.url))
);
await page.route(
`**/api/collection/${room}/blob/${mockImageId}`,
async route => {
return route.fulfill({
status: 200,
body: imageBuffer,
});
}
);
await initMockImage(page);
await page.waitForTimeout(1000);
const img = page.locator('.affine-image-container img');
await expect(img).toBeVisible();
const src = await img.getAttribute('src');
expect(src).toBeDefined();
});