# ── track_erosion_depth ─────────────────────────────────────────────────────── test_that("track_erosion_depth returns correct structure", { data(erosion_profile, package = "soiltillr") result <- track_erosion_depth(erosion_profile, "year", "field_id", "erosion_depth_mm") expect_s3_class(result, "data.frame") expect_true(all(c("year", "field_id", "erosion_depth_mm", "change_mm", "cumulative_loss_mm", "trend") %in% names(result))) }) test_that("track_erosion_depth first year is baseline with NA change", { data(erosion_profile, package = "soiltillr") result <- track_erosion_depth(erosion_profile, "year", "field_id", "erosion_depth_mm") baselines <- result[result$trend == "baseline", ] expect_true(all(is.na(baselines$change_mm))) }) test_that("track_erosion_depth cumulative is non-decreasing", { data(erosion_profile, package = "soiltillr") result <- track_erosion_depth(erosion_profile, "year", "field_id", "erosion_depth_mm") for (fld in unique(result$field_id)) { sub <- result[result$field_id == fld, ] sub <- sub[order(sub$year), ] expect_true(all(diff(sub$cumulative_loss_mm) >= 0)) } }) test_that("track_erosion_depth detects improving trend", { df <- data.frame( year = 2020:2022, field_id = rep("A", 3), erosion_depth_mm = c(5.0, 3.0, 1.5) ) result <- track_erosion_depth(df, "year", "field_id", "erosion_depth_mm", validate = FALSE) expect_true(any(result$trend == "improving")) }) test_that("track_erosion_depth fails on invalid data", { df <- data.frame( year = 2020:2022, field_id = rep("A", 3), erosion_depth_mm = c(5, -1, 2) ) expect_error( track_erosion_depth(df, "year", "field_id", "erosion_depth_mm"), "Data validation failed" ) }) # ── estimate_soil_loss ──────────────────────────────────────────────────────── test_that("estimate_soil_loss returns correct structure", { data(erosion_profile, package = "soiltillr") result <- estimate_soil_loss(erosion_profile, "year", "field_id", "erosion_depth_mm", "bulk_density_g_cm3", slope_col = "slope_pct") expect_s3_class(result, "data.frame") expect_true(all(c("year", "field_id", "erosion_depth_mm", "estimated_loss_t_ha", "loss_category") %in% names(result))) }) test_that("estimate_soil_loss values are positive", { data(erosion_profile, package = "soiltillr") result <- estimate_soil_loss(erosion_profile, "year", "field_id", "erosion_depth_mm", "bulk_density_g_cm3") expect_true(all(result$estimated_loss_t_ha > 0)) }) test_that("estimate_soil_loss loss_category is valid", { data(erosion_profile, package = "soiltillr") result <- estimate_soil_loss(erosion_profile, "year", "field_id", "erosion_depth_mm", "bulk_density_g_cm3") valid_cats <- c("tolerable", "moderate", "severe", "very severe") expect_true(all(as.character(result$loss_category) %in% valid_cats)) }) test_that("estimate_soil_loss uses default bulk density when col missing", { df <- data.frame( year = 2020:2021, field_id = rep("A", 2), erosion_depth_mm = c(3.0, 2.5) ) expect_warning( result <- estimate_soil_loss(df, "year", "field_id", "erosion_depth_mm", "bd_missing"), "default" ) expect_true(all(result$estimated_loss_t_ha > 0)) }) # ── compare_fields ──────────────────────────────────────────────────────────── test_that("compare_fields returns one row per year", { data(erosion_profile, package = "soiltillr") result <- compare_fields(erosion_profile, "year", "field_id", "erosion_depth_mm", "organic_matter_pct") n_years <- length(unique(erosion_profile$year)) expect_equal(nrow(result), n_years) }) test_that("compare_fields includes difference column", { data(erosion_profile, package = "soiltillr") result <- compare_fields(erosion_profile, "year", "field_id", "erosion_depth_mm", "organic_matter_pct") expect_true("erosion_diff_mm" %in% names(result)) }) test_that("compare_fields fails with single field", { df <- data.frame( year = 2020:2022, field_id = rep("A", 3), erosion_depth_mm = c(3, 2.5, 2), om = c(2.8, 3.0, 3.2) ) expect_error( compare_fields(df, "year", "field_id", "erosion_depth_mm", "om"), "two fields" ) })