import { test, expect, type Page } from '@playwright/test'; import { copy, recallFixture, referencePageFixture, referenceContentFixture, requestFixture } from '../src/__tests__/support/recall'; import type { CreateRecallRequest, RecallDetail, SaveDraftRequest } from '../src/api/recalls'; /** HTTP fixtures isolate UI behavior; the backend integration suite covers actual persistence. */ const setup = async (page: Page, previous: RecallDetail | null = null) => { let remote = recallFixture(); let sequence = 0; const saves: SaveDraftRequest[] = []; const creates: CreateRecallRequest[] = []; const sourceReads: string[] = []; await page.addInitScript(() => { localStorage.setItem('isLoggedIn', 'true'); localStorage.setItem('lpt.recall.accountId', 'controlled-account'); localStorage.removeItem('activeSession'); }); await page.route(url => url.pathname.startsWith('/api/'), async route => { const request = route.request(); const url = new URL(request.url()); const path = url.pathname.replace(/^\/api/, ''); let data: unknown = []; if (path === '/review/recalls' && request.method() === 'GET') data = { current: 1, size: 20, total: previous ? 1 : 0, records: previous ? [previous] : [] }; else if (path === '/review/recalls' && request.method() === 'POST') { const body = request.postDataJSON() as CreateRecallRequest; creates.push(body); remote.editorId = body.editorId; remote.scope = { ...remote.scope, ...body.scope }; data = remote; } else if (path === '/review/recalls/R001' && request.method() === 'GET') data = remote; else if (path.endsWith('/resume')) { remote.editorId = request.postDataJSON().editorId; remote.writeToken = `write-${++sequence}`; data = remote; } else if (path.endsWith('/draft')) { const body = request.postDataJSON() as SaveDraftRequest; saves.push(copy(body)); remote.expression = copy(body.expression); remote.writeToken = `write-${++sequence}`; data = remote; } else if (path.endsWith('/references')) { const current = Number(url.searchParams.get('pageNum') || 1); data = { ...referencePageFixture(), current, total: 21, records: Array.from({ length: current < 3 ? 10 : 1 }, (_, i) => ({ ...referencePageFixture().records[0], sourceId: `fragment:${(current - 1) * 10 + i + 7}`, title: `资料${(current - 1) * 10 + i + 1}` })) }; } else if (path.includes('/references/sources/')) { sourceReads.push(path); data = referenceContentFixture(); } else if (path === '/ai/tasks') { const type = request.postDataJSON().type; const task = { ...requestFixture(type === 'RECALL_HINT' ? 'HINT' : 'CHECK'), status: 'RUNNING' as const, result: null }; remote.requests = [task]; data = { ...task, targetNum: 'R001', createdTime: task.submittedAt, finishedTime: null }; } else if (path === '/ai/tasks/AI001') data = { ...remote.requests[0], targetNum: 'R001' }; else if (path === '/markers/tags') data = [{ markerName: '中文', markerCount: 1, objectCount: 1 }]; await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ code: 200, data }) }); }); return { saves, creates, sourceReads, remote: () => remote }; }; const start = async (page: Page) => { await page.goto('/review/by-tag?tag=中文&mode=UNION'); await page.getByRole('button', { name: '开始回忆', exact: true }).click(); await expect(page).toHaveURL(/\/review\/recalls\/R001$/); }; const askHint = async (page: Page, form = '关键词') => { await page.getByRole('button', { name: '提示', exact: true }).click(); await page.getByRole('menuitem', { name: form, exact: true }).click(); }; test('原生节点编辑自动保存,Tab与Enter新增节点默认向右,没有重复节点工具', async ({ page }) => { const state = await setup(page); await start(page); const input = page.getByRole('textbox', { name: '节点内容', exact: true }); await expect(input).toBeVisible(); await expect(page.getByRole('button', { name: '写下你想起的内容' })).toHaveCount(0); await input.fill('中文😀根概念'); await expect.poll(() => state.remote().expression.root.title).toBe('中文😀根概念'); await expect(input).toBeFocused(); await input.press('Enter'); await page.keyboard.press('Tab'); await page.getByRole('textbox', { name: '节点内容', exact: true }).fill('子概念'); await page.keyboard.press('Enter'); await page.keyboard.press('Enter'); await page.getByRole('textbox', { name: '节点内容', exact: true }).fill('同级概念'); await page.keyboard.press('Enter'); await expect.poll(() => state.remote().expression.root.children.length).toBe(2); const root = await page.locator('me-root > me-tpc').boundingBox(); const child = await page.locator('me-main me-tpc').first().boundingBox(); expect(child!.x).toBeGreaterThan(root!.x); for (const name of ['编辑节点内容', '添加关联节点', '添加同级节点', '删除节点']) await expect(page.getByRole('button', { name, exact: true })).toHaveCount(0); expect(state.saves.at(-1)).not.toHaveProperty('expressionVersion'); expect(state.saves.at(-1)).not.toHaveProperty('notes'); }); test('新建回忆永远空白,没有版本选择和编辑历史', async ({ page }) => { const previous = recallFixture(); previous.expression.root.title = '此前保存内容'; const state = await setup(page, previous); await start(page); await expect(page.locator('me-root > me-tpc')).toHaveText(''); expect(state.creates[0]).not.toHaveProperty('carryFrom'); await expect(page.getByText('修改历史', { exact: true })).toHaveCount(0); await expect(page.getByText(/表达 v\d/)).toHaveCount(0); }); test('打开历史导图直接进入编辑,本机没有副本时不需要再点确认', async ({ page }) => { const state = await setup(page); await page.goto('/review/recalls/R001'); await expect(page.locator('#recall-hud-body')).toBeVisible(); await expect(page.getByRole('button', { name: '结束回忆', exact: true })).toBeVisible(); await expect(page.locator('.resume-panel')).toHaveCount(0); await page.getByRole('textbox', { name: '节点内容', exact: true }).fill('接着改的根节点'); await expect.poll(() => state.remote().expression.root.title).toBe('接着改的根节点'); }); test('本机有缓存时直接显示缓存内容,不出现核对面板', async ({ page }) => { const state = await setup(page); await page.addInitScript(() => { localStorage.setItem('lpt.recall.draft.controlled-account.R001', JSON.stringify({ writeToken: 'write-a', expression: { schemaVersion: 1, root: { id: 'root-a', title: '浏览器缓存内容', notes: '', provenance: [], children: [] } }, pendingSave: null, })); }); await page.goto('/review/recalls/R001'); await expect(page.locator('me-root > me-tpc')).toContainText('浏览器缓存内容'); await expect(page.locator('.resume-panel')).toHaveCount(0); await expect(page.getByRole('button', { name: '结束回忆', exact: true })).toBeVisible(); await page.locator('me-root > me-tpc').dblclick({ delay: 50 }); await page.getByRole('textbox', { name: '节点内容', exact: true }).fill('缓存后继续'); await expect.poll(() => state.remote().expression.root.title).toBe('缓存后继续'); }); test('范围没有匹配碎片时不进入编辑页面,提示重新选择范围', async ({ page }) => { const state = await setup(page); state.remote().reference = { status: 'EMPTY', total: 0, limitations: [] }; await page.goto('/review/recalls/R001'); await expect(page.getByText('这个范围没有匹配到知识碎片')).toBeVisible(); await expect(page.getByRole('button', { name: '重新选择范围', exact: true })).toBeVisible(); await expect(page.locator('me-root')).toHaveCount(0); await expect(page.getByRole('button', { name: '提示', exact: true })).toHaveCount(0); await expect(page.getByRole('button', { name: '主动查漏', exact: true })).toHaveCount(0); }); test('结束回忆保存当前导图并退出,保存失败时保留页面和输入', async ({ page }) => { const state = await setup(page); let fail = true; await page.route('**/api/review/recalls/R001/draft', async route => { if (fail) return route.fulfill({ json: { code: 500, message: '测试网络失败', data: null } }); await route.fallback(); }); await start(page); await page.getByRole('textbox', { name: '节点内容', exact: true }).fill('最终内容'); await page.getByRole('button', { name: '结束回忆', exact: true }).click(); await expect(page.getByText('尚未结束回忆。本页输入仍保留,请检查网络后重试。')).toBeVisible(); await expect(page).toHaveURL(/recalls\/R001$/); await expect(page.locator('me-root')).toContainText('最终内容'); fail = false; await page.getByRole('button', { name: '结束回忆', exact: true }).click(); await expect(page).toHaveURL(/\/review$/); expect(state.saves.at(-1)?.expression.root.title).toBe('最终内容'); }); test('碎片默认收起,展开才看到原文开头,查看全文按来源读取,后端分页没有版本参数', async ({ page }) => { const state = await setup(page); await start(page); const materials = page.getByRole('region', { name: '相关碎片', exact: true }); await expect(materials.locator('.material-item')).toHaveCount(10); await expect(materials.locator('.material-preview')).toHaveCount(0); await materials.getByRole('button', { name: '展开', exact: true }).first().click(); await expect(materials.locator('.material-preview').first()).toHaveText('中文😀'); expect(state.sourceReads).toHaveLength(0); await materials.getByRole('button', { name: '查看全文', exact: true }).first().click(); await expect(page.locator('.el-dialog .source-text')).toHaveText('中文😀知识'); expect(state.sourceReads[0]).toContain('/references/sources/fragment%3A7'); await page.getByRole('button', { name: 'Close this dialog' }).click(); await materials.getByRole('button', { name: 'Go to next page' }).click(); await expect(materials.locator('.material-item').first()).toContainText('资料11'); }); test('AI等待在按钮附近展示,结果到达不覆盖等待期间编辑的当前图', async ({ page }) => { const state = await setup(page); await start(page); await page.getByRole('textbox', { name: '节点内容', exact: true }).fill('原内容'); await page.keyboard.press('Enter'); await page.getByRole('button', { name: '主动查漏', exact: true }).click(); await expect(page.locator('.help-actions .ai-runtime')).toContainText('AI处理中'); await expect(page.locator('.records-panel')).toHaveCount(0); await page.locator('me-root > me-tpc').dblclick({ delay: 50 }); await page.getByRole('textbox', { name: '节点内容', exact: true }).fill('等待期间补充'); await page.keyboard.press('Enter'); state.remote().requests[0] = requestFixture(); await expect(page.locator('.records-panel')).toContainText('想想适用条件'); await expect(page.locator('me-root')).toContainText('等待期间补充'); expect(state.remote().expression.root.title).toBe('等待期间补充'); await expect(page.locator('.records-panel')).not.toContainText('任务编号'); }); test('失败原因留在按钮旁,不创建提示内容记录,重新读取不触发生成', async ({ page }) => { const state = await setup(page); await start(page); await askHint(page); await expect(page.locator('.help-actions .ai-runtime')).toContainText('AI处理中'); state.remote().requests[0] = { ...requestFixture('HINT'), status: 'FAILED', errorCode: 'AI_OUTPUT_LIMIT', result: null }; await expect(page.locator('.help-actions .ai-error')).toContainText('长度限制'); await expect(page.locator('.records-panel')).toHaveCount(0); let submissions = 0; page.on('request', request => { if (request.method() === 'POST' && request.url().endsWith('/api/ai/tasks')) submissions++; }); await page.getByRole('button', { name: '重新读取状态' }).click(); expect(submissions).toBe(0); }); test('固定限制说明默认收起,悬浮与点按问号才展开', async ({ page }) => { const state = await setup(page); await start(page); const materials = page.getByRole('region', { name: '相关碎片', exact: true }); const referenceLine = page.getByText('资料可能有遗漏或相互冲突,不作为标准答案。'); await expect(referenceLine).toBeHidden(); await materials.getByRole('button', { name: '相关碎片的说明', exact: true }).hover(); await expect(referenceLine).toBeVisible(); await page.mouse.move(0, 0); await expect(referenceLine).toBeHidden(); await askHint(page); await expect(page.locator('.help-actions .ai-runtime')).toContainText('AI处理中'); state.remote().requests[0] = requestFixture('HINT'); await expect(page.locator('.records-panel')).toHaveCount(1); const hintLine = page.getByText('少量提示,不代表全范围分析'); await expect(hintLine).toBeHidden(); await page.getByRole('button', { name: '这次提示与查漏的说明', exact: true }).click(); await expect(hintLine).toBeVisible(); }); test('360px计时条分组、导图根节点可见、辅助区默认收起、提示形式菜单可用且页面无横向溢出', async ({ page }) => { await page.setViewportSize({ width: 360, height: 640 }); await setup(page); await start(page); await expect(page.locator('.timing-bar')).toContainText(/24:|25:/); await expect(page.locator('.recall-metrics')).toContainText('有效时间'); await expect(page.getByRole('button', { name: '结束回忆', exact: true })).toHaveClass(/el-button--success/); await expect(page.locator('#recall-help-body')).toBeHidden(); await page.getByRole('textbox', { name: '节点内容', exact: true }).fill('根节点'); await page.keyboard.press('Enter'); const canvas = await page.locator('.mind-map-viewer').boundingBox(); const root = await page.locator('me-root > me-tpc').boundingBox(); expect(root!.x).toBeGreaterThanOrEqual(canvas!.x); expect(root!.x + root!.width).toBeLessThanOrEqual(canvas!.x + canvas!.width); expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)).toBe(true); await page.locator('.help-drawer').getByRole('button', { name: '展开提示', exact: true }).click(); await expect(page.locator('#recall-help-body')).toBeVisible(); for (const label of ['提示', '主动查漏']) expect((await page.getByRole('button', { name: label, exact: true }).boundingBox())!.height).toBeGreaterThanOrEqual(44); await page.getByRole('button', { name: '提示', exact: true }).click(); await expect(page.getByRole('menuitem', { name: '小树', exact: true })).toBeVisible(); expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)).toBe(true); await page.keyboard.press('Escape'); }); test('树提示自动挂到现有节点并带来源色,记录默认模糊', async ({ page }) => { const state = await setup(page); await start(page); await page.getByRole('textbox', { name: '节点内容', exact: true }).fill('数据库事务'); await page.keyboard.press('Enter'); await askHint(page, '小树'); await expect(page.locator('.help-actions .ai-runtime')).toContainText('AI处理中'); const hint = requestFixture('HINT'); hint.result!.hints = [{ id: 'hint-tree', form: 'TREE', clue: '事务边界', anchorNodeId: 'root-a', nodes: [{ id: 'n1', parentId: null, text: '原子性' }, { id: 'n2', parentId: 'n1', text: '全部回滚' }], citations: hint.result!.hints[0].citations }]; state.remote().requests[0] = hint; await expect(page.locator('me-tpc', { hasText: '原子性' })).toBeVisible(); await expect(page.locator('me-tpc', { hasText: '全部回滚' })).toBeVisible(); await expect.poll(() => state.remote().expression.root.children.some(node => node.title === '原子性')).toBe(true); const background = await page.locator('me-tpc', { hasText: '原子性' }).evaluate(element => getComputedStyle(element).backgroundColor); expect(background).toBe('rgb(255, 248, 225)'); await expect(page.locator('.hint-content')).toHaveAttribute('aria-hidden', 'true'); await page.getByRole('button', { name: '显示内容', exact: true }).click(); await expect(page.locator('.records-panel')).toContainText('已加入「数据库事务」下'); }); test('节点配色从右键菜单选择并持久化到导图表达式', async ({ page }) => { const state = await setup(page); await start(page); await page.getByRole('textbox', { name: '节点内容', exact: true }).fill('根节点'); await page.keyboard.press('Enter'); await page.locator('me-root > me-tpc').click({ button: 'right' }); await page.getByText('节点配色', { exact: true }).click(); await expect(page.locator('.node-palette')).toBeVisible(); await page.getByRole('button', { name: '琥珀', exact: true }).click(); await expect.poll(() => state.remote().expression.root.style?.background).toBe('#fff8e1'); const background = await page.locator('me-root > me-tpc').evaluate(element => getComputedStyle(element).backgroundColor); expect(background).toBe('rgb(255, 248, 225)'); }); test('触屏没有右键:选中节点后用「配色」小按钮改色', async ({ page }) => { await page.setViewportSize({ width: 375, height: 812 }); const state = await setup(page); await start(page); await page.getByRole('textbox', { name: '节点内容', exact: true }).fill('事务边界'); await page.keyboard.press('Enter'); await page.locator('me-root > me-tpc').click(); const chip = page.getByRole('button', { name: '节点配色', exact: true }); await expect(chip).toBeVisible(); await chip.click(); await expect(page.locator('.node-palette')).toBeVisible(); const box = await page.locator('.node-palette').boundingBox(); expect(box!.y + box!.height).toBeLessThanOrEqual(812); await page.locator('.node-palette').getByRole('button', { name: '蓝', exact: true }).click(); await expect.poll(() => state.remote().expression.root.style?.background).toBe('#e3f2fd'); await expect(page.locator('.node-palette')).toHaveCount(0); }); test('导图铺满工作区,提示与查漏浮在右上角,可收起再展开', async ({ page }) => { await page.setViewportSize({ width: 1440, height: 900 }); const state = await setup(page); await start(page); const stage = await page.locator('.workspace-stage').boundingBox(); const map = await page.locator('.mind-map-viewer').boundingBox(); expect(await page.evaluate(() => document.documentElement.scrollHeight <= innerHeight)).toBe(true); const open = await page.locator('.help-drawer').boundingBox(); expect(map!.width).toBeGreaterThan(stage!.width * 0.95); expect(open!.x).toBeGreaterThan(stage!.x + stage!.width / 2); expect(open!.x).toBeLessThan(map!.x + map!.width); expect(open!.y).toBeGreaterThan(stage!.y); expect(open!.y + open!.height).toBeLessThanOrEqual(stage!.y + stage!.height); const hud = page.locator('.hud'); await expect(page.locator('#recall-hud-body')).toBeVisible(); await hud.getByRole('button', { name: '收起计时', exact: true }).click(); await expect(page.locator('#recall-hud-body')).toBeHidden(); await expect(page.locator('.hud-pill')).toContainText('结束回忆'); await hud.getByRole('button', { name: '展开计时', exact: true }).click(); await expect(page.locator('#recall-hud-body')).toBeVisible(); const drawer = page.locator('.help-drawer'); await drawer.getByRole('button', { name: '收起提示', exact: true }).click(); await expect(page.locator('#recall-help-body')).toBeHidden(); await expect(page.locator('.drawer-head')).toContainText('相关碎片 1 条'); const collapsed = await drawer.boundingBox(); expect(collapsed!.height).toBeLessThan(open!.height); await drawer.getByRole('button', { name: '展开提示', exact: true }).click(); await expect(page.locator('#recall-help-body')).toBeVisible(); await page.getByRole('button', { name: '主动查漏', exact: true }).click(); await expect(page.locator('.help-actions .ai-runtime')).toContainText('AI处理中'); state.remote().requests[0] = requestFixture(); await expect(page.locator('.records-panel')).toBeVisible(); });