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

test("Uncommitted remains readable after branch comparison fails", IOS, async ({t3, app, screen}) => {
  const project = await t3.project("Branch comparison", {"notes.txt":"Original note\n"});
  const git = (args: string[]) => runPromise(run("git", args, {cwd:project}));
  await git(["switch", "-c", "feature"]);
  await git(["-c","user.name=Scenario","-c","user.email=scenario@example.invalid","commit","--allow-empty","-m","Feature commit"]);
  await git(["switch","--orphan","unrelated"]);
  await git(["-c","user.name=Scenario","-c","user.email=scenario@example.invalid","commit","--allow-empty","-m","Unrelated root"]);
  await git(["switch","feature"]);
  await git(["config","branch.feature.t3-merge-base-ref","refs/heads/unrelated"]);
  await runPromise(run("git", ["merge-base","refs/heads/unrelated","HEAD"], {cwd:project,allowExit:[1]}));
  await t3.thread({project,title:"Review dirty notes",messages:["first"]});
  await runPromise(run("python3",["-c","from pathlib import Path; Path('notes.txt').write_text('Original note\\nUncommitted note\\n')"],{cwd:project}));
  await t3.open();
  await screen.getByText("Review dirty notes",{exact:false}).first().tap();
  await screen.getByRole("button","Git controls").tap();
  await screen.getByRole("button", "Review changes, Turn diffs and worktree changes").tap();
  await app.screenshot("review-open");
  await screen.getByRole("button","Select diff").tap();
  await app.screenshot("select-uncommitted");
  const uncommitted = screen.getByRole("button","Uncommitted");
  await expect(uncommitted).toBeEnabled();
  await uncommitted.tap();
  await expect(screen.getByText("Uncommitted note",{exact:false}).first()).toBeVisible();
  await app.screenshot("uncommitted-patch");
  await screen.getByRole("button","Select diff").tap();
  await screen.getByRole("button","Changes").tap();
  await expect(screen.getByText("Review unavailable",{exact:true}).first()).toBeVisible();
  await app.screenshot("changes-error");
});
