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

test_that("treatment_cost_calculation works", {
  scenario_apprentissage <- create_scenario(
    system.file("input_1culture_2pop.xlsx", package = "ambre")
  )
  irrigation_need <- irrigation_need_calculation(scenario_apprentissage)
  result <- collective_treatment_cost_calculation(irrigation_need, grant = 0.5)
   
  expect_true(all(c("scenario", "allocation_key") %in% names(result))) 
  
  scenario_apprentissage <- create_scenario(
  system.file("input_1culture_2pop.xlsx", package = "ambre")
)
  irrigation_need <- irrigation_need_calculation(scenario_apprentissage)
  crop <- scenario_apprentissage$CropName |> unique()
  allocation_custom <- data.frame(CropName = crop,
                                allocation = c(0.5, 0.5))
  result <- collective_treatment_cost_calculation(scenario = irrigation_need, 
                                      grant = 0,
                                      allocation_key = allocation_custom)
  
  
  expect_equal(object = result$allocation_key,
               expected = structure(list(grant = c(0, 0), CropName = c("Tomato", "Corn seed"
), allocation = c(0.5, 0.5)), row.names = 1:2, class = "data.frame"))
  
  allocation_custom <- data.frame(allocation = c(0.5, 05))
  expect_error(object = collective_treatment_cost_calculation(scenario = irrigation_need, 
                                      grant = 0,
                                      allocation_key = allocation_custom), 
               regexp = "'allocation_key' must contain the following columns: CropName, allocation. Missing: CropName" )
})
