Files
PicAnalysis/frontend/e2e/images.spec.ts
wjl 1a0ebde95d feat: 初始化 PicAnalysis 项目
完整的前后端图片分析应用,包含:
- 后端:Express + Prisma + SQLite,101个单元测试全部通过
- 前端:React + TypeScript + Vite,47个单元测试,89.73%覆盖率
- E2E测试:Playwright 测试套件
- MCP集成:Playwright MCP配置完成并测试通过

功能模块:
- 用户认证(JWT)
- 文档管理(CRUD)
- 待办管理(三态工作流)
- 图片管理(上传、截图、OCR)

测试覆盖:
- 后端单元测试:101/101 
- 前端单元测试:47/47 
- E2E测试:通过 
- MCP Playwright测试:通过 

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 20:10:11 +08:00

188 lines
5.8 KiB
TypeScript

import { test, expect } from '@playwright/test';
test.describe('Images', () => {
test.beforeEach(async ({ page }) => {
// 设置已登录状态
await page.goto('/dashboard');
await page.evaluate(() => {
localStorage.setItem('auth-storage', JSON.stringify({
state: {
user: { id: '1', username: 'test' },
token: 'test-token',
isAuthenticated: true,
},
version: 0,
}));
localStorage.setItem('auth_token', 'test-token');
});
// Mock API
await page.route('**/api/images**', (route) => {
if (route.request().method() === 'GET') {
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
success: true,
data: [
{
id: '1',
file_path: '/uploads/test.jpg',
file_size: 102400,
mime_type: 'image/jpeg',
ocr_result: '测试 OCR 结果',
ocr_confidence: 0.95,
processing_status: 'completed',
quality_score: 0.9,
error_message: null,
created_at: '2024-01-01T00:00:00Z',
updated_at: '2024-01-01T00:00:00Z',
user_id: '1',
document_id: null,
},
],
count: 1,
}),
});
}
});
await page.route('**/api/images/pending', (route) => {
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
success: true,
data: [
{
id: '2',
file_path: '/uploads/pending.jpg',
file_size: 51200,
mime_type: 'image/jpeg',
ocr_result: null,
ocr_confidence: null,
processing_status: 'pending',
quality_score: null,
error_message: null,
created_at: '2024-01-01T00:00:00Z',
updated_at: '2024-01-01T00:00:00Z',
user_id: '1',
document_id: null,
},
],
}),
});
});
});
test('应该显示图片列表', async ({ page }) => {
await page.goto('/images');
await expect(page.locator('h1')).toContainText('图片管理');
await expect(page.locator('text=上传图片')).toBeVisible();
await expect(page.locator('text=屏幕截图')).toBeVisible();
});
test('应该显示等待 OCR 的图片', async ({ page }) => {
await page.goto('/images');
await expect(page.locator('text=等待 OCR 处理')).toBeVisible();
await expect(page.locator('text=处理中')).toBeVisible();
});
test('应该显示已完成的图片和 OCR 结果', async ({ page }) => {
await page.goto('/images');
await expect(page.locator('text=测试 OCR 结果')).toBeVisible();
await expect(page.locator('text=95%')).toBeVisible();
await expect(page.locator('text=已完成')).toBeVisible();
});
test('应该打开文件选择器', async ({ page }) => {
await page.goto('/images');
const fileInput = page.locator('input[type="file"]');
await expect(fileInput).toBeAttached();
// 点击选择文件按钮
const chooseButton = page.locator('button:has-text("选择文件")');
await chooseButton.click();
});
test('应该显示图片关联操作', async ({ page }) => {
await page.goto('/images');
// 检查待办按钮
await expect(page.locator('button:has-text("待办")')).toBeVisible();
});
test('应该显示不同状态的图片', async ({ page }) => {
await page.route('**/api/images', (route) => {
if (route.request().method() === 'GET') {
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
success: true,
data: [
{
id: '1',
file_path: '/uploads/completed.jpg',
file_size: 102400,
mime_type: 'image/jpeg',
ocr_result: '完成',
ocr_confidence: 0.95,
processing_status: 'completed',
quality_score: 0.9,
error_message: null,
created_at: '2024-01-01T00:00:00Z',
updated_at: '2024-01-01T00:00:00Z',
user_id: '1',
document_id: null,
},
{
id: '2',
file_path: '/uploads/pending.jpg',
file_size: 51200,
mime_type: 'image/jpeg',
ocr_result: null,
ocr_confidence: null,
processing_status: 'pending',
quality_score: null,
error_message: null,
created_at: '2024-01-01T00:00:00Z',
updated_at: '2024-01-01T00:00:00Z',
user_id: '1',
document_id: null,
},
{
id: '3',
file_path: '/uploads/failed.jpg',
file_size: 76800,
mime_type: 'image/jpeg',
ocr_result: null,
ocr_confidence: null,
processing_status: 'failed',
quality_score: null,
error_message: 'OCR 处理失败',
created_at: '2024-01-01T00:00:00Z',
updated_at: '2024-01-01T00:00:00Z',
user_id: '1',
document_id: null,
},
],
count: 3,
}),
});
}
});
await page.goto('/images');
// 验证不同状态的显示
await expect(page.locator('text=已完成').first()).toBeVisible();
await expect(page.locator('text=处理中')).toBeVisible();
await expect(page.locator('text=失败')).toBeVisible();
});
});