import * as fs from "node:fs/promises";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Root file URL keeps its repository folder in the clone destination", WEB, async ({ t3, app, screen, browser }) => {
  await fs.mkdir("/tmp/factory-pr-46552-evidence-1/clones", { recursive: true });
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Clone destination", messages: ["Hello"] });
  await t3.open("/settings/threads");
  const base = screen.getByRole("textbox", "Add project base directory", { exact: true });
  await base.fill("/tmp/factory-pr-46552-evidence-1/clones");
  await base.press("Enter");
  await t3.open(thread);
  await screen.getByTestId("composer-editor").waitFor();
  await screen.getByRole("combobox", /^Projects/).tap();
  await screen.getByRole("option", "Add project", { exact: true }).tap();
  const palette = screen.getByRole("dialog", "Command palette");
  await palette.getByRole("option", /^Git URL/).tap();
  await palette.getByRole("combobox").fill("file:///repo.git");
  await screen.getByRole("button", "Continue (Enter)").tap();
  await screen.getByRole("button", /^(?:Create & )?Clone \(/).waitFor();
  await palette.getByText("Loading folders...", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("root-file-url-destination");
  const rootDestination = await palette.getByRole("combobox").inputValue();
  await browser.keyboard.press("Escape");
  await palette.waitFor({ state: "hidden" });
  await t3.open(thread);
  await screen.getByTestId("composer-editor").waitFor();
  await screen.getByRole("combobox", /^Projects/).tap();
  await screen.getByRole("option", "Add project", { exact: true }).tap();
  await palette.getByRole("option", /^Git URL/).tap();
  await palette.getByRole("combobox").fill("file:///srv/repo.git");
  await screen.getByRole("button", "Continue (Enter)").tap();
  await screen.getByRole("button", /^(?:Create & )?Clone \(/).waitFor();
  await palette.getByText("Loading folders...", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("nested-file-url-control");
  expect(await palette.getByRole("combobox").inputValue()).toBe("/tmp/factory-pr-46552-evidence-1/clones/repo");
  expect(rootDestination).toBe("/tmp/factory-pr-46552-evidence-1/clones/repo");
});
