From 14d7ddf06bccf85e738f99e1ef762098ec6e9bf8 Mon Sep 17 00:00:00 2001 From: m3ayush Date: Sun, 18 Oct 2026 12:19:25 +0530 Subject: [PATCH] feat(radio): add configurable priority-country coverage --- .env.example | 5 ++ DATA_SOURCES.md | 2 + docs/CURRENT-STATE.md | 27 +++++++ server/providers/radio.js | 8 +- server/providers/radio/catalog.js | 115 +++++++++++++++++++++++++--- src/data/radioProxy.test.mjs | 120 ++++++++++++++++++++++++++++++ 6 files changed, 263 insertions(+), 14 deletions(-) diff --git a/.env.example b/.env.example index a2f8afa24..fdfd8bf48 100644 --- a/.env.example +++ b/.env.example @@ -248,3 +248,8 @@ OVERPASS_UPSTREAMS= # frame-ancestors sources such as "https://example.com", or * for any page. # Unset, no page may frame it. Other pages of the app can never be framed. # GEV_EMBED_FRAME_ANCESTORS= + +# Optional keyless Radio Browser coverage: up to 8 ISO alpha-2 countries. +# Each reserves up to 40 stations within the existing 750-station directory. +# Restart after changing. Leave blank for the existing global selection. +# RADIO_PRIORITY_COUNTRIES=IN,PL diff --git a/DATA_SOURCES.md b/DATA_SOURCES.md index d930bcb8f..0b012e812 100644 --- a/DATA_SOURCES.md +++ b/DATA_SOURCES.md @@ -170,6 +170,8 @@ The optional Local ADS-B layer and the broadcast-FM receiver use radio you recei - **City of Toronto RESCU cameras.** Keyless. The camera list is the City's public `tmcearthcameras.json` on `opendata.toronto.ca` (dataset "Traffic Cameras", Transportation Services), served as JSONP and unwrapped with an anchored pattern. This is the municipal network the Ontario 511 pack does not carry: downtown arterials plus the Gardiner, DVP, Allen and Lake Shore. The list publishes no frame URL, so frame URLs are BUILT from the documented `CameraImages/loc####.jpg` convention and a strictly-digits camera number, pinning the frame proxy to the portal origin by construction; the list fetch refuses redirects and bounds the body it buffers. Frames are fetched at request time and never stored or redistributed. The dataset publishes no camera facing: the `D1`-`D4` fields are the directions of the *comparison* images (static reference stills captured in 2016 under a different path), not a bearing for the live pan-tilt-zoom frame, so headings use the shared id-hash fallback at low confidence and are corrected with the in-app calibration gizmo. A camera that is temporarily out of service still answers 200 with a byte-identical "Image Unavailable" placeholder rather than an error (about 6% of the network at any moment), so such a camera reads as healthy; the frame it shows is the agency's, not a synthesized one. The licence requires the attribution statement above. Disable with `CCTV_TORONTO_ENABLED=0`. - **Camera frame content.** Public camera frames can contain people, vehicles and license plates, and the camera layer does nothing with them beyond putting them on the map. A successful upstream response is relayed as the provider served it: nothing in the camera pipeline blurs, redacts, enhances or restores it, and nothing there runs face, plate or object detection or otherwise recognises or classifies what is in the picture. (The ALPR layer described below maps where plate-reader cameras are, from locations contributors tagged in OpenStreetMap; it reads nothing from any camera frame.) When an upstream has no frame for a camera, what you see instead is a Google Street View still or the app's own placeholder card; the panel's source badge names which of them you are looking at, and a refresh that fails after a frame has arrived leaves the last good picture on screen rather than replacing it. The browser resamples each frame for display: onto a fixed 1920x1080 surface for the camera's projection plane in the scene, and onto a 192x108 canvas for the small ambient card. That scaling is the only change made to the picture, and the only pixels the camera layer reads back are a 64x36 downscale hashed to tell whether the picture has changed. Live streams are piped through; the still path reads the image into memory to pass it on — TxDOT wraps its JPEG in a JSON envelope, which is decoded there — and no frame is written to disk (the disk cache, `.gev-cache/`, holds feed JSON for other layers and never camera imagery). Outside that pipeline, the voice assistant is the one feature that sends imagery anywhere: when it answers a question about what is in view it may attach a downscaled screenshot of the rendered scene — including any camera plane on screen — to the request it sends to the voice model. Frames are fetched only from server-registered camera URLs — a client cannot hand the proxy a URL (see [SECURITY.md](SECURITY.md)). What the app does stays inside the line README draws: no named-person search, no face recognition, no tracking individuals. - **Transit (GTFS-Realtime).** The proxy requests only registered operator feeds, validates redirects, bounds responses, and uses conditional requests, short snapshot caching and failure backoff. All sixteen registered regions are live-enabled; each can be disabled independently. Vehicles use delayed playback between reports, with report age and waiting states shown separately. Session-local selected trails are available for every feed. MBTA additionally retains up to 15 minutes of recently observed positions in the running proxy, so available history can survive a browser reload; server restart clears it. History reads never fetch upstream, and other feeds have no proxy retention. Nothing is archived to disk or collected in the background. Operator text credits are included as a courtesy and do not imply endorsement. Heights are aligned to work with Google 3D tiles; subway markers are shown at street level because these feeds do not supply depth. The TTC feed is BusTime, the surface system: it carries buses and streetcars and no subway at all. About four in ten of its vehicles report no route at all (unassigned or deadheading), and those are shown with the feed default rather than a route-established mode. It sends no ETag or Last-Modified, so its conditional requests fall through to the ordinary snapshot cache. Three Golden Horseshoe neighbours run alongside it — MiWay, Hamilton HSR and Durham Region Transit — each bus-only and each polled only near its own region. Durham Region Transit publishes no bearing on any vehicle, so those sprites take their heading from observed movement like any other feed that omits it. Guelph, Kingston and Barrie extend the same pattern eastward and north. Hamilton, Guelph, Burlington and YRT publish route ids that are not the number on the vehicle, so those entries carry a route-name table taken from each operator’s own published GTFS; any id missing from a table renders unchanged rather than blank. A label is never invented: YRT runs a route `9101` that appears nowhere in its published routes, so it stays as the feed sends it. Burlington’s is the one that cannot be shortcut: `351` is route 1 while `3510` is route 10, so the table is a real join rather than a trimmed prefix. GO Transit / Metrolinx is deliberately absent: its REALTIME feed requires an API key issued by manual review, which the keyless admission rule excludes (its static schedule download is keyless, but carries no vehicle positions). Barrie and YRT/Viva each invite acceptance of their licence through a form, and each licence also states that using the data is itself acceptance; this deployment relies on that clause, which is recorded in both entries. Grand River Transit in Waterloo is absent for a different reason: it answers, but its feed header was months stale and it publishes no per-vehicle timestamps, so it would render vehicles that look live and are not. Ottawa’s OC Transpo falls to the same keyless admission rule as GO Transit — its realtime feed requires an API key. London Transit and Thunder Bay Transit publish vehicle positions over plain HTTP with no HTTPS endpoint available. Every other Ontario system — Brampton, Oakville, Niagara, Milton, Windsor, Greater Sudbury, UP Express and a dozen smaller operators — publishes static GTFS schedules only, with no realtime vehicle positions to register. Between the nine included and these exclusions, every Ontario realtime vehicle-position feed catalogued in the Mobility Database is accounted for. +- **Radio country coverage (optional).** Set `RADIO_PRIORITY_COUNTRIES=IN,PL` in the local `.env` and restart to include stations beyond the global click charts. The operator may choose up to eight unique ISO alpha-2 country codes; no key is needed. Each country adds one bounded query (at most 200 rows) and reserves up to 40 unique, usable stations inside the existing 750-station catalog. Country seeds precede specialist-tag seeds and global popularity fill; unused slots return to the shared pool. This is a coverage preference, not an exhaustive national directory or a guarantee of stations in every city. Countries with no eligible HTTPS MP3/AAC geolocated stations remain empty. An unset list preserves global selection. Country requests share the directory cache and three-worker pool; a failed country query cannot replace a healthy snapshot. Failed/degraded refreshes back off for one minute, with at most three mirrors per query. + - **Radio Browser.** `/api/radio/stations` discovers official API mirrors, makes bounded and coalesced healthy/geolocated HTTPS-station queries, caches the normalized public-domain directory for 45 minutes, and may serve the last good catalog for up to seven days during an outage. Refreshes must meet minimum accepted-query and station coverage before replacing a warm catalog; schema-valid responses whose rows all fail the product's health policy do not count as successful queries. A usable partial cold catalog is explicitly `DEGRADED`, and malformed or empty successful payloads are rejected atomically. Every directory and click-count request rejects redirects, validates all resolved addresses as globally routable (including reserved/documentation IPv4 and special/non-global IPv6 exclusions), and pins the TLS connection to a validated address. Only MP3/AAC non-HLS directory rows with public HTTPS stream targets are returned; favicons are intentionally omitted. Pressing play connects one browser audio element directly to the selected broadcaster and calls the directory's click counter through known-ID-only `POST /api/radio/click/:uuid`. GEV never proxies, caches, records, bundles, or redistributes audio. Radio Browser supplies station-level tags, not dependable current-song or upcoming-program metadata, so Radio filtering never claims either. Direct playback exposes the listener's IP address to the broadcaster, whose own stream terms apply. - **TomTom Traffic.** Optional and BYOK: without `TOMTOM_API_KEY` the traffic layer runs its built-in simulation and no TomTom data (or attribution) appears. With a key, flow vector tiles are fetched through the server-side `/api/tomtom` proxy (120 s cache + a daily tile-budget governor — `TOMTOM_DAILY_TILE_BUDGET`, default 6,000 — sized so a 31-day month stays inside TomTom's 200K monthly free allowance; still a configurable application safety ceiling, not a guarantee; TomTom's [current pricing](https://docs.tomtom.com/pricing/) lists 200K free tile requests per month) and the "Traffic flow data © TomTom" credit is registered in the Data attribution popover the moment live mode activates. TomTom data is served live and cached only transiently (≤120 s TTL under `.gev-cache/`, gitignored) — it is not bundled or redistributed. One 23 KB point-in-time tile snapshot is committed as a decode-test fixture (`src/data/fixtures/`, © TomTom, never served to the app). - **Routing (OSRM / FOSSGIS).** `/api/route` proxies the public OSRM instances FOSSGIS e.V. runs for the OpenStreetMap community (car, bike, foot profiles). They are keyless and intended for fair, interactive use — the proxy rate-limits clients (60/min, 200/min total), caps route span and response size, caches each route for 10 minutes, and identifies itself in the User-Agent. Heavy or automated use must run its own OSRM. Routes are computed from OpenStreetMap data (ODbL); the Directions layer and voice routes never redistribute them. A route the router cannot find is reported as such; the Directions layer draws nothing in that case, and voice routes fall back to a clearly labeled straight line. diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 9562dc9f5..014bfd849 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -5807,3 +5807,30 @@ datacenters, dams and the Nepal locator retain that shared credit while displaye Military fragments stitch by source identity. Different mapped parcels merge only when their footprints touch or contain one another, never across a seam tolerance gap. + + +### Radio priority-country coverage + +Optional `RADIO_PRIORITY_COUNTRIES` is a server-owned comma-separated list of +up to eight ISO alpha-2 codes (for example `IN,PL`). Restart after changing it. +It needs no API key and adds no POWER UP entry. An empty list preserves the +existing global/tag-based directory. Invalid configuration fails at startup. + +Each country adds one Radio Browser search capped at 200 rows, with existing +HTTPS/geolocation/codec validation and an additional matching-country check. +Up to 40 unique accepted stations per country are seeded before the specialist +tag queries and global popularity fill, within the unchanged 750-station cap. +No country gets exhaustive coverage; an empty valid country response is allowed. +The response's `coverage.priorityCountries` reports accepted query status and +selected counts when configured. The existing broad/specialist health policy +still applies separately; a failed country query marks the refresh degraded +and cannot evict a healthy cached catalog. Queries share the existing three-worker +pool, single-flight refresh and 45-minute cache. At most three mirrors are tried +per query; failed or partial refreshes retry after one minute. The seven-day +last-good expiry and known-station-only click reporting remain in force. + +Validation: `node --test src/data/radioProxy.test.mjs` covers configuration, +country quotas under global popularity pressure, duplicate records, concurrency, +cache reuse, country mismatch/oversize/empty responses, stale fallback and retry +backoff. `node scripts/qa-radio.mjs --url http://localhost:4173` covers the existing +750-station browser presentation and playback contract. diff --git a/server/providers/radio.js b/server/providers/radio.js index 5dd4b196f..983e61d09 100644 --- a/server/providers/radio.js +++ b/server/providers/radio.js @@ -7,9 +7,13 @@ export { publicRadioHttpsUrl, } from './radio/stations.js'; export function radioBrowserProxy() { - const middleware = createRadioProxyMiddleware(); + let middleware; + const initialize = () => + (middleware ||= createRadioProxyMiddleware({ + priorityCountries: process.env.RADIO_PRIORITY_COUNTRIES || '', + })); const install = (server) => { - server.middlewares.use('/api/radio', middleware); + server.middlewares.use('/api/radio', initialize()); }; return { name: 'radio-browser-proxy', diff --git a/server/providers/radio/catalog.js b/server/providers/radio/catalog.js index 36a3eb730..fa1b7f6be 100644 --- a/server/providers/radio/catalog.js +++ b/server/providers/radio/catalog.js @@ -1,3 +1,4 @@ +import { normalizeRadioCountryInput } from '../../../src/data/radioCountry.js'; import { lookup as lookupDns } from 'node:dns/promises'; import { randomUUID } from 'node:crypto'; @@ -26,6 +27,32 @@ import { RADIO_UUID_RE, RADIO_FALLBACK_MIRRORS, } from './constants.js'; +const PRIORITY_COUNTRY_LIMIT = 8; +const COUNTRY_QUERY_LIMIT = 200; +const COUNTRY_RESERVED_STATIONS = 40; +const REFRESH_RETRY_MS = 60_000; +const MAX_MIRROR_ATTEMPTS = 3; + +/** Validate an operator-owned list; malformed configuration must not silently widen queries. */ +export function parseRadioPriorityCountries(value = '') { + const entries = Array.isArray(value) ? value : String(value).split(','); + const countries = []; + for (const entry of entries) { + const code = String(entry).trim().toUpperCase(); + if (!code) continue; + if (!/^[A-Z]{2}$/.test(code) || !normalizeRadioCountryInput(code).valid) + throw new TypeError( + 'RADIO_PRIORITY_COUNTRIES requires ISO alpha-2 country codes', + ); + if (!countries.includes(code)) countries.push(code); + } + if (countries.length > PRIORITY_COUNTRY_LIMIT) + throw new RangeError( + 'RADIO_PRIORITY_COUNTRIES supports at most eight countries', + ); + return countries; +} + export async function mapRadioConcurrent(values, concurrency, mapper) { const results = new Array(values.length); let cursor = 0; @@ -48,7 +75,9 @@ export function createRadioProxyMiddleware({ fetchImpl = null, lookupImpl = lookupDns, now = Date.now, + priorityCountries = [], } = {}) { + const countries = parseRadioPriorityCountries(priorityCountries); let mirrorCache = { origins: [...RADIO_FALLBACK_MIRRORS], cachedAt: 0 }; let mirrorPromise = null; let catalogCache = null; @@ -60,6 +89,9 @@ export function createRadioProxyMiddleware({ const catalogInstance = randomUUID(); let servedStationIds = new Set(); let refreshPromise = null; + let retryAfter = 0; + let partialCatalog = null; + let refreshError = null; async function fetchJson(url, maxBytes = RADIO_RESPONSE_MAX_BYTES) { const destination = radioProxyDestination(url); @@ -138,7 +170,7 @@ export function createRadioProxyMiddleware({ async function fetchPath(pathname) { let lastError = null; - for (const origin of await mirrors()) { + for (const origin of (await mirrors()).slice(0, MAX_MIRROR_ATTEMPTS)) { try { return await fetchJson(`${origin}${pathname}`); } catch (error) { @@ -159,20 +191,27 @@ export function createRadioProxyMiddleware({ 'aviation', 'marine', 'traffic', - ]; + ].map((tag) => ({ tag })); + const baseQueryCount = queries.length; + queries.push(...countries.map((country) => ({ country }))); const outcomes = await mapRadioConcurrent( queries, 3, - async (tag, index) => { + async ({ tag, country }, index) => { const params = new URLSearchParams({ has_geo_info: 'true', is_https: 'true', hidebroken: 'true', order: 'clickcount', reverse: 'true', - limit: index === 0 ? '1800' : '220', + limit: country + ? String(COUNTRY_QUERY_LIMIT) + : index === 0 + ? '1800' + : '220', }); if (tag) params.set('tag', tag); + if (country) params.set('countrycode', country); try { const rows = await fetchPath(`/json/stations/search?${params}`); if (!Array.isArray(rows)) @@ -194,7 +233,12 @@ export function createRadioProxyMiddleware({ ); const stations = rows .map(normalizeRadioBrowserStation) - .filter(Boolean); + .filter( + (station) => + station && (!country || station.countryCode === country), + ); + if (country && rows.length > COUNTRY_QUERY_LIMIT) + throw new Error('Radio Browser exceeded the country query limit'); const requestedTag = cleanRadioText(tag, 80) .toLocaleLowerCase() .replace(/[_-]+/g, ' ') @@ -213,7 +257,9 @@ export function createRadioProxyMiddleware({ // Query coverage is based on accepted rows, not merely a payload that // happens to match the upstream schema. Specialist responses must // also contain an accepted station tagged for the requested category. - succeeded: stations.length > 0 && requestedTagCovered, + succeeded: country + ? rows.length === 0 || stations.length > 0 + : stations.length > 0 && requestedTagCovered, stations, }; } catch { @@ -235,9 +281,24 @@ export function createRadioProxyMiddleware({ seen.add(station.id); selected.push(station); }; + // Reserve country slots first; low-click regional stations cannot lose all + // representation to the global charts. Keep room for all eight specialist + // seeds (8 * 45) and global fill even at the eight-country limit (8 * 40). + for (const rows of resultSets.slice(baseQueryCount)) { + const ranked = [...rows].sort( + (a, b) => b.clickCount - a.clickCount || a.id.localeCompare(b.id), + ); + let added = 0; + for (const station of ranked) { + if (seen.has(station.id)) continue; + take(station); + if (++added >= COUNTRY_RESERVED_STATIONS) break; + } + } // Seed specialist station-tag queries before popularity fill so operational // categories remain represented even when global click charts skew musical. - for (const rows of resultSets.slice(1)) rows.slice(0, 45).forEach(take); + for (const rows of resultSets.slice(1, baseQueryCount)) + rows.slice(0, 45).forEach(take); resultSets .flat() .sort( @@ -245,12 +306,14 @@ export function createRadioProxyMiddleware({ ) .forEach(take); const timestamp = now(); - const successfulQueries = outcomes.filter( - (outcome) => outcome.succeeded, - ).length; + const successfulQueries = outcomes + .slice(0, baseQueryCount) + .filter((outcome) => outcome.succeeded).length; const broadQueryHealthy = outcomes[0].succeeded && outcomes[0].stations.length > 0; const healthReasons = []; + if (outcomes.slice(baseQueryCount).some((outcome) => !outcome.succeeded)) + healthReasons.push('priority-country-query-unhealthy'); if (!broadQueryHealthy) healthReasons.push('broad-query-unhealthy'); if (successfulQueries < RADIO_CATALOG_MIN_SUCCESSFUL_QUERIES) healthReasons.push('query-coverage-below-policy'); @@ -259,7 +322,18 @@ export function createRadioProxyMiddleware({ const degraded = healthReasons.length > 0; const coverage = { successfulQueries, - totalQueries: queries.length, + totalQueries: baseQueryCount, + ...(countries.length + ? { + priorityCountries: countries.map((country, index) => ({ + countryCode: country, + succeeded: outcomes[baseQueryCount + index].succeeded, + stationCount: selected.filter( + (station) => station.countryCode === country, + ).length, + })), + } + : {}), stationCount: selected.length, healthyStationMinimum: RADIO_CATALOG_HEALTHY_MIN_STATIONS, }; @@ -310,7 +384,24 @@ export function createRadioProxyMiddleware({ return { ...catalogCache, stale: false }; } if (!refreshPromise) { - refreshPromise = refreshCatalog().finally(() => { + refreshPromise = (async () => { + if (now() < retryAfter) { + if (refreshError) throw refreshError; + return partialCatalog; + } + try { + const catalog = await refreshCatalog(); + refreshError = null; + partialCatalog = catalog.degraded ? catalog : null; + retryAfter = catalog.degraded ? now() + REFRESH_RETRY_MS : 0; + return catalog; + } catch (error) { + refreshError = error; + partialCatalog = null; + retryAfter = now() + REFRESH_RETRY_MS; + throw error; + } + })().finally(() => { refreshPromise = null; }); } diff --git a/src/data/radioProxy.test.mjs b/src/data/radioProxy.test.mjs index 60463205b..a5ac3dee7 100644 --- a/src/data/radioProxy.test.mjs +++ b/src/data/radioProxy.test.mjs @@ -533,3 +533,123 @@ test('a mirror answering an invalid reason phrase rejects instead of crashing', const crashed = mockMirrorStatus(t, 200, 'OK\x7f'); await assert.rejects(Promise.race([fetchMirror(), crashed]), TypeError); }); + +test('priority-country configuration is bounded, normalized and validated before requests', async () => { + const { parseRadioPriorityCountries } = await import('../../server/providers/radio/catalog.js'); + assert.deepEqual(parseRadioPriorityCountries(' in, PL,in, '), ['IN', 'PL']); + assert.deepEqual(parseRadioPriorityCountries(''), []); + for (const value of ['ZZ', 'India', 'IN&limit=9999', 'US,CA,IN,PL,DE,FR,GB,JP,AU']) { + assert.throws(() => createProxy({ priorityCountries: value }), /RADIO_PRIORITY_COUNTRIES/); + } +}); + +test('low-click country stations survive a full global catalog, with bounded cached queries and deduplication', async () => { + const calls = []; + let active = 0; + let peak = 0; + const middleware = createProxy({ + priorityCountries: 'IN,PL', + fetchImpl: async (url) => { + const parsed = new URL(url); + calls.push(parsed); + if (parsed.pathname === '/json/servers') return responseJson([{ name: 'de1.api.radio-browser.info' }]); + if (parsed.pathname.startsWith('/json/url/')) return responseJson({ ok: true }); + peak = Math.max(peak, ++active); + await new Promise((resolve) => setTimeout(resolve, 1)); + active--; + const country = parsed.searchParams.get('countrycode'); + if (country) { + assert.equal(parsed.searchParams.get('limit'), '200'); + assert.equal(parsed.searchParams.get('is_https'), 'true'); + const rows = catalogRows(country === 'IN' ? '10000000' : '20000000', 'community', 60) + .map((row) => ({ ...row, countrycode: country, clickcount: 0 })); + return responseJson([rows[0], ...rows]); + } + return responseJson(catalogRows('30000000', queryTag(url) || 'news', 900)); + }, + }); + const responses = await Promise.all([invoke(middleware, '/stations'), invoke(middleware, '/stations')]); + const body = JSON.parse(responses[0].body); + assert.equal(body.degraded, false); + assert.equal(body.stations.length, 750); + assert.equal(new Set(body.stations.map((row) => row.id)).size, 750); + for (const country of ['IN', 'PL']) + assert.ok(body.stations.filter((row) => row.countryCode === country).length >= 40); + assert.ok(body.stations.some((row) => row.countryCode === 'US')); + assert.equal(calls.filter((url) => url.pathname.includes('/stations/')).length, 11); + assert.ok(peak <= 3); + const count = calls.length; + await invoke(middleware, '/stations'); + assert.equal(calls.length, count, 'country queries share the 45-minute cache'); + const id = body.stations.find((row) => row.countryCode === 'IN').id; + assert.equal((await invoke(middleware, `/click/${id}`, 'POST')).status, 204); +}); + +test('an unavailable country retains the last healthy snapshot instead of silently losing its stations', async () => { + let clock = 4_000_000; + let fail = false; + const middleware = createProxy({ + priorityCountries: ['IN'], now: () => clock, + fetchImpl: async (url) => { + if (String(url).includes('/json/servers')) return responseJson([{ name: 'de1.api.radio-browser.info' }]); + if (new URL(url).searchParams.has('countrycode')) { + if (fail) throw new Error('offline'); + return responseJson([station({ countrycode: 'IN', country: 'India', tags: 'community' })]); + } + return responseJson(healthyRowsForQuery(url)); + }, + }); + const first = JSON.parse((await invoke(middleware, '/stations')).body); + assert.equal(first.degraded, false); + clock += 46 * 60 * 1000; + fail = true; + const stale = JSON.parse((await invoke(middleware, '/stations')).body); + assert.equal(stale.stale, true); + assert.match(stale.degradedReason, /priority-country-query-unhealthy/); + assert.deepEqual(stale.stations, first.stations); + assert.equal(stale.acceptedGeneration, first.acceptedGeneration); + clock += 8 * 24 * 60 * 60 * 1000; + assert.equal((await invoke(middleware, '/stations')).status, 503); +}); + +test('country discovery rejects wrong-country and oversized responses, but accepts an honest empty country', async () => { + for (const mode of ['wrong', 'oversized', 'empty']) { + const middleware = createProxy({ + priorityCountries: ['IN'], + fetchImpl: async (url) => { + if (String(url).includes('/json/servers')) return responseJson([{ name: 'de1.api.radio-browser.info' }]); + if (new URL(url).searchParams.has('countrycode')) return responseJson( + mode === 'empty' ? [] : mode === 'wrong' ? [station()] : + catalogRows('10000000', 'community', 201).map((row) => ({ ...row, countrycode: 'IN' })), + ); + return responseJson(healthyRowsForQuery(url)); + }, + }); + const body = JSON.parse((await invoke(middleware, '/stations')).body); + assert.equal(body.degraded, mode !== 'empty'); + assert.equal(body.coverage.priorityCountries[0].stationCount, 0); + assert.equal(body.coverage.priorityCountries[0].succeeded, mode === 'empty'); + } +}); + + +test('failed cold country refreshes are coalesced and back off instead of hammering mirrors', async () => { + let clock = 9_000_000; + let calls = 0; + const middleware = createProxy({ + priorityCountries: 'IN', now: () => clock, + fetchImpl: async (url) => { + calls++; + if (String(url).includes('/json/servers')) return responseJson([{ name: 'de1.api.radio-browser.info' }]); + throw new Error('offline'); + }, + }); + assert.equal((await invoke(middleware, '/stations')).status, 503); + const initial = calls; + assert.ok(initial <= 31, 'ten queries try at most three mirrors, plus discovery'); + assert.equal((await invoke(middleware, '/stations')).status, 503); + assert.equal(calls, initial); + clock += 60_001; + assert.equal((await invoke(middleware, '/stations')).status, 503); + assert.ok(calls > initial); +});