import { readFileSync } from "node:fs";
import { join } from "node:path";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("An encoded Azure link saved on base recovers without relinking", WEB, async ({ t3, app, screen, browser }) => {
  const thread = JSON.parse(readFileSync(join(process.env.FACTORY_RESTORE_HOME!, "thread.json"), "utf8"));
  await t3.open(thread);
  await screen.getByRole("textbox", "Message Encoded repository link").waitFor();
  await browser.keyboard.press("Control+k");
  await screen.getByPlaceholder("Search commands, projects, and threads...").fill("Show linked pull requests");
  await screen.getByText("Show linked pull requests", { exact: true }).tap();
  await screen.getByRole("dialog", "Command palette").waitFor({ state: "hidden" });
  await screen.getByRole("region", "Workspace panel: Pull requests").getByRole("link", { name: /323/ }).tap();
  try {
    await expect(screen.getByText("Verify encoded repository link", { exact: true }).first()).toBeVisible({ timeout: 60_000 });
    await screen.getByText("Encoding proof: this pull request should load for either spelling of the repository path.", { exact: true }).waitFor({ timeout: 60_000 });
    await expect(screen.getByRole("button", { name: /^Encoded repository link, Sample/ }).getByRole("link", { name: /Open: Verify encoded repository link/ })).toBeVisible({ timeout: 120_000 });
  } finally {
    await app.screenshot("saved-encoded-pull-request");
  }
});
