Files
worldmonitor/tests/seed-eia-petroleum.test.mjs
Elie Habib 64c906a406 feat(eia): gold-standard /api/eia/petroleum (Railway seed → Redis → Vercel reads only) (#3161)
* feat(eia): move /api/eia/petroleum to gold-standard (Railway seed → Redis → Vercel reads only)

Live api.eia.gov fetches from the Vercel edge function were causing
FUNCTION_INVOCATION_TIMEOUT 504s on /api/eia/petroleum (Sydney edge →
US origin with no timeout, no cache, no stale fallback — one EIA blip
blew the 25s budget).

- New seeder scripts/seed-eia-petroleum.mjs — fetches WTI/Brent/
  production/inventory from api.eia.gov with per-fetch 15s timeouts,
  writes energy:eia-petroleum:v1 with the {_seed, data} envelope.
  Accepts 1-of-4 series; 0-of-4 routes to contract-mode RETRY so
  seed-meta stays stale and the bundle retries on next cron.
- Bundled into seed-bundle-energy-sources.mjs (daily, 90s timeout) —
  no new Railway service needed.
- Rewrote api/eia/[[...path]].js as a Redis-only reader via
  readJsonFromUpstash. Same response shape for backward compat with
  widgets/MCP/external callers. 503 + Retry-After on miss (never 504).
- Registered eiaPetroleum in api/health.js STANDALONE_KEYS + gated as
  ON_DEMAND_KEYS for the deploy window; promote to SEED_META
  (maxStaleMin: 4320) in a follow-up after ~7 days of clean cron.
- Tests: 14 seeder unit tests + 9 edge handler tests.

Audit result: /api/eia/petroleum was the only Vercel route fetching
dashboard data live. Every other fetch(https://…) in api/ is
auth/payments/notifications/user-initiated enrichment.

* fix(eia): close silent-stale window — add SEED_META + seed-health registration

Review finding on PR #3161: without a SEED_META entry, readSeedMeta
returns seedStale: null and classifyKey never reaches STALE_SEED.
That meant a broken Railway cron or missing EIA_API_KEY after the first
successful seed would keep /api/eia/petroleum serving stale data for
up to 7 days (TTL) while /api/health reported OK.

- api/health.js: add SEED_META.eiaPetroleum with maxStaleMin=4320
  (72h = 3× daily bundle cadence). Keep eiaPetroleum in ON_DEMAND_KEYS
  so the Vercel-instant / Railway-delayed deploy window doesn't CRIT
  on first seed, but stale-after-seed now properly fires STALE_SEED.
- api/seed-health.js: register energy:eia-petroleum in SEED_DOMAINS
  (intervalMin=1440) so the secondary health endpoint reports it too.
- Updated ON_DEMAND_KEYS comment to reflect freshness is now enforced.
2026-04-18 14:40:00 +04:00

112 lines
4.0 KiB
JavaScript

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import {
CANONICAL_KEY,
SERIES,
parseSeries,
countSeries,
validatePetroleum,
declareRecords,
} from '../scripts/seed-eia-petroleum.mjs';
describe('seed-eia-petroleum constants', () => {
it('CANONICAL_KEY is versioned under energy:', () => {
assert.equal(CANONICAL_KEY, 'energy:eia-petroleum:v1');
});
it('SERIES maps the four expected indicators to EIA series ids', () => {
assert.deepEqual(Object.keys(SERIES).sort(), ['brent', 'inventory', 'production', 'wti']);
assert.equal(SERIES.wti, 'PET.RWTC.W');
assert.equal(SERIES.brent, 'PET.RBRTE.W');
assert.equal(SERIES.production, 'PET.WCRFPUS2.W');
assert.equal(SERIES.inventory, 'PET.WCESTUS1.W');
});
});
describe('parseSeries', () => {
const shape = (values) => ({ response: { data: values } });
it('returns current/previous/date/unit from a 2-row response', () => {
const parsed = parseSeries(shape([
{ value: '76.23', period: '2026-04-11', unit: 'dollars per barrel' },
{ value: '75.10', period: '2026-04-04', unit: 'dollars per barrel' },
]));
assert.deepEqual(parsed, {
current: 76.23,
previous: 75.10,
date: '2026-04-11',
unit: 'dollars per barrel',
});
});
it('falls back to null previous when only one value is returned', () => {
const parsed = parseSeries(shape([
{ value: '76.23', period: '2026-04-11', unit: 'dollars per barrel' },
]));
assert.equal(parsed?.current, 76.23);
assert.equal(parsed?.previous, null);
});
it('coerces numeric values expressed as strings', () => {
const parsed = parseSeries(shape([{ value: '13100', period: '2026-04-11', unit: 'MBBL' }]));
assert.equal(parsed?.current, 13100);
assert.equal(typeof parsed?.current, 'number');
});
it('returns null when response.data is missing', () => {
assert.equal(parseSeries(undefined), null);
assert.equal(parseSeries({}), null);
assert.equal(parseSeries({ response: {} }), null);
});
it('returns null when response.data is empty', () => {
assert.equal(parseSeries(shape([])), null);
});
it('returns null when the first value is non-numeric', () => {
assert.equal(parseSeries(shape([{ value: 'N/A', period: '2026-04-11', unit: 'x' }])), null);
});
it('tolerates a non-numeric previous and returns null for it', () => {
const parsed = parseSeries(shape([
{ value: '76.23', period: '2026-04-11', unit: 'u' },
{ value: 'withheld', period: '2026-04-04', unit: 'u' },
]));
assert.equal(parsed?.current, 76.23);
assert.equal(parsed?.previous, null);
});
});
describe('countSeries + validatePetroleum + declareRecords', () => {
const point = { current: 1, previous: 0, date: '2026-04-11', unit: 'u' };
it('countSeries returns 0 for null/undefined/empty', () => {
assert.equal(countSeries(null), 0);
assert.equal(countSeries(undefined), 0);
assert.equal(countSeries({}), 0);
});
it('countSeries counts only present series', () => {
assert.equal(countSeries({ wti: point }), 1);
assert.equal(countSeries({ wti: point, brent: point }), 2);
assert.equal(countSeries({ wti: point, brent: point, production: point, inventory: point }), 4);
});
it('validatePetroleum accepts any non-empty aggregate (1-of-4 is OK)', () => {
assert.equal(validatePetroleum({ wti: point }), true);
assert.equal(validatePetroleum({ wti: point, brent: point, production: point, inventory: point }), true);
});
it('validatePetroleum rejects fully-empty aggregates', () => {
assert.equal(validatePetroleum({}), false);
assert.equal(validatePetroleum(null), false);
assert.equal(validatePetroleum(undefined), false);
});
it('declareRecords returns the series count (drives contract-mode OK vs RETRY)', () => {
assert.equal(declareRecords({}), 0);
assert.equal(declareRecords({ wti: point }), 1);
assert.equal(declareRecords({ wti: point, brent: point, production: point, inventory: point }), 4);
});
});