import { expect, it } from "vitest";
import { parseCsv, readTable, toCsv, toXlsx } from "./table.js";

it("CSV survives quotes, commas, newlines and neutralises formulas", () => {
  const csv = toCsv(["a", "b"], [['say "hi", ok', "line1\nline2"], ["=1+1", null]]);
  expect(parseCsv(csv)).toEqual([["a", "b"], ['say "hi", ok', "line1\nline2"], ["'=1+1", ""]]);
});
it("reads back what it writes in both formats", async () => {
  const header = ["Trainee ID", "phone"], rows = [["PBPS-1", "0911223344"]];
  const want = { header: ["trainee_id", "phone"], rows: [{ trainee_id: "PBPS-1", phone: "0911223344" }] };
  expect(await readTable("x.csv", Buffer.from(toCsv(header, rows)))).toEqual(want);
  expect(await readTable("x.xlsx", await toXlsx("s", header, rows, ["phone"]))).toEqual(want);
  await expect(readTable("x.xlsx", Buffer.from("nope"))).rejects.toThrow("could not be read");
});
