Files
wrong-notebook/e2e/upload-correction.spec.ts

155 lines
6.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { test, expect } from '@playwright/test';
import path from 'path';
import { acceptConfirmDialog, ADMIN_PASSWORD } from './helpers';
test('Upload image, correct, save, and verify in notebook', async ({ page }) => {
// 增加测试超时时间
test.setTimeout(90000);
// Mock specific API calls to avoid external dependencies (AI)
await page.route('**/api/analyze', async route => {
// Return predictable mock analysis
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
questionText: '2 + 2 = ?',
answerText: '4',
analysis: 'Simple addition analysis.',
knowledgePoints: ['Math', 'Addition'],
subject: '数学', // Try to match the one we will create
requiresImage: false
})
});
});
// 1. Login
await page.goto('/login');
await page.getByLabel(/邮箱|Email/).fill('admin@localhost');
await page.getByLabel(/^密码$|^Password$/).fill(ADMIN_PASSWORD);
await page.getByRole('button', { name: /登录|Login/ }).click();
await page.waitForURL('**/', { timeout: 15000 });
// 开启「导入前人工确认」,识别后进入校对页而不是直接入库
const prefRes = await page.request.patch('/api/user', { data: { confirmBeforeImport: true } });
expect(prefRes.ok()).toBeTruthy();
// 2. Ensure a Notebook exists
// Go to Notebooks page
await page.goto('/notebooks');
// 按名称精确查找「数学」错题本;其他错题本卡片上也可能显示「数学」学科标签,不能按页面文字判断
const findMathNotebook = async () => {
const notebooks: Array<{ id: string; name: string }> = await (await page.request.get('/api/notebooks')).json();
return notebooks.find((n) => n.name === '数学');
};
if (!(await findMathNotebook())) {
await page.getByRole('button', { name: /新建|New|Create/ }).first().click();
await expect(page.getByRole('dialog')).toBeVisible({ timeout: 5000 });
await page.locator('#name').fill('数学');
// 等创建接口返回后再离开页面,否则请求可能被导航中断
const created = page.waitForResponse((r) => r.url().endsWith('/api/notebooks') && r.request().method() === 'POST');
await page.getByRole('button', { name: /创建|Create/ }).last().click();
expect((await created).ok()).toBeTruthy();
}
const mathNotebook = await findMathNotebook();
expect(mathNotebook).toBeDefined();
// 3. Go back to Home for Upload
await page.goto('/');
await page.waitForLoadState('networkidle');
// 4. Upload Image
const filePath = path.join(__dirname, './fixtures/math_test.png');
// UploadZone handles drag-drop but exposes a hidden input
await page.setInputFiles('input[type="file"]', filePath);
// 5. Handle Image Cropper - 增加等待时间
await expect(page.getByRole('dialog')).toBeVisible({ timeout: 10000 });
await expect(page.getByRole('heading', { name: /裁剪|Crop/ })).toBeVisible({ timeout: 5000 });
// Click Confirm
await page.getByRole('button', { name: /确认|Confirm/ }).click();
// 6. Wait for Editor (Mocked analysis returns immediately)
// Look for Editor Title "校对" or "Review"
await expect(page.getByRole('heading', { level: 2 })).toContainText(/校对|Review|Correct/, { timeout: 10000 });
// 7. Correct the Question Text
// Prepend "试题:" to the question text
// Use first textarea which corresponds to Question
const questionBox = page.locator('textarea').nth(0);
await expect(questionBox).toHaveValue('2 + 2 = ?', { timeout: 5000 }); // From mock
await questionBox.fill('试题:2 + 2 = ?');
// Verify knowledge points (from mock)
await expect(page.locator('body')).toContainText('Addition');
// 8. Select Notebook (if not matched)
// Our mock returned "数学". The code tries to auto-select.
const notebookSelector = page.locator('button[role="combobox"]').first();
// Check text content of the button
if (!(await notebookSelector.textContent())?.includes('数学')) {
await notebookSelector.click();
await page.getByRole('option', { name: '数学' }).click();
}
// 9. Save
// Click "Save to Notebook" / "保存"
await page.getByRole('button', { name: /保存|Save/ }).click();
// 10. Verify Redirection and Content
// Should redirect to /notebooks/[id]
await page.waitForURL(/\/notebooks\/.+/, { timeout: 10000 });
// Verify headers or content
await expect(page.getByRole('heading', { level: 1 })).toContainText('数学');
// Verify the new item is present
// Question text should be "试题:2 + 2 = ?"
await expect(page.locator('body')).toContainText('试题:2 + 2 = ?');
// Verify Tags
await expect(page.locator('body')).toContainText('Addition');
// Verify Mastery Status (To Review / 待复习)
await expect(page.locator('.badge, .inline-flex').filter({ hasText: /待复习|Review/ }).first()).toBeVisible();
// 11. Delete ALL Error Items (Cleanup) to ensure notebook can be deleted
while (true) {
const items = page.locator('a[href^="/error-items/"]');
const count = await items.count();
console.log(`Found ${count} error items to delete.`);
if (count === 0) break;
// Click the first item
await items.first().click();
// Wait for Detail Page
await expect(page.getByRole('heading', { level: 1, name: /详情|Detail/ })).toBeVisible({ timeout: 5000 });
// Click Delete Button
await page.getByRole('button', { name: /删除|Delete/ }).click();
await acceptConfirmDialog(page);
// Wait to be redirected back to notebook page
await page.waitForURL(/\/notebooks\/.+/, { timeout: 5000 });
await expect(page.getByRole('heading', { level: 1 })).toBeVisible();
await page.waitForTimeout(500);
}
// 12. Verify Item Deleted
await expect(page.getByText('试题:2 + 2 = ?')).not.toBeVisible();
// 13. Delete the Notebook:列表不再有删除按钮,进入错题本详情页删除
await page.goto(`/notebooks/${mathNotebook!.id}`);
await page.getByRole('button', { name: /^(删除|Delete)$/ }).click();
await acceptConfirmDialog(page);
await page.waitForURL(/\/notebooks$/);
// 14. Verify Notebook Deleted
expect(await findMathNotebook()).toBeUndefined();
});