test_that("material flow indicators match hand calculation", { m <- material_flow(farm_flows) p <- m[m$stage == "packhouse", ] expect_equal(p$total_input, 1000 + 40) expect_equal(p$recovery_rate, 50 / 100) expect_equal(p$waste_intensity, 50 / 900) expect_equal(p$mci, 1 - (40 + 50) / (2 * 1040) * 0.9) s <- m[m$stage == "system", ] expect_equal(s$unaccounted_flow, sum(m$unaccounted_flow[1:5])) expect_true(all(m$mci >= 0 & m$mci <= 1)) m2 <- material_flow(farm_flows[1, ], utility = 2) expect_gt(m2$mci[1], m$mci[1]) expect_output(print(m), "Material-flow") }) test_that("mass balance violations are flagged", { bad <- farm_flows bad$product[1] <- 5000 expect_warning(material_flow(bad), "mass balance") expect_error(material_flow(farm_flows, product = "nope")) }) test_that("residue potential and valorisation are correct", { rp <- residue_potential(c("a", "b"), c(100, 200), c(1.5, 2), competing_use = c(0.5, 0), availability = 0.5) expect_equal(rp$generated, c(150, 400, 550)) expect_equal(rp$surplus, c(37.5, 200, 237.5)) rv <- residue_valorisation(valorisation_pathways, residue = 10, baseline = "open burning", carbon_price = 100) b <- valorisation_pathways[valorisation_pathways$pathway == "biochar", ] r <- rv[rv$pathway == "biochar", ] nv <- b$yield * b$price + b$avoided_cost - b$processing_cost - b$collection_cost expect_equal(r$net_value, nv) expect_equal(r$social_net_value, nv - 100 * b$emissions) expect_equal(r$emissions_avoided, 1.5 - b$emissions) expect_equal(r$abatement_cost, -nv / (1.5 - b$emissions)) expect_equal(r$total_net_value, 10 * nv) expect_true(all(diff(rv$social_net_value) <= 0)) expect_error(residue_valorisation(valorisation_pathways, baseline = "x")) })