193 lines
13 KiB
JavaScript
193 lines
13 KiB
JavaScript
// Offline specification QA only; never imported by the application.
|
|
const assert = require('node:assert/strict');
|
|
const { execFileSync } = require('node:child_process');
|
|
const { readFileSync } = require('node:fs');
|
|
const path = require('node:path');
|
|
const root = path.resolve(__dirname, '../../..');
|
|
const git = (...args) => execFileSync('git', args, { cwd: root, encoding: 'utf8', maxBuffer: 4 * 1024 * 1024 });
|
|
const load = () => JSON.parse(readFileSync(path.join(__dirname, 'GEOGRAPHY_CONTRACT_FIXTURES.json'), 'utf8'));
|
|
function validate(fixture, matrix, coverage) {
|
|
assert.equal(fixture.schema, 1);
|
|
assert.equal(fixture.purpose, 'offline-specification-only');
|
|
assert.equal(fixture.sourceCommit, '39fb9df06904e44916e258da0ed975dd46b9691c');
|
|
assert.equal(fixture.baseline, 'eb2538772e1bfabbeb291e4a5efa177c79074de1');
|
|
const unique = (values, label) => assert.equal(new Set(values).size, values.length, `duplicate ${label}`);
|
|
const sorted = values => [...values].sort();
|
|
const codes = [...matrix.matchAll(/^\| [^|]+ \| ([A-Z]{2}) \|/gm)].map(match => match[1]);
|
|
assert.equal(codes.length, 59);
|
|
unique(codes, 'matrix code');
|
|
assert(matrix.includes('XK is a CLDR user-assigned territory code, not ISO 3166-1'));
|
|
// This pinned CSV has no quoted fields; fail rather than silently parse a new format.
|
|
assert(!coverage.includes('"'), 'CSV format changed; review parser');
|
|
const [header, ...lines] = coverage.trim().split(/\r?\n/).map(line => line.split(','));
|
|
const columns = 'territoryCode,scopeReview,countrySelectable,administrativeHierarchy,canonicalResolution,mapCountrySwitching,canonicalLocationFilters,providerCoverage,countryAwareResults,multiCountrySavedSearches,marketLanguageOptions,marketCatalogue,currencyAndTimezone,mobile,accessibility,countryTestEvidence,humanApproval,productionAvailability,existingNavFixture'.split(',');
|
|
assert.deepEqual(header, columns);
|
|
unique(lines.map(row => row[0]), 'coverage code');
|
|
assert.deepEqual(sorted(lines.map(row => row[0])), sorted(codes));
|
|
const statuses = ['NOT IMPLEMENTED', 'IMPLEMENTED', 'TESTED', 'BLOCKED', 'AWAITING REVIEW', 'ACCEPTED', 'RELEASED'];
|
|
for (const row of lines) {
|
|
assert.equal(row.length, columns.length);
|
|
for (let i = 1; i < row.length; i++) {
|
|
assert(statuses.includes(row[i]), `invalid status ${row[0]} ${columns[i]}`);
|
|
let expected = 'NOT IMPLEMENTED';
|
|
if (['scopeReview', 'humanApproval'].includes(columns[i])) expected = 'AWAITING REVIEW';
|
|
if (columns[i] === 'administrativeHierarchy' && ['NO', 'SE', 'GB'].includes(row[0])) expected = 'AWAITING REVIEW';
|
|
if (columns[i] === 'productionAvailability' || (columns[i] === 'providerCoverage' && ['NO', 'SE', 'GB'].includes(row[0]))) expected = 'BLOCKED';
|
|
if (columns[i] === 'existingNavFixture' && row[0] === 'NO') expected = 'TESTED';
|
|
assert.equal(row[i], expected, `unqualified coverage change ${row[0]} ${columns[i]}`);
|
|
}
|
|
}
|
|
const nodes = new Map(fixture.nodes.map(node => [node.id, node]));
|
|
unique(fixture.nodes.map(node => node.id), 'node ID');
|
|
for (const node of fixture.nodes) {
|
|
assert(codes.includes(node.country));
|
|
assert(['county', 'municipality', 'town', 'settlement', 'state', 'region'].includes(node.type));
|
|
assert(node.id.startsWith(`s:${node.country}:${node.type}:`), 'typed country identity mismatch');
|
|
assert.equal(node.edition, 'synthetic-1');
|
|
assert.equal(node.provenance, 'synthetic-no-boundary-claim');
|
|
assert.equal(typeof node.label, 'string');
|
|
assert(node.label.trim());
|
|
assert(!('coordinates' in node || 'geometry' in node || 'providerCode' in node), 'unqualified spatial/provider data');
|
|
}
|
|
unique(fixture.edges.map(edge => JSON.stringify(edge)), 'edge');
|
|
for (const edge of fixture.edges) {
|
|
assert(nodes.has(edge.child) && nodes.has(edge.parent), 'unresolved edge');
|
|
assert.notEqual(edge.child, edge.parent);
|
|
assert.equal(nodes.get(edge.child).country, nodes.get(edge.parent).country, 'cross-country edge');
|
|
assert(['administrative-parent', 'settlement-intersects', 'same-footprint'].includes(edge.type));
|
|
if (edge.type === 'settlement-intersects') assert.equal(nodes.get(edge.child).type, 'settlement');
|
|
}
|
|
// Reachability checks fixture declarations only; this is not a runtime scope compiler.
|
|
function ancestor(child, parent, trail = []) {
|
|
assert(!trail.includes(child), 'administrative cycle');
|
|
return fixture.edges.filter(edge => edge.type === 'administrative-parent' && edge.child === child)
|
|
.some(edge => edge.parent === parent || ancestor(edge.parent, parent, [...trail, child]));
|
|
}
|
|
for (const node of nodes.keys()) assert(!ancestor(node, node), 'administrative cycle');
|
|
function checkBranches(branches, countries) {
|
|
unique(countries, 'search country');
|
|
assert(countries.every(country => codes.includes(country)));
|
|
unique(branches.map(branch => branch.root), 'branch');
|
|
for (const branch of branches) {
|
|
assert(nodes.has(branch.root), 'unresolved scope');
|
|
assert(countries.includes(nodes.get(branch.root).country), 'incompatible country scope');
|
|
unique(branch.refinements, 'refinement');
|
|
for (const refinement of branch.refinements) {
|
|
assert(nodes.has(refinement) && ancestor(refinement, branch.root), 'invalid refinement');
|
|
assert.equal(nodes.get(refinement).country, nodes.get(branch.root).country);
|
|
}
|
|
}
|
|
}
|
|
unique(fixture.scenarios.map(scenario => scenario.name), 'scenario name');
|
|
assert(fixture.scenarios.length >= 12);
|
|
for (const scenario of fixture.scenarios) {
|
|
checkBranches(scenario.branches, scenario.countries);
|
|
assert(scenario.expanded.every(id => nodes.has(id)));
|
|
const leaves = scenario.branches.flatMap(branch => branch.refinements.length
|
|
? branch.refinements.filter(id => !branch.refinements.some(other => id !== other && ancestor(other, id))) : [branch.root]);
|
|
unique(leaves, 'effective scope');
|
|
for (const surface of ['query', 'tree', 'map']) {
|
|
assert.deepEqual(sorted(scenario.expected[surface]), sorted(leaves), `incorrect ${surface} scope: ${scenario.name}`);
|
|
}
|
|
if (scenario.equivalentTo) {
|
|
const other = fixture.scenarios.find(item => item.name === scenario.equivalentTo);
|
|
assert(other && other !== scenario, 'missing equivalent selection');
|
|
assert.deepEqual(scenario.countries, other.countries, 'expansion changes search countries');
|
|
assert.deepEqual(scenario.branches, other.branches, 'expansion changes selected branches');
|
|
assert.deepEqual(scenario.expected, other.expected, 'expansion changes effective scope');
|
|
}
|
|
assert.deepEqual(JSON.parse(JSON.stringify(scenario)), scenario, 'fixture roundtrip');
|
|
}
|
|
const countryChanges = [fixture.countryChange, ...fixture.additionalCountryChanges];
|
|
for (const change of countryChanges) {
|
|
checkBranches(change.beforeBranches, change.beforeCountries);
|
|
checkBranches(change.afterBranches, change.afterCountries);
|
|
const retained = change.beforeBranches.filter(branch => change.afterCountries.includes(nodes.get(branch.root).country));
|
|
assert.deepEqual(change.afterBranches, retained, 'only incompatible filters may clear');
|
|
assert.deepEqual(change.cleared, change.beforeBranches.filter(branch => !retained.includes(branch)).map(branch => branch.root));
|
|
assert(change.explanation.trim(), 'country change explanation required');
|
|
assert.deepEqual(change.after, change.before, 'unrelated preferences/saved search changed');
|
|
assert.deepEqual(Object.keys(change.before).sort(), ['adLanguages', 'documentLanguages', 'residence', 'savedSearch', 'uiLocale']);
|
|
}
|
|
const { uiLocale: previousLocale, ...previousSelection } = fixture.localeChange.before;
|
|
const { uiLocale: nextLocale, ...nextSelection } = fixture.localeChange.after;
|
|
assert(typeof previousLocale === 'string' && previousLocale.trim());
|
|
assert(typeof nextLocale === 'string' && nextLocale.trim());
|
|
assert.notEqual(previousLocale, nextLocale, 'fixture must exercise an actual locale change');
|
|
checkBranches(previousSelection.branches, previousSelection.countries);
|
|
checkBranches(nextSelection.branches, nextSelection.countries);
|
|
assert.deepEqual(nextSelection, previousSelection, 'locale change rewrites search or document preferences');
|
|
assert.equal(fixture.journeys.length, 4);
|
|
for (const journey of fixture.journeys) {
|
|
assert(codes.includes(journey.residence));
|
|
assert(journey.countries.length && journey.countries.every(country => codes.includes(country)));
|
|
unique(journey.countries, 'journey country');
|
|
assert.equal(typeof journey.uiLocale, 'string');
|
|
assert(journey.adLanguages.length && journey.documentLanguages.length);
|
|
assert(journey.adLanguages.every(language => typeof language === 'string'));
|
|
assert(journey.documentLanguages.every(language => typeof language === 'string'));
|
|
}
|
|
return { countries: codes.length, coverageCells: lines.length * (columns.length - 1), syntheticNodes: nodes.size,
|
|
syntheticScenarios: fixture.scenarios.length, preferenceJourneys: fixture.journeys.length, countryChangeInvariants: countryChanges.length,
|
|
localeChangeInvariants: 1,
|
|
qualification: 'offline specification consistency only' };
|
|
}
|
|
function selfTest(fixture, matrix, coverage) {
|
|
const cases = [
|
|
['false release coverage', (f, m, c) => [f, m, c.replace('BLOCKED', 'RELEASED')]],
|
|
['cross-country edge', f => { f.edges[0].parent = 's:NO:county:vestfold'; }],
|
|
['invalid refinement', f => { f.scenarios[1].branches[0].refinements = ['s:NO:municipality:tonsberg']; }],
|
|
['unqualified provenance', f => { f.nodes[0].provenance = 'licensed-real-data'; }],
|
|
['duplicate country', (f, m, c) => [f, m, c + c.trim().split('\n')[1] + '\n']],
|
|
['missing country', (f, m, c) => [f, m, c.trim().split('\n').slice(0, -1).join('\n')]],
|
|
['invented country', (f, m, c) => [f, m, c.replace('AD,', 'ZZ,')]],
|
|
['invalid status', (f, m, c) => [f, m, c.replace('BLOCKED', 'HEALTHY')]],
|
|
['NO fixture credited to GB', (f, m, c) => [f, m, c.replace(/^GB,(.*),NOT IMPLEMENTED$/m, 'GB,$1,TESTED')]],
|
|
['wrong type identity', f => { f.nodes[0].type = 'town'; }],
|
|
['missing edition', f => { delete f.nodes[0].edition; }],
|
|
['invented geometry', f => { f.nodes[0].geometry = {}; }],
|
|
['unresolved scope', f => { f.scenarios[0].branches[0].root = 'missing'; }],
|
|
['parent incorrectly ORed with town', f => { f.scenarios[1].expected.query.push(f.scenarios[1].branches[0].root); }],
|
|
['map divergent from query', f => { f.scenarios[1].expected.map = []; }],
|
|
['unrelated language reset', f => { f.countryChange.after.uiLocale = 'nb-NO'; }],
|
|
['saved search overwritten', f => { f.countryChange.after.savedSearch = 'rewritten'; }],
|
|
['invisible country filter', f => { f.countryChange.afterBranches = f.countryChange.beforeBranches; }],
|
|
['silent country clearing', f => { f.countryChange.explanation = ''; }],
|
|
['settlement overlap used as containment', f => {
|
|
f.scenarios[9].branches[0].refinements = ['s:SE:settlement:stockholm'];
|
|
}],
|
|
['administrative cycle', f => { f.edges.push({child: f.edges[0].parent, parent: f.edges[0].child, type: 'administrative-parent'}); }],
|
|
['invalid journey country', f => { f.journeys[0].countries = ['ZZ']; }],
|
|
['BE FR retains an incompatible filter', f => { f.additionalCountryChanges[0].afterBranches = f.additionalCountryChanges[0].beforeBranches; }],
|
|
['BE FR drops a compatible branch', f => { f.additionalCountryChanges[0].afterBranches = []; }],
|
|
['country change lacks an explanation', f => { f.additionalCountryChanges[1].explanation = ''; }],
|
|
['temporary country change rewrites a saved definition', f => { f.additionalCountryChanges[0].after.savedSearch.countries = ['BE']; }],
|
|
['locale switch changes country scope', f => { f.localeChange.after.countries = ['BE']; }],
|
|
['locale switch changes document language', f => { f.localeChange.after.documentLanguages = ['nl']; }],
|
|
['expansion broadens the selected branch', f => {
|
|
f.scenarios[13].branches[0].refinements = [];
|
|
for (const surface of ['query', 'tree', 'map']) f.scenarios[13].expected[surface] = ['s:BE:region:brussels', 's:FR:town:paris'];
|
|
}],
|
|
['equal labels merge distinct countries', f => { f.scenarios[15].expected.query.pop(); }],
|
|
];
|
|
for (const [name, mutate] of cases) {
|
|
const candidate = structuredClone(fixture);
|
|
const inputs = mutate(candidate, matrix, coverage) || [candidate, matrix, coverage];
|
|
assert.throws(() => validate(...inputs), undefined, `must reject ${name}`);
|
|
}
|
|
return cases.length;
|
|
}
|
|
const fixture = load();
|
|
// Read committed inputs directly; never copy a second register or use another worktree's dirty files.
|
|
const bindings = [
|
|
[fixture.sourceCommit, 'docs/i18n/00/EUROPE_COUNTRY_LANGUAGE_MATRIX.md', '4b88eb52e91d1c1ad0bb9082ed3ce075e0602dbd'],
|
|
[fixture.sourceCommit, 'docs/i18n/00/EUROPE_RELEASE_COVERAGE.csv', '395fc09934a516a2445fddb092b5de18c9469080'],
|
|
['0ec9f98e6ed4a646fc5e017d8eb6fade07653f37', 'docs/global-jobs/00/GEOGRAPHY_DECISIONS.md', '48f108ef02648f53490ae505b223ebff321264bb'],
|
|
];
|
|
for (const [commit, file, blob] of bindings) assert.equal(git('rev-parse', `${commit}:${file}`).trim(), blob, `source binding: ${file}`);
|
|
const matrix = git('show', `${fixture.sourceCommit}:docs/i18n/00/EUROPE_COUNTRY_LANGUAGE_MATRIX.md`);
|
|
const coverage = git('show', `${fixture.sourceCommit}:docs/i18n/00/EUROPE_RELEASE_COVERAGE.csv`);
|
|
const result = validate(fixture, matrix, coverage);
|
|
if (process.argv.includes('--self-test')) result.rejectedMutations = selfTest(fixture, matrix, coverage);
|
|
console.log(JSON.stringify(result, null, 2));
|