import { test, expect } from "./helpers/fixtures"; import { EMPLOYEE, FINANCE, MANAGER, login, logout } from "./helpers/auth"; import { openNavLink } from "./helpers/navigation"; import { TEST_IDS } from "./helpers/testIds"; test("E2E 主链路:登录→报销→待办审批→票据OCR→支付→登出", async ({ page, reimbursementDetail, workflowDetail }) => { // 主链路包含 OCR 等待和三角色切换,full-local 下会超过默认 120s。 test.setTimeout(240_000); // 1) employee: create reimbursement await login(page, EMPLOYEE); await (await openNavLink(page, "我的报销")).click(); await expect(page).toHaveURL(/\/reimbursements/); await expect(page.getByTestId("reimbursements-create")).toBeVisible(); const createResponsePromise = page.waitForResponse((response) => { const url = new URL(response.url()); return response.request().method() === "POST" && url.pathname === "/api/reimbursements" && response.ok(); }); await page.getByTestId("reimbursements-create").click(); await page.getByRole("menuitem", { name: "空白报销" }).click(); const createPayload = (await (await createResponsePromise).json()) as { data?: { id?: string } }; const reimbursementId = createPayload.data?.id; if (!reimbursementId) { throw new Error(`创建报销单后未返回 id: ${JSON.stringify(createPayload)}`); } await page.goto(`/reimbursements/${reimbursementId}`); await reimbursementDetail.assertLoaded(); // 2) employee: upload invoice and wait OCR await (await openNavLink(page, "票据中心")).click(); const marker = `MOCK_E2E_${Date.now()}_${Math.random().toString(16).slice(2)}`; const invoiceFileName = `e2e-${Date.now()}.pdf`; const uploadResponsePromise = page.waitForResponse((response) => { const url = new URL(response.url()); return response.request().method() === "POST" && url.pathname === "/api/invoice-pool/upload" && response.ok(); }); await page.getByTestId("invoice-upload-file").setInputFiles({ name: invoiceFileName, mimeType: "application/pdf", buffer: Buffer.from(`%PDF-1.4\n${marker}\n%%EOF\n`, "utf8"), }); await page.getByTestId("invoice-upload-submit").click(); const uploadPayload = (await (await uploadResponsePromise).json()) as { data?: { invoicePoolId?: string | null } }; const invoiceId = uploadPayload.data?.invoicePoolId; if (!invoiceId) { throw new Error(`上传发票后未返回票据 ID: ${JSON.stringify(uploadPayload)}`); } await expect(page.getByTestId("invoice-detail-panel")).toBeVisible(); await expect(page.getByTestId("invoice-detail-panel")).toContainText("状态:可用", { timeout: 90_000 }); // 预览(popup 存在即可) const popupPromise = page.waitForEvent("popup"); const invoiceRow = page.getByTestId("invoice-row").filter({ hasText: invoiceFileName }).first(); await expect(invoiceRow).toBeVisible(); await invoiceRow.getByRole("button").click(); await page.getByTestId("invoice-preview-button").click(); const popup = await popupPromise; await expect(popup).toHaveURL(new RegExp(`/api/invoice-pool/${invoiceId}`)); await popup.close(); // 3) bind invoice to reimbursement await page.getByTestId("invoice-bind-invoice").selectOption(invoiceId); await page.getByTestId("invoice-bind-reimbursement").selectOption(reimbursementId); await page.getByTestId("invoice-bind-submit").click(); // 4) submit reimbursement await page.goto(`/reimbursements/${reimbursementId}`); await reimbursementDetail.assertLoaded(); await reimbursementDetail.submitReimbursement(); await expect(page.getByText(/状态:(reviewing|审批中)/)).toBeVisible(); // 5) manager approve await logout(page, EMPLOYEE.name); await login(page, MANAGER); await (await openNavLink(page, "待我处理")).click(); await expect(page.getByTestId(TEST_IDS.workflow.tasksTable)).toBeVisible(); const managerTaskRow = page.getByTestId(TEST_IDS.workflow.taskRow).filter({ hasText: reimbursementId }).first(); await expect(managerTaskRow).toBeVisible(); await managerTaskRow.getByTestId(TEST_IDS.workflow.detailLink).click(); await workflowDetail.assertLoaded(); await workflowDetail.approve(); await expect(workflowDetail.summary).toContainText(/状态:(completed|已完成)/); await logout(page, MANAGER.name); // 6) finance approve + pay await login(page, FINANCE); await (await openNavLink(page, "待我处理")).click(); const financeTaskRow = page.getByTestId(TEST_IDS.workflow.taskRow).filter({ hasText: reimbursementId }).first(); await expect(financeTaskRow).toBeVisible(); await financeTaskRow.getByTestId(TEST_IDS.workflow.detailLink).click(); await workflowDetail.assertLoaded(); await workflowDetail.approve(); await expect(workflowDetail.summary).toContainText(/状态:(completed|已完成)/); await page.goto(`/reimbursements/${reimbursementId}`); await reimbursementDetail.assertLoaded(); await expect(page.getByText(/状态:(approved|已批准)/)).toBeVisible({ timeout: 30_000 }); await page.getByTestId("reimbursement-pay-voucher-no").fill(`E2E-${Date.now()}`); await reimbursementDetail.payBtn.click(); await expect(page.getByText(/状态:(paid|已支付)/)).toBeVisible({ timeout: 30_000 }); await logout(page, FINANCE.name); // 7) guard: unauth -> login await page.goto("/dashboard"); await expect(page).toHaveURL(/\/login/); });