test_that("API key helpers are session-only and deterministic", { clear_stat_api_key("example") on.exit(clear_stat_api_key("example"), add = TRUE) expect_null(stat_api_key("example")) nm <- set_stat_api_key("example", "secret-value") expect_equal(nm, "STATFIDELITY_EXAMPLE_API_KEY") expect_equal(stat_api_key("example"), "secret-value") }) test_that("custom REST registration stores no key", { register_stat_api("example_nso", "https://api.example.gov/v1", search_path = "search", api_key_name = "token", overwrite = TRUE) x <- official_stat_providers() z <- x[x$id == "example_nso", , drop = FALSE] expect_equal(nrow(z), 1) expect_true(z$api_key) expect_false(any(grepl("secret", unlist(z), fixed = TRUE))) }) test_that("additional URL builders are deterministic", { expect_match(ons_search_url("unemployment"), "api.beta.ons.gov.uk/v1/search", fixed = TRUE) expect_match(ons_search_url("unemployment"), "q=unemployment", fixed = TRUE) expect_match(unsd_sdg_series_url(), "unstats.un.org/SDGAPI/v1/sdg/Series/List", fixed = TRUE) expect_match(unsd_sdg_data_url("SL_UEM_TOTL_ZS"), "seriesCode=SL_UEM_TOTL_ZS", fixed = TRUE) expect_match(bls_url("LNS14000000", latest = TRUE), "latest=true", fixed = TRUE) expect_match(imf_api_url("dataflow/IMF/all/latest"), "api.imf.org/external/sdmx/3.0/dataflow/IMF/all/latest", fixed = TRUE) }) test_that("UNSD fixture maps to canonical provider data", { p <- test_path("fixtures", "unsd_data.json") obj <- jsonlite::fromJSON(p, flatten = TRUE) dat <- AIofficial:::.sf_unsd_series_table(obj) out <- AIofficial:::.sf_unsd_to_provider(dat, "SL_UEM_TOTL_ZS", "fixture://unsd") expect_s3_class(out, "stat_provider_data") expect_equal(nrow(out), 2) expect_equal(out$.geo[1], "Spain") expect_equal(out$.time[1], "2025") expect_equal(out$.value[1], 10.4) }) test_that("BLS fixture maps to canonical provider data", { p <- test_path("fixtures", "bls.json") obj <- jsonlite::fromJSON(p, simplifyVector = FALSE) out <- AIofficial:::.sf_bls_to_provider(obj, "LNS14000000", "fixture://bls") expect_s3_class(out, "stat_provider_data") expect_equal(nrow(out), 1) expect_equal(out$.indicator_code[1], "LNS14000000") expect_equal(out$.value[1], 4.2) expect_equal(out$.time[1], "2026-M07") }) test_that("built-in provider registry advertises search capabilities", { x <- official_stat_providers(include_custom = FALSE) expect_true(all(c("eurostat", "worldbank", "oecd", "unsd_sdg", "ons", "bls", "imf") %in% x$id)) expect_true(x$text_search[x$id == "ons"]) expect_true(x$data_fetch[x$id == "unsd_sdg"]) })