HardYards/web/world/js/tests/weather.selftest.js
m3ultra 1f99cd9bca Rain gets physical units for ponding; night pass on wildnight
SPRINT4 §Lane C 2/3/4.

PONDING DATA (decision 10). rainAt() was a dimensionless 0..1 — fine for drop
count and opacity, useless for water mass. Lane B would have had to invent the
mm/hr scale, which is exactly the "default-off code tuned by a constant I
invented" they rightly reverted. So the scale is storm data now:
rain.peakMmPerHour (validated 0..300; a rain curve without one is a hard error,
since ponding would silently use the default instead of what the author meant),
plus wind.rainMmPerHour(t) and rainDepthMm(t0,t1). RAIN_TIME_COMPRESSION=40 is
exported from weather.core so B applies it cloth-side rather than either of us
hardcoding 40 twice: how hard it rains is mine, how much water a sail holds is
theirs.

Calibrated to B's own arithmetic, and the numbers land on it:
  storm_02  80 mm/hr severe  -> 50.9 mm = 3.12 kN/corner   (B predicted 3.1)
  storm_03  30 mm/hr moderate -> 8.3 mm = 0.51 kN/corner   (teases a carabiner)
  storm_01   8 mm/hr shower   -> 0.9 mm = 0.06 kN/corner   (harmless, as designed)
against a storm_02 wind load of 0.2-1.1 kN. A flat rig should drown in the wild
night; a hypar pools nothing and won't notice. Asserted with B's arithmetic so
the storms are provably fit for their water before their cloth lands.

DECISION 7 helper for Lane A: sky.gardenExposure(bed, t) = rainAt x (1-shadow),
the whole drain term in one call. Note it moves on its own — the rain shadow
follows the wind, so the southerly change walks the dry patch off the bed and
the drain climbs with no corner having failed.

NIGHT PASS. sky.night is now the author's call (was: inferred from a darkness
threshold), and darkening scene.background did nothing anyway — the cloud dome
covers it at 0.85 opacity, so an overcast-grey texture was what you actually saw.
The dome now tints AND crushes (a lerp alone lands #717273: it runs in linear
space and the texture is baked near-white). Stops at 0.78 because the yard has no
lights and a storm you can't see is a black screen. Lightning now lights the
cloud it's inside (#3e3e3f -> #d4ddf2), and fires on the biggest gusts via
sky.lightningGustPow, not just the three authored strikes — driven off the
telegraph so the flash lands with the gust that earned it.

Also: c.test's storm list was hardcoded to two storms while the node runner globs
the directory, so storm_03 was untested in the browser half. Its own ponding
assert caught it.

Selftest 195/0/0 (was 184). Verified live: night reads as night, flash lights the
cloud, exposure responds.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 02:28:09 +10:00

524 lines
25 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
// SHADES — Lane C — weather selftest.
//
// Imports the pure core only (no THREE, no DOM), so this runs in node OR from
// Lane A's selftest.html. Fixed-dt loops, never rAF (rAF pauses in hidden tabs
// — PLAN3D §0).
//
// node web/world/js/tests/run-node.mjs
//
// Lane A: `import { runWeatherSuite } from './js/tests/weather.selftest.js'` and
// call it with the fetched storm defs.
import {
createWindField, validateStorm, gustEnvelope, GUST, RAIN_TIME_COMPRESSION,
} from '../weather.core.js';
const DT = 1 / 60;
// yard is ~30×20 m, origin at centre — probe the corners and the middle
const PROBES = [
{ x: 0, z: 0 }, { x: -14, z: -9 }, { x: 14, z: -9 },
{ x: -14, z: 9 }, { x: 14, z: 9 }, { x: 5, z: -3 },
];
// t1 and t2 from Lane A's landed yard (THREADS: "yard layout is now FACT")
const TREE_SHELTERS = [
{ x: -9, z: 2, radius: 3, strength: 0.45, length: 14 },
{ x: 8, z: -2, radius: 2.5, strength: 0.4, length: 12 },
];
/**
* The case list, defined once and run by two harnesses: run-node.mjs for a
* one-second tuning loop, and js/tests/c.test.js for Lane A's selftest.html at
* merge time. Cases are sync, matching testkit's Suite.test(label, fn).
*
* @param {Object<string, object>} storms parsed storm defs, keyed by name
* @returns {{cases: {name:string, fn:() => void}[], metrics: object}}
* `metrics` fills in as the cases run — read it after, not before.
*/
export function weatherCases(storms) {
const cases = [];
const metrics = {};
const test = (name, fn) => cases.push({ name, fn });
const assert = (cond, msg) => { if (!cond) throw new Error(msg); };
const defs = Object.entries(storms);
// ---- 1. gust telegraph lead (PLAN3D §5-C.5: always >= 1.2 s before ramp) ----
// The whole gust read is "you SEE it coming, then it hits". If the lead ever
// collapses, the storm stops being fair and starts being a dice roll.
for (const [name, def] of defs) {
test(`${name}: gust telegraph lead >= 1.2s`, () => {
const field = createWindField(def);
assert(field.gusts.length > 0, 'storm scheduled no gusts at all');
const step = 1 / 240;
let worst = Infinity;
for (const g of field.gusts) {
// when does THIS gust first actually push?
let rise = null;
for (let t = g.t0; t < g.endAt; t += step) {
if (gustEnvelope(t - g.t0, g.pow) > 1e-6) { rise = t; break; }
}
assert(rise !== null, `gust at t=${g.t0.toFixed(2)} never rises`);
// when did the HUD first get told about it?
let announced = null;
for (let t = Math.max(0, g.t0 - 2); t < rise; t += step) {
const tg = field.telegraph(t);
if (tg && Math.abs(tg.eta - (g.rampAt - t)) < 1e-6) { announced = t; break; }
}
assert(announced !== null, `gust at t=${g.t0.toFixed(2)} was never telegraphed`);
const lead = rise - announced;
worst = Math.min(worst, lead);
assert(lead >= 1.2,
`gust at t=${g.t0.toFixed(2)}: telegraph lead ${lead.toFixed(3)}s < 1.2s`);
// and the telegraph window must be silent — no force before the ramp
for (let t = g.t0; t < g.rampAt; t += step) {
assert(gustEnvelope(t - g.t0, g.pow) === 0,
`gust at t=${g.t0.toFixed(2)} pushes during its telegraph window`);
}
}
metrics[`${name}.worstTelegraphLead`] = +worst.toFixed(3);
});
}
// ---- 2. wind.sample continuity ----
// Sail load goes with wind², so a discontinuity here is an impulse that can
// snap a corner out of nowhere. Both axes matter: time (a standing player)
// and space (a running one).
const MAX_JUMP = 1.5; // m/s per 1/60 frame
for (const [name, def] of defs) {
test(`${name}: wind continuity in time (< ${MAX_JUMP} m/s per frame)`, () => {
const field = createWindField(def).setShelters(TREE_SHELTERS);
const a = { x: 0, y: 0, z: 0 }, b = { x: 0, y: 0, z: 0 };
let worst = 0, worstT = 0, worstP = null;
for (const p of PROBES) {
field.vecAt(p.x, p.z, 0, a);
for (let t = DT; t <= field.duration; t += DT) {
field.vecAt(p.x, p.z, t, b);
const d = Math.hypot(b.x - a.x, b.y - a.y, b.z - a.z);
if (d > worst) { worst = d; worstT = t; worstP = p; }
a.x = b.x; a.y = b.y; a.z = b.z;
}
}
metrics[`${name}.maxTemporalJump`] = +worst.toFixed(4);
assert(worst < MAX_JUMP,
`jump ${worst.toFixed(3)} m/s at t=${worstT.toFixed(2)} probe=(${worstP.x},${worstP.z})`);
});
test(`${name}: wind continuity in space (< ${MAX_JUMP} m/s per 0.1 m)`, () => {
const field = createWindField(def).setShelters(TREE_SHELTERS);
const a = { x: 0, y: 0, z: 0 }, b = { x: 0, y: 0, z: 0 };
let worst = 0, worstAt = null;
// sweep the yard at the storm's angriest moments, straight through both trees
for (const t of [10, 30, 57, 64, 80]) {
for (let z = -10; z <= 10; z += 1) {
field.vecAt(-15, z, t, a);
for (let x = -15 + 0.1; x <= 15; x += 0.1) {
field.vecAt(x, z, t, b);
const d = Math.hypot(b.x - a.x, b.y - a.y, b.z - a.z);
if (d > worst) { worst = d; worstAt = { x: +x.toFixed(1), z, t }; }
a.x = b.x; a.y = b.y; a.z = b.z;
}
}
}
metrics[`${name}.maxSpatialJump`] = +worst.toFixed(4);
assert(worst < MAX_JUMP,
`jump ${worst.toFixed(3)} m/s at ${JSON.stringify(worstAt)}`);
});
}
// ---- 3. storm JSON validator ----
for (const [name, def] of defs) {
test(`${name}: validates`, () => {
const { ok, errors } = validateStorm(def, name);
assert(ok, errors.join('; '));
});
}
// A validator that only ever says yes isn't a validator.
test('validator rejects broken storms', () => {
const base = () => JSON.parse(JSON.stringify(storms.storm_02_wildnight));
const cases = [
['duration missing', (d) => { delete d.duration; }],
['duration negative', (d) => { d.duration = -5; }],
['baseCurve absent', (d) => { delete d.baseCurve; }],
['baseCurve non-monotonic t', (d) => { d.baseCurve = [[0, 5], [50, 9], [20, 7], [90, 6]]; }],
['baseCurve negative speed', (d) => { d.baseCurve = [[0, 5], [90, -2]]; }],
['baseCurve ends before duration', (d) => { d.baseCurve = [[0, 5], [40, 9]]; }],
['dirCurve absent', (d) => { delete d.dirCurve; }],
['gusts absent', (d) => { delete d.gusts; }],
['gusts.minGap zero', (d) => { d.gusts.minGap = 0; }],
['gusts.maxGap < minGap', (d) => { d.gusts.minGap = 9; d.gusts.maxGap = 4; }],
['gusts overlap (minGap < gust length)', (d) => { d.gusts.minGap = 2; }],
['debris event with no model', (d) => { d.events = [{ t: 10, type: 'debris' }]; }],
['event with no type', (d) => { d.events = [{ t: 10 }]; }],
['windchange that dirCurve never delivers', (d) => {
d.dirCurve = [[0, 0.9], [90, 1.0]];
d.events = [{ t: 55, type: 'windchange', telegraph: 6 }];
}],
];
for (const [label, mutate] of cases) {
const d = base();
mutate(d);
const { ok } = validateStorm(d, 'broken');
assert(!ok, `validator ACCEPTED a storm with: ${label}`);
}
});
// ---- 4. determinism ----
// Everything downstream (selftest fast-forward, Lane B's byte-equal load
// traces) rests on this. Two builds of the same storm must be indiscernible.
test('same def + same seed => identical trace', () => {
const def = storms.storm_02_wildnight;
const a = createWindField(def).setShelters(TREE_SHELTERS);
const b = createWindField(def).setShelters(TREE_SHELTERS);
const va = { x: 0, y: 0, z: 0 }, vb = { x: 0, y: 0, z: 0 };
for (let t = 0; t <= def.duration; t += DT) {
for (const p of PROBES) {
a.vecAt(p.x, p.z, t, va);
b.vecAt(p.x, p.z, t, vb);
assert(va.x === vb.x && va.z === vb.z,
`diverged at t=${t.toFixed(3)} probe=(${p.x},${p.z})`);
}
}
assert(a.gusts.length === b.gusts.length, 'gust timelines differ in length');
a.gusts.forEach((g, i) => {
assert(g.t0 === b.gusts[i].t0 && g.pow === b.gusts[i].pow, `gust ${i} differs`);
});
});
test('different seed => different storm', () => {
const def = storms.storm_02_wildnight;
const a = createWindField(def, { seed: 1 });
const b = createWindField(def, { seed: 2 });
const same = a.gusts.length === b.gusts.length
&& a.gusts.every((g, i) => g.t0 === b.gusts[i].t0 && g.pow === b.gusts[i].pow);
assert(!same, 'seed is being ignored — every storm would be identical');
});
// ---- 5. sampling order must not matter ----
// sample() is called by sail/player/debris/rain in whatever order the frame
// happens to run. If it ever depends on call order, storms stop replaying.
test('sample order independent', () => {
const def = storms.storm_02_wildnight;
// same fixed t values, walked in two different orders, on two fields
const times = [0, 3.5, 17.3, 4.1, 55.0, 88.9, 63.2, 21.7, 39.9, 70.4];
const sorted = [...times].sort((a, b) => a - b);
const inOrder = createWindField(def).setShelters(TREE_SHELTERS);
const shuffled = createWindField(def).setShelters(TREE_SHELTERS);
const va = { x: 0, y: 0, z: 0 }, vb = { x: 0, y: 0, z: 0 };
const seen = new Map();
for (const t of sorted) {
inOrder.vecAt(3, -2, t, va);
seen.set(t, { x: va.x, z: va.z });
}
for (const t of times) { // deliberately out of order, and jumping backwards
shuffled.vecAt(3, -2, t, vb);
const want = seen.get(t);
assert(vb.x === want.x && vb.z === want.z,
`sampling order changed the wind at t=${t}: ${vb.x},${vb.z} vs ${want.x},${want.z}`);
}
});
// ---- 6. the storms are what the design says they are (PLAN3D §7) ----
// storm_02 has to be able to destroy a flat cheap rig; storm_01 must not.
test('storm_02 is genuinely violent, storm_01 is not', () => {
const wild = createWindField(storms.storm_02_wildnight);
const gentle = createWindField(storms.storm_01_gentle);
const peak = (f) => {
let mx = 0, mxBase = 0;
for (let t = 0; t <= f.duration; t += DT) {
mx = Math.max(mx, f.uniformSpeed(t));
mxBase = Math.max(mxBase, f.uniformSpeed(t) - f.gustOnly(t));
}
return { mx, mxBase };
};
const w = peak(wild), g = peak(gentle);
metrics['storm_02.peakGustSpeed'] = +w.mx.toFixed(2);
metrics['storm_02.peakSustained'] = +w.mxBase.toFixed(2);
metrics['storm_01.peakGustSpeed'] = +g.mx.toFixed(2);
metrics['storm_01.peakSustained'] = +g.mxBase.toFixed(2);
assert(w.mx >= 30, `storm_02 peaks at only ${w.mx.toFixed(1)} m/s — won't break a cheap rig`);
assert(w.mxBase >= 18, `storm_02 sustained peaks at only ${w.mxBase.toFixed(1)} m/s`);
assert(g.mx <= 15, `storm_01 peaks at ${g.mx.toFixed(1)} m/s — too wild for the gentle storm`);
assert(w.mx > g.mx * 2, 'storm_02 should be far worse than storm_01');
});
// ---- 7. wind change actually swings the wind ----
test('storm_02 southerly change swings the wind', () => {
const f = createWindField(storms.storm_02_wildnight);
const ev = (storms.storm_02_wildnight.events || []).find((e) => e.type === 'windchange');
assert(ev, 'storm_02 has no windchange event');
const before = f.dirAt(ev.t - 5);
const after = f.dirAt(ev.t + 8);
const swing = Math.abs(after - before);
metrics['storm_02.changeSwingRad'] = +swing.toFixed(3);
assert(swing > 0.9, `change only swings ${swing.toFixed(2)} rad — should be a real slew`);
// and the player must be warned before it lands
assert((ev.telegraph ?? 0) >= 4, 'windchange telegraph is too short to react to');
});
// ---- 8. shelters ----
test('tree wind shadow bites downwind and nowhere else', () => {
const def = storms.storm_02_wildnight;
const bare = createWindField(def);
const shad = createWindField(def).setShelters([{ x: 0, z: 0, radius: 3, strength: 0.5, length: 14 }]);
const t = 30;
const d = bare.dirAt(t);
const dx = Math.cos(d), dz = Math.sin(d);
const lee = { x: dx * 5, z: dz * 5 }; // 5 m downwind of the tree
const luv = { x: -dx * 5, z: -dz * 5 }; // 5 m upwind
const leeS = shad.speedAt(lee.x, lee.z, t), leeB = bare.speedAt(lee.x, lee.z, t);
const luvS = shad.speedAt(luv.x, luv.z, t), luvB = bare.speedAt(luv.x, luv.z, t);
metrics['shelter.leeDrop'] = +(1 - leeS / leeB).toFixed(3);
assert(leeS < leeB * 0.85, `lee side only dropped to ${(leeS / leeB).toFixed(2)}× — shadow too weak`);
assert(Math.abs(luvS - luvB) < 1e-9, 'upwind side is being sheltered — shadow is pointing the wrong way');
});
// ---- 9. vertical structure (SPRINT3 decision 8: fraction of TOTAL) ----
// Cloth pressure goes with dot(wind, normal). A flat panel's normal points at
// the sky, so in a purely horizontal wind that dot is ~0 and "lie it flat and
// ignore the storm" wins — the opposite of the game. The downdraft is now a
// fraction of the LOCAL total wind speed (was: gust power), so a flat roof is
// pressed whenever it's windy, not only at gust peaks. Lane B owns the
// cloth-side no-free-lunch assert; these are the wind side.
test('downdraft is a fixed fraction of the local horizontal speed', () => {
const def = storms.storm_02_wildnight;
const f = createWindField(def);
const frac = def.gusts.downdraftOfTotal;
assert(Math.abs(f.downFrac - frac) < 1e-12, `field downFrac ${f.downFrac} != json ${frac}`);
const out = { x: 0, y: 0, z: 0 };
let peakDown = 0;
for (const p of PROBES) {
for (let t = 0; t <= f.duration; t += DT) {
f.vecAt(p.x, p.z, t, out);
const horiz = Math.hypot(out.x, out.z);
assert(out.y <= 1e-9, `vertical went UP (${out.y.toFixed(3)}) at t=${t.toFixed(2)} — downdraft only`);
// out.y must be exactly -frac * horizontal, everywhere, always
assert(Math.abs(out.y + frac * horiz) < 1e-9,
`downdraft ${out.y.toFixed(3)} != -${frac}×${horiz.toFixed(3)} at t=${t.toFixed(2)}`);
peakDown = Math.min(peakDown, out.y);
}
}
metrics['storm_02.peakDowndraft'] = +peakDown.toFixed(2);
// Held at downdraftOfTotal 0.15 → ~-4.9 m/s; target 0.45 → ~-14.7. Floor at
// -3 so this proves "a real downdraft exists" across the whole transition
// range without false-failing when the joint step bumps the value.
assert(peakDown < -3, `peak downdraft only ${peakDown.toFixed(2)} m/s — a flat sail would still shrug it off`);
});
test('downdraft rides the wind: present when windy, gone when calm', () => {
// The point of fraction-of-total: it's not a gust-only feature any more. Some
// sustained-wind moment between gusts must still carry a real downdraft, and a
// hypothetically dead-calm field must carry none.
const f = createWindField(storms.storm_02_wildnight);
let sustainedDown = 0;
for (let t = 0; t <= f.duration; t += DT) {
const inGust = f.gusts.some((g) => t > g.t0 && t < g.endAt);
if (!inGust) sustainedDown = Math.min(sustainedDown, f.verticalAt(0, 0, t));
}
assert(sustainedDown < -2,
`between gusts the downdraft peaks at only ${sustainedDown.toFixed(2)} — total-speed semantics should keep it pressing`);
// dead calm → no downdraft (guards against a constant offset sneaking in)
const calm = createWindField({
duration: 10, baseCurve: [[0, 0], [10, 0]], dirCurve: [[0, 0], [10, 0]],
gusts: { minGap: 6, maxGap: 6, powBase: 0, powRand: 0, powRamp: 0, downdraftOfTotal: 0.5 },
});
for (let t = 0; t <= 10; t += 0.1) {
assert(Math.abs(calm.verticalAt(0, 0, t)) < 1e-9, `air is falling in a dead calm at t=${t.toFixed(1)}`);
}
});
test('downdraft follows the tree shadow (shelters from falling air too)', () => {
const def = storms.storm_02_wildnight;
const f = createWindField(def).setShelters([{ x: 0, z: 0, radius: 3, strength: 0.5, length: 14 }]);
const t = 30;
const d = f.dirAt(t);
const dx = Math.cos(d), dz = Math.sin(d);
const leeDown = Math.abs(f.verticalAt(dx * 5, dz * 5, t)); // downwind of the tree
const openDown = Math.abs(f.verticalAt(-dx * 5, -dz * 5, t)); // upwind, unsheltered
assert(leeDown < openDown * 0.85, `lee downdraft ${leeDown.toFixed(2)} not sheltered vs open ${openDown.toFixed(2)}`);
});
test('downdraftOfTotal 0 gives a perfectly horizontal wind', () => {
const def = JSON.parse(JSON.stringify(storms.storm_02_wildnight));
def.gusts.downdraftOfTotal = 0;
const f = createWindField(def);
const out = { x: 0, y: 0, z: 0 };
for (let t = 0; t <= f.duration; t += 0.05) {
f.vecAt(2, -1, t, out);
assert(out.y === 0, `y=${out.y} at t=${t.toFixed(2)} with downdraft 0 — the opt-out leaks`);
}
});
test('downdraft does not re-time the storm', () => {
// The vertical carries NO rng draws of its own now (it's a pure function of
// local speed), so tuning it cannot possibly shift gust times or powers. Lane
// A hand-drove storm_02 and watched the carabiner blow at t=45.4 and p2
// cascade at t=56; a downdraft tweak silently moving those would be a nasty
// way to lose an afternoon. Determinism is now structural, but still asserted.
const base = storms.storm_02_wildnight;
const a = createWindField(base);
for (const dd of [0, 0.1, 0.22, 0.5, 1]) {
const d = JSON.parse(JSON.stringify(base));
d.gusts.downdraftOfTotal = dd;
const b = createWindField(d);
assert(a.gusts.length === b.gusts.length, `downdraft ${dd} changed the gust count`);
a.gusts.forEach((g, i) => {
assert(g.t0 === b.gusts[i].t0,
`downdraft ${dd} moved gust ${i} from t=${g.t0.toFixed(3)} to ${b.gusts[i].t0.toFixed(3)}`);
assert(g.pow === b.gusts[i].pow, `downdraft ${dd} changed gust ${i}'s power`);
});
// and the HORIZONTAL wind must be byte-identical regardless of downdraft
assert(a.speedAt(3, -2, 47.3) === b.speedAt(3, -2, 47.3), `downdraft ${dd} changed the horizontal wind`);
}
});
test('speedAt stays horizontal — a wind meter does not read falling air', () => {
const f = createWindField(storms.storm_02_wildnight);
const out = { x: 0, y: 0, z: 0 };
for (const p of PROBES) {
for (const t of [12, 40, 60, 75.3]) {
f.vecAt(p.x, p.z, t, out);
assert(Math.abs(f.speedAt(p.x, p.z, t) - Math.hypot(out.x, out.z)) < 1e-9,
`speedAt != horizontal magnitude of sample at t=${t}`);
}
}
});
test('validator rejects a bad downdraft and the renamed field', () => {
for (const dd of [-0.1, 1.5, NaN, 'lots']) {
const d = JSON.parse(JSON.stringify(storms.storm_02_wildnight));
d.gusts.downdraftOfTotal = dd;
assert(!validateStorm(d, 'broken').ok, `validator ACCEPTED downdraftOfTotal = ${dd}`);
}
// the old gust-only field must be rejected, not silently re-meant
const legacy = JSON.parse(JSON.stringify(storms.storm_02_wildnight));
delete legacy.gusts.downdraftOfTotal;
legacy.gusts.downdraft = 0.3;
assert(!validateStorm(legacy, 'legacy').ok, 'validator silently accepted the pre-SPRINT3 downdraft field');
});
// ---- 10. the ponding story (SPRINT4 decision 10) ----
// Lane B owns water-on-cloth; these assert the RAIN DATA can tell the story
// their mass model needs, using their own arithmetic, before their cloth lands.
// B measured: 5 cm over a 25 m² flat sail = 1250 kg = 3.1 kN/corner, against a
// storm_02 wind load of only 0.21.1 kN. So depth is the whole mechanic.
const FLAT_AREA = 25; // m², B's reference flat sail
const KILL_DEPTH_MM = 50; // 5 cm — B's 3.1 kN/corner
/** Water on a flat sail over a whole storm, at decision 10's compression. */
const stormDepthMm = (def) => {
const f = createWindField(def);
return f.rainDepthMm(0, f.duration) * RAIN_TIME_COMPRESSION;
};
/** B's arithmetic: mm over an area → kN per corner (4 corners, 1000 kg/m³). */
const kNPerCorner = (mm) => (mm / 1000) * FLAT_AREA * 1000 * 9.81 / 4 / 1000;
test('storm_02 rain can drown a flat rig; storm_01 rain cannot', () => {
const wild = stormDepthMm(storms.storm_02_wildnight);
const gentle = stormDepthMm(storms.storm_01_gentle);
metrics['storm_02.pondDepth_mm'] = +wild.toFixed(1);
metrics['storm_02.pond_kN_per_corner'] = +kNPerCorner(wild).toFixed(2);
metrics['storm_01.pondDepth_mm'] = +gentle.toFixed(2);
metrics['storm_01.pond_kN_per_corner'] = +kNPerCorner(gentle).toFixed(3);
assert(wild >= KILL_DEPTH_MM * 0.9,
`storm_02 only delivers ${wild.toFixed(1)} mm — Lane B needs ~${KILL_DEPTH_MM} mm to drown a flat rig`);
// and it must dwarf the wind it's competing with (B: 0.21.1 kN/corner)
assert(kNPerCorner(wild) > 1.5,
`storm_02 ponding is only ${kNPerCorner(wild).toFixed(2)} kN/corner — no stronger than the wind`);
// the gentle storm must be unable to hurt anything, or the ramp is a lie
assert(kNPerCorner(gentle) < 0.3,
`storm_01 ponds ${kNPerCorner(gentle).toFixed(2)} kN/corner — a light shower must not threaten a rig`);
assert(wild > gentle * 20, 'the wild night should deliver vastly more water than a shower');
});
test('storm_03 ponding sits between the other two', () => {
const mid = stormDepthMm(storms.storm_03_southerly);
const wild = stormDepthMm(storms.storm_02_wildnight);
const gentle = stormDepthMm(storms.storm_01_gentle);
metrics['storm_03.pondDepth_mm'] = +mid.toFixed(1);
metrics['storm_03.pond_kN_per_corner'] = +kNPerCorner(mid).toFixed(2);
assert(mid > gentle * 3 && mid < wild * 0.5,
`storm_03 delivers ${mid.toFixed(1)} mm — wanted a real middle rung between ${gentle.toFixed(1)} and ${wild.toFixed(1)}`);
// it should threaten a carabiner (1.2 kN) once wind is added, not a shackle (3.2)
assert(kNPerCorner(mid) > 0.25 && kNPerCorner(mid) < 1.2,
`storm_03 ponds ${kNPerCorner(mid).toFixed(2)} kN/corner — should tease a cheap rig, not drown a decent one`);
});
test('rain depth integrates monotonically and matches its curve', () => {
const f = createWindField(storms.storm_02_wildnight);
// depth only ever accumulates
let prev = 0;
for (let t = 1; t <= f.duration; t += 1) {
const d = f.rainDepthMm(0, t);
assert(d >= prev - 1e-9, `rain depth went BACKWARDS at t=${t}: ${d} < ${prev}`);
prev = d;
}
// splitting the interval must give the same water (no double-count, no gap)
const whole = f.rainDepthMm(0, 90);
const split = f.rainDepthMm(0, 30) + f.rainDepthMm(30, 60) + f.rainDepthMm(60, 90);
assert(Math.abs(whole - split) < 0.05,
`depth(0,90)=${whole.toFixed(3)} but the three thirds sum to ${split.toFixed(3)}`);
// dead calm before the rain starts
assert(f.rainDepthMm(0, 0) === 0, 'zero-length interval delivered water');
assert(f.rainDepthMm(10, 5) === 0, 'a backwards interval delivered water');
});
test('rainMmPerHour tracks rainAt against the storm scale', () => {
for (const [name, def] of defs) {
if (!def.rain || !def.rain.curve) continue;
const f = createWindField(def);
const peak = def.rain.peakMmPerHour;
assert(Number.isFinite(peak), `${name} has a rain curve but no peakMmPerHour`);
for (let t = 0; t <= f.duration; t += 2.5) {
const want = f.rainAt(t) * peak;
assert(Math.abs(f.rainMmPerHour(t) - want) < 1e-9,
`${name}: rainMmPerHour ${f.rainMmPerHour(t)} != rainAt×peak ${want} at t=${t}`);
}
// rainAt stays a 0..1 intensity — skyfx uses it for drop count and opacity
for (let t = 0; t <= f.duration; t += 2.5) {
const r = f.rainAt(t);
assert(r >= 0 && r <= 1, `${name}: rainAt ${r} outside 0..1 at t=${t}`);
}
}
});
test('validator rejects a rain curve with no scale, and a silly scale', () => {
const noScale = JSON.parse(JSON.stringify(storms.storm_02_wildnight));
delete noScale.rain.peakMmPerHour;
assert(!validateStorm(noScale, 'x').ok, 'validator accepted a rain curve with no mm/hr scale — ponding would silently use the default');
for (const mm of [-1, 5000, NaN]) {
const d = JSON.parse(JSON.stringify(storms.storm_02_wildnight));
d.rain.peakMmPerHour = mm;
assert(!validateStorm(d, 'x').ok, `validator accepted peakMmPerHour=${mm}`);
}
const hot = JSON.parse(JSON.stringify(storms.storm_02_wildnight));
hot.rain.curve = [[0, 0], [45, 3], [90, 0]];
assert(!validateStorm(hot, 'x').ok, 'validator accepted rain intensity above 1 — the scale is peakMmPerHour, not the curve');
});
return { cases, metrics };
}
/** Run every case and collect results. Used by run-node.mjs. */
export function runWeatherSuite(storms) {
const { cases, metrics } = weatherCases(storms);
const results = cases.map((c) => {
try {
c.fn();
return { name: c.name, ok: true };
} catch (e) {
return { name: c.name, ok: false, err: e.message };
}
});
const pass = results.filter((r) => r.ok).length;
return { suite: 'weather', pass, fail: results.length - pass, results, metrics };
}