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

test("Stashing and restoring code preserves the first line indentation", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("indentation-demo");
  const thread = await t3.thread({ project, title: "Stash code indentation", messages: ["Start"], model: "default" });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.tap();
  await browser.evaluate(() => {
    const editor = document.activeElement;
    if (!(editor instanceof HTMLElement) || !editor.isContentEditable) throw new Error("Composer not focused");
    const clipboardData = new DataTransfer();
    clipboardData.setData("text/plain", '    if ready:\n        print("go")');
    editor.dispatchEvent(new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }));
    return null;
  });
  await app.screenshot("original-indented-draft");
  await composer.press("Control+s");
  await expect(composer).toHaveText("");
  await app.screenshot("stashed-draft");
  await composer.press("Control+s");
  await screen.getByText('print("go")', { exact: false }).waitFor();
  await app.screenshot("restored-code-indentation");
  const restored = await browser.evaluate(() => {
    const editor = document.querySelector<HTMLElement>('[role="textbox"]');
    return editor ? [...editor.querySelectorAll("p")].map((p) => p.textContent).join("\n") : null;
  });
  expect(restored).toBe('    if ready:\n        print("go")');
});
