import { execFileSync } from "node:child_process";
import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { test, WEB } from "./t3.ts";

test("SSH host aliases sync the same GitHub pull requests", WEB, async ({ t3, app, screen }) => {
  for (const host of ["github.com", "github-work", "gh-work"]) {
    await t3.project(host, {
      ".git/config": `[remote "origin"]\nurl = git@${host}:pingdotgg/t3code.git\nfetch = +refs/heads/*:refs/remotes/origin/*\n`,
    });
  }
  const rows = JSON.parse(execFileSync("sqlite3", ["-readonly", "-json", `${T3_HOME}/userdata/statev2.sqlite`, "SELECT project_id, title FROM projection_projects WHERE deleted_at IS NULL"], { encoding: "utf8" }));
  const alias = rows.find((row: { title: string }) => row.title === "gh-work");
  await t3.open(`/pull-requests?projectId=${alias.project_id}&involvement=all&state=open&sort=newest`);
  try {
    await expect(screen.getByText(/^#\d+$/).first()).toBeVisible({ timeout: 90_000 });
  } finally {
    await app.screenshot("ssh-alias-inbox");
  }
});
