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("Review preserves sparse index tracked files and shows the new file", WEB, async ({ t3, app, screen }) => {
  const project = await t3.project("sparse-review", {
    "included/tracked.ts": "export const included = true;\n",
    "excluded/tracked.ts": "export const excluded = true;\n",
  });
  const thread = await t3.thread({ project, title: "Review sparse checkout", messages: ["first"] });
  await runPromise(Effect.gen(function* () {
    yield* run("git", ["sparse-checkout", "set", "--cone", "--sparse-index", "included"], { cwd: project });
    const fs = yield* FileSystem.FileSystem;
    yield* fs.writeFileString(`${project}/included/new.ts`, "export const newFile = true;\n");
  }));
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel", { exact: true }).tap();
  await screen.getByText("Diff", { exact: true }).tap();
  await screen.getByRole("button", /^Diff scope:/).tap();
  await screen.getByRole("menuitemradio", "Uncommitted", { exact: true }).tap();
  await screen.getByRole("button", /^(Expand|Collapse) /).first().waitFor();
  await app.screenshot("sparse-index-working-tree-review");
  await expect(screen.getByRole("button", /^(Expand|Collapse) included\/new\.ts$/)).toBeVisible();
  await expect(screen.getByRole("button", /^(Expand|Collapse) included\/tracked\.ts$/)).not.toBeVisible();
  await expect(screen.getByRole("button", /^(Expand|Collapse) excluded\/tracked\.ts$/)).not.toBeVisible();
});
