import { chmod, writeFile } from "node:fs/promises";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("A new worktree starts its thread when a checkout hook prints over 1 MB", WEB, async ({ t3, app, screen, browser }) => {
  const repo = await t3.project("Noisy checkout", { "README.md": "Synthetic worktree reproduction.\n" });
  await writeFile(`${repo}/.git/hooks/post-checkout`, '#!/bin/sh\nhead -c 1100000 /dev/zero | tr "\\0" x >&2\n');
  await chmod(`${repo}/.git/hooks/post-checkout`, 0o755);
  await t3.open();
  await screen.getByRole("combobox", "Workspace").tap();
  await screen.getByRole("option", "New worktree").tap();
  await screen.getByRole("textbox").fill("Give me a short tour");
  await screen.getByRole("textbox").press("Enter");
  try {
    await expect(screen.getByText("This turn is a replay.", { exact: false })).toBeVisible({ timeout: 60000 });
  } finally {
    await browser.evaluate(() => {
      document.querySelector<HTMLButtonElement>('button[aria-label="Dismiss error"]')?.click();
    });
    await app.screenshot("worktree-result");
  }
});
