Add Julia+Postgres calculation service with shared golden vectors
- service/: Julia HTTP API (HTTP.jl + LibPQ), formulas.jl as source of truth - service/db/001_init.sql: calculation_runs (append-only), provinces, eto_monthly - tests/golden/vectors.json: single fixture set enforced on both JS and Julia - .gitignore: keep secrets (DB env, contracts, financial records, ransomware note) out - AGENTS.md: document service commands and formula-change rule
This commit is contained in:
1 parent
dd232b1c02
commit
dd65e9061f
14 files changed
+1467
No files matched your search
@@ -0,0 +1,52 @@
|
||||
/* tests/golden.test.mjs — เทียบ core/*.js กับชุดทดสอบร่วม tests/golden/vectors.json
|
||||
ชุดเดียวกับที่ service/test/runtests.jl ใช้ → ถ้าสองฝั่งเพี้ยนจากกัน test จะ fail
|
||||
รัน: node --test tests/*
|
||||
*/
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
import { calculateCropWaterDemand } from "../core/crop_water.js";
|
||||
import { calculatePondVolume } from "../core/pond.js";
|
||||
|
||||
const document = JSON.parse(readFileSync(new URL("./golden/vectors.json", import.meta.url), "utf8"));
|
||||
const TOLERANCE = document.tolerance ?? 0.01;
|
||||
|
||||
function runCase(testCase) {
|
||||
const input = testCase.input;
|
||||
if (testCase.kind === "crop_water") {
|
||||
return calculateCropWaterDemand({
|
||||
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
|
||||
});
|
||||
}
|
||||
if (testCase.kind === "reservoir") {
|
||||
return calculatePondVolume({
|
||||
shape: input.shape,
|
||||
top_length_m: input.top_length_m,
|
||||
top_width_m: input.top_width_m,
|
||||
depth_m: input.depth_m,
|
||||
side_slope_horizontal_per_vertical: input.side_slope_horizontal_per_vertical,
|
||||
usable_depth_percent: input.usable_depth_percent,
|
||||
catchment_rai: input.catchment_rai,
|
||||
rainfall_mm: input.rainfall_mm,
|
||||
runoff_coefficient: input.runoff_coefficient
|
||||
});
|
||||
}
|
||||
throw new Error("kind ไม่รู้จัก: " + testCase.kind);
|
||||
}
|
||||
|
||||
for (const testCase of document.cases) {
|
||||
test(`golden: ${testCase.id}`, () => {
|
||||
const result = runCase(testCase);
|
||||
for (const [key, expected] of Object.entries(testCase.expected)) {
|
||||
const actual = key === "value" ? result.value : result.details[key];
|
||||
assert.ok(
|
||||
Math.abs(actual - expected) <= TOLERANCE,
|
||||
`${testCase.id}.${key}: ได้ ${actual} แต่ vectors กำหนด ${expected} (±${TOLERANCE})`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
{
|
||||
"schema_version": "1.0",
|
||||
"description_th": "ชุดทดสอบร่วม (golden vectors) — ผลลัพธ์ต้องตรงกันทั้ง core/*.js และ service/src/formulas.jl",
|
||||
"tolerance": 0.01,
|
||||
"cases": [
|
||||
{
|
||||
"id": "crop_water_reference_crop",
|
||||
"kind": "crop_water",
|
||||
"input": {
|
||||
"eto_mm_per_day": 3.71,
|
||||
"crop_coefficient": 1.0,
|
||||
"area_rai": 10,
|
||||
"irrigation_efficiency_percent": 75
|
||||
},
|
||||
"expected": {
|
||||
"value": 79.15,
|
||||
"etc_mm_per_day": 3.71,
|
||||
"net_m3_per_day": 59.36,
|
||||
"gross_m3_per_week": 554.03
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "crop_water_drip_efficiency",
|
||||
"kind": "crop_water",
|
||||
"input": {
|
||||
"eto_mm_per_day": 5.22,
|
||||
"crop_coefficient": 0.85,
|
||||
"area_rai": 25,
|
||||
"irrigation_efficiency_percent": 60
|
||||
},
|
||||
"expected": {
|
||||
"value": 295.8,
|
||||
"etc_mm_per_day": 4.44,
|
||||
"net_m3_per_day": 177.48,
|
||||
"gross_m3_per_month": 8874.0
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "pond_box_with_rain",
|
||||
"kind": "reservoir",
|
||||
"input": {
|
||||
"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
|
||||
},
|
||||
"expected": {
|
||||
"value": 4608.0,
|
||||
"excavated_volume_m3": 4800.0,
|
||||
"usable_volume_m3": 3840.0,
|
||||
"rain_harvest_m3": 768.0
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "pond_trapezoid_slope",
|
||||
"kind": "reservoir",
|
||||
"input": {
|
||||
"shape": "trapezoid",
|
||||
"top_length_m": 40,
|
||||
"top_width_m": 30,
|
||||
"depth_m": 4,
|
||||
"side_slope_horizontal_per_vertical": 1.5,
|
||||
"usable_depth_percent": 80
|
||||
},
|
||||
"expected": {
|
||||
"value": 2415.97,
|
||||
"excavated_volume_m3": 3308.92,
|
||||
"usable_volume_m3": 2415.97,
|
||||
"bottom_length_m": 28.0,
|
||||
"bottom_width_m": 18.0
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user