/// <reference lib="dom" />
import type { IssueDetail, IssueRef } from "@t3tools/contracts";
import { expect } from "e2e";

import { test, WEB } from "./t3.ts";

test(
  "Assignee search ignores surrounding spaces",
  WEB,
  async ({ t3, app, browser, screen }) => {
    const project = await t3.project("demo");
    await t3.thread({ project, title: "Assignee search proof", messages: ["first"] });
    await browser.addInitScript(() => {
      const send = WebSocket.prototype.send;
      WebSocket.prototype.send = function (data) {
        const request: { _tag: string; id: string; tag: string; payload: IssueRef } | null =
          typeof data === "string" ? JSON.parse(data) : null;
        if (request?._tag !== "Request" || !["issues.detail", "issues.candidates"].includes(request.tag))
          return send.call(this, data);
        const reply = (exit: unknown) =>
          this.dispatchEvent(
            new MessageEvent("message", {
              data: JSON.stringify({ _tag: "Exit", requestId: request.id, exit }),
            }),
          );
        if (request.tag === "issues.candidates") {
          queueMicrotask(() => reply({ _tag: "Success", value: {
            labels: [], assignees: [
              { id: "alice", login: "alice", name: "Alice Example", avatarUrl: null },
              { id: "bob", login: "bob", name: "Bob Example", avatarUrl: null },
            ], truncated: false,
          } }));
          return;
        }
        const { projectId, tracker, scope, key } = request.payload;
        const detail: IssueDetail = {
          projectId,
          tracker,
          scope,
          key,
          host: "github.com",
          displayKey: "#42",
          projectTitle: "demo",
          title: "Publish the release notes",
          body: "Check the release notes.",
          url: "https://github.com/example/demo/issues/42",
          author: null,
          state: { category: "open", name: "Open" },
          createdAt: "2026-10-01T00:00:00.000Z",
          updatedAt: "2026-10-01T00:00:00.000Z",
          closedAt: null,
          assignees: [],
          labels: [],
          commentCount: 0,
          writable: true,
          capabilities: {
            create: false,
            edit: false,
            comment: false,
            labels: false,
            assignees: true,
            reactions: false,
            templates: false,
            linkedPullRequests: false,
            events: false,
            search: true,
          },
          viewerPermissions: {
            setState: false,
            comment: false,
            edit: false,
            assignees: true,
            labels: false,
          },
          states: [],
          viewer: null,
          comments: [],
          commentsCursor: null,
          events: [],
          linkedPullRequests: [],
        };
        queueMicrotask(() => reply({ _tag: "Success", value: detail }));
      };
    });

    await t3.open("/issues?selectedProjectId=demo&tracker=github&scope=example/demo&key=42");
    await screen.getByText("Publish the release notes", { exact: true }).waitFor();
    await screen.getByRole("combobox", "Change assignees", { exact: true }).tap();
    const search = screen.getByPlaceholder("Search people");
    await search.fill("alice");
    await screen.getByText("alice", { exact: true }).waitFor();
    await app.screenshot("exact-login");
    await search.fill(" alice ");
    await app.screenshot("spaced-login");
    await expect(screen.getByText("alice", { exact: true })).toBeVisible();
    await expect(screen.getByText("bob", { exact: true })).not.toBeVisible();
    await search.fill(" Alice Example ");
    await app.screenshot("spaced-name");
    await expect(screen.getByText("alice", { exact: true })).toBeVisible();
    await expect(screen.getByText("bob", { exact: true })).not.toBeVisible();
  },
);
