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

test("Reopening an issue restores its shareable URL and survives reload", WEB, async ({ t3, app, browser, screen }) => {
  await t3.project("demo");
  await browser.addInitScript(() => {
    const issue = {
      projectId: "demo", tracker: "github", host: "github.com", scope: "example/demo",
      key: "42", displayKey: "#42", projectTitle: "demo", title: "Retain reopened issue selection",
      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,
    };
    const send = WebSocket.prototype.send;
    WebSocket.prototype.send = function(data) {
      const request = typeof data === "string" ? JSON.parse(data) : null;
      if (request?._tag !== "Request" || !["issues.list", "issues.detail"].includes(request.tag)) return send.call(this, data);
      const value = request.tag === "issues.list" ? {
        entries: [issue], errors: [], nextCursors: {}, viewers: {},
        trackers: [{ tracker: "github", host: "github.com", projectCount: 1, capabilities: null, configured: true, detail: null }],
      } : {
        ...issue, body: "Reopening this tab should preserve issue 42 in the URL, including after reload.",
        writable: false, capabilities: { create: false, edit: false, comment: false, labels: false,
          assignees: false, reactions: false, templates: false, linkedPullRequests: false, events: false, search: true },
        viewerPermissions: { setState: false, comment: false, edit: false, labels: false, assignees: false },
        states: [], viewer: null, comments: [], commentsCursor: null, events: [], linkedPullRequests: [],
      };
      queueMicrotask(() => this.dispatchEvent(new MessageEvent("message", { data: JSON.stringify({
        _tag: "Exit", requestId: request.id, exit: { _tag: "Success", value },
      }) })));
    };
  });
  await t3.open("/issues");
  await screen.getByText("Retain reopened issue selection", { exact: true }).waitFor({ timeout: 60_000 });
  await screen.getByText("Retain reopened issue selection", { exact: true }).tap();
  await screen.getByText("Reopening this tab should preserve issue 42 in the URL, including after reload.", { exact: true }).waitFor();
  await browser.keyboard.press("Control+w");
  await screen.getByText("Reopening this tab should preserve issue 42 in the URL, including after reload.", { exact: true }).waitFor({ state: "hidden" });
  await browser.keyboard.press("Control+Shift+t");
  await screen.getByText("Reopening this tab should preserve issue 42 in the URL, including after reload.", { exact: true }).waitFor();
  const reopenedUrl = await browser.url();
  await app.screenshot("reopened-issue");
  await app.open(reopenedUrl);
  await screen.getByRole("searchbox", "Search issues").waitFor({ timeout: 60_000 });
  await app.screenshot("after-reload");
  expect(JSON.parse(new URL(reopenedUrl).searchParams.get("key") ?? "null")).toBe("42");
  await expect(screen.getByText("Reopening this tab should preserve issue 42 in the URL, including after reload.", { exact: true })).toBeVisible();
});
