import { execFileSync } from "node:child_process";
import { mkdirSync, mkdtempSync } from "node:fs";
import { join } from "node:path";
import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { test, WEB, makeState, runPromise } from "./t3.ts";

test("Repository identity preserves checkout CR and bare repository LF paths", WEB, async ({ t3, app, screen }) => {
  const control = await t3.project("control", { "app/README.md": "Nested application" });
  const git = (cwd: string, ...args: string[]) => execFileSync("git", args, { cwd, encoding: "utf8" });
  const origin = "https://github.com/pingdotgg/t3code.git";
  git(control, "remote", "add", "origin", origin);
  const root = mkdtempSync("/tmp/factory-pr-38114-evidence-1/fixtures-");
  const cr = join(root, "crrepo\r");
  mkdirSync(join(cr, "app"), { recursive: true });
  git(cr, "init", "--quiet");
  git(cr, "remote", "add", "origin", origin);
  const bare = join(root, "bare\nlf");
  const bareControl = join(root, "barecontrol");
  for (const path of [bare, bareControl]) {
    mkdirSync(path);
    git(path, "init", "--bare", "--quiet");
    git(path, "remote", "add", "origin", origin);
  }
  const { cli } = makeState(T3_HOME);
  for (const [path, title] of [[join(control, "app"), "control-app"], [join(cr, "app"), "cr-checkout"], [bareControl, "bare-control"], [bare, "lf-bare"]]) {
    await runPromise(cli(["project", "add", path!, "--title", title!]));
  }
  await t3.open();
  await screen.getByRole("combobox", "Filter threads by project", { exact: true }).tap();
  await app.screenshot("repository-project-filter");
  await expect(screen.getByRole("option", "cr-checkout", { exact: false })).toHaveCount(0);
  await expect(screen.getByRole("option", "lf-bare", { exact: false })).toHaveCount(0);
  await expect(screen.getByRole("option", "pingdotgg/t3code", { exact: false })).toHaveCount(1);
});
