import { describe, expect, it } from "vitest";
import {
  createWorkbookArrayBuffer,
  createWorkbookArrayBufferFromSheets,
} from "./workbook-export.js";

describe("workbook export helpers", () => {
  it("writes a single-sheet workbook with primitive values", async () => {
    const buffer = await createWorkbookArrayBuffer("Skills", [
      ["Skill", "Count", "Active"],
      ["TypeScript", 4, true],
      ["Planning", 2, false],
    ]);

    const ExcelJS = await import("exceljs");
    const workbook = new ExcelJS.Workbook();
    await workbook.xlsx.load(buffer as Parameters<typeof workbook.xlsx.load>[0]);

    const worksheet = workbook.getWorksheet("Skills");
    expect(worksheet).toBeDefined();
    expect(worksheet?.getRow(1).values).toEqual([, "Skill", "Count", "Active"]);
    expect(worksheet?.getRow(2).values).toEqual([, "TypeScript", 4, true]);
    expect(worksheet?.getRow(3).values).toEqual([, "Planning", 2, false]);
  });

  it("writes all provided sheets into the workbook", async () => {
    const buffer = await createWorkbookArrayBufferFromSheets([
      {
        name: "Overview",
        rows: [["Metric", "Value"], ["Resources", 12]],
      },
      {
        name: "People Finder",
        rows: [["Name", "Skills"], ["Peter Parker", "Staffing, Forecasting"]],
      },
    ]);

    const ExcelJS = await import("exceljs");
    const workbook = new ExcelJS.Workbook();
    await workbook.xlsx.load(buffer as Parameters<typeof workbook.xlsx.load>[0]);

    expect(workbook.worksheets.map((sheet) => sheet.name)).toEqual([
      "Overview",
      "People Finder",
    ]);
    expect(workbook.getWorksheet("Overview")?.getRow(2).values).toEqual([, "Resources", 12]);
    expect(workbook.getWorksheet("People Finder")?.getRow(2).values).toEqual([
      ,
      "Peter Parker",
      "Staffing, Forecasting",
    ]);
  });
});
