Files
Agent-System-Frontend/e2e/rdm-duties.playwright.ts
T
weichen 6106db250c Complete project member duty and permission settings flows
Why: Raw identifiers and mixed settings scopes made project administration difficult and hid authorization boundaries.

Evidence: SELF receipts /tmp/project-settings-frontend-full-green-20261014-973d52c1.json and /tmp/project-settings-browser-fixture-final-20261014-973d52c1.json record 284 tests, typecheck, build and 15 real React browser fixture flows. Live composition and formal acceptance remain NOT_RUN.

Impact: Add searchable member and document selectors, role filters, versioned duty changes, removal impact review and shared permission details/matrix. Consume server capabilities and settings scopes, preserve personal preferences and default restoration, and discard late responses.

Refs: P3/P4/P5/P6/P7, docs/records/2026-10-14-rdm-settings-completion-contract.md
2026-10-14 02:29:06 +00:00

273 lines
18 KiB
TypeScript

// Real React/Picker interaction with deterministic HTTP responses; no live account or database.
import { test, expect, type Page } from '@playwright/test'
test.skip(!process.env.PLAYWRIGHT_BASE_URL, 'set PLAYWRIGHT_BASE_URL to an isolated frontend server')
function deferred() {
let resolve!: () => void
const promise = new Promise<void>(ok => { resolve = ok })
return { promise, resolve }
}
async function dutiesPage(page: Page, options: { canAssign?: boolean; missingVersion?: boolean } = {}) {
const writes: { command: string; params: Record<string, unknown> }[] = []
const calls: { command: string; params: Record<string, unknown> }[] = []
const holds = new Map<string, { started: ReturnType<typeof deferred>; release: ReturnType<typeof deferred> }>()
const active = new Set<string>()
const submissions: Record<string, unknown> = { 'doc-A': null, 'doc-B': null }
const canAssign = options.canAssign !== false
const commands = ['rdm.project.member.list', 'rdm.project.member.add', 'rdm.project.member.remove',
'rdm.project.member.set-role', 'rdm.project.member.impact', 'rdm.account.list', 'rdm.role.options',
'rdm.duty.catalog', 'rdm.duty.tree', 'rdm.duty.get', 'rdm.review.assign', 'rdm.review.assign.doc', 'rdm.review.assign.doc.batch',
'rdm.review.doc.submit', 'rdm.review.doc.approve', 'rdm.review.doc.reject', 'rdm.review.doc.final', 'rdm.review.doc.status']
const docs = [
{ docId: 'doc-A', title: '文档A', typeKey: 'design', typeLabel: '设计方案', sections: 1, dutyVersion: 3, assigned: true, author: 'alice', reviewers: ['carol'], approver: 'dave' },
{ docId: 'doc-B', title: '文档B', typeKey: 'design', typeLabel: '设计方案', sections: 1, dutyVersion: 7, assigned: true, author: 'bob', reviewers: ['carol'], approver: 'dave' },
]
const members = ['alice', 'bob', 'carol', 'dave'].map(owner => ({ owner, role: owner === 'alice' ? 'owner' : 'member', addedBy: '', addedAt: '', valid: true,
profRole: { roleId: owner === 'bob' ? 'systems' : 'software', name: owner === 'bob' ? '系统工程师' : '软件工程师' },
dutyEligibility: { author: { allowed: true }, reviewer: { allowed: true }, approver: { allowed: true } } }))
const filteredDocs = (params: Record<string, unknown>) => docs.filter(document => !params.query || (params.match === 'exact'
? document.docId === params.query || document.title === params.query : `${document.docId} ${document.title}`.includes(String(params.query))))
await page.route('**/api/**', async route => {
const request = route.request()
const path = new URL(request.url()).pathname
let body: unknown = []
if (path === '/api/health') body = { ok: true }
else if (path === '/api/me') body = { owner: 'alice', name: 'Alice', auth: true, isAdmin: false }
else if (path === '/api/projects') body = [{ id: 'host-1', name: '职责测试项目', createdAt: '', updatedAt: '',
settings: { 'rdm.projectId': 'domain-7', 'rdm.state': 'ready' } }]
else if (path === '/api/settings') body = { schema: [], defaults: {}, user: {}, project: {} }
else if (path === '/api/commands') body = commands.map(id => ({ id, enabled: true }))
else if (path.startsWith('/api/commands/')) {
const command = decodeURIComponent(path.substring('/api/commands/'.length))
const params = request.postDataJSON().params as Record<string, unknown>
expect(params.projectId).toBe('domain-7')
calls.push({ command, params })
const hold = holds.get(`${command}:${params.docId ?? ''}`)
if (hold) { hold.started.resolve(); await hold.release.promise }
let payload: unknown = {}
if (command === 'rdm.duty.catalog') payload = { docs: filteredDocs(params), next: '', count: filteredDocs(params).length, total: docs.length, canAssignDuties: canAssign,
filters: { stages: [], typeKeys: [{ value: 'design', label: '设计方案' }], assignments: [{ value: 'assigned', label: '已分配' }, { value: 'unassigned', label: '未分配' }], statuses: [] } }
else if (command === 'rdm.duty.tree') payload = { nodes: filteredDocs(params).map(document => ({ id: document.docId, kind: 'doc', label: document.title, selectable: true, docId: document.docId,
children: [{ id: document.docId + '-section', kind: 'section', label: '方案章节', selectable: false, children: [] }] })), next: '', canAssignDuties: canAssign }
else if (command === 'rdm.role.options') payload = { roles: [{ roleId: 'systems', name: '系统工程师' }, { roleId: 'software', name: '软件工程师' }] }
else if (command === 'rdm.project.member.list') payload = { members: members.filter(person => (!params.query || person.owner.includes(String(params.query))) && (!params.roleId || person.profRole.roleId === params.roleId)),
next: '', accountDirectoryAvailable: true, canManageMembers: true, canAssignDuties: canAssign, canConfigurePermissions: false }
else if (command === 'rdm.duty.get') {
const doc = docs.find(d => d.docId === params.docId)!
payload = { docId: doc.docId, title: doc.title, assigned: doc.assigned,
roles: { author: doc.author, reviewers: doc.reviewers, approver: doc.approver }, sections: [], canAssignDuties: canAssign,
...(options.missingVersion ? {} : { dutyVersion: doc.dutyVersion }),
active: active.has(doc.docId) ? { round: 1, state: 'submitted', createdBy: 'alice', duties: { Author: doc.author, Reviewers: doc.reviewers, Approver: doc.approver } } : null }
} else if (command === 'rdm.review.doc.status') payload = { submission: submissions[String(params.docId)] }
else {
writes.push({ command, params })
if (command === 'rdm.review.doc.submit') {
const document = docs.find(document => document.docId === params.docId)!
active.add(document.docId)
submissions[document.docId] = { round: 1, state: 'submitted', createdBy: 'alice',
duties: { Author: document.author, Reviewers: document.reviewers, Approver: document.approver }, reviews: {} }
}
if (command === 'rdm.review.assign.doc' || command === 'rdm.review.assign.doc.batch') {
const entries = command === 'rdm.review.assign.doc' ? [params] : params.docs as Record<string, unknown>[]
for (const entry of entries) {
const document = docs.find(document => document.docId === entry.docId)!
expect(entry.expectedDutyVersion).toBe(document.dutyVersion)
const changed = entry.author !== document.author || entry.approver !== document.approver
|| JSON.stringify(entry.reviewers) !== JSON.stringify(document.reviewers)
if (changed && active.has(document.docId)) expect(entry.confirmInvalidation || params.confirmInvalidation).toBe(true)
if (changed) {
document.author = String(entry.author); document.reviewers = entry.reviewers as string[]; document.approver = String(entry.approver); document.dutyVersion++
active.delete(document.docId)
}
}
}
}
body = { payload }
} else if (path.endsWith('/cards')) body = { cards: [] }
await route.fulfill({ contentType: 'application/json', body: JSON.stringify(body) })
})
await page.goto('/')
await page.getByRole('button', { name: '项目设置', exact: true }).click()
await page.getByRole('tab', { name: /文档职责/ }).click()
await expect(page.getByRole('button', { name: '文档A', exact: true })).toBeVisible()
return { writes, calls, active, submissions,
hold(command: string, docId: string) {
const pending = { started: deferred(), release: deferred() }
holds.set(`${command}:${docId}`, pending)
return pending
} }
}
async function chooseMember(page: Page, label: string, owner: string) {
await page.locator(`.picker[aria-label="${label}"]`).getByRole('combobox').fill(owner)
await page.getByRole('option', { name: new RegExp(`^${owner}`) }).click()
}
async function settleResponse(page: Page, command: string, release: () => void) {
const response = page.waitForResponse(r => r.url().endsWith(`/api/commands/${command}`))
release()
await response
await page.evaluate(() => new Promise<void>(done => requestAnimationFrame(() => requestAnimationFrame(() => done()))))
}
test('a newly assigned document can submit its first whole-document review', async ({ page }) => {
const fixture = await dutiesPage(page)
await page.getByRole('button', { name: '文档A', exact: true }).click()
await expect(page.getByText('尚无整文提交记录', { exact: true })).toBeVisible()
const submit = page.getByRole('button', { name: '整文提交(编制人)', exact: true })
await expect(submit).toBeEnabled()
await submit.click()
await expect(page.getByText('整文已提交审核', { exact: true })).toBeVisible()
await expect(submit).toBeDisabled()
await expect(page.getByRole('heading', { name: '整文审签 · 第 1 轮 · 审核中', exact: true })).toBeVisible()
expect(fixture.writes).toEqual([{ command: 'rdm.review.doc.submit', params: { projectId: 'domain-7', docId: 'doc-A' } }])
})
test('viewing active duties is passive; only a dirty save asks for confirmation', async ({ page }) => {
const fixture = await dutiesPage(page)
fixture.active.add('doc-A')
const dialogs: string[] = []
let accept = false
page.on('dialog', async dialog => { dialogs.push(dialog.message()); accept ? await dialog.accept() : await dialog.dismiss() })
await page.getByRole('button', { name: '文档A', exact: true }).click()
await expect(page.getByRole('heading', { name: /文档A · 当前职责/ })).toBeVisible()
const save = page.getByRole('button', { name: '保存当前职责', exact: true })
await expect(save).toBeDisabled()
expect(dialogs).toEqual([])
await chooseMember(page, '编制人', 'bob')
await expect(save).toBeEnabled()
expect(dialogs).toEqual([])
await save.click()
expect(dialogs).toHaveLength(1)
expect(fixture.writes).toEqual([])
accept = true
await save.click()
await expect(page.getByText('职责已保存,已重新读取当前状态', { exact: true })).toBeVisible()
expect(dialogs).toHaveLength(2)
expect(fixture.writes).toEqual([{ command: 'rdm.review.assign.doc', params: {
projectId: 'domain-7', docId: 'doc-A', author: 'bob', reviewers: ['carol'], approver: 'dave', expectedDutyVersion: 3, confirmInvalidation: true,
} }])
})
test('a late document detail cannot overwrite another selection or its saved roles', async ({ page }) => {
const fixture = await dutiesPage(page)
const old = fixture.hold('rdm.duty.get', 'doc-A')
await page.getByRole('button', { name: '文档A', exact: true }).click()
await old.started.promise
await page.getByRole('button', { name: '文档B', exact: true }).click()
await expect(page.getByRole('heading', { name: '文档B · 当前职责', exact: true })).toBeVisible()
await settleResponse(page, 'rdm.duty.get', old.release.resolve)
await expect(page.getByRole('heading', { name: '文档B · 当前职责', exact: true })).toBeVisible()
expect(fixture.calls.filter(c => c.command === 'rdm.review.doc.status' && c.params.docId === 'doc-A')).toEqual([])
await chooseMember(page, '批准人', 'alice')
await page.getByRole('button', { name: '保存当前职责', exact: true }).click()
await expect(page.getByText('职责已保存,已重新读取当前状态', { exact: true })).toBeVisible()
expect(fixture.writes).toEqual([{ command: 'rdm.review.assign.doc', params: {
projectId: 'domain-7', docId: 'doc-B', author: 'bob', reviewers: ['carol'], approver: 'alice', expectedDutyVersion: 7, confirmInvalidation: false,
} }])
})
test('document ID and title searches share list and tree selection without making chapters selectable', async ({ page }) => {
const fixture = await dutiesPage(page)
await page.getByLabel('文档匹配方式', { exact: true }).selectOption('exact')
await page.getByRole('textbox', { name: '搜索文档', exact: true }).fill('doc-B')
await expect(page.getByRole('button', { name: '文档A', exact: true })).toHaveCount(0)
await expect(page.getByRole('button', { name: '文档B', exact: true })).toBeVisible()
await page.getByLabel('文档浏览方式', { exact: true }).selectOption('tree')
await page.getByRole('button', { name: '文档B', exact: true }).click()
await expect(page.getByRole('heading', { name: '文档B · 当前职责', exact: true })).toBeVisible()
await page.getByRole('button', { name: '展开 文档B', exact: true }).click()
await expect(page.getByText('方案章节 · 章节', { exact: true })).toBeVisible()
await expect(page.getByRole('button', { name: '方案章节', exact: true })).toHaveCount(0)
expect(fixture.writes).toEqual([])
expect(fixture.calls.some(call => call.command === 'rdm.duty.tree' && call.params.query === 'doc-B' && call.params.match === 'exact')).toBe(true)
})
test('professional role filtering keeps duty candidates within project membership and selected labels stay visible', async ({ page }) => {
const fixture = await dutiesPage(page)
await page.getByRole('button', { name: '文档A', exact: true }).click()
await page.getByLabel('专业角色筛选', { exact: true }).selectOption('systems')
await page.locator('.picker[aria-label="编制人"]').getByRole('combobox').focus()
await expect(page.getByRole('option', { name: /^bob/ })).toBeVisible()
await expect(page.getByRole('option', { name: /^carol/ })).toHaveCount(0)
await page.getByRole('option', { name: /^bob/ }).click()
await expect(page.locator('.picker[aria-label="编制人"] .picker-selected')).toContainText('bob')
expect(fixture.calls.some(call => call.command === 'rdm.project.member.list' && call.params.roleId === 'systems')).toBe(true)
expect(fixture.calls.some(call => call.command === 'rdm.account.list')).toBe(false)
})
test('missing version or caller assignment capability keeps duty editors read only', async ({ page }) => {
const fixture = await dutiesPage(page, { canAssign: false, missingVersion: true })
await page.getByRole('button', { name: '文档A', exact: true }).click()
await expect(page.getByRole('button', { name: '保存当前职责', exact: true })).toBeDisabled()
await expect(page.locator('.picker[aria-label="编制人"]').getByRole('combobox')).toBeDisabled()
await expect(page.getByText('职责版本尚未就绪,只能查看;请升级项目服务后重新读取', { exact: true })).toBeVisible()
expect(fixture.writes).toEqual([])
})
test('batch preview carries each document version and explicitly confirms active review invalidation', async ({ page }) => {
const fixture = await dutiesPage(page)
fixture.active.add('doc-A')
const prompts: string[] = []
page.on('dialog', async dialog => { prompts.push(dialog.message()); await dialog.accept() })
await page.getByRole('checkbox', { name: '选择 文档A', exact: true }).check()
await page.getByRole('checkbox', { name: '选择 文档B', exact: true }).check()
await chooseMember(page, '批量编制人', 'bob')
await chooseMember(page, '批量审核人', 'carol')
await chooseMember(page, '批量批准人', 'dave')
await page.getByRole('button', { name: '预览并分配 2 份文档', exact: true }).click()
await expect(page.getByText('批量职责已保存,已重新读取目录', { exact: true })).toBeVisible()
expect(prompts).toHaveLength(1)
expect(prompts[0]).toContain('第 1 轮将失效')
expect(fixture.writes).toEqual([{ command: 'rdm.review.assign.doc.batch', params: { projectId: 'domain-7', confirmInvalidation: true,
docs: [
{ docId: 'doc-A', author: 'bob', reviewers: ['carol'], approver: 'dave', expectedDutyVersion: 3, confirmInvalidation: true },
{ docId: 'doc-B', author: 'bob', reviewers: ['carol'], approver: 'dave', expectedDutyVersion: 7, confirmInvalidation: true },
] } }])
})
test('a batch keeping existing duties does not claim its active review will be invalidated', async ({ page }) => {
const fixture = await dutiesPage(page)
fixture.active.add('doc-A')
const prompts: string[] = []
page.on('dialog', async dialog => { prompts.push(dialog.message()); await dialog.accept() })
await page.getByRole('checkbox', { name: '选择 文档A', exact: true }).check()
await chooseMember(page, '批量编制人', 'alice')
await chooseMember(page, '批量审核人', 'carol')
await chooseMember(page, '批量批准人', 'dave')
await page.getByRole('button', { name: '预览并分配 1 份文档', exact: true }).click()
await expect(page.getByText('批量职责已保存,已重新读取目录', { exact: true })).toBeVisible()
expect(prompts[0]).not.toContain('失效')
expect(fixture.writes[0].params.confirmInvalidation).toBe(false)
expect(fixture.active.has('doc-A')).toBe(true)
})
test('a late review status cannot replace the selected document frozen review', async ({ page }) => {
const fixture = await dutiesPage(page)
const submission = (round: number, author: string) => ({ round, state: 'submitted', digest: '', createdBy: author,
createdAt: '', duties: { author, reviewers: ['carol'], approver: 'dave' }, reviews: {}, sections: {} })
fixture.submissions['doc-A'] = submission(1, 'alice')
fixture.submissions['doc-B'] = submission(2, 'bob')
const old = fixture.hold('rdm.review.doc.status', 'doc-A')
await page.getByRole('button', { name: '文档A', exact: true }).click()
await old.started.promise
await page.getByRole('button', { name: '文档B', exact: true }).click()
await expect(page.getByRole('heading', { name: /整文审签 · 第 2 轮/ })).toBeVisible()
await settleResponse(page, 'rdm.review.doc.status', old.release.resolve)
await expect(page.getByRole('heading', { name: /整文审签 · 第 2 轮/ })).toBeVisible()
await expect(page.getByRole('heading', { name: /整文审签 · 第 1 轮/ })).toHaveCount(0)
})
test('closing the project panel invalidates a pending detail before its follow-up status read', async ({ page }) => {
const fixture = await dutiesPage(page)
const old = fixture.hold('rdm.duty.get', 'doc-A')
await page.getByRole('button', { name: '文档A', exact: true }).click()
await old.started.promise
await page.getByRole('dialog').getByRole('button', { name: '关闭', exact: true }).click()
await expect(page.getByRole('tab', { name: /文档职责/ })).toHaveCount(0)
await settleResponse(page, 'rdm.duty.get', old.release.resolve)
expect(fixture.calls.filter(c => c.command === 'rdm.review.doc.status' && c.params.docId === 'doc-A')).toEqual([])
})