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

test("Setup output keeps whole emoji at both truncation boundaries", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("emoji-demo", {
    "t3.json": JSON.stringify({ scripts: [{ name: "Emoji setup", command: `node -e 'process.stdout.write("🙂".repeat(2100) + "x"); setTimeout(() => process.stdout.write("\\n"), 500)' && sleep 90`, runOnWorktreeCreate: true, async: false }] }),
  });
  const first = await t3.thread({ project, title: "Import setup action", messages: ["Prepare the project"] });
  await t3.open(first);
  await screen.getByRole("button", "Project actions", { exact: true }).tap();
  await screen.getByRole("menuitem", /Emoji setup/).tap();
  await screen.getByRole("button", "Run Emoji setup", { exact: true }).waitFor();
  const result = await runPromise(makeState(T3_HOME).cli(["thread", "start", "--worktree", "main", "--branch", "emoji-2", project, "Run setup", "--title", "Emoji setup output", "--provider", "devReplay", "--model", "showcase", "--json"]));
  const thread = JSON.parse(result.stdout);
  await t3.open(thread);
  await browser.locator('[aria-label="Worktree setup"] pre').waitFor();
  await app.screenshot("setup-emoji-output");
  const output = await browser.evaluate(() => document.querySelector('[aria-label="Worktree setup"] pre')?.textContent ?? "");
  expect(output.isWellFormed()).toBe(true);
  expect(output.includes("�")).toBe(false);
});
