mirror of
https://github.com/bitbaum/substrata.git
synced 2026-12-04 06:08:02 +01:00
Material page (/resources/[slug]), one anatomy for every resource: at-a-glance tiles (world production and year, top producer and share, HHI with its method, exporters restricting it), production by country as a chartkit BarChart over the ranking table, reserves vs production as two ShareBars, year-over-year, the bottlenecks it holds up, export restrictions grouped by measure kind with counts, then each country as a <details> row (count and newest date in the summary; the top five open on desktop, all closed on a phone), events, sources. Country page (/world/[iso2]) through EntityProfile with country modules: header with roles as one scrolling row, at-a-glance tiles, what it produces as bars of world share (drawn against the whole world, so 1.6% looks like 1.6%), companies, rules, restrictions, events, related, discussion. /world?country=xx redirects there; the country entity links there. The Niger rule: a headline or a produces chart never leads with a share under 1% or a rank past 10th; those go under "Also produces, small shares". A measured uranium table (World Nuclear Association, fetched by scripts/research/wna-uranium.py into research/wna-uranium.json, joined like EIA) makes Niger read uranium first, 8th in the world. chartkit BarChart gains `max`, the value a full bar stands for. check:places-layout measures the four pages at 320/390/1440, light and dark, with chatkit's broken-word audit and chartkit's chart audit. Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE Co-authored-by: Claude <noreply@anthropic.com>
85 lines
3.6 KiB
TypeScript
85 lines
3.6 KiB
TypeScript
/**
|
|
* The USGS world tables, as the map and the country panel read them.
|
|
*
|
|
* The parser (scripts/research/usgs-mcs.py) already refuses to write a file
|
|
* whose rows do not add up to the world total. These tests pin what the
|
|
* TypeScript layer promises the map: a country USGS does not list is absent
|
|
* (not zero), a withheld figure is not a number, and a share of a "more than"
|
|
* total is not computed.
|
|
*/
|
|
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
|
|
import { chapters, countryRows, numeric, seriesOf, worldRow } from '../lib/resources/usgs';
|
|
import { choropleth, choroplethOptions } from '../lib/resources/choropleth';
|
|
import { RESOURCE_KINDS } from '../config/substrata-resources';
|
|
|
|
test('every USGS chapter maps to a Substrata resource and adds up to its world total', () => {
|
|
const ids = new Set(RESOURCE_KINDS.map((r) => r.id as string));
|
|
for (const chapter of chapters()) {
|
|
assert.ok(ids.has(chapter.resource), `${chapter.slug}: unknown resource ${chapter.resource}`);
|
|
assert.ok(
|
|
/^https:\/\/(pubs\.usgs\.gov|www\.eia\.gov|world-nuclear\.org)\//.test(chapter.url),
|
|
chapter.slug,
|
|
);
|
|
const world = worldRow(chapter);
|
|
assert.ok(world, `${chapter.slug}: no world row`);
|
|
for (const series of seriesOf(chapter)) {
|
|
if (series.measure !== 'production') continue;
|
|
for (const key of Object.values(series.keys)) {
|
|
const total = numeric(world.cells[key], series.unit, series.unit);
|
|
if (!total || world.cells[key].moreThan) continue;
|
|
if (chapter.rows.some((r) => r.cells[key]?.withheld)) continue;
|
|
const sum = chapter.rows
|
|
.filter((r) => r.kind !== 'world')
|
|
.reduce((s, r) => s + (numeric(r.cells[key], series.unit, series.unit) ?? 0), 0);
|
|
assert.ok(
|
|
Math.abs(sum - total) / total < 0.06,
|
|
`${chapter.slug} ${key}: ${sum} vs ${total}`,
|
|
);
|
|
}
|
|
}
|
|
for (const row of countryRows(chapter)) assert.match(row.iso2 ?? '', /^[a-z]{2}$/, row.name);
|
|
}
|
|
});
|
|
|
|
test('nickel: Indonesia leads, shares are value ÷ world total, unlisted countries are absent', () => {
|
|
const map = choropleth('nickel');
|
|
assert.ok(map);
|
|
assert.equal(map.year, 2025);
|
|
assert.equal(map.unit, 't');
|
|
const id = map.values.id;
|
|
assert.equal(id.rank, 1);
|
|
assert.equal(id.value, 2_600_000);
|
|
assert.ok(Math.abs((id.share ?? 0) - 2_600_000 / 3_900_000) < 1e-9);
|
|
assert.equal(map.values.ph.rank, 2);
|
|
assert.equal(map.values.ru.rank, 3);
|
|
assert.equal(map.values.tl, undefined, 'Timor-Leste is not in the USGS nickel table');
|
|
assert.ok(map.source.url.endsWith('mcs2026-nickel.pdf'));
|
|
assert.ok(map.hhi !== null && map.hhi > 0.4, 'nickel is concentrated in Indonesia');
|
|
});
|
|
|
|
test('withheld is not a number, and a "more than" world total yields no share', () => {
|
|
const lithium = choropleth('lithium');
|
|
assert.equal(lithium?.values.us.status, 'withheld');
|
|
assert.equal(lithium?.values.us.value, null);
|
|
assert.equal(lithium?.values.us.rank, null);
|
|
const reserves = choropleth('nickel', { measure: 'reserves' });
|
|
assert.equal(reserves?.world.moreThan, true);
|
|
assert.equal(reserves?.values.id.share, null);
|
|
assert.equal(reserves?.values.id.rank, 1);
|
|
});
|
|
|
|
test('bauxite ranks the mine, not the alumina refinery; unknown resources return null', () => {
|
|
const bauxite = choropleth('bauxite');
|
|
assert.equal(bauxite?.series, 'mine-bauxite');
|
|
assert.equal(bauxite?.values.gn.rank, 1);
|
|
assert.equal(choropleth('neon'), null);
|
|
assert.equal(
|
|
choropleth('natural-gas')?.values.ru.rank,
|
|
2,
|
|
'EIA dry gas: Russia second to the US',
|
|
);
|
|
assert.ok(choroplethOptions().some((o) => o.resource === 'gallium'));
|
|
});
|