feat: 自由回忆工作区重做,复习页拆成独立视图

This commit is contained in:
2026-11-20 09:42:42 +08:00
parent 4937ed9cac
commit 15d84decfb
63 changed files with 4367 additions and 4166 deletions
+103 -197
View File
@@ -1,209 +1,115 @@
import { test, expect } from '@playwright/test';
import { recallFixture } from '../src/__tests__/support/recall';
const pageOf = (records: unknown[], total = records.length, current = 1) => ({ records, total, size: 20, current, pages: Math.ceil(total / 20) });
const SCOPE_TITLE = '选择复习范围';
/** 后端分页形状:列表接口都返回 { records, total, ... } */
const pageOf = (records: unknown[], total = records.length) => ({
records,
total,
size: records.length || 20,
current: 1,
pages: total === 0 ? 0 : 1,
});
const mockTasksData = {
code: 200,
data: pageOf(
[
{
taskNum: 'T001',
taskName: '学习 Vue3',
reportCount: 3,
fragmentCount: 5,
effectiveTime: 9000,
},
{
taskNum: 'T002',
taskName: '学习 TypeScript',
reportCount: 1,
fragmentCount: 2,
effectiveTime: 4500,
},
],
45,
),
};
const mockTasksEmpty = { code: 200, data: pageOf([]) };
const mockFeed = { code: 200, data: [] };
const mockTagMaps = { code: 200, data: pageOf([]) };
const mockEmptyList = { code: 200, data: [] };
const mockEmptyMap = { code: 200, data: {} };
const mockSortPreference = {
code: 200,
data: { preset: 'TIME_FIRST', description: '久未接触的更容易出现' },
};
test.describe('Review page', () => {
test.describe('Review 当前标签范围与回忆记录', () => {
test.beforeEach(async ({ page }) => {
await page.route('**/api/review/tasks**', (route) =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify(mockTasksData),
}),
);
await page.route('**/api/review/feed**', (route) =>
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(mockFeed) }),
);
await page.route('**/api/review/tag-mind-map/list**', (route) =>
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(mockTagMaps) }),
);
// 总览还会取排序偏好、任务标记与待归类;不拦住这些请求就会打到真实后端并 401,
// 401 处理会清掉登录标记并跳回 /login,用例与真实后端状态耦合。
await page.route('**/api/review/sort-preference**', (route) =>
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(mockSortPreference) }),
);
await page.route('**/api/markers/batch**', (route) =>
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(mockEmptyMap) }),
);
await page.route('**/api/report-fragments/pending-ownership**', (route) =>
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(mockEmptyList) }),
);
await page.route('**/api/review/summary**', (route) =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ code: 200, data: { taskTotal: 45, reportTotal: 4, fragmentTotal: 7 } }),
}),
);
await page.route('**/api/tasks/options**', (route) =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ code: 200, data: [{ taskNum: 'T001', taskName: '学习 Vue3' }] }),
}),
);
await page.goto('/login');
await page.evaluate(() => localStorage.setItem('isLoggedIn', 'true'));
await page.goto('/review');
await page.waitForSelector('.review-page', { timeout: 10_000 });
await page.addInitScript(() => { localStorage.setItem('isLoggedIn', 'true'); localStorage.removeItem('activeSession'); });
await page.route(url => url.pathname.startsWith('/api/'), async route => {
const url = new URL(route.request().url()); let data: unknown = [];
if (url.pathname === '/api/markers/tags') data = [{ markerName: '中文', markerCount: 2, objectCount: 2 }];
else if (url.pathname === '/api/review/tag-mind-map/list' || url.pathname === '/api/review/recalls') data = pageOf([]);
else if (url.pathname === '/api/review/summary') data = { taskTotal: 45, reportTotal: 4, fragmentTotal: 7 };
else if (url.pathname === '/api/review/sort-preference') data = { preset: 'TIME_FIRST', description: '久未接触的更容易出现' };
else if (url.pathname === '/api/tasks/options') data = [{ taskNum: 'T001', taskName: '学习 Vue3' }];
await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ code: 200, data }) });
});
});
test('displays summary metrics and task list', async ({ page }) => {
// 指标收成一行紧凑统计条,不再占三张卡片
// 汇总走独立接口:数字是全量口径(45 个任务),不是分页后本页的 2 条
const stats = page.locator('.page-stats');
await expect(stats).toContainText('任务 45');
await expect(stats).toContainText('学习报告 4');
await expect(stats).toContainText('学习残片 7');
// 两个模块同位置分 Tab,Tab 上带条数
await expect(page.getByRole('tab')).toHaveCount(2)
await expect(page.getByRole('tab', { name: /按任务复习/ })).toContainText('45')
await expect(page.getByRole('tab', { name: /按标签复习/ })).toBeVisible()
// 任务以卡片列表展示(不再是 el-table)
await expect(page.locator('.task-card')).toHaveCount(2);
await expect(page.getByText('学习 Vue3')).toBeVisible();
await expect(page.getByRole('button', { name: '回忆复习' })).toHaveCount(2);
test('按碎片标签选择范围,小字说明标签与任务的关系', async ({ page }) => {
const requests: string[] = []; page.on('request', request => requests.push(request.url()));
await page.goto('/review'); await expect(page.getByRole('heading', { name: SCOPE_TITLE })).toBeVisible();
await expect(page.getByText('标签是知识碎片的分类标记。', { exact: false })).toBeVisible();
await expect(page.getByRole('tab')).toHaveCount(0); await expect(page.locator('.task-card')).toHaveCount(0);
await expect(page.locator('.page-stats')).toContainText('任务 45'); expect(requests.some(url => url.includes('/markers/tags?objectType=FRAGMENT'))).toBe(true);
expect(requests.some(url => url.includes('/review/tasks'))).toBe(false);
await page.getByRole('combobox', { name: '碎片标签' }).focus(); await page.keyboard.press('ArrowDown');
await page.getByRole('option', { name: '中文', exact: true }).click(); await page.getByRole('heading', { name: SCOPE_TITLE }).click();
await page.getByRole('radio', { name: '交集(同时带全部标签)' }).focus(); await page.keyboard.press('Space');
await page.getByRole('button', { name: '选择这个范围' }).click(); await expect(page).toHaveURL(/by-tag.*mode=INTERSECT/);
await expect(page.getByRole('button', { name: '开始回忆', exact: true })).toBeVisible();
});
test('切到按标签复习:写入 URL 并懒加载该 Tab 的内容', async ({ page }) => {
await page.getByRole('tab', { name: /按标签复习/ }).click()
await expect(page).toHaveURL(/tab=tag/)
await expect(page.getByText('已生成的标签导图')).toBeVisible()
// 刷新后仍落在标签 Tab(URL 即状态)
await page.reload()
await expect(page.getByText('已生成的标签导图')).toBeVisible()
test('当前回忆记录分页,旧任务导图与旧对比记录入口已移除', async ({ page }) => {
const requested: string[] = [];
await page.route('**/api/review/recalls**', route => {
const url = new URL(route.request().url()); requested.push(url.search);
const detail = recallFixture(); detail.scope.displayName = url.searchParams.get('pageNum') === '2' ? '第二页回忆' : '第一页草稿';
return route.fulfill({ json: { code: 200, data: pageOf([detail], 21, Number(url.searchParams.get('pageNum') || 1)) } });
});
await page.goto('/review'); await page.getByText('自由回忆记录', { exact: true }).click();
await expect(page.getByText('第一页草稿', { exact: true })).toBeVisible(); await expect(page.getByRole('button', { name: '查看草稿' })).toBeVisible();
await page.locator('.review-history .btn-next').click(); await expect(page.getByText('第二页回忆', { exact: true })).toBeVisible();
expect(requested.some(query => query.includes('pageNum=2'))).toBe(true);
await expect(page.getByText('早期任务导图与回忆记录', { exact: true })).toHaveCount(0);
});
test('任务超过一页时出现分页,翻页会带上页号', async ({ page }) => {
const requested: string[] = []
await page.route('**/api/review/tasks**', (route) => {
requested.push(route.request().url())
const isSecondPage = route.request().url().includes('pageNum=2')
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
code: 200,
data: { ...pageOf(mockTasksData.data.records as unknown[], 45), current: isSecondPage ? 2 : 1 },
}),
})
})
await page.goto('/review')
await page.waitForSelector('.task-card')
const pagination = page.locator('.list-pagination')
await expect(pagination).toBeVisible()
await pagination.locator('.btn-next').click()
await expect.poll(() => requested.some((url) => url.includes('pageNum=2'))).toBe(true)
test('低频操作仍在更多菜单', async ({ page }) => {
await page.goto('/review'); await page.getByRole('button', { name: '更多操作' }).click();
for (const name of ['导出学习数据', '标签管理', '智能排序偏好']) await expect(page.getByRole('menuitem', { name })).toBeVisible();
});
test('低频操作收进「更多」菜单', async ({ page }) => {
await expect(page.getByRole('button', { name: '更多操作' })).toBeVisible();
// 刷新已移除;导出与标签管理不在页面上平铺
await expect(page.getByRole('button', { name: '刷新' })).toHaveCount(0);
await expect(page.getByRole('button', { name: '导出数据' })).toHaveCount(0);
await page.getByRole('button', { name: '更多操作' }).click();
await expect(page.getByRole('menuitem', { name: '导出学习数据' })).toBeVisible();
await expect(page.getByRole('menuitem', { name: '标签管理' })).toBeVisible();
await expect(page.getByRole('menuitem', { name: '智能排序偏好' })).toBeVisible();
test('标签错误可重试,空态提供下一步动作', async ({ page }) => {
await page.route('**/api/markers/tags?**', route => route.fulfill({ json: { code: 500, data: null } }));
await page.goto('/review'); await expect(page.getByText('标签未能读取,请检查网络后重试。')).toBeVisible();
await expect(page.getByText('还没有带标签的碎片。', { exact: false })).toHaveCount(0);
await page.route('**/api/markers/tags?**', route => route.fulfill({ json: { code: 200, data: [] } }));
await page.getByRole('button', { name: '重试读取标签' }).click();
await expect(page.getByText('还没有带标签的碎片。', { exact: false })).toBeVisible(); await expect(page.getByRole('button', { name: '查看学习碎片' })).toBeVisible();
});
test('shows empty state when no tasks', async ({ page }) => {
await page.route('**/api/review/summary**', (route) =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ code: 200, data: { taskTotal: 0, reportTotal: 0, fragmentTotal: 0 } }),
}),
);
await page.route('**/api/review/tasks**', (route) =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify(mockTasksEmpty),
}),
);
await page.goto('/review');
await page.waitForSelector('.review-page', { timeout: 10_000 });
// 三个 0 不承载信息:统计条收起,而不是显示「任务 0」
await expect(page.locator('.page-stats')).toHaveCount(0);
await expect(page.getByText('暂无复习任务')).toBeVisible();
// 空态必须有下一步动作
await expect(page.getByRole('button', { name: '新建学习任务' })).toBeVisible();
test('375px长标签可读且不会横向溢出', async ({ page }) => {
const name = '长标签'.repeat(85);
await page.route('**/api/markers/tags?**', route => route.fulfill({ json: { code: 200, data: [{ markerName: name, markerCount: 1, objectCount: 1 }] } }));
await page.setViewportSize({ width: 375, height: 812 }); await page.goto('/review');
await page.getByRole('combobox', { name: '碎片标签' }).focus(); await page.keyboard.press('ArrowDown');
await page.getByRole('option', { name, exact: true }).click(); await page.getByRole('heading', { name: SCOPE_TITLE }).click();
const geometry = await page.locator('.tag-scope').evaluate(scope => {
const text = scope.querySelector('.el-select__tags-text')!; const chip = text.closest('.el-tag')!;
const select = scope.querySelector('.el-select__wrapper')!; const label = scope.querySelector('label[for="recall-tags"]')!;
const bounds = (node: Element) => { const rect = node.getBoundingClientRect(); return { top: rect.top, bottom: rect.bottom, left: rect.left, right: rect.right }; };
return { text: bounds(text), chip: bounds(chip), select: bounds(select), label: bounds(label) };
});
expect(geometry.text.bottom).toBeLessThanOrEqual(geometry.chip.bottom); expect(geometry.chip.bottom).toBeLessThanOrEqual(geometry.select.bottom);
expect(geometry.select.top).toBeGreaterThanOrEqual(geometry.label.bottom); expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)).toBe(true);
await page.getByRole('button', { name: '选择这个范围' }).click(); await expect(page.getByRole('button', { name: '开始回忆', exact: true })).toBeVisible();
});
test('接口失败时提示加载失败而不是显示空列表', async ({ page }) => {
await page.route('**/api/review/tasks**', (route) =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ code: 500, message: '服务异常', data: null }),
}),
);
await page.goto('/review');
await page.waitForSelector('.review-page', { timeout: 10_000 });
await expect(page.getByText('复习数据加载失败,请稍后重试')).toBeVisible();
await expect(page.getByRole('button', { name: '重新加载' })).toBeVisible();
await expect(page.getByText('暂无复习任务')).toHaveCount(0);
test('进入工作区及浏览器返回时恢复页面顶部', async ({ page }) => {
await page.goto('/review'); await page.getByRole('combobox', { name: '碎片标签' }).focus(); await page.keyboard.press('ArrowDown');
await page.getByRole('option', { name: '中文', exact: true }).click(); await page.getByRole('heading', { name: SCOPE_TITLE }).click();
await page.evaluate(() => { document.querySelector<HTMLElement>('.app-shell')!.scrollTop = 170; });
await page.getByRole('button', { name: '选择这个范围' }).click(); await expect(page.getByRole('button', { name: '开始回忆', exact: true })).toBeVisible();
await expect.poll(() => page.locator('.app-shell').evaluate(node => node.scrollTop)).toBe(0);
await page.goBack(); await expect(page.getByRole('heading', { name: SCOPE_TITLE })).toBeVisible();
await expect.poll(() => page.locator('.app-shell').evaluate(node => node.scrollTop)).toBe(0);
});
test('详情页返回按钮为「返回总览」并回到复习总览', async ({ page }) => {
await page.route('**/api/review/task/**', (route) =>
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ code: 200, data: [] }) }),
);
await page.locator('.task-card').first().click();
await expect(page).toHaveURL(/\/review\/detail\/task\/T001/, { timeout: 5000 });
const backBtn = page.locator('.head .back-btn');
await expect(backBtn).toHaveText('← 返回总览');
await backBtn.click();
await expect(page).toHaveURL(/\/review$/, { timeout: 5000 });
test('学习记录详情仍可返回总览', async ({ page }) => {
await page.goto('/review/detail/task/T001'); const back = page.locator('.head .back-btn'); await expect(back).toHaveText('← 返回总览');
await back.click(); await expect(page).toHaveURL(/\/review$/);
});
});
for (const entry of ['/review/detail/task/T001', '/review/detail/fragment/7']) {
test(`REVIEW来源从${entry}进入仍有任务历史、导出和回忆入口`, async ({ page }) => {
const requests: string[] = []; const exportContent = '# 学习 Vue3\n\n会话知识碎片\n\n复习引导收获全文';
await page.addInitScript(() => { localStorage.setItem('isLoggedIn', 'true'); localStorage.removeItem('activeSession'); });
await page.route(url => url.pathname.startsWith('/api/'), async route => {
const url = new URL(route.request().url()); requests.push(url.pathname); let data: unknown = [];
if (url.pathname === '/api/review/task/T001') data = [
{ id: 7, sourceType: 'FRAGMENT', taskNum: 'T001', taskName: '学习 Vue3', content: '复习引导收获', contextType: 'REVIEW', contextNum: 'R007', createdTime: '2026-11-15T10:00:00' },
{ id: 6, sourceType: 'FRAGMENT', taskNum: 'T001', taskName: '学习 Vue3', content: '会话知识碎片', contextType: 'SESSION', contextNum: 'S006', createdTime: '2026-11-14T10:00:00' },
];
else if (url.pathname === '/api/review/fragment/7') data = { id: 7, content: '复习引导收获全文', ownerTaskNum: 'T001', contextType: 'REVIEW', contextNum: 'R007' };
else if (url.pathname === '/api/markers/batch') data = {};
else if (url.pathname === '/api/export/task/T001') data = { fileName: 'task-T001.md', contentType: 'text/markdown', content: exportContent };
else if (url.pathname === '/api/review/recalls') data = pageOf([]);
await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ code: 200, data }) });
});
await page.goto(entry); await expect(page.getByText('任务:学习 Vue3', { exact: true })).toBeVisible();
await expect(page.getByText('来源:复习记录', { exact: true })).toBeVisible(); await expect(page.getByText('复习引导收获全文', { exact: true })).toBeVisible();
await expect(page.getByRole('heading', { name: '该任务下的所有学习记录' })).toBeVisible(); await expect(page.locator('.history-session')).toHaveCount(2);
expect(requests.some(path => path.startsWith('/api/study-sessions/'))).toBe(false);
await page.getByRole('button', { name: '导出该任务', exact: true }).click(); const downloading = page.waitForEvent('download');
await page.getByRole('dialog').getByRole('button', { name: '导出', exact: true }).click(); const download = await downloading;
expect(download.suggestedFilename()).toBe('task-T001.md'); const stream = await download.createReadStream(); const chunks: Buffer[] = [];
for await (const chunk of stream!) chunks.push(Buffer.from(chunk)); expect(Buffer.concat(chunks).toString()).toBe(exportContent);
await page.getByRole('button', { name: '回忆复习', exact: true }).click(); await expect(page).toHaveURL(/review\/by-tag\?entryType=FRAGMENT/);
await expect(page.getByRole('heading', { name: SCOPE_TITLE })).toBeVisible();
});
}