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

test("Identical fenced drafts keep syntax colors after switching threads", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("syntax-demo");
  const b = await t3.thread({ project, title: "Draft B", messages: ["Start"], model: "default" });
  const c = await t3.thread({ project, title: "Draft C", messages: ["Start"], model: "default" });
  await browser.setViewport({ width: 1440, height: 900 });
  await t3.open(b);
  const paste = async () => {
    await screen.getByRole("textbox").tap();
    await browser.evaluate(() => {
      const editor = document.activeElement!;
      const clipboardData = new DataTransfer();
      clipboardData.setData("text/plain", "```typescript\nconst x = 1;\n```");
      editor.dispatchEvent(new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }));
    });
    await browser.locator('[data-testid="composer-editor"] pre code span[style]').first().waitFor();
  };
  await paste();
  await browser.locator(`[data-sidebar-row$=":${c.threadId}"]`).click();
  await paste();
  await app.screenshot("initial-highlighted-draft");
  await browser.locator(`[data-sidebar-row$=":${b.threadId}"]`).click();
  await browser.locator('[data-testid="composer-editor"] pre code').waitFor();
  await browser.locator(`[data-sidebar-row$=":${c.threadId}"]`).click();
  await browser.locator('[data-testid="composer-editor"] pre code').waitFor();
  await app.screenshot("reopened-draft");
  await expect(browser.locator('[data-testid="composer-editor"] pre code span[style]')).toHaveCount(5);
});
