55 行
2.4 KiB
TypeScript
55 行
2.4 KiB
TypeScript
import { existsSync } from "node:fs";
|
|
import path from "node:path";
|
|
|
|
import { test, expect } from "../helpers/fixtures";
|
|
import { EMPLOYEE, login } from "../helpers/auth";
|
|
import { openInvoicePool, resolveLiveOcrUser, uploadLiveInvoiceAndWait } from "../helpers/liveInvoiceOcr";
|
|
|
|
test("手工 live 腾讯 OCR 辅助复核:真实 VAT 发票上传后进入可处理状态", async ({ page }, testInfo) => {
|
|
test.skip(process.env.PLAYWRIGHT_MANUAL !== "1", "仅在显式手工 Playwright 模式下执行");
|
|
test.skip(process.env.PLAYWRIGHT_OCR_LIVE !== "1", "仅在显式 live OCR 辅助复核模式下执行");
|
|
|
|
const invoiceFilePath = process.env.PLAYWRIGHT_OCR_LIVE_FILE?.trim() ?? "";
|
|
test.skip(!invoiceFilePath || !existsSync(invoiceFilePath) || path.extname(invoiceFilePath).toLowerCase() !== ".pdf", "需要提供存在的 PDF 格式 PLAYWRIGHT_OCR_LIVE_FILE");
|
|
|
|
const liveOcrUser = resolveLiveOcrUser(EMPLOYEE);
|
|
|
|
await login(page, liveOcrUser);
|
|
await openInvoicePool(page);
|
|
|
|
const result = await uploadLiveInvoiceAndWait(page, invoiceFilePath);
|
|
const evidence = {
|
|
status: result.status,
|
|
durationMs: result.durationMs,
|
|
invoiceId: result.invoiceId,
|
|
invoiceCode: result.invoiceCode,
|
|
invoiceNumber: result.invoiceNumber,
|
|
sellerName: result.sellerName,
|
|
totalAmount: result.totalAmount,
|
|
invoiceDate: result.invoiceDate,
|
|
bannerText: result.bannerText,
|
|
};
|
|
console.log(`[live-ocr] ${JSON.stringify(evidence)}`);
|
|
await testInfo.attach("live-ocr-result.json", {
|
|
body: JSON.stringify(evidence, null, 2),
|
|
contentType: "application/json",
|
|
});
|
|
|
|
if (result.status === "ocr_failed") {
|
|
const allowDuplicate = process.env.PLAYWRIGHT_OCR_ALLOW_DUPLICATE === "1";
|
|
const resultText = `${result.bannerText}\n${result.detailText}`;
|
|
const explainableTerminal = /重复|相同发票|duplicate|已用于报销|已使用|used/i.test(resultText);
|
|
if (!allowDuplicate || !explainableTerminal) {
|
|
throw new Error(`live 腾讯 OCR 失败:${result.bannerText || result.detailText}`);
|
|
}
|
|
await page.context().clearCookies().catch(() => undefined);
|
|
return;
|
|
}
|
|
|
|
expect(result.invoiceCode ?? result.invoiceNumber, JSON.stringify(result)).toBeTruthy();
|
|
expect(result.totalAmount, JSON.stringify(result)).toBeTruthy();
|
|
expect(result.totalAmount, JSON.stringify(result)).not.toBe("-");
|
|
|
|
await page.context().clearCookies().catch(() => undefined);
|
|
});
|