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

test("Select All in large rendered Markdown copies the complete file only", WEB, async ({ t3, app, browser, screen }) => {
  const markdown = '# Large Markdown Preview\n\nOpening sentinel line of big.md.\n\n' + Array.from({length: 5000}, (_, i) => `Paragraph ${i + 1}: ${'Synthetic documentation text for preview selection. '.repeat(6)}\n\n`).join('') + 'Closing sentinel line of big.md\n';
  const project = await t3.project('markdown-preview', {'big.md': markdown});
  const thread = await t3.thread({project, title: 'Chat thread with messages', messages: ['Transcript text from the chat thread: please summarize big.md']});
  await t3.open(thread);
  await screen.getByText('fixture turn complete', {exact: false}).waitFor();
  await browser.keyboard.press('Control+p');
  await screen.getByPlaceholder('Search files…').fill('big.md');
  await screen.getByRole('option', 'big.md', {exact: false}).tap();
  await screen.getByRole('button', 'Show rendered markdown').tap();
  await screen.getByText(/^Paragraph 1:/).tap();
  await browser.keyboard.press('ControlOrMeta+A');
  const selected = await browser.evaluate(() => window.getSelection()?.toString() ?? '');
  await app.screenshot('select-all');
  await browser.keyboard.press('ControlOrMeta+C');
  await browser.evaluate(() => {
    const box = document.createElement('textarea');
    box.setAttribute('aria-label', 'Clipboard proof');
    box.style.cssText = 'position:fixed;bottom:20px;left:20px;width:650px;height:220px;z-index:99999;background:white;color:black;font:16px monospace;border:3px solid #555';
    document.body.append(box);
    box.focus();
    return null;
  });
  await browser.keyboard.press('ControlOrMeta+V');
  const copied = await browser.evaluate(() => (document.querySelector('textarea[aria-label="Clipboard proof"]') as HTMLTextAreaElement).value);
  await browser.keyboard.press('Control+End');
  await app.screenshot('pasted-file-end');
  expect(selected.includes('Chat thread with messages')).toBe(false);
  expect(selected.startsWith('Large Markdown Preview')).toBe(true);
  expect(copied).toBe(markdown);
});
