import { expect, test, type Page } from '@playwright/test' import { document as sample } from '../src/documentFixture' import type { ScopeRule, ScopeRuleDecision } from '../src/scopeRules' const cases = [ { id: 'parent', directory_path: '产品', title: '总览.pdf' }, { id: 'child', directory_path: '产品/手册', title: '手册.pdf' }, { id: 'sibling', directory_path: '产品/固件', title: '固件.pdf' }, ] const existing: ScopeRule = { target_type: 'directory', connection_id: 'pp-1', directory_path: '产品', visibility: 'external', reason: '父目录已公开', updated_by: 'QA工程师', updated_at: sample.updated_at, } async function scopeFixture(page: Page, canWrite = true) { const rules = new Map([['产品', existing]]) const changes: ScopeRuleDecision[] = [] await page.route('**/*', async (route) => { const request = route.request() const url = new URL(request.url()) if (url.hostname !== '127.0.0.1' || url.port !== '4175') return route.abort() if (url.pathname === '/api/v1/scope-rules') { if (request.method() === 'GET' && url.searchParams.get('connection_id') === 'pp-1') { return route.fulfill({ json: { data: [...rules.values()] } }) } if (request.method() === 'PUT') { const input = request.postDataJSON() as ScopeRuleDecision changes.push(input) if (!canWrite) return route.fulfill({ status: 403, json: { error: 'forbidden' } }) if ( input.target_type !== 'directory' || input.connection_id !== 'pp-1' || !cases.some((item) => item.directory_path === input.directory_path) || !request.headers()['idempotency-key'] || !input.reason.trim() ) { return route.fulfill({ status: 400, json: { error: 'invalid fixture input' } }) } const rule = { ...existing, ...input } rules.set(input.directory_path, rule) return route.fulfill({ json: { data: rule } }) } } if (url.pathname === '/api/v1/source-items' && request.method() === 'GET') { const items = cases.map((item) => { const rule = rules.get(item.directory_path) ?? rules.get('产品')! return { ...sample, ...item, visibility: rule.visibility, visibility_origin: 'directory_rule', scope_rule_path: rule.directory_path, } }) return route.fulfill({ json: { data: items, summary: { total: items.length }, offset: 0, limit: 100 } }) } if (url.pathname.startsWith('/api/')) return route.fulfill({ status: 404, json: { error: 'unexpected fixture request' } }) return route.continue() }) await page.goto('/e2e/scope-rules.html') return changes } function openDirectory(page: Page, name: string) { return page .getByRole('complementary', { name: '来源目录' }) .getByRole('button', { name: new RegExp(`^▸ ${name} \\d+$`) }) .click() } async function saveInternal(page: Page) { const editor = page.getByRole('region', { name: '对内对外范围设置' }) await editor.getByLabel('拟修改为').selectOption('internal') await expect(editor.getByRole('button', { name: '保存范围' })).toBeDisabled() await editor.getByLabel('判断原因').fill(' 仅供内部使用 ') await editor.getByRole('button', { name: '保存范围' }).click() } test('编辑器保存目录范围并在刷新后读回', async ({ page }) => { const changes = await scopeFixture(page) await openDirectory(page, '产品/手册') await saveInternal(page) await expect(page.getByText('当前设置:对内')).toBeVisible() expect(changes).toEqual([ { target_type: 'directory', connection_id: 'pp-1', directory_path: '产品/手册', source_item_id: '', visibility: 'internal', reason: '仅供内部使用', }, ]) await page.reload() await openDirectory(page, '产品/手册') await expect(page.getByText('当前设置:对内')).toBeVisible() await expect(page.getByRole('row', { name: /手册.pdf/ })).toContainText('继承目录设置:产品/手册') }) test('编辑器显示只读拒绝并保留原规则', async ({ page }) => { const changes = await scopeFixture(page, false) await openDirectory(page, '产品/手册') await saveInternal(page) await expect(page.getByRole('alert')).toHaveText('保存范围规则失败(HTTP 403)') expect(changes).toHaveLength(1) await page.reload() await openDirectory(page, '产品/手册') await expect(page.getByText('当前没有本级设置')).toBeVisible() await expect(page.getByRole('row', { name: /手册.pdf/ })).toContainText('继承目录设置:产品') }) test('编辑器刷新并显示子目录规则和兄弟目录的继承来源', async ({ page }) => { await scopeFixture(page) await openDirectory(page, '产品/手册') await saveInternal(page) await expect(page.getByRole('row', { name: /手册.pdf/ })).toContainText('继承目录设置:产品/手册') await openDirectory(page, '产品/固件') const sibling = page.getByRole('row', { name: /固件.pdf/ }) await expect(sibling).toContainText('对外') await expect(sibling).toContainText('继承目录设置:产品') })