Files
co-lite/solvway-admin-portal/app/lib/xlsx.test.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

160 lines
6.7 KiB
TypeScript

import { test, expect } from "vitest";
import ExcelJS from "exceljs";
import { toXlsx } from "./xlsx";
async function readBack(bytes: Uint8Array) {
const wb = new ExcelJS.Workbook();
await wb.xlsx.load(bytes.slice().buffer);
return wb.worksheets[0];
}
const COLUMNS = [
{ key: "name", title: "Name" },
{ key: "amount", title: "Amount" },
];
test("writes a named sheet with the header row then one row per record", async () => {
const ws = await readBack(
await toXlsx(
[
{ name: "Starter Pack", amount: 250 },
{ name: "Elite Pack", amount: 5200 },
],
COLUMNS,
"Sales",
),
);
expect(ws.name).toBe("Sales");
expect(ws.getSheetValues().slice(1).map((r) => (r as unknown[]).slice(1))).toEqual([
["Name", "Amount"],
["Starter Pack", 250],
["Elite Pack", 5200],
]);
});
test("the header row is bold and frozen", async () => {
const ws = await readBack(await toXlsx([{ name: "a", amount: 1 }], COLUMNS, "S"));
expect(ws.getRow(1).getCell(1).font?.bold).toBe(true);
expect(ws.getRow(1).getCell(2).font?.bold).toBe(true);
expect(ws.getRow(2).getCell(1).font?.bold).toBeFalsy();
expect(ws.views[0]).toMatchObject({ state: "frozen", ySplit: 1 });
});
test("each column is as wide as its longest value", async () => {
const ws = await readBack(
await toXlsx([{ name: "A much longer product name", amount: 5 }], COLUMNS, "S"),
);
const nameWidth = ws.getColumn(1).width ?? 0;
const amountWidth = ws.getColumn(2).width ?? 0;
expect(nameWidth).toBeGreaterThanOrEqual("A much longer product name".length);
expect(amountWidth).toBeGreaterThanOrEqual("Amount".length);
expect(amountWidth).toBeLessThan(nameWidth);
});
// exceljs drops a width equal to its default (9) when writing, so a 7-character column fell back to
// Excel's narrower default. Every column must keep an explicit width.
test("a column whose longest value is 7 characters keeps its width", async () => {
const ws = await readBack(
await toXlsx([{ channel: "Web", amount: "1234.00" }], [
{ key: "channel", title: "Channel" },
{ key: "amount", title: "Amount" },
], "S"),
);
expect(ws.getColumn(1).width).toBeGreaterThanOrEqual("Channel".length + 2);
expect(ws.getColumn(2).width).toBeGreaterThanOrEqual("1234.00".length + 2);
});
// Member names, emails and addresses are member-typed. A cell must never be written as a formula,
// so "=HYPERLINK(...)" stays the literal text the member typed.
test("a value starting with = + - @ is written as text, never as a formula", async () => {
const values = ['=HYPERLINK("http://x","y")', "+60 987654321", "-2+3", "@SUM(A1)", "-"];
const ws = await readBack(
await toXlsx(values.map((v) => ({ name: v })), [{ key: "name", title: "V" }], "S"),
);
for (let i = 0; i < values.length; i++) {
const cell = ws.getRow(i + 2).getCell(1);
expect(cell.formula).toBeUndefined();
expect(cell.value).toBe(values[i]);
}
});
// Money arrives pre-formatted ("-78.00"). It must be a number so the sheet can sum it, and keep its 2dp.
test("plain decimal strings become numbers that keep their decimal places", async () => {
const ws = await readBack(
await toXlsx(
[{ name: "-78.00", amount: "1250.50" }, { name: "0123", amount: "900101145678" }],
COLUMNS,
"S",
),
);
expect(ws.getRow(2).getCell(1).value).toBe(-78);
expect(ws.getRow(2).getCell(1).numFmt).toBe("0.00");
expect(ws.getRow(2).getCell(2).value).toBe(1250.5);
expect(ws.getRow(2).getCell(2).numFmt).toBe("0.00");
// Digit-only strings are identifiers (codes, IC numbers), not numbers: they stay text.
expect(ws.getRow(3).getCell(1).value).toBe("0123");
expect(ws.getRow(3).getCell(2).value).toBe("900101145678");
});
test("a decimal with more digits than a double holds stays text", async () => {
const ws = await readBack(await toXlsx([{ name: "1234567890123.123456" }], COLUMNS, "S"));
expect(ws.getRow(2).getCell(1).value).toBe("1234567890123.123456");
});
test("missing values are empty cells", async () => {
const ws = await readBack(await toXlsx([{ name: null }], COLUMNS, "S"));
expect(ws.getRow(2).getCell(1).value).toBeNull();
expect(ws.getRow(2).getCell(2).value).toBeNull();
});
// manual-do-issue §6.5: the tracking template's courier column is a dropdown of active courier codes.
// The values live on a hidden "Lists" sheet so the list is not capped by Excel's 255-character inline limit.
test("listValidations puts a dropdown on the named column, sourced from a hidden Lists sheet", async () => {
const bytes = await toXlsx(
[{ name: "a", amount: 1 }],
[{ key: "name", title: "Name" }, { key: "courierCode", title: "Courier" }],
"Tracking",
{ listValidations: [{ column: "courierCode", values: ["JNT", "POSLAJU", "NINJAVAN"] }] },
);
const wb = new ExcelJS.Workbook();
await wb.xlsx.load(bytes.slice().buffer);
expect(wb.worksheets[0].name).toBe("Tracking");
const lists = wb.getWorksheet("Lists")!;
expect(lists.state).toBe("hidden");
expect([1, 2, 3].map(r => lists.getCell(r, 1).value)).toEqual(["JNT", "POSLAJU", "NINJAVAN"]);
const dv = wb.worksheets[0].getCell("B2").dataValidation;
expect(dv).toMatchObject({ type: "list", formulae: ["Lists!$A$1:$A$3"] });
expect(wb.worksheets[0].getCell("B1000").dataValidation).toMatchObject({ type: "list" });
expect(wb.worksheets[0].getCell("A2").dataValidation).toBeFalsy();
});
test("without listValidations there is no Lists sheet", async () => {
const wb = new ExcelJS.Workbook();
await wb.xlsx.load((await toXlsx([{ name: "a", amount: 1 }], COLUMNS, "S")).slice().buffer);
expect(wb.getWorksheet("Lists")).toBeUndefined();
});
// localise-admin-people: sheet names now come from admin-editable copy, which Excel restricts.
test("a sheet name with characters Excel forbids, or over 31 characters, still exports", async () => {
const ws = await readBack(await toXlsx([{ name: "a", amount: 1 }], COLUMNS, "Sales: by item [all] / a*b?c\\ and more text"));
expect(ws.name).toBe("Sales- by item -all- - a-b-c- a");
});
// Export Sheet Names Are Always Valid (listing-exports): Excel reserves "History"; exceljs throws on it.
test.each([
["History", "History 1"],
["HISTORY", "HISTORY 1"],
["history", "history 1"],
["'quoted'", "quoted"],
["", "Sheet1"],
])("sheet name %j becomes %j", async (given, expected) => {
const ws = await readBack(await toXlsx([{ name: "a", amount: 1 }], COLUMNS, given));
expect(ws.name).toBe(expected);
});
test("a name of only illegal characters still exports with a non-empty name", async () => {
const ws = await readBack(await toXlsx([{ name: "a", amount: 1 }], COLUMNS, "***"));
expect(ws.name.length).toBeGreaterThan(0);
expect(ws.name.startsWith("'") || ws.name.endsWith("'")).toBe(false);
});