import { expect } from "e2e";
import JSZip from "jszip";
import { test, WEB } from "./t3.ts";

async function workbook(prefix: number) {
  const zip = new JSZip();
  const ns = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
  zip.file("[Content_Types].xml", '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/><Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/><Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/></Types>');
  zip.file("_rels/.rels", `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="${ns}/officeDocument" Target="xl/workbook.xml"/></Relationships>`);
  zip.file("xl/workbook.xml", `<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="${ns}"><sheets><sheet name="Sheet1" sheetId="1" r:id="rId1"/></sheets></workbook>`);
  zip.file("xl/_rels/workbook.xml.rels", `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="${ns}/worksheet" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="${ns}/sharedStrings" Target="sharedStrings.xml"/></Relationships>`);
  zip.file("xl/sharedStrings.xml", `<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="2" uniqueCount="2"><si><t>${"a".repeat(prefix)}🚀</t></si><si><t>Row two</t></si></sst>`);
  zip.file("xl/worksheets/sheet1.xml", '<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><dimension ref="A1:A2"/><sheetData><row r="1"><c r="A1" t="s"><v>0</v></c></row><row r="2"><c r="A2" t="s"><v>1</v></c></row></sheetData></worksheet>');
  return zip.generateAsync({ type: "uint8array" });
}

test("XLSX cell truncation keeps whole emoji", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Workbook preview", { "rocket.xlsx": await workbook(4095), "control.xlsx": await workbook(4096) });
  const thread = await t3.thread({ project, title: "Review workbook", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  await screen.getByRole("button", "Maximize panel").tap();
  await screen.getByRole("treeitem", "control.xlsx").tap();
  await screen.getByText("Preview limited", { exact: false }).waitFor({ timeout: 120_000 });
  await screen.getByText("Row two", { exact: true }).waitFor();
  await app.screenshot("control-workbook");
  await screen.getByRole("treeitem", "rocket.xlsx").tap();
  await expect.poll(() => browser.evaluate(() => document.body.textContent?.includes("No preview for this file") || document.querySelector('[role="grid"]') !== null)).toBe(true);
  await app.screenshot("rocket-workbook");
  await expect(screen.getByText("Row two", { exact: true })).toBeVisible();
  await expect(screen.getByText("Preview limited", { exact: false })).toBeVisible();
});
