import { gameOverButtons } from "../../src/utils/gameover"

describe("gameOverButtons", () => {
  let originalSearch: string

  beforeAll(() => {
    originalSearch = globalThis.location?.search || ""
  })

  afterAll(() => {
    if (globalThis.history && globalThis.location) {
      globalThis.history.replaceState({}, "", originalSearch || "?")
    }
  })

  describe("rematch", () => {
    it("should suppress remote rematch parameters", () => {
      if (globalThis.history) {
        globalThis.history.replaceState({}, "", "?")
      }

      const html = gameOverButtons.rematch(
        "opponent-123",
        "Alice",
        "sagu",
        "turn-123"
      )
      expect(html).toBe("")
    })

    it("should ignore custom and transport parameters", () => {
      if (globalThis.history) {
        globalThis.history.replaceState(
          {},
          "",
          "?userId=me&userName=Me&tableId=t123&websocketserver=ws://localhost&tableSize=5&raceTo=5&first=true"
        )
      }

      const html = gameOverButtons.rematch(
        "opponent-123",
        "Alice",
        "sagu",
        "turn-123"
      )
      expect(html).toBe("")
    })
  })

  it("offers a local new game for every mode", () => {
    const html = gameOverButtons.forMode(
      false,
      "opponent-123",
      "Alice",
      "sagu",
      "turn-123"
    )
    expect(html).toContain('data-notification-action="reload"')
    expect(html).not.toContain("rematch")
    expect(html).not.toContain("lobby")
  })
})
