// FE-ATLAS2-PAINT-101 to FE-ATLAS2-PAINT-120: the country and region names placed // over the globe, their collision rules and how their elements are reused. import { beforeEach, describe, expect, it } from 'vitest'; import type { AtlasMeshManifest, GlobeEngine, RegionIndex } from './GlobeEngine'; import { placeLabels, type GlobeLabel } from './globeLabels'; type ManifestCountry = AtlasMeshManifest['countries'][number]; type Feature = RegionIndex['features'][number]; interface FakeLabelEngine { manifest: { countries: ManifestCountry[] } | null; zoom: number; vp: { width: number; height: number }; regionIndex: Map; regionAlpha: number; focus: string | null; /** Visibility by "x,y" of the anchor; 1 when not listed. */ visibility: Map; } /** * A globe engine whose projection is the identity: an anchor [x, y] lands on * screen at x, y, so a test places labels in pixels directly. */ function fakeEngine(over: Partial = {}) { const state: FakeLabelEngine = { manifest: { countries: [] }, zoom: 1, vp: { width: 1000, height: 800 }, regionIndex: new Map(), regionAlpha: 0, focus: null, visibility: new Map(), ...over, }; const engine = { get manifest() { return state.manifest; }, get zoom() { return state.zoom; }, get vp() { return state.vp; }, get regionIndex() { return state.regionIndex; }, get regionAlpha() { return state.regionAlpha; }, focusCountry: () => state.focus, project: (lon: number, lat: number) => ({ x: lon, y: lat, visible: state.visibility.get(`${lon},${lat}`) ?? 1, shade: 1, }), } as unknown as GlobeEngine; return { engine, state }; } function mc(a2: string | null, x: number, y: number, area = 1000, anchor = true): ManifestCountry { return { a2, a3: a2 ?? 'XXX', name: a2 ?? '', anchor: anchor ? [x, y] : null, bbox: [], area }; } function feat(over: Partial = {}): Feature { return { code: null, name: null, nameEn: null, anchor: null, counted: true, ...over }; } function index(features: Feature[]): RegionIndex { return { bbox: [], counted: features.length, features, files: {} }; } const nameOf = (a2: string) => `Name ${a2}`; let host: HTMLDivElement; let pool: GlobeLabel[]; beforeEach(() => { host = document.createElement('div'); pool = []; }); /** The labels that show, by text. */ function shownTexts(): string[] { return pool .filter((l) => l.el.style.opacity !== '0') .map((l) => l.el.textContent ?? '') .sort(); } function labelOf(text: string): GlobeLabel | undefined { return pool.find((l) => l.el.textContent === text && l.el.style.opacity !== '0'); } describe('placeLabels: country names', () => { it('FE-ATLAS2-PAINT-101: places nothing before the manifest has loaded', () => { const { engine } = fakeEngine({ manifest: null }); placeLabels(host, pool, engine, nameOf); expect(host.children).toHaveLength(0); expect(pool).toHaveLength(0); }); it('FE-ATLAS2-PAINT-102: names a large country in view at its anchor, in a fresh element on the host', () => { const { engine } = fakeEngine({ manifest: { countries: [mc('FR', 100, 200)] } }); placeLabels(host, pool, engine, nameOf); expect(pool).toHaveLength(1); const el = pool[0].el; expect(host.firstChild).toBe(el); expect(pool[0].key).toBe('FR'); expect(el.className).toBe('atlas-globe-label text-caption'); expect(el.textContent).toBe('Name FR'); expect(el.dataset.region).toBe(''); expect(el.style.transform).toBe('translate3d(100.0px, 200.0px, 0) translate(-50%, -50%)'); expect(el.style.opacity).toBe('1'); }); it('FE-ATLAS2-PAINT-103: leaves out countries too small for the zoom, unless they are the user own', () => { // At zoom 1 a country needs 700 square degrees; one of the user's own needs 30 % of that. const countries = [mc('BE', 100, 100, 300), mc('LU', 400, 100, 300), mc('MT', 700, 100, 100)]; const { engine } = fakeEngine({ manifest: { countries } }); placeLabels(host, pool, engine, nameOf, (a2) => a2 === 'BE' || a2 === 'MT'); expect(shownTexts()).toEqual(['Name BE']); }); it('FE-ATLAS2-PAINT-104: zooming in brings smaller countries in', () => { const { engine, state } = fakeEngine({ manifest: { countries: [mc('BE', 100, 100, 300)] } }); placeLabels(host, pool, engine, nameOf); expect(shownTexts()).toEqual([]); state.zoom = 2; placeLabels(host, pool, engine, nameOf); expect(shownTexts()).toEqual(['Name BE']); }); it('FE-ATLAS2-PAINT-105: skips countries without a code, an anchor, a name, or out of view', () => { const countries = [ mc(null, 100, 100), mc('AQ', 200, 100, 1000, false), mc('NO', 300, 100), // Past the margin left, right, above and below the view. mc('W1', -50, 300), mc('E1', 1050, 300), mc('N1', 500, -30), mc('S1', 500, 830), // Behind the limb. mc('BK', 600, 600), // Inside the margin: still named. mc('IN', -30, 400), ]; const { engine } = fakeEngine({ manifest: { countries }, visibility: new Map([['600,600', 0.1]]) }); placeLabels(host, pool, engine, (a2) => (a2 === 'NO' ? null : a2)); expect(shownTexts()).toEqual(['IN']); }); it('FE-ATLAS2-PAINT-106: fades a name in as it turns from the limb and as region fills take over', () => { const { engine } = fakeEngine({ manifest: { countries: [mc('FR', 100, 100)] }, visibility: new Map([['100,100', 0.15]]), regionAlpha: 0.5, }); placeLabels(host, pool, engine, nameOf); expect(Number(pool[0].el.style.opacity)).toBeCloseTo(0.6 * (1 - 0.5 * 0.55), 6); }); it('FE-ATLAS2-PAINT-107: of two overlapping names the larger country wins', () => { const { engine } = fakeEngine({ manifest: { countries: [mc('SM', 100, 100, 800), mc('IT', 110, 104, 3000)] } }); placeLabels(host, pool, engine, nameOf); expect(shownTexts()).toEqual(['Name IT']); expect(pool).toHaveLength(1); }); it('FE-ATLAS2-PAINT-108: the user own country outranks a larger neighbour', () => { const { engine } = fakeEngine({ manifest: { countries: [mc('CH', 100, 100, 1000), mc('DE', 110, 104, 3000)] } }); placeLabels(host, pool, engine, nameOf, (a2) => a2 === 'CH'); expect(shownTexts()).toEqual(['Name CH']); }); it('FE-ATLAS2-PAINT-109: a name on screen keeps its place against a slightly larger one, not a much larger one', () => { const { engine, state } = fakeEngine({ manifest: { countries: [mc('AA', 100, 100, 1000)] } }); placeLabels(host, pool, engine, nameOf); const first = pool[0].el; state.manifest = { countries: [mc('AA', 100, 100, 1000), mc('BB', 104, 100, 1400)] }; placeLabels(host, pool, engine, nameOf); expect(shownTexts()).toEqual(['Name AA']); expect(labelOf('Name AA')?.el).toBe(first); state.manifest = { countries: [mc('AA', 100, 100, 1000), mc('BB', 104, 100, 1600)] }; placeLabels(host, pool, engine, nameOf); expect(shownTexts()).toEqual(['Name BB']); }); it('FE-ATLAS2-PAINT-110: a name that stays keeps its element and only moves', () => { const { engine, state } = fakeEngine({ manifest: { countries: [mc('FR', 100, 100)] } }); placeLabels(host, pool, engine, nameOf); const el = pool[0].el; state.manifest = { countries: [mc('FR', 150.04, 120)] }; placeLabels(host, pool, engine, nameOf); expect(pool).toHaveLength(1); expect(pool[0].el).toBe(el); expect(el.style.transform).toBe('translate3d(150.0px, 120.0px, 0) translate(-50%, -50%)'); }); it('FE-ATLAS2-PAINT-111: a name that goes hides its element, and the next new name reuses it', () => { const { engine, state } = fakeEngine({ manifest: { countries: [mc('FR', 100, 100)] } }); placeLabels(host, pool, engine, nameOf); const el = pool[0].el; state.manifest = { countries: [] }; placeLabels(host, pool, engine, nameOf); expect(el.style.opacity).toBe('0'); expect(shownTexts()).toEqual([]); state.manifest = { countries: [mc('ES', 300, 300)] }; placeLabels(host, pool, engine, nameOf); expect(pool).toHaveLength(1); expect(host.children).toHaveLength(1); expect(pool[0].key).toBe('ES'); expect(el.textContent).toBe('Name ES'); expect(el.style.opacity).toBe('1'); }); it('FE-ATLAS2-PAINT-112: shows at most 150 names at once', () => { const countries: ManifestCountry[] = []; for (let i = 0; i < 170; i++) { countries.push(mc(`C${i}`, 20 + (i % 20) * 48, 20 + Math.floor(i / 20) * 30, 1000 + i)); } const { engine } = fakeEngine({ manifest: { countries } }); placeLabels(host, pool, engine, (a2) => a2); expect(pool).toHaveLength(150); // The largest win the cut. expect(labelOf('C169')).toBeDefined(); expect(labelOf('C0')).toBeUndefined(); }); }); describe('placeLabels: region names', () => { const US = mc('US', 500, 400, 9000); const usRegions = () => index([ feat({ code: 'US-KS', name: 'Kansas', anchor: [300, 300] }), feat({ code: null, name: 'Texas', nameEn: 'Texas (en)', anchor: [600, 500] }), feat({ code: 'US-AK', name: 'Alaska', anchor: null }), feat({ code: 'US-HI', name: 'Hawaii', anchor: [1200, 300] }), feat({ code: 'US-GU', name: 'Guam', anchor: [-10, 300] }), feat({ code: 'US-VI', name: 'Virgin', anchor: [300, -5] }), feat({ code: 'US-AS', name: 'Samoa', anchor: [300, 900] }), feat({ code: 'US-ME', name: 'Maine', anchor: [800, 100] }), ]); it('FE-ATLAS2-PAINT-113: from the wide-screen region zoom on, a country with regions names them instead of itself', () => { const { engine } = fakeEngine({ manifest: { countries: [US, mc('CA', 900, 700, 9000)] }, zoom: 18, regionIndex: new Map([['US', usRegions()]]), regionAlpha: 0.8, visibility: new Map([['800,100', 0.05]]), }); placeLabels(host, pool, engine, nameOf); expect(shownTexts()).toEqual(['Kansas', 'Name CA', 'Texas (en)']); const kansas = labelOf('Kansas'); expect(kansas?.key).toBe('US:US-KS'); expect(kansas?.el.dataset.region).toBe('1'); expect(kansas?.el.style.opacity).toBe('0.8'); expect(labelOf('Texas (en)')?.key).toBe('US:Texas'); expect(labelOf('Name CA')?.el.dataset.region).toBe(''); }); it('FE-ATLAS2-PAINT-114: below the region zoom the country keeps its own name', () => { const { engine } = fakeEngine({ manifest: { countries: [US] }, zoom: 17, regionIndex: new Map([['US', usRegions()]]), }); placeLabels(host, pool, engine, nameOf); expect(shownTexts()).toEqual(['Name US']); }); it('FE-ATLAS2-PAINT-115: on a phone-wide view region names start earlier', () => { const { engine } = fakeEngine({ manifest: { countries: [US] }, zoom: 14, vp: { width: 600, height: 800 }, regionIndex: new Map([['US', usRegions()]]), regionAlpha: 1, }); placeLabels(host, pool, engine, nameOf); expect(shownTexts()).toEqual(['Kansas', 'Texas (en)']); }); it('FE-ATLAS2-PAINT-116: a region named by its plain name falls back to it, and one without any name shows empty', () => { const { engine } = fakeEngine({ manifest: { countries: [] }, zoom: 20, regionIndex: new Map([ [ 'FR', index([ feat({ code: 'FR-IDF', name: 'Île-de-France', anchor: [100, 100] }), feat({ code: 'FR-X', anchor: [400, 400] }), ]), ], ]), regionAlpha: 1, }); placeLabels(host, pool, engine, nameOf); expect(pool.map((l) => l.el.textContent).sort()).toEqual(['', 'Île-de-France']); }); /** Two countries with one region each at nearly the same spot: only one of them can be named. */ function rivals(focus: string | null, countries: ManifestCountry[]) { return fakeEngine({ manifest: { countries }, zoom: 20, focus, regionIndex: new Map([ // Mexico's region is right in the middle of the view. ['MX', index([feat({ code: 'MX-SON', name: 'Sonora', anchor: [500, 400] })])], ['US', index([feat({ code: 'US-AZ', name: 'Arizona', anchor: [510, 404] })])], ]), regionAlpha: 1, }); } it('FE-ATLAS2-PAINT-117: the regions of the country under the middle of the view come first', () => { const { engine } = rivals('US', [mc('US', 520, 410), mc('MX', 480, 420)]); placeLabels(host, pool, engine, nameOf); expect(shownTexts()).toEqual(['Arizona']); }); it('FE-ATLAS2-PAINT-118: a country under the middle without regions puts no country first, so the nearest region wins', () => { const { engine } = rivals('ES', [mc('US', 520, 410), mc('MX', 480, 420)]); placeLabels(host, pool, engine, nameOf); expect(shownTexts()).toEqual(['Sonora']); }); it('FE-ATLAS2-PAINT-119: over open water the country with regions whose anchor is nearest the middle comes first', () => { // US's anchor is the nearer one; Mexico's lies behind the limb. const near = rivals(null, [mc('US', 520, 410), mc('MX', 501, 401)]); near.state.visibility.set('501,401', 0); placeLabels(host, pool, near.engine, nameOf); expect(shownTexts()).toEqual(['Arizona']); // No anchor near enough the middle (or none at all): nobody comes first. pool = []; const far = rivals(null, [mc('US', 950, 750), mc('MX', 0, 0, 1000, false)]); placeLabels(host, pool, far.engine, nameOf); expect(shownTexts()).toEqual(['Sonora']); }); it('FE-ATLAS2-PAINT-120: shows at most 120 region names, leaving room for country names', () => { const features: Feature[] = []; for (let i = 0; i < 130; i++) { features.push(feat({ code: `R${i}`, name: `R${i}`, anchor: [30 + (i % 13) * 70, 30 + Math.floor(i / 13) * 30] })); } const { engine } = fakeEngine({ manifest: { countries: [mc('FR', 500, 700, 5000)] }, zoom: 20, regionIndex: new Map([['XX', index(features)]]), regionAlpha: 1, }); placeLabels(host, pool, engine, nameOf); const regions = pool.filter((l) => l.el.dataset.region === '1' && l.el.style.opacity !== '0'); expect(regions).toHaveLength(120); expect(labelOf('Name FR')).toBeDefined(); }); });