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

const W = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
const R = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
async function doc(spaced: boolean, malformed: boolean) {
  const header = spaced ? 'header 1.xml' : 'header1.xml';
  const zip = new JSZip();
  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="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/><Override PartName="/word/${header}" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml"/></Types>`);
  zip.file('_rels/.rels', `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="${R}/officeDocument" Target="word/document.xml"/></Relationships>`);
  zip.file('word/document.xml', `<w:document xmlns:w="${W}" xmlns:r="${R}"><w:body><w:p><w:r><w:t>Body text: quarterly results are up.</w:t></w:r></w:p><w:sectPr><w:headerReference w:type="default" r:id="rId1"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720"/></w:sectPr></w:body></w:document>`);
  zip.file('word/_rels/document.xml.rels', `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="${R}/header" Target="${header}"/>${malformed ? `<Relationship Id="rId9" Type="${R}/hyperlink" Target=" https://"/>` : ''}</Relationships>`);
  zip.file(`word/${header}`, `<w:hdr xmlns:w="${W}"><w:p><w:r><w:t>HEADER TEXT VISIBLE</w:t></w:r></w:p></w:hdr>`);
  return zip.generateAsync({type:'uint8array'});
}

test('Word previews preserve spaced headers and omit malformed external links', WEB, async ({t3, app, browser, screen}) => {
  const variants = [
    ['control-header1', false, false],
    ['spaced-header', true, false],
    ['malformed-link-spaced-header', true, true],
    ['malformed-link-control-header', false, true],
  ] as const;
  const files: Record<string, Uint8Array> = {};
  for (const [name, spaced, malformed] of variants) files[`${name}.docx`] = await doc(spaced, malformed);
  const project = await t3.project('Word preview proof', files);
  const thread = await t3.thread({project, title:'Word preview proof', messages:['Start'], model:'multi-turn'});
  await t3.open(thread);
  await screen.getByTestId('composer-editor').waitFor();
  const results = [];
  for (const [name] of variants) {
    await browser.keyboard.press('Control+p');
    await screen.getByPlaceholder('Search files…').waitFor();
    await screen.getByRole('option', `${name}.docx ${name}.docx`, {exact:true}).tap();
    await screen.getByText(/Body text: quarterly results are up\.|No preview for this file/).first().waitFor({timeout:120000});
    await app.screenshot(name);
    results.push(await browser.evaluate(() => ({
      body: document.body.innerText.includes('Body text: quarterly results are up.'),
      header: document.body.innerText.includes('HEADER TEXT VISIBLE'),
      error: document.body.innerText.includes('Invalid URL'),
    })));
  }
  expect(results).toEqual(variants.map(() => ({body:true,header:true,error:false})));
});
