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("A partial commit preserves the excluded index and working tree", WEB, async ({ t3, app, screen, browser }) => {
  await browser.emulateMedia({ colorScheme: "dark" });
  const project = await t3.project("demo", { "later.txt": "original\n" });
  const git = async (...args: string[]) => (await runPromise(run("git", args, { cwd: project }))).stdout;
  const write = (file: string, text: string) => runPromise(Effect.flatMap(FileSystem.FileSystem, fs => fs.writeFileString(`${project}/${file}`, text)));
  await git("config", "user.name", "Scenario");
  await git("config", "user.email", "scenario@example.invalid");
  const thread = await t3.thread({ project, title: "Partial commit staging", messages: ["Start"], model: "default" });
  await write("later.txt", "for the next commit\n");
  await git("add", "later.txt");
  await write("later.txt", "still editing\n");
  await write("include.txt", "ready\n");
  await t3.open(thread);
  await screen.getByRole("button", "Git action options").tap();
  await screen.getByRole("menuitem", "Commit", { exact: true }).tap();
  await screen.getByRole("button", "Open later.txt in editor").waitFor();
  await screen.getByRole("button", "Edit", { exact: true }).tap();
  await screen.getByRole("checkbox", "Include later.txt").tap();
  await screen.getByRole("button", "Open later.txt in editor").getByText("Excluded", { exact: true }).waitFor();
  await screen.getByPlaceholder("Leave empty to auto-generate").fill("Commit include.txt");
  await app.screenshot("selected-files");
  await screen.getByRole("button", "Commit", { exact: true }).tap();
  await expect.poll(() => git("log", "-1", "--format=%s")).toBe("Commit include.txt\n");
  await screen.getByRole("button", "Toggle terminal drawer").tap();
  const terminal = screen.getByLabel("Terminal input");
  await terminal.focus();
  await browser.keyboard.type("export PS1='$ '; clear; printf '\\nCommitted files:\\n'; git diff-tree --no-commit-id --name-only -r HEAD; printf '\\nExcluded staged content:\\n'; git show :later.txt; printf '\\nExcluded working-tree content:\\n'; cat later.txt; printf '\\nGit status:\\n'; git status --porcelain; printf '\\nPROBE COMPLETE\\n'");
  await browser.keyboard.press("Enter");
  // The terminal's canvas has no DOM text; its own copy provides a completion marker.
  await expect.poll(async () => {
    await browser.evaluate(() => { document.body.dataset.copied = ""; document.addEventListener("copy", event => { document.body.dataset.copied = event.clipboardData?.getData("text/plain") ?? ""; }, { once: true }); });
    await terminal.focus();
    await browser.keyboard.press("Control+Shift+A");
    await browser.keyboard.press("Control+Shift+C");
    return browser.evaluate(() => document.body.dataset.copied ?? "");
  }).toContain("PROBE COMPLETE");
  await app.screenshot("excluded-staging-after-commit");
  await expect(await git("show", ":later.txt")).toBe("for the next commit\n");
  await expect(await git("status", "--porcelain")).toBe("MM later.txt\n");
});
