test_that("example availability requires the requested Python exports", {
  module_has_attrs <- getFromNamespace(".weightederm_module_has_attrs", "weightederm")
  examples_available <- getFromNamespace(".weightederm_examples_available", "weightederm")
  module <- new.env(parent = emptyenv())
  module$WERMLeastSquares <- TRUE
  module$WERMLogistic <- TRUE

  expect_true(
    module_has_attrs(module, "WERMLeastSquares")
  )
  expect_false(
    module_has_attrs(
      module,
      c("WERMLeastSquares", "WERMHuber")
    )
  )
  expect_true(
    examples_available(
      "WERMLeastSquares",
      module = module
    )
  )
  expect_false(
    examples_available(
      "WERMHuber",
      module = module
    )
  )
})

test_that("example availability recognises attributes on a live Python module", {
  module_has_attrs <- getFromNamespace(".weightederm_module_has_attrs", "weightederm")
  examples_available <- getFromNamespace(".weightederm_examples_available", "weightederm")
  source_root <- normalizePath(test_path("..", ".."), winslash = "/", mustWork = TRUE)
  python_path <- file.path(
    dirname(source_root),
    "weightederm", ".venv", "bin", "python"
  )
  skip_if_not(file.exists(python_path), "project Python virtualenv not found")

  reticulate::use_python(python_path, required = TRUE)
  skip_if_not(
    reticulate::py_module_available("weightederm"),
    "weightederm Python package not available"
  )

  module <- reticulate::import("weightederm", delay_load = FALSE)

  expect_true(
    module_has_attrs(
      module,
      c("WERMLeastSquares", "WERMHuber", "WERMLogistic")
    )
  )
  expect_true(
    examples_available(
      c("WERMLeastSquares", "WERMHuber", "WERMLogistic"),
      module = module
    )
  )
})
