完整的前后端图片分析应用,包含: - 后端: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>
227 lines
6.5 KiB
TypeScript
227 lines
6.5 KiB
TypeScript
import { test, expect } from '@playwright/test';
|
|
|
|
test.describe('Todos', () => {
|
|
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/todos**', (route) => {
|
|
if (route.request().method() === 'GET') {
|
|
route.fulfill({
|
|
status: 200,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({
|
|
success: true,
|
|
data: [
|
|
{
|
|
id: '1',
|
|
title: '测试待办',
|
|
description: '测试描述',
|
|
priority: 'medium',
|
|
status: 'pending',
|
|
created_at: '2024-01-01T00:00:00Z',
|
|
updated_at: '2024-01-01T00:00:00Z',
|
|
user_id: '1',
|
|
document_id: null,
|
|
category_id: null,
|
|
due_date: null,
|
|
completed_at: null,
|
|
confirmed_at: null,
|
|
},
|
|
],
|
|
}),
|
|
});
|
|
}
|
|
});
|
|
|
|
await page.route('**/api/todos/pending', (route) => {
|
|
route.fulfill({
|
|
status: 200,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({
|
|
success: true,
|
|
data: [
|
|
{
|
|
id: '1',
|
|
title: '待办任务',
|
|
description: '待完成',
|
|
priority: 'high',
|
|
status: 'pending',
|
|
created_at: '2024-01-01T00:00:00Z',
|
|
updated_at: '2024-01-01T00:00:00Z',
|
|
user_id: '1',
|
|
document_id: null,
|
|
category_id: null,
|
|
due_date: null,
|
|
completed_at: null,
|
|
confirmed_at: null,
|
|
},
|
|
],
|
|
}),
|
|
});
|
|
});
|
|
|
|
await page.route('**/api/todos/completed', (route) => {
|
|
route.fulfill({
|
|
status: 200,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({
|
|
success: true,
|
|
data: [
|
|
{
|
|
id: '2',
|
|
title: '已完成任务',
|
|
description: '已完成',
|
|
priority: 'low',
|
|
status: 'completed',
|
|
created_at: '2024-01-01T00:00:00Z',
|
|
updated_at: '2024-01-01T00:00:00Z',
|
|
user_id: '1',
|
|
document_id: null,
|
|
category_id: null,
|
|
due_date: null,
|
|
completed_at: '2024-01-01T00:00:00Z',
|
|
confirmed_at: null,
|
|
},
|
|
],
|
|
}),
|
|
});
|
|
});
|
|
});
|
|
|
|
test('应该显示待办列表', async ({ page }) => {
|
|
await page.goto('/todos');
|
|
|
|
await expect(page.locator('h1')).toContainText('待办事项');
|
|
await expect(page.locator('text=待办任务')).toBeVisible();
|
|
});
|
|
|
|
test('应该筛选待办状态', async ({ page }) => {
|
|
await page.goto('/todos');
|
|
|
|
// 点击"待办"筛选
|
|
await page.click('button:has-text("待办")');
|
|
await expect(page.locator('text=待办任务')).toBeVisible();
|
|
|
|
// 点击"已完成"筛选
|
|
await page.click('button:has-text("已完成")');
|
|
await expect(page.locator('text=已完成任务')).toBeVisible();
|
|
|
|
// 点击"全部"筛选
|
|
await page.click('button:has-text("全部")');
|
|
});
|
|
|
|
test('应该创建新待办', async ({ page }) => {
|
|
await page.route('**/api/todos', (route) => {
|
|
if (route.request().method() === 'POST') {
|
|
route.fulfill({
|
|
status: 201,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({
|
|
success: true,
|
|
data: {
|
|
id: '3',
|
|
title: '新待办',
|
|
description: '新待办描述',
|
|
priority: 'medium',
|
|
status: 'pending',
|
|
created_at: '2024-01-01T00:00:00Z',
|
|
updated_at: '2024-01-01T00:00:00Z',
|
|
user_id: '1',
|
|
document_id: null,
|
|
category_id: null,
|
|
due_date: null,
|
|
completed_at: null,
|
|
confirmed_at: null,
|
|
},
|
|
}),
|
|
});
|
|
}
|
|
});
|
|
|
|
await page.goto('/todos');
|
|
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/todos/*', (route) => {
|
|
if (route.request().method() === 'PUT') {
|
|
route.fulfill({
|
|
status: 200,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({
|
|
success: true,
|
|
data: {
|
|
id: '1',
|
|
title: '待办任务',
|
|
description: '待完成',
|
|
priority: 'high',
|
|
status: 'completed',
|
|
created_at: '2024-01-01T00:00:00Z',
|
|
updated_at: '2024-01-01T00:00:00Z',
|
|
user_id: '1',
|
|
document_id: null,
|
|
category_id: null,
|
|
due_date: null,
|
|
completed_at: '2024-01-01T00:00:00Z',
|
|
confirmed_at: null,
|
|
},
|
|
}),
|
|
});
|
|
}
|
|
});
|
|
|
|
await page.goto('/todos');
|
|
|
|
// 点击复选框
|
|
const checkbox = page.locator('.border-gray-300').first();
|
|
await checkbox.click();
|
|
|
|
// 验证状态更新(已完成的样式)
|
|
await expect(page.locator('.line-through')).toBeVisible();
|
|
});
|
|
|
|
test('应该删除待办', async ({ page }) => {
|
|
await page.route('**/api/todos/*', (route) => {
|
|
if (route.request().method() === 'DELETE') {
|
|
route.fulfill({
|
|
status: 200,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({
|
|
success: true,
|
|
message: '待办已删除',
|
|
}),
|
|
});
|
|
}
|
|
});
|
|
|
|
await page.goto('/todos');
|
|
|
|
// 点击删除按钮
|
|
const deleteButton = page.locator('button').filter({ hasText: '' }).nth(1);
|
|
await deleteButton.click();
|
|
|
|
// 确认删除
|
|
page.on('dialog', (dialog) => dialog.accept());
|
|
});
|
|
});
|