fix(seed): use Comtrade reporter codes for US, FR, IT (not UN M49) (#2984)

* fix(seed): use Comtrade reporter code 842 for US, not UN M49 840 (fixes #2967)

* fix(seed): add FR (251) and IT (381) to Comtrade reporter overrides

Audit found France and Italy also use non-standard Comtrade reporter
codes that differ from UN M49, same root cause as US (842 vs 840).

---------

Co-authored-by: octo-patch <octo-patch@github.com>
Co-authored-by: Elie Habib <elie.habib@gmail.com>
This commit is contained in:
Octopus
2026-04-12 22:55:43 +08:00
committed by GitHub
parent 281a7c0728
commit 89fe297fa2
2 changed files with 41 additions and 1 deletions

View File

@@ -79,6 +79,14 @@ const ISO2_TO_UN = Object.fromEntries(
Object.entries(UN_TO_ISO2).map(([un, iso2]) => [iso2, un]),
);
// UN Comtrade uses non-standard reporter codes for some countries.
// These override the standard UN M49 codes from un-to-iso2.json.
const COMTRADE_REPORTER_OVERRIDES = {
FR: '251', // UN M49 standard is 250, but Comtrade registers France as reporter 251
IT: '381', // UN M49 standard is 380, but Comtrade registers Italy as reporter 381
US: '842', // UN M49 standard is 840, but Comtrade registers the US as reporter 842
};
/**
* @param {Array<string[]>} commands
*/
@@ -242,7 +250,7 @@ export async function main() {
for (let i = 0; i < countries.length; i++) {
const [iso2] = countries[i];
const unCode = ISO2_TO_UN[iso2];
const unCode = COMTRADE_REPORTER_OVERRIDES[iso2] ?? ISO2_TO_UN[iso2];
if (!unCode) {
console.warn(` ${iso2}: no UN code, skipping`);
continue;

View File

@@ -295,6 +295,38 @@ describe('Comtrade bilateral HS4 seeder (scripts/seed-comtrade-bilateral-hs4.mjs
'seeder: must call extendExistingTtl when lock is skipped',
);
});
it('defines COMTRADE_REPORTER_OVERRIDES for all countries with non-standard Comtrade codes', () => {
assert.ok(
src.includes('COMTRADE_REPORTER_OVERRIDES'),
'seeder: must define COMTRADE_REPORTER_OVERRIDES to handle non-standard Comtrade reporter codes',
);
assert.ok(
src.includes("FR: '251'"),
"seeder: COMTRADE_REPORTER_OVERRIDES must map FR to '251' (Comtrade reporter code, not UN M49 250)",
);
assert.ok(
src.includes("IT: '381'"),
"seeder: COMTRADE_REPORTER_OVERRIDES must map IT to '381' (Comtrade reporter code, not UN M49 380)",
);
assert.ok(
src.includes("US: '842'"),
"seeder: COMTRADE_REPORTER_OVERRIDES must map US to '842' (Comtrade reporter code, not UN M49 840)",
);
});
it('applies COMTRADE_REPORTER_OVERRIDES before falling back to ISO2_TO_UN for reporter code lookup', () => {
const overrideIdx = src.indexOf('COMTRADE_REPORTER_OVERRIDES[iso2]');
const iso2ToUnIdx = src.indexOf('ISO2_TO_UN[iso2]', overrideIdx);
assert.ok(
overrideIdx !== -1,
'seeder: must use COMTRADE_REPORTER_OVERRIDES when resolving the Comtrade reporter code',
);
assert.ok(
iso2ToUnIdx !== -1 && iso2ToUnIdx > overrideIdx,
'seeder: COMTRADE_REPORTER_OVERRIDES must be checked before ISO2_TO_UN (override takes precedence)',
);
});
});
// ─── Service function ────────────────────────────────────────────────────────