feat(energy): product supply shock scenario RPC (Phase 4 PR C) (#2768)

* feat(energy): ComputeEnergyShockScenario RPC + country brief shock UI (Phase 4 PR C)

Adds on-demand product supply shock scenario computation from JODI Oil, Comtrade and IEA stocks data.

* fix(tests): add runtime + intelligence-client stubs to resilience harness

ResilienceWidget imports @/services/runtime which dynamically imports
@/services/widget-store. Without stubbing runtime, esbuild bundled the
chain and failed on loadFromStorage not exported by the utils stub.

* fix(energy): dataAvailable requires Comtrade data; gate shock widget on jodiOilAvailable

- dataAvailable now requires both jodiOil and comtradeHasData, eliminating the contradiction of returning true with an "insufficient data" assessment
- Collapsed redundant !hasComtradeData branch into the unified !dataAvailable guard
- Gate renderShockScenarioWidget() behind data.jodiOilAvailable in CountryDeepDivePanel to avoid rendering a widget that will always return dataAvailable: false

* fix(energy): zero-import hasData=false; extract pure shock-compute for real unit coverage

- `totalImports === 0` in `computeGulfShare` now returns `hasData: false` so the
  handler correctly falls through to the "insufficient data" branch instead of
  treating empty Comtrade rows as usable Gulf-share data
- Extract `clamp`, `computeGulfShare`, `computeEffectiveCoverDays`, `buildAssessment`,
  `GULF_PARTNER_CODES`, and `CHOKEPOINT_EXPOSURE` into `_shock-compute.ts`
- Handler delegates pure computation to imported functions; `getGulfCrudeShare` still
  owns Redis I/O and calls `computeGulfShare(flows)` for the math
- Tests now import the real functions via `.js` ESM extension; all 24 test cases
  exercise actual production logic (was previously reimplemented inline)

* fix(energy): handle net-exporter in shock assessment; fix tautological chokepoint tests

* fix(energy): move net-exporter branch before low-Gulf-share check in buildAssessment

A net exporter with gulfCrudeShare < 0.1 (e.g. Norway) incorrectly
received "low Gulf crude dependence" instead of "net oil exporter".
Adds regression test to cover the ordering case.
This commit is contained in:
Elie Habib
2026-04-06 22:37:35 +04:00
committed by GitHub
parent 864b57f1fc
commit b9b552cfcd
13 changed files with 891 additions and 1 deletions

File diff suppressed because one or more lines are too long