import { expect, type Page } from "@playwright/test"; import { TEST_IDS } from "../testIds"; export class ReimbursementDetailPageObject { readonly page: Page; constructor(page: Page) { this.page = page; } get overview() { return this.page.getByTestId("reimbursement-desktop-workspace"); } get summary() { return this.overview.locator("section.workbench-hero"); } get itemsTable() { return this.page.getByTestId("reimbursement-expense-items"); } get addItemBtn() { return this.page.getByTestId(TEST_IDS.reimbursement.itemAdd); } get budgetHint() { return this.page.getByTestId(TEST_IDS.reimbursement.budgetHint); } get workflowProgress() { return this.page.getByTestId(TEST_IDS.reimbursement.workflowProgress); } get invoices() { return this.page.getByTestId("approval-invoices"); } get invoiceAddBtn() { return this.page.getByTestId(TEST_IDS.reimbursement.invoiceAdd); } get invoiceDialog() { return this.page.getByTestId(TEST_IDS.reimbursement.invoiceDialog); } get invoiceUploadFile() { return this.page.getByTestId(TEST_IDS.reimbursement.invoiceUploadFile); } get invoiceUploadSubmit() { return this.page.getByTestId(TEST_IDS.reimbursement.invoiceUploadSubmit); } get submitBtn() { return this.page.getByTestId(TEST_IDS.reimbursement.submit); } get withdrawBtn() { return this.page.getByTestId(TEST_IDS.reimbursement.withdraw); } get payBtn() { return this.page.getByTestId(TEST_IDS.reimbursement.pay); } async assertLoaded() { await expect(this.overview).toBeVisible({ timeout: 15_000 }); await expect(this.summary).toBeVisible({ timeout: 15_000 }); } async assertStatus(statusText: string) { await expect(this.summary).toContainText(statusText); } async uploadInvoicePdf(fileName: string, buffer: Buffer) { const invoiceCards = this.invoices.getByTestId("approval-invoice-evidence"); const invoiceCountBeforeUpload = await invoiceCards.count(); await this.invoiceAddBtn.click(); await expect(this.invoiceDialog).toBeVisible(); const uploadResponsePromise = this.page.waitForResponse((response) => { const url = new URL(response.url()); return response.request().method() === "POST" && url.pathname === "/api/invoice-pool/upload" && response.ok(); }); await this.invoiceUploadFile.setInputFiles({ name: fileName, mimeType: "application/pdf", buffer, }); await this.invoiceUploadSubmit.scrollIntoViewIfNeeded(); await this.invoiceUploadSubmit.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)}`); } // OCR selects the invoice; binding remains an explicit user action. const invoiceRow = this.invoiceDialog.locator("label").filter({ hasText: fileName }); await expect(invoiceRow.getByRole("checkbox")).toBeChecked({ timeout: 90_000 }); const target = invoiceRow.getByRole("combobox", { name: "添加到哪条明细" }); const existingItems = target.locator('option:not([value=""])'); await expect(existingItems).toHaveCount(1); await target.selectOption((await existingItems.getAttribute("value"))!); await this.invoiceDialog.getByRole("button", { name: "添加到报销单", exact: true }).click(); await expect(invoiceCards).toHaveCount(invoiceCountBeforeUpload + 1); await expect(this.itemsTable.getByTestId("approval-item-evidence")).toHaveCount(1); await expect(this.invoiceDialog).toHaveCount(0); return invoiceId; } async addItem(amount = "10.00") { await this.addItemBtn.click(); const expenseType = this.page.getByTestId("reimbursement-item-expense-type"); await expect(expenseType.locator("option")).not.toHaveCount(1); const value = await expenseType.locator("option").evaluateAll(options => (options as HTMLOptionElement[]).find(option => option.textContent === "邮寄快递")?.value); expect(value, "synthetic fixture requires the seeded courier expense type").toBeTruthy(); await expenseType.selectOption(value!); await this.page.getByTestId("reimbursement-item-amount").fill(amount); await this.page.getByTestId("reimbursement-item-description").fill("合成快递费用,低于票面金额部分为非报销支出"); await this.page.getByTestId("reimbursement-item-save").click(); await expect(this.page.getByTestId("reimbursement-item-editor")).toHaveCount(0); await expect(this.itemsTable.getByTestId("approval-item-evidence")).toHaveCount(1); } async submitReimbursement() { const precheck = this.page.waitForResponse(response => new URL(response.url()).pathname.endsWith("/precheck") && response.request().method() === "POST"); const submitted = this.page.waitForResponse(response => new URL(response.url()).pathname.endsWith("/submit") && response.request().method() === "POST"); await this.submitBtn.click(); const check = await precheck; expect(check.ok()).toBe(true); const data = (await check.json()).data; if (data?.decision === "warn" || data?.decision === "manual_review" || data?.standardChecks?.some((item: {exceeded: boolean; controlMode: string}) => item.exceeded && item.controlMode === "warn")) { await this.page.getByRole("dialog", { name: "确认提交报销单?" }).getByRole("button", { name: "确认提交", exact: true }).click(); } const response = await submitted; expect(response.ok(), `synthetic submit HTTP ${response.status()}`).toBe(true); return (await response.json()).data as {currentTaskId: string}; } }