test_that("cloudos.configure creates profile successfully", {
  # Skip if not in an interactive environment or CI
  skip_on_cran()
  skip_if_not(interactive(), "Skipping interactive test")
  
  # Create a temporary config directory for testing
  temp_config <- tempfile("cloudos_test_config")
  dir.create(temp_config, recursive = TRUE)
  
  # Set temp config directory
  old_env <- Sys.getenv("CLOUDOS_CONFIG_DIR", unset = NA)
  Sys.setenv(CLOUDOS_CONFIG_DIR = temp_config)
  
  on.exit({
    # Restore original environment
    if (is.na(old_env)) {
      Sys.unsetenv("CLOUDOS_CONFIG_DIR")
    } else {
      Sys.setenv(CLOUDOS_CONFIG_DIR = old_env)
    }
    # Clean up temp directory
    unlink(temp_config, recursive = TRUE)
  })
  
  # Configure profile
  expect_message(
    cloudos.configure(
      profilename = "test_profile",
      apikey = "test-key-12345",
      workspace_id = "test-workspace-67890",
      set_default = TRUE
    ),
    "configured successfully"
  )
  
  # Check config file exists
  config_file <- file.path(temp_config, ".cloudos_config.json")
  expect_true(file.exists(config_file))
})


test_that("cloudos.profile_list returns empty df when no profiles", {
  # Skip on CRAN
  skip_on_cran()
  
  # Create a temporary config directory for testing
  temp_config <- tempfile("cloudos_test_config")
  dir.create(temp_config, recursive = TRUE)
  
  # Set temp config directory
  old_env <- Sys.getenv("CLOUDOS_CONFIG_DIR", unset = NA)
  Sys.setenv(CLOUDOS_CONFIG_DIR = temp_config)
  
  on.exit({
    # Restore original environment
    if (is.na(old_env)) {
      Sys.unsetenv("CLOUDOS_CONFIG_DIR")
    } else {
      Sys.setenv(CLOUDOS_CONFIG_DIR = old_env)
    }
    # Clean up temp directory
    unlink(temp_config, recursive = TRUE)
  })
  
  expect_message(
    profiles <- cloudos.profile_list(),
    "No profiles configured"
  )
  
  expect_s3_class(profiles, "data.frame")
  expect_equal(nrow(profiles), 0)
})


test_that("load_profile fails with helpful error when no config", {
  # Skip on CRAN
  skip_on_cran()
  
  # Create a temporary config directory for testing
  temp_config <- tempfile("cloudos_test_config")
  dir.create(temp_config, recursive = TRUE)
  
  # Set temp config directory
  old_env <- Sys.getenv("CLOUDOS_CONFIG_DIR", unset = NA)
  Sys.setenv(CLOUDOS_CONFIG_DIR = temp_config)
  
  on.exit({
    # Restore original environment
    if (is.na(old_env)) {
      Sys.unsetenv("CLOUDOS_CONFIG_DIR")
    } else {
      Sys.setenv(CLOUDOS_CONFIG_DIR = old_env)
    }
    # Clean up temp directory
    unlink(temp_config, recursive = TRUE)
  })
  
  expect_error(
    cloudosR:::load_profile("test_profile"),
    "No configuration file found"
  )
})


test_that("validate_required_string catches empty strings", {
  expect_error(
    cloudosR:::validate_required_string("", "test_param"),
    "test_param is required"
  )
  
  expect_error(
    cloudosR:::validate_required_string(NULL, "test_param"),
    "test_param is required"
  )
})

