Files
TREK/server/tests/unit/nest/atlas-region-files.test.ts
T
Maurice 948c9ef57b fix(atlas): show Ireland's 26 counties instead of its four provinces
geoBoundaries has no county layer for Ireland: its ADM1 is the four
provinces and its ADM2 the 166 local electoral areas, so the Atlas showed
Ireland as four provinces next to a UK of counties.

The Atlas builder can now take a country's regions from another source.
Ireland's come from Tailte Éireann's 26 counties (CC BY 4.0), with Dublin,
Cork, Galway and Limerick each one county, under their ISO 3166-2:IE
codes. Those outlines come at full detail, so the builder simplifies them
itself and keeps the borders two counties share identical on both sides,
so no point between them falls into neither. ATLAS_ONLY rebuilds just the
named countries without downloading the whole world.

A migration clears Ireland's cached place regions so they are worked out
again as counties, moves visited rows that name a county onto its code,
and hides each county of a province a user had hidden. Provinces a user
marked as visited are kept as they are.

Fixes #2922
2026-11-11 21:26:31 +00:00

90 lines
4.2 KiB
TypeScript

import { getRegionFromCoords, getRegionGeoGz, getRegionGeoJson } from '../../../src/nest/atlas/atlas-geo';
import { describe, it, expect } from 'vitest';
import zlib from 'zlib';
// The admin-1 regions behind the Atlas region layer and region lookups, read from the
// committed geoBoundaries files (server/assets/atlas/admin1/<ISO_A2>.geojson.gz), so these
// double as a guard that the bundle ships current sub-national data (#1119).
type RegionFeature = { properties: { iso_a2: string; iso_3166_2: string; name: string } };
type RegionGeo = { type: string; features: RegionFeature[] };
const regionGeo = (codes: string[]) => JSON.parse(getRegionGeoJson(codes).toString('utf8')) as RegionGeo;
const countriesOf = (geo: RegionGeo) => new Set(geo.features.map((f) => f.properties.iso_a2));
describe('getRegionGeoJson', () => {
it('ATLAS-SVC-017: returns an empty FeatureCollection for a country with no admin-1 features', () => {
expect(regionGeo(['ZZ'])).toEqual({ type: 'FeatureCollection', features: [] });
});
it('ATLAS-SVC-018: returns the current geoBoundaries regions for a country, case-insensitively', () => {
const result = regionGeo(['no']);
expect(result.type).toBe('FeatureCollection');
expect(result.features.length).toBeGreaterThan(0);
expect(countriesOf(result)).toEqual(new Set(['NO']));
const names = result.features.map((f) => f.properties.name);
const codes = result.features.map((f) => f.properties.iso_3166_2);
// Post-2020 reform is present…
expect(codes).toContain('NO-34'); // Innlandet
expect(codes).toContain('NO-46'); // Vestland
// …and the merged-away pre-2020 counties are gone (the original #1119 bug).
expect(names).not.toContain('Oppland');
expect(names).not.toContain('Hordaland');
expect(names).not.toContain('Sogn og Fjordane');
});
it('ATLAS-SVC-048: joins several countries into one FeatureCollection, each once, skipping unknown codes', () => {
const result = regionGeo(['DE', 'zz', 'FR', 'de']);
expect(countriesOf(result)).toEqual(new Set(['DE', 'FR']));
expect(result.features.length).toBe(regionGeo(['DE']).features.length + regionGeo(['FR']).features.length);
});
});
describe('getRegionGeoGz', () => {
it("ATLAS-SVC-049: returns the country's gzipped FeatureCollection, case-insensitively", async () => {
const gz = await getRegionGeoGz('no');
expect(gz).toBeInstanceOf(Buffer);
const geo = JSON.parse(zlib.gunzipSync(gz ?? Buffer.alloc(0)).toString('utf8')) as RegionGeo;
expect(geo.type).toBe('FeatureCollection');
expect(countriesOf(geo)).toEqual(new Set(['NO']));
});
it('ATLAS-SVC-050: returns null for a country without regions and for anything that is not a two-letter code', async () => {
const codes = ['ZZ', 'NOR', '', '../admin0', '..', 'N/'];
expect(await Promise.all(codes.map((code) => getRegionGeoGz(code)))).toEqual(codes.map(() => null));
});
});
describe('Ireland resolves to its counties (#2922)', () => {
// Inland, coastal and near-border places, one county each. The counties come from
// Tailte Éireann and are simplified by the builder itself, so a point that lands in no
// county or in the wrong one would mean the simplification opened a gap or moved a border.
const places: [string, number, number, string][] = [
['Dublin, Trinity College', 53.3438, -6.2546, 'IE-D'],
['Howth village', 53.3866, -6.07, 'IE-D'],
['Cork, St. Patrick Street', 51.8985, -8.4731, 'IE-CO'],
['Galway, Eyre Square', 53.2743, -9.0491, 'IE-G'],
['Killarney', 52.0599, -9.5044, 'IE-KY'],
['Cliffs of Moher visitor centre', 52.9719, -9.4265, 'IE-CE'],
['Kilkenny Castle', 52.6503, -7.2492, 'IE-KK'],
['Letterkenny', 54.9503, -7.7334, 'IE-DL'],
['Westport', 53.8008, -9.5223, 'IE-MO'],
['Cashel, Rock of Cashel', 52.5201, -7.8906, 'IE-TA'],
];
it.each(places)('ATLAS-SVC-051: %s lies in its county', async (_name, lat, lng, code) => {
expect((await getRegionFromCoords('IE', lat, lng))?.region_code).toBe(code);
});
it('ATLAS-SVC-052: a county comes back under its English name', async () => {
expect(await getRegionFromCoords('ie', 53.3438, -6.2546)).toEqual({
country_code: 'IE',
region_code: 'IE-D',
region_name: 'Dublin',
});
});
});