import { createRequire } from 'node:module'; import { describe, expect, it } from 'vitest'; import { MAP_HEIGHT, MAP_WIDTH, buildWorldGeometry, fitProjection } from '@/lib/map-geo'; const require = createRequire(import.meta.url); // eslint-disable-next-line @typescript-eslint/no-explicit-any const atlas = require('world-atlas/countries-110m.json') as any; describe('fitProjection (Equal Earth)', () => { it('maps the origin to the centre of the viewport and keeps the sphere inside it', () => { const p = fitProjection({ type: 'Sphere' }); const c = p([0, 0])!; expect(c[0]).toBeCloseTo(MAP_WIDTH / 2, 0); expect(c[1]).toBeCloseTo(MAP_HEIGHT / 2, 0); for (const [lng, lat] of [ [-180, 0], [180, 0], [0, 90], [0, -90], [-73.6, 45.5], ] as Array<[number, number]>) { const q = p([lng, lat])!; expect(q[0]).toBeGreaterThanOrEqual(0); expect(q[0]).toBeLessThanOrEqual(MAP_WIDTH); expect(q[1]).toBeGreaterThanOrEqual(0); expect(q[1]).toBeLessThanOrEqual(MAP_HEIGHT); } // west is left, north is up const montreal = p([-73.6, 45.5])!; const tokyo = p([139.7, 35.7])!; const capeTown = p([18.4, -33.9])!; expect(montreal[0]).toBeLessThan(tokyo[0]); expect(montreal[1]).toBeLessThan(capeTown[1]); }); }); describe('buildWorldGeometry', () => { const geo = buildWorldGeometry(atlas); it('produces one path per country with ISO3 keys, without Antarctica', () => { expect(geo.countries.length).toBeGreaterThan(170); const iso = geo.countries.map((c) => c.iso3); expect(iso).toContain('USA'); expect(iso).toContain('FRA'); expect(iso).toContain('KOR'); expect(iso).toContain('XKX'); expect(iso).not.toContain('ATA'); expect(geo.countries.filter((c) => c.iso3 === null).map((c) => c.name).sort()).toEqual(['N. Cyprus', 'Somaliland']); for (const c of geo.countries) expect(c.d.length).toBeGreaterThan(5); expect(geo.sphere.startsWith('M')).toBe(true); }); it('projects lng/lat to viewBox pixels and rejects invalid coordinates', () => { const p = geo.project(-73.6, 45.5)!; expect(p[0]).toBeGreaterThan(0); expect(p[0]).toBeLessThan(MAP_WIDTH / 2); expect(p[1]).toBeLessThan(MAP_HEIGHT / 2); expect(geo.project(NaN, 10)).toBeNull(); expect(geo.project(10, 91)).toBeNull(); expect(geo.project(181, 0)).toBeNull(); }); });