test_that("pip_data basic checks", {
  skip_if_offline()
  skip_on_cran()
  skip_on_ci()

  res <- pip_data(c("ZAF", "ZMB"))
  expect_s3_class(res, "data.frame")
  expect_gt(nrow(res), 0L)
})

test_that("pip CSV responses are trimmed and blanks become NA", {
  httr2::local_mocked_responses(function(req) {
    httr2::response(
      status_code = 200L,
      url = req$url,
      headers = list("content-type" = "text/csv"),
      body = charToRaw("country_code,region,value\nZAF , ,1.5\nZMB,SSA,\n")
    )
  })

  res <- pip_data("ZAF")
  expect_equal(res$country_code, c("ZAF", "ZMB"))
  expect_equal(res$region, c(NA, "SSA"))
  expect_equal(res$value, c(1.5, NA))
})

test_that("pip_data nowcast requires fill_gaps", {
  expect_error(pip_data(nowcast = TRUE), "fill_gaps")
})

test_that("PIP poverty inputs require finite numeric scalars", {
  invalid <- list("2.15", NA_real_, c(1, 2), Inf, TRUE)

  for (x in invalid) {
    expect_error(pip_data(povline = x))
    expect_error(pip_data(povline = NULL, popshare = x))
    expect_error(pip_cp(povline = x))
    expect_error(pip_group(povline = x))
    expect_error(pip_group(povline = NULL, popshare = x))
  }
})

test_that("PIP poverty inputs must be within the range accepted by the API", {
  expect_snapshot(error = TRUE, {
    pip_data(povline = -1)
    pip_data(povline = 2700.1)
    pip_data(povline = NULL, popshare = 1.5)
    pip_cp(povline = -1)
    pip_group(povline = NULL, popshare = 50)
  })
})

test_that("PIP version inputs reject values that are not 8 or 4 digits", {
  expect_snapshot(error = TRUE, {
    pip_data(release_version = "v20260324")
    pip_data(ppp_version = "2017a")
    pip_cp(release_version = 2026)
    pip_group(ppp_version = 20170101)
  })
})

test_that("pip_cp basic checks", {
  skip_if_offline()
  skip_on_cran()
  skip_on_ci()

  res <- pip_cp("ZAF")
  expect_s3_class(res, "data.frame")
  expect_gt(nrow(res), 0L)
})

test_that("pip_group basic checks", {
  skip_if_offline()
  skip_on_cran()
  skip_on_ci()

  res <- pip_group(c("AFE", "LAC"))
  expect_s3_class(res, "data.frame")
  expect_gt(nrow(res), 0L)
})

test_that("pip_versions basic checks", {
  skip_if_offline()
  skip_on_cran()
  skip_on_ci()

  res <- pip_versions()
  expect_s3_class(res, "data.frame")
  expect_gt(nrow(res), 0L)
})

test_that("pip_citation basic checks", {
  skip_if_offline()
  skip_on_cran()
  skip_on_ci()

  res <- pip_citation()
  expect_s3_class(res, "data.frame")
  expect_shape(res, dim = c(1L, 3L))
})

test_that("pip_aux basic checks", {
  skip_if_offline()
  skip_on_cran()
  skip_on_ci()

  res <- pip_aux()
  expect_vector(res, ptype = character())

  res <- pip_aux("countries")
  expect_s3_class(res, "data.frame")
  expect_gt(nrow(res), 0L)
})

test_that("pip_valid_params basic checks", {
  skip_if_offline()
  skip_on_cran()
  skip_on_ci()

  res <- pip_valid_params()
  expect_s3_class(res, "data.frame")
  expect_gt(nrow(res), 0L)
})

test_that("pip_info basic checks", {
  skip_if_offline()
  skip_on_cran()
  skip_on_ci()

  res <- pip_info()
  expect_type(res, "list")
  expect_gt(length(res), 0L)
  expect_named(res)
})

test_that("pip_health_check basic checks", {
  skip_if_offline()
  skip_on_cran()
  skip_on_ci()

  res <- pip_health_check()
  expect_vector(res, ptype = character(), size = 1L)
})
