/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB, runPromise, rpcClient } from "./t3.ts";
import * as Effect from "effect/Effect";
import { ProviderInstanceId } from "@t3tools/contracts";
import { T3_HOME } from "../e2e.config.ts";
import { run } from "./shell.ts";

const TITLE = "Έλεγχος ασφάλειας & ποιότητας: ενημέρωση sitemap + καταγραφή ευρημάτων";
const URL = "https://github.com/Georgeaforest/forest-holding-site/pull/1";

test("Greek sigma has the same linked suggestion and title ranking", WEB, async ({t3, app, browser, screen}) => {
  const project = await t3.project("Sigma completion");
  await runPromise(run("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], {cwd:project}));
  const forest = await t3.project("Greek title repository");
  await runPromise(run("git", ["remote", "add", "origin", "https://github.com/Georgeaforest/forest-holding-site.git"], {cwd:forest}));
  await runPromise(Effect.scoped(Effect.gen(function* () {
    const client = yield* rpcClient(T3_HOME, app.baseUrl ?? "");
    yield* client["server.refreshProviders"]({instanceId:ProviderInstanceId.make("devReplay"),cwd:project,fresh:true,refreshModels:true});
  })));
  const thread = await t3.thread({project, title:"Greek sigma suggestions", messages:["Start"], model:"default"});
  await t3.open(thread);
  await screen.getByText("Start", {exact:true}).waitFor();
  await browser.keyboard.press("Control+k");
  await screen.getByRole("dialog", "Command palette").getByRole("combobox").fill("> Link pull request to thread");
  await screen.getByRole("dialog", "Command palette").getByText("Link pull request to thread", {exact:true}).tap();
  await screen.getByPlaceholder("Pull request URL or #42").fill(URL);
  await screen.getByPlaceholder("Pull request URL or #42").press("Enter");
  await screen.getByPlaceholder("Pull request URL or #42").waitFor({state:"detached"});
  const results: Record<string, boolean> = {};
  const settle = async () => {
    await browser.mouse.move(10, 400);
    await browser.evaluate(() => new Promise(resolve => setTimeout(resolve, 20000)));
  };
  for (const [name, query] of [["final", "έλεγχος"], ["medial", "έλεγχοσ"]]) {
    const composer = screen.getByRole("textbox");
    await composer.fill(`#${query}`);
    await settle();
    results[`composer-${name}`] = await browser.evaluate(() => [...document.querySelectorAll('[data-composer-command-drawer] [role="option"]')].some(e => e.textContent?.includes("georgeaforest/forest-holding-site")));
    await browser.keyboard.press("ArrowUp");
    await browser.evaluate(() => new Promise(resolve => setTimeout(resolve, 1000)));
    await app.screenshot(`composer-${name}-sigma-settled`);
    await composer.fill("");
  }
  for (const [name, query] of [["final", "έλεγχος"], ["medial", "έλεγχοσ"]]) {
    await t3.open(`/pull-requests?${new URLSearchParams({q:query,repository:"Georgeaforest/forest-holding-site"})}`);
    await screen.getByRole("searchbox", "Search pull requests").waitFor();
    await settle();
    results[`list-${name}-title-match`] = await browser.evaluate((title) => {
      const el = [...document.querySelectorAll('*')].find(e => e.textContent === title && e.children.length === 0);
      const row = el?.closest('button');
      return !!row && !row.textContent?.includes('matched in the description');
    }, TITLE);
    await app.screenshot(`list-${name}-sigma-title-ranking`);
  }
  expect(results).toEqual({"composer-final":true,"composer-medial":true,"list-final-title-match":true,"list-medial-title-match":true});
});
