- 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
53 lines
2.1 KiB
JavaScript
53 lines
2.1 KiB
JavaScript
/* 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})`
|
|
);
|
|
}
|
|
});
|
|
}
|