# runtests.jl — ทดสอบสูตรฝั่ง Julia ด้วยชุดทดสอบร่วมเดียวกับ JS # julia --project=service service/test/runtests.jl # (ไม่ใช้ Test.jl แบบ suite เพื่อให้รันได้แม้ยังไม่ลง package เพิ่ม) include(joinpath(@__DIR__, "..", "src", "formulas.jl")) using JSON3 const VECTORS = JSON3.read(read(joinpath(@__DIR__, "..", "..", "tests", "golden", "vectors.json"), String)) const TOLERANCE = haskey(VECTORS, :tolerance) ? VECTORS[:tolerance] : 0.01 function run_case(test_case) input = test_case[:input] if test_case[:kind] == "crop_water" return Formulas.calculate_crop_water_demand( eto_mm_per_day = input[:eto_mm_per_day], crop_coefficient = input[:crop_coefficient], area_rai = input[:area_rai], irrigation_efficiency_percent = input[:irrigation_efficiency_percent], ) elseif test_case[:kind] == "reservoir" return Formulas.calculate_pond_volume( shape = get(input, :shape, "trapezoid"), top_length_m = input[:top_length_m], top_width_m = input[:top_width_m], depth_m = input[:depth_m], side_slope_horizontal_per_vertical = get(input, :side_slope_horizontal_per_vertical, 1.5), usable_depth_percent = input[:usable_depth_percent], catchment_rai = get(input, :catchment_rai, 0.0), rainfall_mm = get(input, :rainfall_mm, 0.0), runoff_coefficient = get(input, :runoff_coefficient, 0.8), ) end error("kind ไม่รู้จัก: $(test_case[:kind])") end failures = 0 for test_case in VECTORS[:cases] result = run_case(test_case) for (key, expected) in test_case[:expected] actual = key == :value ? result["value"] : result["details"][String(key)] ok = abs(Float64(actual) - Float64(expected)) <= TOLERANCE if ok println("ok $(test_case[:id]).$key = $actual") else global failures += 1 println("FAIL $(test_case[:id]).$key: ได้ $actual แต่ vectors กำหนด $expected") end end end if failures > 0 println("\nล้มเหลว $failures จุด") exit(1) end println("\nผ่านทั้งหมด — Julia ตรงกับ core/*.js ตาม golden vectors")