import './_planet.mjs';
import { test } from 'node:test';
import assert from 'node:assert/strict';
const { riverStats, riverMinOrder, cellSpacingKm } = await import('../js/riverlod.js');
// r5 reference world: total river length per Strahler order (km, from its rivers.geojson) and its cell spacing
const r5 = { length: { 1: 3177232, 2: 430077, 3: 141166, 4: 38607, 5: 8174, 6: 1273 } };
const cell = cellSpacingKm(5, 6371);
test('H3 res-5 cells on an Earth-sized planet lie ≈ 16 km apart', () => {
assert.ok(Math.abs(cell - 15.9) < 0.2, String(cell));
});
test('a phone at 26 km/px (the view that merged into blue) draws orders ≥ 3', () => {
assert.equal(riverMinOrder(26, r5, cell), 3); // order 3 ≈ 358 km apart ≥ 208; order 2 ≈ 117 km is not
});
test('closer in, smaller rivers join; first-order ones only when a cell spans ≥ 8 px', () => {
assert.equal(riverMinOrder(14, r5, cell), 2);
assert.equal(riverMinOrder(cell / 8, r5, cell), 1);
assert.equal(riverMinOrder(cell / 7, r5, cell), 2);
});
test('far out: only the largest rivers; no scale yet: the largest; no data: all', () => {
assert.equal(riverMinOrder(1e4, r5, cell), 6);
assert.equal(riverMinOrder(null, r5, cell), 6);
assert.equal(riverMinOrder(5, { length: {} }, cell), 1);
});
test('riverStats sums each order\'s length', async () => {
const line = (o, ...lons) => ({ properties: { order: o }, geometry: { coordinates: lons.map(x => [x, 0]) } });
const s = riverStats({ features: [line(1, 0, 1), line(1, 2, 3, 4), line(2, 0, 0.5)] });
const { distanceKm } = await import('../js/geo.js');
const deg = distanceKm({ lat: 0, lon: 0 }, { lat: 0, lon: 1 }); // km per degree on this planet's equator
assert.ok(Math.abs(s.length[1] - 3 * deg) < 1, String(s.length[1]));
assert.ok(Math.abs(s.length[2] - 0.5 * deg) < 1, String(s.length[2]));
});