/// <reference lib="dom" />
import { expect } from "e2e";
import JSZip from "jszip";
import { test, WEB } from "./t3.ts";

test("Copy signed ODS numeric formats while escaping formula-shaped text", WEB, async ({ t3, app, browser, screen }) => {
  const zip = new JSZip();
  zip.file("mimetype", "application/vnd.oasis.opendocument.spreadsheet");
  const cells = [
    ['currency', '-1234.5', '-1 234,50 kr'],
    ['float', '-150000', '-1.50E5'],
    ['float', '-1.5', '-1 1/2'],
    ['float', '-42', '-42'],
    ['string', '', '=1+1'],
    ['string', '', '+SUM(A1)'],
    ['string', '', '-1+2'],
    ['string', '', '@SUM(A1)'],
  ];
  zip.file("content.xml", `<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.2"><office:body><office:spreadsheet><table:table table:name="Sheet1"><table:table-row>${cells.map(([type, value, text]) => `<table:table-cell office:value-type="${type}" ${value ? `office:value="${value}"` : ''}><text:p>${text}</text:p></table:table-cell>`).join('')}</table:table-row></table:table></office:spreadsheet></office:body></office:document-content>`);
  const project = await t3.project("ws", { "signed-formats.ods": await zip.generateAsync({type:"nodebuffer"}) });
  const seed = {project, title:"Copy signed spreadsheet values", messages:["Inspect signed numeric cells and formula-shaped text."]};
  const thread = await t3.thread({...seed, model:"multi-turn"}).catch((error) => {
    if (!String(error).includes("Advertised models: default")) throw error;
    return t3.thread({...seed, model:"default"});
  });
  await t3.open(thread);
  const composer = screen.getByRole("textbox", "Message Copy signed spreadsheet values");
  await composer.waitFor();
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  await screen.getByRole("treeitem", "signed-formats.ods").tap();
  const first = screen.getByRole("cell", "-1 234,50 kr", {exact:true});
  await first.waitFor({timeout:60000});
  await browser.evaluate(() => { Object.defineProperty(navigator, "clipboard", {value:undefined}); return null; });
  await first.tap();
  await browser.keyboard.press("ControlOrMeta+a");
  await browser.keyboard.press("ControlOrMeta+c");
  await composer.tap();
  await browser.keyboard.press("ControlOrMeta+v");
  await app.screenshot("copied-signed-values-and-escaped-text");
  await expect(composer).toContainText("-1 234,50 kr\t-1.50E5\t-1 1/2\t-42\t'=1+1\t'+SUM(A1)\t'-1+2\t'@SUM(A1)");
});
