Files
wrong-notebook/e2e/notebook-sharing.spec.ts
margrop c4fb3959b9 feat: 将错题本共享成员管理收进页头弹窗并支持回车邀请
- 无共享成员时不再占用首屏;页头按钮显示共享人数。
- 非所有者共享来源只在页头展示,弹窗内保留只读提示与退出共享。
- 邀请区域改为表单提交,邮箱输入框回车即可邀请。
Refs: https://github.com/margrop/wrong-notebook/issues/81
2026-12-04 18:33:27 +08:00

147 lines
8.9 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 './support/fixtures';
import type { BrowserContext, Page } from '@playwright/test';
import { acceptConfirmDialog } from './helpers';
/**
* 错题本共享与权限流 E2E:
* 覆盖邀请、接受/拒绝、只读限制、权限升级(只读 → 可编辑)、成员移除,以及共享错题本参与原题组卷。
*/
test.describe('错题本共享与权限', () => {
test('[E2E-S01] 邀请只读成员 → 接受 → 只读限制 → 升级可编辑 → 移除成员', async ({ browser, page, scenario }) => {
const questionText = `共享错题测试题。${scenario.marker}`;
const item = await scenario.item('A', { questionText });
// 1. 所有者(学生 A)登录并进入错题本详情页,从页头「共享成员」打开弹窗,回车邀请学生 B(默认只读)
await scenario.login(page, false);
await page.goto(`/notebooks/${scenario.book.id}`);
await expect(page.getByText(questionText)).toBeVisible();
await expect(page.getByRole('dialog')).toHaveCount(0);
await page.getByRole('button', { name: '共享成员', exact: true }).click();
const membersDialog = page.getByRole('dialog').filter({ has: page.getByRole('heading', { name: '共享成员' }) });
await expect(membersDialog).toBeVisible();
await membersDialog.locator('#share-email').fill(scenario.otherUser.email);
await membersDialog.locator('#share-role').selectOption('read');
await membersDialog.locator('#share-email').press('Enter');
await expect(membersDialog.getByText('E2E 学生 B')).toBeVisible();
await expect(membersDialog.getByText('待接受')).toBeVisible();
await page.keyboard.press('Escape');
await expect(membersDialog).not.toBeVisible();
// 2. 成员(学生 B)在独立浏览器上下文登录,在 /notebooks 看到邀请并接受
const memberContext: BrowserContext = await browser.newContext();
const memberPage: Page = await memberContext.newPage();
await scenario.guardPage(memberPage);
try {
await scenario.login(memberPage, true);
await memberPage.goto('/notebooks');
await expect(memberPage.getByText('共享邀请')).toBeVisible();
await expect(memberPage.getByText(/共享自 E2E 学生 A.*只读/)).toBeVisible();
await memberPage.getByRole('button', { name: '接受', exact: true }).click();
await expect(memberPage.getByText('共享邀请')).toHaveCount(0);
// 3. 成员进入共享错题本:能看到题目,但只读模式下没有「多选」「添加错题」按钮
await memberPage.goto(`/notebooks/${scenario.book.id}`);
await expect(memberPage.getByText(questionText)).toBeVisible();
await memberPage.getByRole('button', { name: '共享成员', exact: true }).click();
const memberDialog = memberPage.getByRole('dialog').filter({ has: memberPage.getByRole('heading', { name: '共享成员' }) });
await expect(memberDialog.getByText('只读成员可以查看题目,但不能修改')).toBeVisible();
await expect(memberDialog.getByRole('button', { name: '退出共享', exact: true })).toBeVisible();
await memberPage.keyboard.press('Escape');
await expect(memberDialog).not.toBeVisible();
await expect(memberPage.getByRole('button', { name: '多选', exact: true })).toHaveCount(0);
await expect(memberPage.getByRole('button', { name: '添加错题' })).toHaveCount(0);
// 只读成员进入错题详情:可查看原图与练习,但没有删除、编辑、重生成等写入口
await memberPage.goto(`/error-items/${item.id}`);
await expect(memberPage.getByText(questionText)).toBeVisible();
await expect(memberPage.getByRole('button', { name: '删除', exact: true })).toHaveCount(0);
await expect(memberPage.getByRole('button', { name: /编辑/ })).toHaveCount(0);
await expect(memberPage.getByRole('button', { name: '重新生成完整解答' })).toHaveCount(0);
await expect(memberPage.getByRole('button', { name: '举一反三' })).toBeVisible();
await memberPage.goto(`/notebooks/${scenario.book.id}`);
// 4. 所有者刷新后看到成员已生效,将权限升级为「可编辑」
await page.reload();
await expect(page.getByText('共享(1)')).toBeVisible();
await page.getByRole('button', { name: '共享成员', exact: true }).click();
await expect(membersDialog.getByText('已生效')).toBeVisible();
const memberRow = membersDialog.locator('div', { hasText: 'E2E 学生 B' }).filter({ has: page.getByRole('button', { name: '移除' }) }).last();
await memberRow.getByLabel('权限').selectOption('edit');
// 5. 成员刷新后获得编辑权限:可见「多选」和「添加错题」
await memberPage.reload();
await expect(memberPage.getByRole('button', { name: '多选', exact: true })).toBeVisible();
await expect(memberPage.getByRole('button', { name: '添加错题' })).toBeVisible();
// 6. 所有者移除成员,成员失去访问权限
await memberRow.getByRole('button', { name: '移除' }).click();
await acceptConfirmDialog(page);
await expect(membersDialog.getByText('还没有共享成员')).toBeVisible();
await page.keyboard.press('Escape');
await expect(membersDialog).not.toBeVisible();
await memberPage.goto('/notebooks');
await expect(memberPage.getByText('共享自 E2E 学生 A')).toHaveCount(0);
} finally {
await memberContext.close();
}
});
test('[E2E-S02] 拒绝邀请不生效;接受共享后可用共享错题本原题组卷', async ({ browser, page, scenario }) => {
await scenario.item('A', { questionText: `共享组卷题一。${scenario.marker}` });
await scenario.item('B', { questionText: `共享组卷题二。${scenario.marker}` });
// 所有者邀请学生 B
await scenario.login(page, false);
await page.goto(`/notebooks/${scenario.book.id}`);
await page.getByRole('button', { name: '共享成员', exact: true }).click();
const membersDialog = page.getByRole('dialog').filter({ has: page.getByRole('heading', { name: '共享成员' }) });
await expect(membersDialog).toBeVisible();
await membersDialog.locator('#share-email').fill(scenario.otherUser.email);
await membersDialog.locator('#share-email').press('Enter');
await expect(membersDialog.getByText('待接受')).toBeVisible();
const memberContext: BrowserContext = await browser.newContext();
const memberPage: Page = await memberContext.newPage();
await scenario.guardPage(memberPage);
try {
await scenario.login(memberPage, true);
await memberPage.goto('/notebooks');
await expect(memberPage.getByText('共享邀请')).toBeVisible();
// 先拒绝邀请
await memberPage.getByRole('button', { name: '拒绝', exact: true }).click();
await expect(memberPage.getByText('共享邀请')).toHaveCount(0);
await expect(memberPage.getByText('共享自 E2E 学生 A')).toHaveCount(0);
// 所有者重新邀请,成员接受
await page.reload();
await page.getByRole('button', { name: '共享成员', exact: true }).click();
await expect(membersDialog).toBeVisible();
await membersDialog.locator('#share-email').fill(scenario.otherUser.email);
await membersDialog.locator('#share-email').press('Enter');
await expect(membersDialog.getByText('待接受')).toBeVisible();
await memberPage.reload();
await memberPage.getByRole('button', { name: '接受', exact: true }).click();
await expect(memberPage.getByText('共享邀请')).toHaveCount(0);
// 成员在 /exams 使用共享错题本创建原题试卷(含 2 题的共享数学本)
await memberPage.goto('/exams');
await memberPage.getByRole('checkbox', { name: '数学 2' }).check();
await memberPage.getByRole('button', { name: '原题', exact: true }).click();
await memberPage.locator('#exam-count').fill('2');
await memberPage.getByRole('button', { name: '创建原题试卷', exact: true }).click();
await memberPage.waitForURL(/\/exams\/[^/]+$/);
const paperId = new URL(memberPage.url()).pathname.split('/').pop()!;
const questions = await scenario.db.examQuestion.findMany({ where: { paperId } });
expect(questions).toHaveLength(2);
} finally {
await memberContext.close();
}
});
});