# WARNING - Generated by {fusen} from dev/flat_scenario.Rmd: do not edit by hand # nolint: line_length_linter.

test_that("scenario works", {
  expect_true(inherits(create_scenario, "function"))
})

test_that("scenario returns a dataframe", {
  expect_true(is.data.frame(create_scenario(system.file("input_1culture_2pop.xlsx", package = "ambre"))))
})

test_that("create_scenario stop conditions work", {
  # Test non-character filepath
  expect_error(create_scenario(123), "filename must be a string")

  # Test non-existent filepath
  expect_error(create_scenario("nonexistent_file.xlsx"), "filename must exist")

  # Test invalid Area (not numeric)
  temp_file <- tempfile(fileext = ".xlsx")
  writexl::write_xlsx(data.frame(Area = "invalid", nb_population = 100), temp_file)
  expect_error(create_scenario(temp_file), "Area must be a numeric value")

  # Test Area out of range (too small)
  writexl::write_xlsx(data.frame(Area = 0, nb_population = 100), temp_file)
  expect_error(create_scenario(temp_file), "Area must be strictly greater than 0 ha and smaller than 10,000 ha")

  # Test Area out of range (too large)
  writexl::write_xlsx(data.frame(Area = 10000, nb_population = 100), temp_file)
  expect_error(create_scenario(temp_file), "Area must be strictly greater than 0 ha and smaller than 10,000 ha")
  
   # Test invalid nb_population (not numeric)
  temp_file <- tempfile(fileext = ".xlsx")
  writexl::write_xlsx(data.frame(Area = 0, nb_population = "invalid"), temp_file)
  expect_error(create_scenario(temp_file), "nb_population must be a numeric value")

  # Test nb_population out of range (too small)
  writexl::write_xlsx(data.frame(Area = 100, nb_population = 0), temp_file)
  expect_error(create_scenario(temp_file), "nb_population must be strictly greater than 0 and smaller than 5,000")

  # Test nb_population out of range (too large)
  writexl::write_xlsx(data.frame(Area = 100, nb_population = 5000), temp_file)
  expect_error(create_scenario(temp_file), "nb_population must be strictly greater than 0 and smaller than 5,000")

  # Clean up
  unlink(temp_file)
})

# Test that column names is correct
test_that("scenario column names are correct", {
  expect_equal(
    object = names(create_scenario(
      system.file("input_1culture_2pop.xlsx", package = "ambre")
    )),
    expected = c("CropName", "Area", "PopulationName", "nb_population", "PathName",                                                                                                                   
"STEPtreatmentName", "CollectiveTreatmentName", "InitialProcessName", 
"SupplementaryProcessName", "nb_day_decay", "config", "CropID", 
"CropHeight", "PopulationID", "PathID", "MatrixID", "STEPtreatmentID", 
"CollectiveTreatmentID", "InitialProcessID", "SupplementaryProcessID", 
"InitialTrainName", "InitialTrainID", "SupplementaryTrainName", 
"SupplementaryTrainID")
  )
})

# Test usage
test_that("return the right IDs associated to queries", {
  scenario_tested <- create_scenario(
    system.file("input_1culture_2pop.xlsx", package = "ambre")
  )
  expect_equal(object = scenario_tested$CropID,
               expected = c(6, 1))
  
  expect_equal(object = scenario_tested$PopulationID,
               expected = c(1, 2))
  
  expect_equal(object = scenario_tested$PathID,
               expected = c(1, 4))
  
  expect_equal(object = scenario_tested$STEPtreatmentID,
               expected = list(1, 1))
  
  expect_equal(object = scenario_tested$SupplementaryProcessID,
               expected = list(26, 12))
})

