# Compare every .rds in baseline_new against baseline_orig for exact equality.
orig.dir <- 'test/baseline_orig'
new.dir  <- 'test/baseline_new'
files <- sort(list.files(orig.dir, pattern = "\\.rds$"))

norm <- function(x) {
  # Strip rownames (set by subsetting) so we compare values/structure, not row labels.
  strip <- function(d) { rownames(d) <- NULL; d }
  if (is.data.frame(x)) return(strip(x))
  if (is.list(x)) return(lapply(x, function(e) if (is.data.frame(e)) strip(e) else e))
  x
}

all.ok <- TRUE
for (f in files) {
  nm <- sub("\\.rds$", "", f)
  o <- norm(readRDS(file.path(orig.dir, f)))
  np <- file.path(new.dir, f)
  if (!file.exists(np)) { cat(sprintf("%-14s MISSING in new\n", nm)); all.ok <- FALSE; next }
  n <- norm(readRDS(np))
  eq <- isTRUE(all.equal(o, n))
  if (eq) {
    cat(sprintf("%-14s IDENTICAL\n", nm))
  } else {
    all.ok <- FALSE
    cat(sprintf("%-14s *** DIFFERS ***\n", nm))
    cat("   ", paste(utils::head(all.equal(o, n), 8), collapse = "\n    "), "\n")
  }
}
cat(if (all.ok) "\nALL IDENTICAL\n" else "\nDIFFERENCES FOUND\n")
