import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import { run } from "./shell.ts";
import { runPromise, test, WEB } from "./t3.ts";

test("New thread opens its selected branch diff before the first message", WEB, async ({ t3, app, screen }) => {
  const project = await t3.project("branch-review", {
    "t3.json": JSON.stringify({ defaultThreadEnvMode: "local" }),
    "removed.txt": Array.from({ length: 50 }, (_, i) => `Original line ${i + 1}`).join("\n") + "\n",
  });
  const git = (...args: string[]) => runPromise(run("git", args, { cwd: project }));
  await git("checkout", "-b", "feature");
  await runPromise(Effect.gen(function* () {
    const fs = yield* FileSystem.FileSystem;
    yield* fs.remove(`${project}/removed.txt`);
    yield* fs.writeFileString(`${project}/added.txt`, Array.from({ length: 30 }, (_, i) => `Feature line ${i + 1}`).join("\n") + "\n");
  }));
  await git("add", "--all");
  await git("-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "-m", "Feature changes");
  await t3.open();
  const changes = screen.getByRole("button", /^Changes/);
  await changes.waitFor();
  await app.screenshot("new-thread-branch-changes");
  await expect(changes).toBeEnabled();
  await changes.tap();
  await screen.getByRole("button", /^(Expand|Collapse) added\.txt$/).waitFor();
  await app.screenshot("branch-diff-open");
  await expect(screen.getByRole("button", /^(Expand|Collapse) removed\.txt$/)).toBeVisible();
});
