import { expect, test, type APIRequestContext } from "@playwright/test"; import { apiGetData, apiPostData, createAuthenticatedApiClient, login } from "./helpers/auth"; const ADMIN = { identifier: "admin@hyinda.com", password: "AdminPassword1234", name: "王顺利", }; type FeatureFlagItem = { flagKey: string; overrideMode: "inherit" | "force_on" | "force_off"; rolloutType: "global" | "department_allowlist"; departmentIds: string[]; note: string; version: number; effectiveEnabled: boolean; }; async function readApiData(response: Awaited>) { const payload = (await response.json()) as { data?: T; error?: { code?: string; message?: string } }; if (!response.ok()) { throw new Error(`${payload.error?.code ?? response.status()}: ${payload.error?.message ?? "request_failed"}`); } return payload.data as T; } async function ensureFeatureFlags(api: APIRequestContext, keys: string[]) { const list = await apiGetData<{ items: FeatureFlagItem[] }>(api, "/api/system/feature-flags"); for (const key of keys) { const item = list.items.find((flag) => flag.flagKey === key); if (!item) { throw new Error(`Missing feature flag: ${key}`); } if (item.overrideMode === "force_on" && item.effectiveEnabled) { continue; } await readApiData<{ item: FeatureFlagItem }>( await api.put(`/api/system/feature-flags/${key}`, { data: { overrideMode: "force_on", rolloutType: "global", departmentIds: [], note: "playwright e2e", expectedVersion: item.version, }, }), ); } } test("E2E 涉密边界:敏感摘要不可搜索,编号可命中", async ({ page }) => { const adminClient = await createAuthenticatedApiClient(ADMIN); const suffix = Date.now(); const sensitivePhrase = `极密摘要${suffix}`; const contractNo = `HT-${suffix}`; const docNo = `DOC-${suffix}`; await ensureFeatureFlags(adminClient.api, [ "global_search_v1", "project_domain_v1", "project_linking_v1", "confidential_doc_refs_v1", "public_security_hardening_v1", ]); const project = await apiPostData<{ item: { id: string; name: string } }>(adminClient.api, "/api/projects", { name: `涉密项目 ${suffix}`, clientName: `涉密客户 ${suffix}`, summary: "用于 R23 涉密边界 E2E", isConfidential: true, }); const contract = await apiPostData<{ item: { id: string } }>(adminClient.api, "/api/contracts-registry", { contractNo, projectId: project.item.id, contractType: "service", counterpartyName: `客户 ${suffix}`, summary: sensitivePhrase, confidentialityLevel: "restricted", archiveLocation: `NAS/Contracts/${suffix}`, offlineArchiveNo: `ARC-${suffix}`, }); await apiPostData<{ item: { id: string } }>(adminClient.api, "/api/confidential-doc-refs", { projectId: project.item.id, contractRegistryId: contract.item.id, title: `资料引用 ${suffix}`, documentNo: docNo, confidentialityLevel: "confidential", storageType: "offline_archive", storagePath: `OFFLINE/${suffix}`, offlineArchiveNo: `DOC-ARC-${suffix}`, }); await login(page, ADMIN); await page.goto(`/projects/${project.item.id}`); await expect(page.getByText(sensitivePhrase)).toHaveCount(0); await page.getByTestId("global-search-trigger").click(); const searchInput = page.getByPlaceholder("搜索人员、项目、合同、公告、资产、请假、报销、周报、待办"); await searchInput.fill(sensitivePhrase); await expect(page.getByRole("button", { name: new RegExp(contractNo) })).toHaveCount(0); await expect(page.getByRole("button", { name: new RegExp(docNo) })).toHaveCount(0); await searchInput.fill(contractNo); await expect(page.getByRole("button", { name: new RegExp(contractNo) })).toBeVisible(); await searchInput.fill(docNo); await expect(page.getByRole("button", { name: new RegExp(docNo) })).toBeVisible(); await adminClient.dispose(); });