# Tests for IsingEntropy() and its deprecated alias IsingEntrophy().
# On a small 3-node model, IsingEntropy() must return a finite, non-negative
# number, and the deprecated IsingEntrophy() must return the identical value
# while emitting a deprecation warning.
# Plain stopifnot tests, no package dependencies beyond IsingSampler itself.

library("IsingSampler")

set.seed(4321)

## Small test model -----------------------------------------------------------
N <- 3
graph <- matrix(0, N, N)
graph[upper.tri(graph)] <- runif(N * (N - 1) / 2, -0.5, 1)
graph <- graph + t(graph)
thresholds <- runif(N, -1, 1)

## 1) IsingEntropy() returns a finite non-negative number ----------------------
ent <- IsingEntropy(graph, thresholds)
stopifnot(is.numeric(ent), length(ent) == 1L)
stopifnot(is.finite(ent))
stopifnot(ent >= 0)

## 2) The deprecated IsingEntrophy() returns the identical value ---------------
entOld <- suppressWarnings(IsingEntrophy(graph, thresholds))
stopifnot(isTRUE(all.equal(ent, entOld)))

## 3) IsingEntrophy() emits a (deprecation) warning ---------------------------
warned <- FALSE
invisible(withCallingHandlers(
  IsingEntrophy(graph, thresholds),
  warning = function(w) {
    warned <<- TRUE
    invokeRestart("muffleWarning")
  }
))
stopifnot(warned)

## 4) delta != 0 exercises the Blume-Capel path and changes the entropy --------
## Use three (non-symmetric) response options so 'delta' has an identifiable
## effect (with two response options delta is absorbed into the thresholds).
graph3 <- matrix(0, N, N)
graph3[upper.tri(graph3)] <- runif(N * (N - 1) / 2, -0.5, 1)
graph3 <- graph3 + t(graph3)
thr3 <- runif(N, -1, 1)

ent0   <- IsingEntropy(graph3, thr3, responses = c(0L, 1L, 2L), delta = 0)
entHalf <- IsingEntropy(graph3, thr3, responses = c(0L, 1L, 2L), delta = 0.5)
stopifnot(is.finite(ent0), is.finite(entHalf))
stopifnot(!isTRUE(all.equal(ent0, entHalf)))

## 5) NodeInformation() returns non-negative mutual information -----------------
NI <- NodeInformation(graph, thresholds)
stopifnot(is.numeric(NI), length(NI) == ncol(graph))
stopifnot(all(is.finite(NI)))
# Mutual information is non-negative (allow tiny numerical tolerance):
stopifnot(all(NI > -1e-10))

cat("All entropy tests passed.\n")
