import { expect } from "e2e";
import { run } from "./shell.ts";
import { test, WEB, runPromise } from "./t3.ts";
const TITLE = "界".repeat(99) + "𠀀";
test("GitLab reference search retains whole Unicode characters", WEB, async ({t3,app,browser,screen}) => {
 const project = await t3.project("Unicode references");
 await runPromise(run("git",["remote","add","origin","https://gitlab.com/julius54/unicode-reference-proof-1795830273.git"],{cwd:project}));
 await runPromise(run("git",["fetch","origin"],{cwd:project}));
 await runPromise(run("git",["checkout","-B","proof-2","origin/proof-2"],{cwd:project}));
 const thread = await t3.thread({project,title:"GitLab reference completion",messages:["Ready to review"]});
 await browser.setViewport({width:1280,height:900});
 await browser.addInitScript(() => {
  const send = WebSocket.prototype.send;
  (window as any).referenceSettled = new Promise<void>((resolve) => {
   WebSocket.prototype.send = function(data) {
    if (typeof data === "string") {
     const request = JSON.parse(data);
     if (request._tag === "Request" && request.tag.includes("filterOptions") && JSON.stringify(request.payload).includes("界".repeat(99))) {
      this.addEventListener("message", function finished(event) {
       const response = JSON.parse(String(event.data));
       if(response._tag === "Exit" && response.requestId === request.id) {
        this.removeEventListener("message", finished); resolve();
       }
      });
     }
    }
    return send.call(this,data);
   };
  });
 });
 await t3.open(thread);
 await screen.getByText("Second MR", {exact:true}).first().tap({timeout:60000});
 await screen.getByRole("button", "Comment on merge request", {exact:true}).tap({timeout:60000});
 const input=screen.getByPlaceholder("Leave a comment", {exact:true});
 await input.fill("!界界界");
 await screen.getByRole("option").filter({hasText:"界界界"}).first().waitFor({timeout:60000});
 await input.fill("!"+TITLE);
 await screen.getByText(/Couldn't search the host/).or(screen.getByRole("option").filter({hasText:TITLE})).first().waitFor({timeout:60000});
 await browser.evaluate(async () => { await (window as any).referenceSettled; });
 await app.screenshot("unicode-reference-completion");
 await expect(screen.getByRole("option").filter({hasText:TITLE}).first()).toBeVisible();
});
