import { copyFile, chmod } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { test, WEB } from "./t3.ts";

test("Subpath Forgejo setup guidance directs users to tea", WEB, async ({ t3, app, screen }) => {
  await copyFile(process.env.FORGEJO_PROOF_BIN!, join(T3_HOME, "bin/fj"));
  await chmod(join(T3_HOME, "bin/fj"), 0o755);
  await t3.project("Forgejo demo");
  await t3.open("/settings/source-control");
  await screen.getByRole("button", "Rescan server environment").tap();
  await screen.getByText("fj v0.7.0").waitFor({ state: "visible" });
  await screen.getByRole("switch", "Offer Forgejo / Gitea when adding or publishing a project").tap();
  await app.screenshot("source-control-settings");
  await app.open("/");
  await screen.getByRole("button", "Add project", { exact: true }).tap();
  await screen.getByRole("combobox").fill("forgejo");
  await screen.getByRole("button", "Setup Required").hover();
  await screen.getByText("Authenticate this server with", { exact: false }).waitFor({ state: "visible" });
  await app.screenshot("forgejo-subpath-setup-hint");
  await expect(screen.getByText("Authenticate this server with", { exact: false })).toContainText("For a server under a URL subpath");
  await expect(screen.getByText("Authenticate this server with", { exact: false })).toContainText("tea login add");
});
