/// <reference lib="dom" />
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import { run } from "./shell.ts";
import { T3_HOME } from "../e2e.config.ts";
import { runPromise, test, WEB } from "./t3.ts";

test("Collapsed submodule clears changes after external commit and push", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("parent");
  const git = (cwd: string, ...args: string[]) => runPromise(run("git", args, { cwd }));
  const write = (path: string, text: string) => runPromise(Effect.flatMap(FileSystem.FileSystem, fs => fs.writeFileString(path, text)));
  for (const name of ["parent", "sub"]) await git(project, "init", "--bare", `../${name}.git`);
  await git(project, "remote", "add", "origin", "../parent.git");
  await git(project, "push", "-u", "origin", "main");
  await git(project, "init", "--initial-branch=main", "../seed");
  const seed = `${project}/../seed`;
  await write(`${seed}/lib.txt`, "Original line\n");
  await git(seed, "add", ".");
  await git(seed, "-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "-m", "Initial library");
  await git(seed, "remote", "add", "origin", "../sub.git");
  await git(seed, "push", "-u", "origin", "main");
  await git(project, "-c", "protocol.file.allow=always", "submodule", "add", "-b", "main", "../sub.git", "sub");
  await git(project, "-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "-am", "Add submodule");
  await git(project, "push");
  const settingsPath = `${T3_HOME}/userdata/settings.json`;
  const original = await runPromise(Effect.flatMap(FileSystem.FileSystem, fs => fs.readFileString(settingsPath)));
  await write(settingsPath, JSON.stringify({...JSON.parse(original), defaultRuntimeMode: "approval-required"}));
  const thread = await t3.thread({ project, title: "Submodule changes", messages: ["Append forty lines to sub/lib.txt"], model: "approval" });
  await browser.setViewport({width:1600,height:1000});
  await t3.open(thread);
  await screen.getByRole("button", "Approve", {exact:true}).waitFor();
  await write(`${project}/sub/lib.txt`, "Original line\n" + Array.from({length:40}, (_, i) => `Added line ${i + 1}\n`).join(""));
  await screen.getByRole("button", "Approve", {exact:true}).tap();
  await screen.getByRole("button", "Stop generation").waitFor({state:"hidden", timeout:30000});
  await write(settingsPath, original);
  const row = screen.getByRole("group", "Repository sub", {exact:true});
  await row.waitFor({timeout:30000});
  await screen.getByText("+40", {exact:true}).waitFor();
  await app.screenshot("dirty-collapsed-submodule");
  const sub = `${project}/sub`;
  await git(sub, "add", "lib.txt");
  await git(sub, "-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "-m", "Append forty lines");
  await git(sub, "push");
  await git(project, "add", "sub");
  await git(project, "-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "-m", "Update library");
  await git(project, "push");
  const status = await git(sub, "status", "--porcelain");
  if (status.stdout.trim()) throw new Error("Fixture submodule is not clean");
  await runPromise(Effect.sleep("65 seconds"));
  await app.screenshot("collapsed-submodule-after-commit-push");
  await expect(row).not.toContainText("+40");
});
