/// <reference lib="dom" />
import { expect } from "e2e";
import { readdir, readFile, writeFile } from "node:fs/promises";
import { T3_HOME } from "../e2e.config.ts";
import { test, WEB } from "./t3.ts";
const DOCX = "UEsDBBQAAAAAABKLbl2sbhJa3AAAANwAAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbDxUeXBlcyB4bWxucz0iaHR0cDovL3NjaGVtYXMub3BlbnhtbGZvcm1hdHMub3JnL3BhY2thZ2UvMjAwNi9jb250ZW50LXR5cGVzIj48T3ZlcnJpZGUgUGFydE5hbWU9Ii93b3JkL2RvY3VtZW50LnhtbCIgQ29udGVudFR5cGU9ImFwcGxpY2F0aW9uL3ZuZC5vcGVueG1sZm9ybWF0cy1vZmZpY2Vkb2N1bWVudC53b3JkcHJvY2Vzc2luZ21sLmRvY3VtZW50Lm1haW4reG1sIi8+PC9UeXBlcz5QSwMEFAAAAAAAEotuXWF7L0PyAAAA8gAAAAsAAABfcmVscy8ucmVsczxSZWxhdGlvbnNoaXBzIHhtbG5zPSJodHRwOi8vc2NoZW1hcy5vcGVueG1sZm9ybWF0cy5vcmcvcGFja2FnZS8yMDA2L3JlbGF0aW9uc2hpcHMiPjxSZWxhdGlvbnNoaXAgSWQ9InJJZDEiIFR5cGU9Imh0dHA6Ly9zY2hlbWFzLm9wZW54bWxmb3JtYXRzLm9yZy9vZmZpY2VEb2N1bWVudC8yMDA2L3JlbGF0aW9uc2hpcHMvb2ZmaWNlRG9jdW1lbnQiIFRhcmdldD0id29yZC9kb2N1bWVudC54bWwiLz48L1JlbGF0aW9uc2hpcHM+UEsDBBQAAAAAABKLbl3biZnC5gAAAOYAAAARAAAAd29yZC9kb2N1bWVudC54bWw8dzpkb2N1bWVudCB4bWxuczp3PSJodHRwOi8vc2NoZW1hcy5vcGVueG1sZm9ybWF0cy5vcmcvd29yZHByb2Nlc3NpbmdtbC8yMDA2L21haW4iPjx3OmJvZHk+PHc6cD48dzpyPjx3OnQgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+VGhpcyBvY2N1cnJlZCA8L3c6dD48dzp0PjE8L3c6dD48dzpub0JyZWFrSHlwaGVuLz48dzp0PjI0IGhvdXJzPC93OnQ+PC93OnI+PC93OnA+PC93OmJvZHk+PC93OmRvY3VtZW50PlBLAQIUAxQAAAAAABKLbl2sbhJa3AAAANwAAAATAAAAAAAAAAAAAACAAQAAAABbQ29udGVudF9UeXBlc10ueG1sUEsBAhQDFAAAAAAAEotuXWF7L0PyAAAA8gAAAAsAAAAAAAAAAAAAAIABDQEAAF9yZWxzLy5yZWxzUEsBAhQDFAAAAAAAEotuXduJmcLmAAAA5gAAABEAAAAAAAAAAAAAAIABKAIAAHdvcmQvZG9jdW1lbnQueG1sUEsFBgAAAAADAAMAuQAAAD0DAAAAAA==";
test("DOCX attachment preserves a numeric range's non-breaking hyphen", WEB, async ({t3,app,browser,screen}) => {
 const project = await t3.project("docx-proof", {"extracted-attachment.txt":"Waiting for fresh extraction"});
 await t3.open();
 await screen.getByRole("textbox").fill("Read the attached DOCX and report its attachment text.");
 await browser.evaluate((encoded) => {
  const bytes=Uint8Array.from(atob(encoded), c=>c.charCodeAt(0));
  const data=new DataTransfer();
  data.items.add(new File([bytes],"numeric-range.docx",{type:"application/vnd.openxmlformats-officedocument.wordprocessingml.document"}));
  document.querySelector('[contenteditable="true"]')!.dispatchEvent(new ClipboardEvent("paste",{clipboardData:data,bubbles:true,cancelable:true}));
  return null;
 }, DOCX);
 await screen.getByRole("textbox").press("Enter");
 await screen.getByText("This turn is a replay.",{exact:false}).waitFor({state:"visible",timeout:120000});
 const dir=T3_HOME+"/userdata/attachments";
 const files=await readdir(dir);
 const sidecar=files.find(f=>f.endsWith(".docx.txt") && !f.startsWith("pending"));
 if(!sidecar) throw new Error("Fresh claimed DOCX text sidecar missing: "+files.join(","));
 const text=await readFile(dir+"/"+sidecar,"utf8");
 await writeFile(project+"/extracted-attachment.txt",text);
 await screen.getByRole("button","Toggle right panel").tap();
 await screen.getByText("Files",{exact:true}).tap();
 await expect.poll(() => browser.evaluate(() => {
  for(const host of document.querySelectorAll("*")) {
   const row=host.shadowRoot?.querySelector<HTMLElement>('[data-item-path="extracted-attachment.txt"]');
   if(row) {row.click();return true;}
  }
  return false;
 })).toBe(true);
 const editorText=()=>browser.evaluate(()=>{
  for(const host of document.querySelectorAll("*")) {
   const content=host.shadowRoot?.querySelector("[data-content]");
   if(content) return content.textContent??"";
  }return "";
 });
 await expect.poll(editorText).toContain("This occurred");
 await app.screenshot("extracted-attachment-text");
 expect(text).toContain("This occurred 1\u201124 hours");
});
