/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Repository keys omit passwords when the login contains @", WEB, async ({ t3, app, browser, screen }) => {
  for (const [name, remote] of [
    ["repo-https", "https://alice@corp.com:dummy-password@git.example.com/repo.git"],
    ["repo-ssh", "ssh://alice@corp.com:dummy-password@git.example.com/repo.git"],
    ["repo-ftp", "ftp://alice@corp.com:dummy-password@git.example.com/repo.git"],
    ["repo-path-at", "ssh://alice@corp.com:dummy-password@git.example.com/team/repo@home.git"],
  ]) {
    await t3.project(name, { ".git/config": `[remote "origin"]\n\turl = ${remote}\n` });
  }
  await t3.open();
  await browser.locator("[data-draft-project-trigger]").waitFor();
  await browser.locator("[data-draft-project-trigger]").tap();
  await screen.getByRole("option", "Add project").waitFor();
  const identities = await browser.evaluate(async () => {
    const { readProjects } = await import("/src/state/entities.ts");
    return readProjects().filter((p) => p.title.startsWith("repo-")).map((p) => ({
      title: p.title,
      canonicalKey: p.repositoryIdentity?.canonicalKey,
      displayName: p.repositoryIdentity?.displayName,
      owner: p.repositoryIdentity?.owner,
    })).sort((a, b) => a.title.localeCompare(b.title));
  });
  await browser.evaluate((rows) => {
    const panel = document.createElement("aside");
    panel.style.cssText = "position:fixed;z-index:2147483647;inset:24px 24px 24px 290px;background:#111827;color:#e5e7eb;border:1px solid #64748b;border-radius:12px;padding:28px;font:15px/1.65 monospace;overflow:auto";
    const title = document.createElement("h1");
    title.textContent = "Repository identities — live client data";
    title.style.cssText = "font:600 22px/1.4 system-ui;margin:0 0 12px";
    const note = document.createElement("p");
    note.textContent = "Read-only inspection of readProjects(). Synthetic login: alice@corp.com; synthetic password: dummy-password. Expected: no password in canonicalKey, displayName or owner; SSH login and repository-path @ retained.";
    note.style.cssText = "font:14px/1.5 system-ui;color:#a5b4fc;margin:0 0 22px";
    panel.append(title, note);
    for (const row of rows) {
      const heading = document.createElement("h2");
      heading.textContent = row.title;
      heading.style.cssText = "font:600 17px system-ui;color:#93c5fd;margin:20px 0 6px";
      const pre = document.createElement("pre");
      pre.style.cssText = "white-space:pre-wrap;overflow-wrap:anywhere;margin:0;font:14px/1.6 monospace";
      pre.textContent = `canonicalKey: ${row.canonicalKey}\ndisplayName: ${row.displayName}\nowner: ${row.owner ?? "(none)"}`;
      panel.append(heading, pre);
    }
    document.body.append(panel);
    return null;
  }, identities);
  await app.screenshot("repository-identity-live-client-data");
  expect(identities.map((p) => p.canonicalKey)).toEqual([
    "ftp://git.example.com/repo",
    "https://git.example.com/repo",
    "git.example.com/team/repo%40home",
    "ssh://alice@corp.com@git.example.com/repo",
  ]);
  expect(identities.every((p) => !JSON.stringify(p).includes("dummy-password"))).toBe(true);
});
