mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
feat(cockpit): the owner flags load at startup from one generated 15 KB sheet; the loading status names the place packs while they load on first lookup
This commit is contained in:
1 parent
63aa45c520
commit
5a6cf22be1
35 files changed
+643
-216
No files matched your search
+93
-61
@@ -1,75 +1,59 @@
|
||||
// src/ui/ownerFlag.test.mjs — a satellite owner's mark: a country's flag with
|
||||
// its localized name, a text badge for an organisation, nothing when unknown.
|
||||
// its localized name, drawn from the owner flag sheet; a text badge for an
|
||||
// organisation; nothing when unknown.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { OWNER_COUNTRY_CODES } from '../layers/satellites/classes.js';
|
||||
import { OWNER_FLAG_SHEET } from './ownerFlagSheet.js';
|
||||
import {
|
||||
loadFlagUrl,
|
||||
OWNER_FLAG_SHEET_URL,
|
||||
ownerFlagStyle,
|
||||
ownerMark,
|
||||
preloadOwnerFlags,
|
||||
regionName,
|
||||
shownOwnerMark,
|
||||
} from './ownerFlag.js';
|
||||
|
||||
const FLAG = (code) => `/node_modules/flag-icons/flags/4x3/${code}.svg`;
|
||||
const LOADERS = Object.freeze({
|
||||
[FLAG('us')]: async () => '/assets/us-abc.svg',
|
||||
[FLAG('fr')]: async () => '/assets/fr-def.svg',
|
||||
});
|
||||
|
||||
test('an owner country shows its flag, named in the reader language', () => {
|
||||
assert.deepEqual(
|
||||
ownerMark({ countryCode: 'us', label: 'USA' }, { loaders: LOADERS }),
|
||||
{ kind: 'flag', code: 'US', label: 'United States' },
|
||||
);
|
||||
assert.deepEqual(ownerMark({ countryCode: 'us', label: 'USA' }), {
|
||||
kind: 'flag',
|
||||
code: 'US',
|
||||
label: 'United States',
|
||||
});
|
||||
assert.equal(
|
||||
ownerMark(
|
||||
{ countryCode: 'FR', label: null },
|
||||
{ locale: 'de', loaders: LOADERS },
|
||||
).label,
|
||||
ownerMark({ countryCode: 'FR', label: null }, { locale: 'de' }).label,
|
||||
'Frankreich',
|
||||
);
|
||||
});
|
||||
|
||||
test('an organisation without a country shows a text badge', () => {
|
||||
assert.deepEqual(
|
||||
ownerMark({ countryCode: null, label: 'ESA' }, { loaders: LOADERS }),
|
||||
{ kind: 'badge', label: 'ESA', title: 'ESA' },
|
||||
);
|
||||
assert.deepEqual(ownerMark({ countryCode: null, label: 'ESA' }), {
|
||||
kind: 'badge',
|
||||
label: 'ESA',
|
||||
title: 'ESA',
|
||||
});
|
||||
});
|
||||
|
||||
test('a country with no flag to show falls back to a badge', () => {
|
||||
test('a country with no flag on the sheet falls back to a badge', () => {
|
||||
assert.deepEqual(
|
||||
ownerMark({ countryCode: 'DE', label: 'Germany' }, { loaders: LOADERS }),
|
||||
ownerMark(
|
||||
{ countryCode: 'DE', label: 'Germany' },
|
||||
{ flags: new Set(['US']) },
|
||||
),
|
||||
{ kind: 'badge', label: 'Germany', title: 'Germany' },
|
||||
);
|
||||
// Natural Earth and UCS write -99 or names, never a flag code.
|
||||
assert.deepEqual(
|
||||
ownerMark(
|
||||
{ countryCode: '-99', label: 'Multinational' },
|
||||
{ loaders: LOADERS },
|
||||
),
|
||||
{ kind: 'badge', label: 'Multinational', title: 'Multinational' },
|
||||
);
|
||||
assert.deepEqual(ownerMark({ countryCode: '-99', label: 'Multinational' }), {
|
||||
kind: 'badge',
|
||||
label: 'Multinational',
|
||||
title: 'Multinational',
|
||||
});
|
||||
});
|
||||
|
||||
test('an unknown owner shows nothing', () => {
|
||||
assert.equal(ownerMark(null, { loaders: LOADERS }), null);
|
||||
assert.equal(
|
||||
ownerMark({ countryCode: null, label: ' ' }, { loaders: LOADERS }),
|
||||
null,
|
||||
);
|
||||
});
|
||||
|
||||
test('only the flag asked for loads', async () => {
|
||||
assert.equal(await loadFlagUrl('US', LOADERS), '/assets/us-abc.svg');
|
||||
assert.equal(await loadFlagUrl('DE', LOADERS), null);
|
||||
assert.equal(
|
||||
await loadFlagUrl('FR', {
|
||||
[FLAG('fr')]: async () => {
|
||||
throw new Error('offline');
|
||||
},
|
||||
}),
|
||||
null,
|
||||
);
|
||||
assert.equal(ownerMark(null), null);
|
||||
assert.equal(ownerMark({ countryCode: null, label: ' ' }), null);
|
||||
});
|
||||
|
||||
test('a region name comes from Intl, never the bare code', () => {
|
||||
@@ -77,12 +61,51 @@ test('a region name comes from Intl, never the bare code', () => {
|
||||
assert.equal(regionName('XX', 'en'), null);
|
||||
});
|
||||
|
||||
test('a flag that will not load falls back to its country code, never to nothing', () => {
|
||||
const flag = { kind: 'flag', code: 'us', label: 'United States' };
|
||||
test('the sheet holds every owner country a satellite can be marked with', () => {
|
||||
assert.deepEqual([...OWNER_FLAG_SHEET.codes], [...OWNER_COUNTRY_CODES]);
|
||||
assert.equal(
|
||||
OWNER_FLAG_SHEET.rows,
|
||||
Math.ceil(OWNER_FLAG_SHEET.codes.length / OWNER_FLAG_SHEET.columns),
|
||||
);
|
||||
// The PNG is as large as its layout says (width and height at bytes 16-23).
|
||||
const png = readFileSync(new URL(OWNER_FLAG_SHEET_URL));
|
||||
assert.equal(
|
||||
png.readUInt32BE(16),
|
||||
OWNER_FLAG_SHEET.columns * OWNER_FLAG_SHEET.cellWidthPx,
|
||||
);
|
||||
assert.equal(
|
||||
png.readUInt32BE(20),
|
||||
OWNER_FLAG_SHEET.rows * OWNER_FLAG_SHEET.cellHeightPx,
|
||||
);
|
||||
});
|
||||
|
||||
test('a flag is drawn from its cell of the sheet, at the size asked', () => {
|
||||
const columns = OWNER_FLAG_SHEET.columns;
|
||||
const index = OWNER_FLAG_SHEET.codes.indexOf('US');
|
||||
const style = ownerFlagStyle('US');
|
||||
assert.equal(style.backgroundImage, `url("${OWNER_FLAG_SHEET_URL}")`);
|
||||
assert.equal(
|
||||
style.backgroundSize,
|
||||
`${columns * 20}px ${OWNER_FLAG_SHEET.rows * 15}px`,
|
||||
);
|
||||
assert.equal(
|
||||
style.backgroundPosition,
|
||||
`${-(index % columns) * 20}px ${-Math.floor(index / columns) * 15}px`,
|
||||
);
|
||||
// The first flag sits at the corner.
|
||||
assert.equal(
|
||||
ownerFlagStyle(OWNER_FLAG_SHEET.codes[0]).backgroundPosition,
|
||||
'0px 0px',
|
||||
);
|
||||
assert.equal(ownerFlagStyle('XX'), null);
|
||||
});
|
||||
|
||||
test('the flag shows once the sheet is ready; a failed sheet shows the code', () => {
|
||||
const flag = { kind: 'flag', code: 'US', label: 'United States' };
|
||||
assert.equal(shownOwnerMark(flag), null, 'nothing while it loads');
|
||||
assert.deepEqual(shownOwnerMark(flag, { url: '/flags/us.svg' }), {
|
||||
assert.deepEqual(shownOwnerMark(flag, { ready: true }), {
|
||||
kind: 'flag',
|
||||
url: '/flags/us.svg',
|
||||
style: ownerFlagStyle('US'),
|
||||
label: 'United States',
|
||||
});
|
||||
assert.deepEqual(shownOwnerMark(flag, { failed: true }), {
|
||||
@@ -95,14 +118,23 @@ test('a flag that will not load falls back to its country code, never to nothing
|
||||
assert.equal(shownOwnerMark(null), null);
|
||||
});
|
||||
|
||||
test('flags ship as a map of URLs, not one JS chunk per flag', async () => {
|
||||
const { readFileSync } = await import('node:fs');
|
||||
const source = readFileSync(
|
||||
new URL('./ownerFlag.js', import.meta.url),
|
||||
'utf8',
|
||||
);
|
||||
// A lazy glob emits a stub chunk for each of ~270 flags; eager URLs ship
|
||||
// only strings, and each SVG still loads when an <img> shows it.
|
||||
assert.match(source, /eager:\s*true/);
|
||||
assert.match(source, /query:\s*'\?url&no-inline'/);
|
||||
test('the sheet loads once; a failed load is asked again', async () => {
|
||||
const images = [];
|
||||
const createImage = () => {
|
||||
const image = { onload: null, onerror: null, src: '' };
|
||||
images.push(image);
|
||||
return image;
|
||||
};
|
||||
const first = preloadOwnerFlags({ createImage });
|
||||
assert.equal(images.length, 1);
|
||||
assert.equal(images[0].src, OWNER_FLAG_SHEET_URL);
|
||||
assert.equal(preloadOwnerFlags({ createImage }), first, 'one load at a time');
|
||||
images[0].onerror();
|
||||
assert.equal(await first, false);
|
||||
const second = preloadOwnerFlags({ createImage });
|
||||
assert.equal(images.length, 2, 'asked again after a failure');
|
||||
images[1].onload();
|
||||
assert.equal(await second, true);
|
||||
assert.equal(preloadOwnerFlags({ createImage }), second, 'kept once loaded');
|
||||
assert.equal(images.length, 2);
|
||||
});
|
||||
Reference in new issue
Block a user