/** * The session watch over a raster template a MapLibre map draws (FE-MAP-RASTERHEALTH-*). */ import { act, renderHook } from '@testing-library/react'; import { afterEach, describe, expect, it } from 'vitest'; import { GL_BASEMAP_SOURCE, mapLibreBasemap } from './glBasemap'; import { rasterTemplateFailed, resetRasterHealth, useRasterHealth, watchRasterHealth } from './rasterHealth'; const TEMPLATE = 'https://tile.openstreetmap.de/{z}/{x}/{y}.png'; const STYLE = mapLibreBasemap('trek:template', TEMPLATE, false, []).style; type Listener = (event: { sourceId?: string; tile?: unknown }) => void; /** Just enough of a GL map: the events the watch listens for, fired by hand. */ function fakeMap() { const listeners: Record = {}; const add = (type: string, listener: Listener) => { (listeners[type] ??= []).push(listener); }; return { on: add, once: add, fire: (type: string, event: { sourceId?: string; tile?: unknown } = {}) => listeners[type]?.forEach((listener) => listener(event)), }; } function violation(blockedURI: string): Event { return Object.assign(new Event('securitypolicyviolation'), { blockedURI }); } afterEach(() => resetRasterHealth()); describe('watchRasterHealth', () => { it('FE-MAP-RASTERHEALTH-001: twelve failed tiles without a success mark the template', () => { const map = fakeMap(); watchRasterHealth(map, STYLE); for (let i = 0; i < 11; i++) map.fire('error', { sourceId: GL_BASEMAP_SOURCE }); expect(rasterTemplateFailed(TEMPLATE)).toBe(false); map.fire('error', { sourceId: GL_BASEMAP_SOURCE }); expect(rasterTemplateFailed(TEMPLATE)).toBe(true); }); it('FE-MAP-RASTERHEALTH-002: one tile that arrived ends the watch, later failures are an outage', () => { const map = fakeMap(); watchRasterHealth(map, STYLE); map.fire('data', { sourceId: GL_BASEMAP_SOURCE, tile: {} }); for (let i = 0; i < 20; i++) map.fire('error', { sourceId: GL_BASEMAP_SOURCE }); expect(rasterTemplateFailed(TEMPLATE)).toBe(false); }); it("FE-MAP-RASTERHEALTH-003: errors of the map's other sources do not count", () => { const map = fakeMap(); watchRasterHealth(map, STYLE); for (let i = 0; i < 20; i++) map.fire('error', { sourceId: 'route' }); map.fire('data', { sourceId: GL_BASEMAP_SOURCE }); expect(rasterTemplateFailed(TEMPLATE)).toBe(false); // Still watching: removing the map ends it, so no later test meets this one's listener. map.fire('remove'); }); it('FE-MAP-RASTERHEALTH-004: a CSP refusal of one of its tiles marks the template at once', () => { watchRasterHealth(fakeMap(), STYLE); document.dispatchEvent(violation('https://tile.openstreetmap.de.evil.test/1/2/3.png')); expect(rasterTemplateFailed(TEMPLATE)).toBe(false); document.dispatchEvent(violation('https://tile.openstreetmap.de/5/16/10.png')); expect(rasterTemplateFailed(TEMPLATE)).toBe(true); }); it('FE-MAP-RASTERHEALTH-005: a removed map stops listening for refusals', () => { const map = fakeMap(); watchRasterHealth(map, STYLE); map.fire('remove'); document.dispatchEvent(violation('https://tile.openstreetmap.de/5/16/10.png')); expect(rasterTemplateFailed(TEMPLATE)).toBe(false); }); it('FE-MAP-RASTERHEALTH-006: a style URL is not watched', () => { const map = fakeMap(); watchRasterHealth(map, 'https://tiles.openfreemap.org/styles/liberty'); for (let i = 0; i < 20; i++) map.fire('error', { sourceId: GL_BASEMAP_SOURCE }); expect(rasterTemplateFailed(TEMPLATE)).toBe(false); }); it('FE-MAP-RASTERHEALTH-007: a mark re-renders the maps that read the health', () => { const { result } = renderHook(() => useRasterHealth()); const before = result.current; const map = fakeMap(); watchRasterHealth(map, STYLE); act(() => { for (let i = 0; i < 12; i++) map.fire('error', { sourceId: GL_BASEMAP_SOURCE }); }); expect(result.current).toBeGreaterThan(before); }); });