Files
sprinkler-calculation/tests/core.test.mjs
T
2026-11-16 01:39:59 +07:00

112 lines
3.8 KiB
JavaScript

/* tests/core.test.mjs — เคสอ้างอิงของสูตร (คำนวณคู่ขนานด้วยสคริปต์ Python แล้ว)
รัน: node --test tests/
*/
import test from "node:test";
import assert from "node:assert/strict";
import { calculateCropWaterDemand } from "../core/crop_water.js";
import { calculatePondVolume } from "../core/pond.js";
function close(actual, expected, tolerance = 0.01) {
assert.ok(
Math.abs(actual - expected) <= tolerance,
`ได้ ${actual} แต่ควรเป็น ${expected} (±${tolerance})`
);
}
test("น้ำที่พืชต้องการ: Kc 1.00 (พืชอ้างอิง) 10 ไร่ ประสิทธิภาพ 75%", () => {
const result = calculateCropWaterDemand({
eto_mm_per_day: 3.71,
crop_coefficient: 1.0,
area_rai: 10,
irrigation_efficiency_percent: 75
});
close(result.details.etc_mm_per_day, 3.71);
close(result.details.net_m3_per_day, 59.36);
close(result.value, 79.15);
close(result.details.gross_m3_per_week, 554.05, 0.05);
assert.equal(result.warnings.length, 0);
});
test("น้ำที่พืชต้องการ: Kc 0.85 25 ไร่ ประสิทธิภาพ 60%", () => {
const result = calculateCropWaterDemand({
eto_mm_per_day: 5.22,
crop_coefficient: 0.85,
area_rai: 25,
irrigation_efficiency_percent: 60
});
close(result.details.etc_mm_per_day, 4.44, 0.01);
close(result.details.net_m3_per_day, 177.48);
close(result.value, 295.8);
});
test("น้ำที่พืชต้องการ: ค่าไม่ถูกต้องต้อง throw", () => {
assert.throws(() => calculateCropWaterDemand({
eto_mm_per_day: 0,
crop_coefficient: 1,
area_rai: 10,
irrigation_efficiency_percent: 75
}), RangeError);
assert.throws(() => calculateCropWaterDemand({
eto_mm_per_day: 3,
crop_coefficient: 1,
area_rai: 10,
irrigation_efficiency_percent: 120
}), RangeError);
});
test("น้ำที่พืชต้องการ: Kc นอกช่วงต้องมีคำเตือน (ไม่ throw)", () => {
const result = calculateCropWaterDemand({
eto_mm_per_day: 4,
crop_coefficient: 1.8,
area_rai: 5,
irrigation_efficiency_percent: 80
});
assert.ok(result.warnings.some((text) => text.includes("Kc")));
});
test("สระน้ำ: ผนังตั้งฉาก 40×30 ลึก 4 น้ำใช้การ 80% + น้ำฝน 10 ไร่ 60 มม.", () => {
const result = calculatePondVolume({
shape: "box",
top_length_m: 40,
top_width_m: 30,
depth_m: 4,
usable_depth_percent: 80,
catchment_rai: 10,
rainfall_mm: 60,
runoff_coefficient: 0.8
});
close(result.details.excavated_volume_m3, 4800);
close(result.details.usable_volume_m3, 3840);
close(result.details.rain_harvest_m3, 768);
close(result.value, 4608);
});
test("สระน้ำ: ผนังลาด 1:1.5 ได้ปริมาตรน้อยกว่าผนังตั้งฉาก", () => {
const result = calculatePondVolume({
shape: "trapezoid",
top_length_m: 40,
top_width_m: 30,
depth_m: 4,
side_slope_horizontal_per_vertical: 1.5,
usable_depth_percent: 80
});
close(result.details.bottom_length_m, 28);
close(result.details.bottom_width_m, 18);
close(result.details.excavated_volume_m3, 3308.92, 0.05);
close(result.details.usable_volume_m3, 2415.97, 0.05);
close(result.value, 2415.97, 0.05);
});
test("สระน้ำ: ผนังลาดชันเกินขนาดปากบ่อต้อง throw", () => {
assert.throws(() => calculatePondVolume({
shape: "trapezoid",
top_length_m: 10,
top_width_m: 10,
depth_m: 5,
side_slope_horizontal_per_vertical: 2,
usable_depth_percent: 80
}), RangeError);
});