package de.nowchess.bot

import de.nowchess.api.board.{Color, File, Rank, Square}
import de.nowchess.api.game.GameContext
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers

class PolyglotHashTest extends AnyFunSuite with Matchers:

  test("Initial position produces consistent hash"):
    val ctx = GameContext.initial
    val hash1 = PolyglotHash.hash(ctx)
    val hash2 = PolyglotHash.hash(ctx)
    hash1 shouldEqual hash2

  test("Hash changes when turn changes"):
    val ctx = GameContext.initial
    val hash1 = PolyglotHash.hash(ctx)
    val ctxBlackTurn = ctx.withTurn(Color.Black)
    val hash2 = PolyglotHash.hash(ctxBlackTurn)
    hash1 should not equal hash2

  test("Hash changes when castling rights change"):
    val ctx = GameContext.initial
    val hash1 = PolyglotHash.hash(ctx)
    val noCastling = ctx.withCastlingRights(
      de.nowchess.api.board.CastlingRights(false, false, false, false)
    )
    val hash2 = PolyglotHash.hash(noCastling)
    hash1 should not equal hash2

  test("Hash changes when en passant square changes"):
    val ctx = GameContext.initial
    val hash1 = PolyglotHash.hash(ctx)
    val withEP = ctx.withEnPassantSquare(Some(Square(File.E, Rank.R3)))
    val hash2 = PolyglotHash.hash(withEP)
    hash1 should not equal hash2

  test("Different en passant files produce different hashes"):
    val ctx = GameContext.initial
    val epFileE = ctx.withEnPassantSquare(Some(Square(File.E, Rank.R3)))
    val epFileD = ctx.withEnPassantSquare(Some(Square(File.D, Rank.R3)))
    val hash1 = PolyglotHash.hash(epFileE)
    val hash2 = PolyglotHash.hash(epFileD)
    hash1 should not equal hash2

  test("Removing en passant changes hash"):
    val ctx = GameContext.initial
    val withEP = ctx.withEnPassantSquare(Some(Square(File.E, Rank.R3)))
    val hash1 = PolyglotHash.hash(withEP)
    val noEP = withEP.withEnPassantSquare(None)
    val hash2 = PolyglotHash.hash(noEP)
    hash1 should not equal hash2
