Files
PicAnalysis/frontend/e2e/documents.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

147 lines
4.2 KiB
TypeScript

import { test, expect } from '@playwright/test';
test.describe('Documents', () => {
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/documents**', (route) => {
if (route.request().method() === 'GET') {
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
success: true,
data: [
{
id: '1',
title: '测试文档',
content: '这是测试内容',
created_at: '2024-01-01T00:00:00Z',
updated_at: '2024-01-01T00:00:00Z',
user_id: '1',
category_id: null,
},
],
count: 1,
}),
});
}
});
});
test('应该显示文档列表', async ({ page }) => {
await page.goto('/documents');
await expect(page.locator('h1')).toContainText('文档管理');
await expect(page.locator('text=测试文档')).toBeVisible();
await expect(page.locator('text=这是测试内容')).toBeVisible();
});
test('应该打开创建文档表单', async ({ page }) => {
await page.goto('/documents');
await page.click('button:has-text("新建文档")');
await expect(page.locator('text=新建文档')).toBeVisible();
await expect(page.locator('input[label="标题"]')).toBeVisible();
});
test('应该创建新文档', async ({ page }) => {
await page.route('**/api/documents', (route) => {
if (route.request().method() === 'POST') {
route.fulfill({
status: 201,
contentType: 'application/json',
body: JSON.stringify({
success: true,
data: {
id: '2',
title: '新文档',
content: '新文档内容',
created_at: '2024-01-01T00:00:00Z',
updated_at: '2024-01-01T00:00:00Z',
user_id: '1',
category_id: null,
},
}),
});
}
});
await page.goto('/documents');
await page.click('button:has-text("新建文档")');
await page.fill('input[label="标题"]', '新文档');
await page.fill('textarea[placeholder="文档内容"]', '新文档内容');
await page.click('button:has-text("创建")');
// 验证表单关闭
await expect(page.locator('text=新建文档')).not.toBeVisible();
});
test('应该删除文档', async ({ page }) => {
await page.route('**/api/documents/*', (route) => {
if (route.request().method() === 'DELETE') {
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
success: true,
message: '文档已删除',
}),
});
}
});
await page.goto('/documents');
// 点击删除按钮
const deleteButton = page.locator('button').filter({ hasText: '' }).first();
await deleteButton.click();
// 确认删除
page.on('dialog', (dialog) => dialog.accept());
});
test('应该搜索文档', async ({ page }) => {
await page.route('**/api/documents/search**', (route) => {
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
success: true,
data: [
{
id: '1',
title: '搜索结果',
content: '包含关键词的内容',
created_at: '2024-01-01T00:00:00Z',
updated_at: '2024-01-01T00:00:00Z',
user_id: '1',
category_id: null,
},
],
}),
});
});
await page.goto('/documents');
await page.fill('input[placeholder="搜索文档..."]', '关键词');
await page.click('button:has-text("搜索")');
await expect(page.locator('text=搜索结果')).toBeVisible();
});
});