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("Changes explains when all changed files are hidden tests", WEB, async ({ t3, app, browser, screen }) => {
  await browser.emulateMedia({ colorScheme: "dark" });
  const project = await t3.project("math-demo", {
    "math.ts": "export const add = (a: number, b: number) => a + b;\n",
    "math.test.ts": "// Math tests\n",
  });
  await runPromise(Effect.gen(function* () {
    yield* run("git", ["checkout", "-b", "test-only"], { cwd: project });
    const fs = yield* FileSystem.FileSystem;
    yield* fs.writeFileString(`${project}/math.test.ts`, "// Math tests\n// Added test case\n");
    yield* run("git", ["add", "math.test.ts"], { cwd: project });
    yield* run("git", ["-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "-m", "Add test case"], { cwd: project });
  }));
  const thread = await t3.thread({ project, title: "Review test-only changes", messages: ["first"] });
  await t3.open(thread);
  await screen.getByText("first fixture turn complete", { exact: false }).waitFor();
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByText("Diff", { exact: true }).tap();
  await screen.getByRole("button", /^Diff scope:/).tap();
  await screen.getByRole("menuitemradio", "Changes", { exact: true }).tap();
  await screen.getByRole("button", "Expand math.test.ts", { exact: true }).waitFor();
  await app.screenshot("test-file-visible");
  await screen.getByRole("button", "Hide test files", { exact: true }).tap();
  await screen.getByRole("button", "Expand math.test.ts", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("all-tests-hidden");
  await expect(screen.getByText("1 test file hidden", { exact: true })).toBeVisible();
  await expect(screen.getByRole("button", "Show test files", { exact: true })).toBeVisible();
});
