import { expect } from "e2e";
import { cp, mkdtemp, rm, symlink } from "node:fs/promises";
import { test, WEB } from "./t3.ts";

test("Link pull request validation follows German app language", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("plainfolder", { "README.md": "Synthetic example\n" });
  const outside = await mkdtemp("/tmp/factory-pr-44188-evidence-1/plain-");
  await rm(`${project}/.git`, { recursive: true });
  await cp(project, `${outside}/plainfolder`, { recursive: true });
  await rm(project, { recursive: true });
  await symlink(`${outside}/plainfolder`, project);
  const thread = await t3.thread({ project, title: "Pull request language", messages: ["hallo"], model: "showcase" });
  await t3.open("/settings/general");
  await screen.getByLabel(/^(Language|Sprache)$/, { exact: true }).waitFor({ timeout: 120_000 });
  await screen.getByLabel(/^(Language|Sprache)$/, { exact: true }).tap();
  await screen.getByText("Deutsch", { exact: true }).tap();
  await screen.getByLabel("Sprache", { exact: true }).waitFor();
  await t3.open(thread);
  await screen.getByText("This turn is a replay. It costs nothing, so send it again as often as you need.", { exact: true }).waitFor();
  await browser.keyboard.press("Control+k");
  await screen.getByRole("dialog").getByRole("combobox").fill("> Pull Request mit Thread verknüpfen");
  await screen.getByRole("dialog").getByText("Pull Request mit Thread verknüpfen", { exact: true }).tap();
  const input = screen.getByPlaceholder("Pull-Request-URL oder #42");
  await input.fill("123");
  await browser.keyboard.press("Enter");
  await screen.getByText(/(?:Paste the full pull request URL|Füge die vollständige Pull-Request-URL ein)/).waitFor();
  await app.screenshot("link-pr-number-error-german");
  await expect(screen.getByRole("dialog")).toContainText("Füge die vollständige Pull-Request-URL ein. Dieser Thread hat kein Repository, in dem die Nummer aufgelöst werden kann.");
});
