/// <reference lib="dom" />
import { execFileSync } from "node:child_process";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

const body = '\uFEFF[docs]: https://example.com/guide\n\n- [ ] Before attachment\n\nhttps://github.com/user-attachments/assets/2f8c1a90-1b2c-4d5e-8f90-abcdef123456\n\nSee [docs].\n\n- [ ] After attachment';
const org = process.env.AZURE_DEVOPS_TEST_ORG!;
const prUrl = 'https://dev.azure.com/t3code-factory-test/t3-factory-test/_git/t3-factory-test/pullrequest/492';
function update(description: string) {
  execFileSync('az', ['repos','pr','update','--id','492','--organization',org,'--description',description,'--output','none']);
}
function stored() {
  return execFileSync('az', ['repos','pr','show','--id','492','--organization',org,'--query','description','-o','json'], {encoding:'utf8'});
}
test('BOM references and checkbox edits survive an attachment split', WEB, async ({t3,app,browser,screen}) => {
  update(body);
  const project = await t3.project('Markdown proof');
  execFileSync('git', ['-C',project,'remote','add','origin','https://dev.azure.com/t3code-factory-test/t3-factory-test/_git/t3-factory-test']);
  const thread = await t3.thread({project,title:'Markdown proof',messages:['Ready']});
  await t3.open(thread);
  await t3.open(`/pull-requests?${new URLSearchParams({selectedUrl:prUrl})}`);
  await screen.getByRole('button', /Open #492 Markdown BOM reference proof/).tap();
  await screen.getByRole('button','Edit description',{exact:true}).waitFor({timeout:60000});
  await screen.getByRole('button','Retry video',{exact:true}).waitFor({timeout:120000});
  await app.screenshot('reference-after-attachment');
  const link = await browser.evaluate(() => [...document.querySelectorAll('a')].filter(a=>a.textContent==='docs').map(a=>a.getAttribute('href')));
  await screen.getByRole('checkbox','Toggle task',{exact:true}).nth(0).tap();
  await screen.getByRole('checkbox','Toggle task',{exact:true}).nth(0).waitFor();
  await screen.getByRole('button','Edit description',{exact:true}).tap();
  await screen.getByRole('button','Save',{exact:true}).waitFor();
  await app.screenshot('checkbox-original-description');
  const edited = JSON.parse(stored());
  await screen.getByRole('button','Cancel',{exact:true}).tap();
  await screen.getByRole('checkbox','Toggle task',{exact:true}).nth(1).tap();
  await screen.getByRole('button','Edit description',{exact:true}).tap();
  await screen.getByRole('button','Save',{exact:true}).waitFor();
  await app.screenshot('both-checkboxes-original-description');
  const editedBoth = JSON.parse(stored());
  expect(edited, 'Before-attachment checkbox changes only its original marker').toBe(body.replace('- [ ] Before','- [x] Before'));
  expect(editedBoth, 'Both checkbox edits preserve BOM, definition, attachment and surrounding text').toBe(body.replaceAll('- [ ]','- [x]'));
  expect(link, 'The docs reference after the attachment resolves to the complete URL').toEqual(['https://example.com/guide']);
});
