/// <reference lib="dom" />
import { expect } from "e2e";
import { startSecondServer } from "./second-server.ts";
import { SCENARIO_MACHINE_ENV } from "./start-app.ts";
import { test, WEB } from "./t3.ts";

test("Canonically equivalent machine mentions offer Café", WEB, async ({t3, app, screen, browser}) => {
  const project = await t3.project("demo");
  await t3.thread({project, title: "Mention proof", messages: ["hello"]});
  SCENARIO_MACHINE_ENV.T3CODE_ENVIRONMENT_LABEL = "Café";
  const server = startSecondServer(app.baseUrl ?? "");
  try {
    const {host, token} = await server.ready;
    await t3.open("/settings/labs");
    await screen.getByRole("switch", "Linked environments").check();
    await app.open("/settings/connections");
    await screen.getByRole("button", "Add environment").tap();
    const dialog = screen.getByRole("dialog");
    await dialog.getByPlaceholder("backend.example.com").fill(host);
    await dialog.getByPlaceholder("PAIRCODE").fill(token);
    await dialog.getByRole("button", "Add environment").tap();
    await screen.getByRole("button", /^Café,/).waitFor({timeout: 60000});
    await t3.open();
    const editor = screen.getByRole("textbox");
    await editor.fill("@Café");
    await screen.getByRole("option", /Café.*Machine/).waitFor();
    await app.screenshot("composed-machine-mention");
    await editor.fill("");
    await editor.tap();
    await browser.evaluate(() => { document.execCommand("insertText", false, "@Cafe\u0301"); return null; });
    try {
      await expect(screen.getByRole("option", /Café.*Machine/)).toBeVisible();
    } finally {
      await app.screenshot("decomposed-machine-mention");
    }
  } finally { await server.stop(); }
});
