import { describe, expect, it } from "vitest"; import fs from "fs"; import path from "path"; import ExcelJS from "exceljs"; import { findPackingHeaderRowIndex, isMergedSlaveCell, isPackingFooterNoiseRow, isPackingTitleNoiseRow, readWorksheetGrid, } from "@/services/parse/excel-sheet-grid"; import { parsePackingListBuffer } from "@/services/parse/packing-list"; describe("excel-sheet-grid", () => { it("finds Chinese header row on merged 数据模版", async () => { const file = path.join( process.cwd(), "docx", "邮件", "模板", "数据模版.xlsx", ); const wb = new ExcelJS.Workbook(); await wb.xlsx.readFile(file); const grid = readWorksheetGrid(wb.worksheets[0]); expect(isPackingTitleNoiseRow(grid[0])).toBe(true); expect(findPackingHeaderRowIndex(grid)).toBe(2); expect(grid[2].join("|")).toMatch(/件数.*渠道.*仓库/); }); it("treats merged slaves as empty", async () => { const file = path.join( process.cwd(), "docx", "邮件", "模板", "数据模版.xlsx", ); const wb = new ExcelJS.Workbook(); await wb.xlsx.readFile(file); const ws = wb.worksheets[0]; const master = ws.getCell("B4"); const slave = ws.getCell("B5"); expect(isMergedSlaveCell(master)).toBe(false); expect(isMergedSlaveCell(slave)).toBe(true); const grid = readWorksheetGrid(ws); expect(grid[3][1]).toBe("可不填"); expect(grid[4][1]).toBe(""); }); it("detects footer 注意 merged note row", () => { const cells = [ "注意:1.ups和fedex的件请直接在渠道栏对应写上ups和fedex", "注意:1.ups和fedex的件请直接在渠道栏对应写上ups和fedex", ]; expect( isPackingFooterNoiseRow({ F_Transporter: cells[0] }, cells), ).toBe(true); }); }); describe("数据模版.xlsx merged template", () => { it("parses structured region and skips footer note", async () => { const file = path.join( process.cwd(), "docx", "邮件", "模板", "数据模版.xlsx", ); const buf = fs.readFileSync(file); const parsed = await parsePackingListBuffer(buf, { enableIsoCheck: false }); expect(parsed.errorCode).toBeUndefined(); expect(parsed.lineage.header_row).toBe(3); expect(parsed.shipments.length).toBe(33); expect( parsed.shipments.every( (s) => !String(s.F_Address || "").startsWith("注意"), ), ).toBe(true); expect( parsed.shipments.every( (s) => !String(s.F_Transporter || "").startsWith("注意"), ), ).toBe(true); const las1 = parsed.shipments.find((s) => s.F_FBAID === "FBA16SQQ7PTF"); expect(las1?.F_Contact).toBe("LAS1"); expect(las1?.F_Address).toBe("12300 Bermuda Road"); }); });