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

test("Grouped palette projects use the nested favicon", WEB, async ({ t3, app, browser, screen }) => {
  const mono = await t3.project("mono", {
    "apps/web/public/favicon.svg": '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><rect width="32" height="32" rx="5" fill="#ef2020"/></svg>',
  });
  execFileSync("git", ["remote", "add", "origin", "https://github.com/example/mono.git"], { cwd: mono });
  const outside = await t3.project("server");
  const thread = await t3.thread({ project: outside, title: "Outside the group", messages: ["Ready"] });
  await t3.open(thread);
  await screen.getByRole("textbox", "Message Outside the group").waitFor();
  const palette = screen.getByRole("dialog", "Command palette");
  await browser.keyboard.press("Control+k");
  await palette.getByRole("combobox").fill("> Add project");
  await palette.getByText("Add project", { exact: true }).tap();
  await palette.getByText("Local folder", { exact: true }).tap();
  await palette.getByRole("combobox").fill(`${mono}/apps/web`);
  await browser.keyboard.press("Enter");
  await palette.waitFor({ state: "hidden" });
  await t3.open(thread);
  await screen.getByRole("textbox", "Message Outside the group").waitFor();
  await screen.getByRole("combobox", "Projects", { exact: true }).tap();
  await screen.getByText("example/mono", { exact: true }).waitFor();
  await app.screenshot("sidebar-project-picker");
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("Control+k");
  await palette.getByRole("combobox").fill("mono");
  await palette.getByText("example/mono", { exact: true }).first().waitFor();
  await browser.evaluate(() => {
    const walker = document.createTreeWalker(document.querySelector('[role="dialog"]')!, NodeFilter.SHOW_TEXT);
    while (walker.nextNode()) { const n = walker.currentNode; if (n.textContent?.includes("/home/")) n.textContent = "[synthetic workspace]"; }
    return null;
  });
  await app.screenshot("palette-project-search");
  const search = await browser.evaluate(() => [...document.querySelectorAll('[role="dialog"] [role="option"]')].filter(e => (e.textContent?.trim().startsWith("example/mono") || e.textContent?.trim().startsWith("New thread in example/mono"))).map(e => ({ text: e.textContent, icon: !!e.querySelector('img') })));
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("Control+k");
  await palette.getByRole("combobox").fill("> New thread in...");
  await palette.getByText("New thread in...", { exact: true }).tap();
  await palette.getByText("example/mono", { exact: true }).waitFor();
  await browser.evaluate(() => {
    const walker = document.createTreeWalker(document.querySelector('[role="dialog"]')!, NodeFilter.SHOW_TEXT);
    while (walker.nextNode()) { const n = walker.currentNode; if (n.textContent?.includes("/home/")) n.textContent = "[synthetic workspace]"; }
    return null;
  });
  await app.screenshot("palette-new-thread-projects");
  const submenu = await browser.evaluate(() => [...document.querySelectorAll('[role="dialog"] [role="option"]')].filter(e => (e.textContent?.trim().startsWith("example/mono") || e.textContent?.trim().startsWith("New thread in example/mono"))).map(e => !!e.querySelector('img')));
  expect(search.length > 0 && search.every(e => e.icon) && submenu.length > 0 && submenu.every(Boolean)).toBe(true);
});
