// @effect-diagnostics nodeBuiltinImport:off -- Synthetic git fixtures for real-app proof.
import { readFile, writeFile } from "node:fs/promises";
import { expect } from "e2e";
import { run } from "./shell.ts";
import { T3_HOME } from "../e2e.config.ts";
import { makeState, runPromise, test, WEB } from "./t3.ts";

test("Mixed-case submodule path keys receive checkpoint repository rows", WEB, async ({ t3, app, screen, browser }) => {
  const source = await t3.project("submodule-source", { "notes.txt": "original line\n" });
  const git = (cwd: string, ...args: string[]) => runPromise(run("git", args, { cwd }));
  const commit = async (cwd: string) => {
    await git(cwd, "add", "--all");
    await git(cwd, "-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "-qm", "Submodule fixture");
  };
  const nested = await t3.project("nested-source", { "notes.txt": "original line\n" });
  await git(source, "-c", "protocol.file.allow=always", "submodule", "add", nested, "deps/nestedlib");
  await writeFile(`${source}/.gitmodules`, (await readFile(`${source}/.gitmodules`, "utf8")).replace("path =", "Path ="));
  await commit(source);
  const cases = [];
  for (const key of ["path", "Path", "PATH"]) {
    const project = await t3.project(`parent-${key === "path" ? "lower" : key === "Path" ? "title" : "upper"}`);
    await git(project, "-c", "protocol.file.allow=always", "submodule", "add", source, "vendor/lib");
    await git(project, "-c", "protocol.file.allow=always", "submodule", "update", "--init", "--recursive");
    await git(`${project}/vendor/lib/deps/nestedlib`, "checkout", "main");
    await writeFile(`${project}/.gitmodules`, (await readFile(`${project}/.gitmodules`, "utf8")).replace("path =", `${key} =`));
    await commit(project);
    cases.push({ project, key });
  }
  const completed = [];
  for (const { project, key } of cases) {
    // The question keeps a real Replay turn open after its checkpoint baseline is captured.
    const thread = await t3.thread({ project, title: `${key} submodule checkpoint`, model: "question", messages: ["Replace vendor/lib/notes.txt with edited by agent; do not commit."] });
    await t3.open(thread);
    await browser.locator('[data-pending-user-input-toggle]').waitFor({ timeout: 60_000 });
    await writeFile(`${project}/vendor/lib/notes.txt`, "edited by agent\n");
    await writeFile(`${project}/vendor/lib/deps/nestedlib/notes.txt`, "edited by agent\n");
    await screen.getByText("Strict schemas (Recommended)", { exact: true }).tap();
    await screen.getByText("plan questions fixture complete", { exact: false }).waitFor();
    await screen.getByRole("button", "Stop generation").waitFor({ state: "hidden" });
    completed.push({ thread, key });
  }
  for (const { thread, key } of completed) {
    await t3.open(thread);
    await screen.getByRole("button", "Changes Uncommitted +0 -0").waitFor();
    await runPromise(makeState(T3_HOME).cli(["thread", "show", thread.threadId, "--json"]));
    await app.screenshot(`${key === "path" ? "lowercase-control" : key === "Path" ? "titlecase" : "uppercase"}-checkpoint-repositories`);
  }
  for (const { thread } of completed) {
    await t3.open(thread);
    await expect(screen.getByRole("group", "Repository lib")).toBeVisible();
    await expect(screen.getByRole("group", "Repository lib")).toHaveText(/\+1.*-1/);
  }
  for (const { thread } of completed) {
    await t3.open(thread);
    await expect(screen.getByRole("group", "Repository nestedlib")).toBeVisible();
    await expect(screen.getByRole("group", "Repository nestedlib")).toHaveText(/\+1.*-1/);
  }
});
