# ---- wid_convert -----------------------------------------------------------

test_that("wid_convert errors on non-wid_df and unsupported target", {
  expect_error(wid_convert(data.frame()), "wid_df")
  expect_error(wid_convert(.wid(), target = "xyz"))
})

test_that("wid_convert messages and returns unchanged for all-ratio series", {
  d <- .wid(type = "s")
  expect_message(out <- wid_convert(d, "usd"), "dimensionless")
  expect_equal(out$value, d$value)
})

test_that("wid_convert divides non-ratio values by exchange rate", {
  d  <- .wid(type = "a")
  xr <- .wid(type = "a"); xr$value <- rep(2, nrow(xr))
  with_mocked_bindings(download_wid = function(...) xr, {
    out <- wid_convert(d, "usd")
  }, .package = "widr")
  expect_equal(out$value, d$value / 2, tolerance = 1e-9)
})

test_that("wid_convert with base_year filters exchange rate to that year", {
  d  <- .wid(type = "a")
  xr <- rbind(.wid(type = "a"), .wid(type = "a"))
  xr$year <- c(rep("2019", nrow(xr) / 2), rep("2020", nrow(xr) / 2))
  xr$value <- 4
  with_mocked_bindings(download_wid = function(...) xr, {
    out <- wid_convert(d, "usd", base_year = "2020")
  }, .package = "widr")
  expect_equal(out$value, d$value / 4, tolerance = 1e-9)
})

test_that("wid_convert assigns NA when no matching exchange rate", {
  d  <- .wid(type = "a")
  xr <- .wid(type = "a"); xr$country <- "FR"
  with_mocked_bindings(download_wid = function(...) xr, {
    out <- wid_convert(d, "usd")
  }, .package = "widr")
  expect_true(all(is.na(out$value)))
})

test_that("wid_convert handles wid_df returned by download_wid", {
  d  <- .wid(type = "a")
  xr <- .wid(type = "a"); xr$value <- rep(4, nrow(xr))
  with_mocked_bindings(download_wid = function(...) xr, {
    out <- wid_convert(d, "usd")
  }, .package = "widr")
  expect_equal(out$value, d$value / 4, tolerance = 1e-9)
})

test_that("wid_convert stops when no exchange rates retrieved", {
  d <- .wid(type = "a")
  with_mocked_bindings(
    download_wid = function(...) widr:::.wid_df(),
    { expect_error(wid_convert(d, "usd"), "No exchange rates") },
    .package = "widr"
  )
})

# ---- wid_metadata ----------------------------------------------------------

test_that("wid_metadata errors on non-wid_df", {
  expect_error(wid_metadata(data.frame()), "wid_df")
})

test_that("wid_metadata deduplicates and returns data.frame", {
  dup <- do.call(rbind, rep(list(
    data.frame(variable="sptinc992j", country="US", source="s",
               stringsAsFactors=FALSE)), 2L))
  with_mocked_bindings(
    .get_metadata_variables = function(...) dup,
    { out <- wid_metadata(.wid()) },
    .package = "widr"
  )
  expect_equal(nrow(out), 1L)
})

test_that("wid_metadata deduplicates when result has no 'variable' column", {
  dup <- do.call(rbind, rep(list(
    data.frame(country="US", source="s", stringsAsFactors=FALSE)), 2L))
  with_mocked_bindings(
    .get_metadata_variables = function(...) dup,
    { out <- wid_metadata(.wid()) },
    .package = "widr"
  )
  expect_equal(nrow(out), 1L)
})

test_that("wid_metadata messages when empty result returned", {
  with_mocked_bindings(
    .get_metadata_variables = function(...) data.frame(),
    { expect_message(wid_metadata(.wid()), "No metadata") },
    .package = "widr"
  )
})

# ---- wid_tidy --------------------------------------------------------------

test_that("wid_tidy errors on non-wid_df", {
  expect_error(wid_tidy(data.frame()), "wid_df")
})

test_that("wid_tidy adds decoded columns with correct values", {
  out <- wid_tidy(.wid(), country_names = FALSE)
  expect_true(all(c("indicator","series_type","type_label") %in% names(out)))
  expect_equal(out$indicator[1L],   "sptinc")
  expect_equal(out$series_type[1L], "s")
  expect_equal(out$type_label[1L],  "share")
})

test_that("wid_tidy coerces year to integer and value to double", {
  out <- wid_tidy(.wid(), country_names = FALSE)
  expect_type(out$year,  "integer")
  expect_type(out$value, "double")
})

test_that("wid_tidy skips decode columns when decode=FALSE", {
  out <- wid_tidy(.wid(), decode = FALSE, country_names = FALSE)
  expect_false("indicator" %in% names(out))
})

test_that("wid_tidy joins country_name when country_names=TRUE", {
  out <- wid_tidy(.wid(), decode = FALSE, country_names = TRUE)
  expect_true("country_name" %in% names(out))
  expect_equal(out$country_name[1L],
               wid_countries$description[wid_countries$code == "US"])
})

test_that("wid_tidy sets indicator to NA for unrecognisable variable", {
  d <- .wid(); d$variable <- "ZZZZZZ999z"
  out <- wid_tidy(d, country_names = FALSE)
  expect_true(is.na(out$indicator[1L]))
})