Files
worldmonitor/tests/api-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

114 lines
4.1 KiB
JavaScript

import { strict as assert } from 'node:assert';
import { describe, it, beforeEach, afterEach } from 'node:test';
const originalFetch = globalThis.fetch;
const originalEnv = { ...process.env };
const SAMPLE_PAYLOAD = {
wti: { current: 76.23, previous: 75.10, date: '2026-04-11', unit: 'dollars per barrel' },
brent: { current: 81.02, previous: 80.44, date: '2026-04-11', unit: 'dollars per barrel' },
production: { current: 13100, previous: 13050, date: '2026-04-11', unit: 'MBBL' },
inventory: { current: 458_100, previous: 459_200, date: '2026-04-11', unit: 'MBBL' },
};
const ENVELOPE = {
_seed: {
fetchedAt: 1_700_000_000_000,
recordCount: 4,
sourceVersion: 'eia-petroleum-v1',
schemaVersion: 1,
state: 'OK',
},
data: SAMPLE_PAYLOAD,
};
function makeRequest(path, opts = {}) {
return new Request(`https://worldmonitor.app/api/eia${path}`, {
method: opts.method || 'GET',
headers: { origin: 'https://worldmonitor.app', ...(opts.headers || {}) },
});
}
let handler;
describe('api/eia/[[...path]] — petroleum reader', () => {
beforeEach(async () => {
process.env.UPSTASH_REDIS_REST_URL = 'https://fake-upstash.io';
process.env.UPSTASH_REDIS_REST_TOKEN = 'fake-token';
const mod = await import(`../api/eia/%5B%5B...path%5D%5D.js?t=${Date.now()}`);
handler = mod.default;
});
afterEach(() => {
globalThis.fetch = originalFetch;
Object.keys(process.env).forEach(k => {
if (!(k in originalEnv)) delete process.env[k];
});
Object.assign(process.env, originalEnv);
});
it('OPTIONS returns 204 with CORS headers', async () => {
const res = await handler(makeRequest('/petroleum', { method: 'OPTIONS' }));
assert.equal(res.status, 204);
assert.ok(res.headers.get('access-control-allow-origin'));
});
it('disallowed origin returns 403', async () => {
const res = await handler(makeRequest('/petroleum', { headers: { origin: 'https://evil.example' } }));
assert.equal(res.status, 403);
});
it('non-GET returns 405', async () => {
const res = await handler(makeRequest('/petroleum', { method: 'POST' }));
assert.equal(res.status, 405);
});
it('/health returns configured:true', async () => {
const res = await handler(makeRequest('/health'));
assert.equal(res.status, 200);
const body = await res.json();
assert.equal(body.configured, true);
});
it('/petroleum returns 200 with data on Upstash hit (envelope unwrapped)', async () => {
globalThis.fetch = async (url) => {
assert.match(String(url), /fake-upstash\.io\/get\/energy%3Aeia-petroleum%3Av1/);
return new Response(JSON.stringify({ result: JSON.stringify(ENVELOPE) }), { status: 200 });
};
const res = await handler(makeRequest('/petroleum'));
assert.equal(res.status, 200);
const body = await res.json();
assert.deepEqual(body, SAMPLE_PAYLOAD);
assert.match(res.headers.get('cache-control') || '', /max-age=1800/);
assert.match(res.headers.get('cache-control') || '', /stale-while-revalidate=86400/);
});
it('/petroleum returns 503 with hint when Redis key is missing', async () => {
globalThis.fetch = async () => new Response(JSON.stringify({ result: null }), { status: 200 });
const res = await handler(makeRequest('/petroleum'));
assert.equal(res.status, 503);
const body = await res.json();
assert.match(body.error, /not yet seeded/i);
assert.ok(body.hint);
assert.equal(res.headers.get('cache-control'), 'no-store');
assert.equal(res.headers.get('retry-after'), '300');
});
it('/petroleum returns 503 (not 504) when Upstash itself errors', async () => {
globalThis.fetch = async () => new Response('bad gateway', { status: 502 });
const res = await handler(makeRequest('/petroleum'));
assert.equal(res.status, 503);
});
it('/petroleum returns 503 when Upstash throws', async () => {
globalThis.fetch = async () => { throw new Error('connection refused'); };
const res = await handler(makeRequest('/petroleum'));
assert.equal(res.status, 503);
});
it('unknown path returns 404', async () => {
const res = await handler(makeRequest('/unknown'));
assert.equal(res.status, 404);
});
});