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

test("Commit selection follows visible files and preserves absent exclusions", IOS, async ({ t3, app, screen }) => {
  const project = await t3.project("commit-demo", { "alpha.ts": "export const alpha = 1;\n", "beta.ts": "export const beta = 1;\n" });
  await runPromise(run("git", ["checkout", "-b", "feature"], { cwd: project }));
  await t3.thread({ project, title: "commit repro", messages: ["Start"] });
  const modify = (name: string) => runPromise(Effect.gen(function* () {
    const fs = yield* FileSystem.FileSystem;
    yield* fs.writeFileString(`${project}/${name}.ts`, `export const ${name} = 1;\n// changed\n`);
  }));
  await modify("alpha");
  await modify("beta");
  await t3.open();
  await screen.getByText("commit repro", { exact: false }).first().tap({ timeout: 120_000 });
  await screen.getByRole("button", "Git controls").tap({ timeout: 60_000 });
  await screen.getByText("More", { exact: false }).first().tap();
  await screen.getByText("Commit, 2 files changed", { exact: false }).first().tap();
  await screen.getByRole("checkbox", "beta.ts").tap();
  await screen.getByText("1 of 2 selected", { exact: false }).waitFor();
  await app.screenshot("beta-excluded");
  await runPromise(run("git", ["checkout", "--", "beta.ts"], { cwd: project }));
  await screen.getByText("1 of 1 selected", { exact: false }).waitFor({ timeout: 60_000 });
  await app.screenshot("all-visible-files-selected");
  await expect(screen.getByRole("button", "Deselect all")).toBeVisible();
  await screen.getByRole("button", "Deselect all").tap();
  await modify("beta");
  await screen.getByText("0 of 2 selected", { exact: false }).waitFor({ timeout: 60_000 });
  await app.screenshot("returned-beta-stays-excluded");
  await expect(screen.getByText("0 of 2 selected", { exact: false })).toBeVisible();
});
