import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Removed and sent file comments do not steal editor typing", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Editor comments", {
    "notes.md": "Line one\nLine two\nLine three\nLine four\nLine five\nLine six\nLine seven\nLine eight\nLine nine\nLine ten\n",
  });
  const thread = await t3.thread({ project, title: "File comment focus", messages: ["Ready"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByText("Files", { exact: true }).tap();
  await screen.getByRole("treeitem", "notes.md").tap();
  await screen.getByRole("button", "Hide file explorer").tap();

  const composer = screen.getByTestId("composer-editor");
  const editor = screen.getByRole("textbox", "notes.md", { exact: true });
  const addComment = async () => {
    await browser.locator('[data-column-number="8"]').tap();
    await screen.getByRole("textbox", "Comment on lines L8").fill("fix this line");
    await browser.keyboard.press("Control+Enter");
  };
  const clearComposer = async () => {
    await composer.press("Control+a");
    await browser.keyboard.press("Backspace");
    await browser.keyboard.press("Backspace");
    await expect(composer).toHaveText("");
  };
  const typeInFile = async (line: number, label: string) => {
    await browser.locator(`[data-line="${line}"]`).tap();
    await browser.keyboard.press("End");
    await editor.pressSequentially("QW", { delay: 500 });
    await app.screenshot(`${label}-after-typing`);
    await expect.soft(editor).toBeFocused({ timeout: 1000 });
    await expect.soft(browser.locator(`[data-line="${line}"]`)).toHaveText(line === 3 ? "Line threeQW" : "Line fourQW", { timeout: 1000 });
    await expect.soft(composer).toHaveText("", { timeout: 1000 });
    await expect.soft(screen.getByRole("button", "Delete comment")).toHaveCount(0, { timeout: 1000 });
    await app.screenshot(`${label}-result`);
  };

  await addComment();
  await app.screenshot("comment-in-composer");
  await clearComposer();
  await app.screenshot("removed-before-typing");
  await typeInFile(3, "removed");

  // Reset editor-local annotations so the send path starts with one fresh comment on both commits.
  await clearComposer();
  await browser.reload();
  await addComment();
  await screen.getByRole("button", "Submit message").tap();
  await screen.getByText("second fixture turn complete", { exact: false }).textContent();
  await screen.getByRole("button", "Stop generation").waitFor({ state: "hidden" });
  await screen.getByText("Working", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("sent-before-typing");
  await typeInFile(4, "sent");

  await clearComposer();
  await browser.reload();
  await addComment();
  await browser.locator('[data-column-number="6"]').tap();
  const draft = screen.getByRole("textbox", "Comment on lines L6");
  await draft.fill("unfinished draft");
  await app.screenshot("current-comment-and-unfinished-draft-remain");
  await expect.soft(draft).toHaveValue("unfinished draft");
  await expect.soft(composer).toContainText("notes.md L8");
  await expect.soft(screen.getByRole("button", "Delete comment")).toHaveCount(1);
});
