49 lines
2.4 KiB
TypeScript
49 lines
2.4 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import ExcelJS from "exceljs";
|
|
import { readSpreadsheet } from "@/infra/spreadsheet/parse";
|
|
import { createModel, addSource, createJob, saveMappings, startProcess } from "@/server/spreadsheet-adapter/service";
|
|
import { ctxOf, createReadyDb, depsOf, IDS } from "../helpers/world";
|
|
|
|
describe("fixtures XLSX", () => {
|
|
it("lê abas, fórmula como texto, data, coluna vazia e caracteres especiais sem executar fórmula", async () => {
|
|
const book = new ExcelJS.Workbook();
|
|
const first = book.addWorksheet("Dados");
|
|
first.addRow(["Nome", "Valor", ""]);
|
|
first.getCell("A2").value = "José & Ação";
|
|
first.getCell("B2").value = new Date("2026-01-15T00:00:00.000Z");
|
|
first.mergeCells("A3:B3");
|
|
first.getCell("A3").value = "=1+1";
|
|
book.addWorksheet("Outra").addRow(["ignorar"]);
|
|
const bytes = Buffer.from(await book.xlsx.writeBuffer());
|
|
const parsed = await readSpreadsheet("livro.xlsx", bytes, { sheet: "Dados", headerRow: 1 });
|
|
expect(parsed.grid.sheetNames).toContain("Outra");
|
|
expect(parsed.grid.headers).toContain("Nome");
|
|
const flat = parsed.grid.rows.flat().join("|");
|
|
expect(flat).toContain("José & Ação");
|
|
expect(flat).not.toMatch(/^2$/);
|
|
expect(flat).toContain("=1+1");
|
|
});
|
|
|
|
it("não altera os bytes do modelo ao processar", async () => {
|
|
const db = await createReadyDb();
|
|
const deps = depsOf(db);
|
|
const ana = ctxOf("ana", "alpha", "owner", true);
|
|
const model = await createModel(deps, ana, {
|
|
name: "Livro",
|
|
filename: "modelo.csv",
|
|
bytes: Buffer.from("Nome\n", "utf8"),
|
|
requiredColumns: ["Nome"],
|
|
});
|
|
const before = await deps.storage.get(`workspaces/${IDS.alpha}/models/${model.id}/original`);
|
|
const job = await createJob(deps, ana, model.id, "xlsx-model");
|
|
const source = await addSource(deps, ana, job.id, { filename: "fonte.csv", bytes: Buffer.from("Nome\nJosé\n", "utf8") });
|
|
if (!("job" in source)) throw new Error("fonte");
|
|
const sourceId = source.job.sources[0]?.id ?? "";
|
|
await saveMappings(deps, ana, job.id, sourceId, [{ sourceColumn: "Nome", modelColumn: "Nome" }]);
|
|
await startProcess(deps, ana, job.id, sourceId, "xlsx-proc");
|
|
const after = await deps.storage.get(`workspaces/${IDS.alpha}/models/${model.id}/original`);
|
|
expect(after.equals(before)).toBe(true);
|
|
await db.close();
|
|
});
|
|
});
|