|
| 1 | +import { validateStyleMin } from '@maplibre/maplibre-gl-style-spec'; |
| 2 | +import { defaultLayerSpecs, inspectLayerSpecs } from '@/components/map'; |
| 3 | +import { buildStyleBundle, findUnresolvedRefs } from '@/lib/styleBundle'; |
| 4 | + |
| 5 | +const variants = { explore: defaultLayerSpecs, inspect: inspectLayerSpecs }; |
| 6 | +const bundle = buildStyleBundle({ variants }); |
| 7 | +const entries = Object.entries(bundle.collections); |
| 8 | +const styles = entries.flatMap(([key, c]) => Object.entries(c.styles).map(([variant, style]) => [key, variant, style])); |
| 9 | + |
| 10 | +describe('style bundle', () => { |
| 11 | + it('has one collection per theme/type in the layer specs', () => { |
| 12 | + const expected = new Set( |
| 13 | + [...defaultLayerSpecs, ...inspectLayerSpecs] |
| 14 | + .filter((s) => s.metadata?.['overture:theme']) |
| 15 | + .map((s) => `${s.metadata['overture:theme']}/${s.metadata['overture:type']}`) |
| 16 | + ); |
| 17 | + expect(Object.keys(bundle.collections).sort()).toEqual([...expected].sort()); |
| 18 | + }); |
| 19 | + |
| 20 | + it('places every collection layer in exactly one style, in manifest order', () => { |
| 21 | + for (const [variant, specs] of Object.entries(variants)) { |
| 22 | + const collectionSpecs = specs.filter((s) => s.metadata?.['overture:theme']); |
| 23 | + const bundled = styles.filter(([, v]) => v === variant).flatMap(([, , style]) => style.layers.map((l) => l.id)); |
| 24 | + expect(bundled.sort()).toEqual(collectionSpecs.map((s) => s.id).sort()); |
| 25 | + } |
| 26 | + for (const [key, variant, style] of styles) { |
| 27 | + const order = style.layers.map((l) => l.id); |
| 28 | + const manifest = variants[variant].map((s) => s.id).filter((id) => order.includes(id)); |
| 29 | + expect([key, order]).toEqual([key, manifest]); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + it('only puts a collection\'s own layers in its styles', () => { |
| 34 | + for (const [key, , style] of styles) { |
| 35 | + for (const layer of style.layers) { |
| 36 | + expect(`${layer.metadata['overture:theme']}/${layer.metadata['overture:type']}`).toBe(key); |
| 37 | + } |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + it('produces valid standalone MapLibre styles', () => { |
| 42 | + for (const [key, variant, style] of styles) { |
| 43 | + const errors = validateStyleMin(style); |
| 44 | + expect([key, variant, errors]).toEqual([key, variant, []]); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + it('leaves no unresolved token references', () => { |
| 49 | + expect(findUnresolvedRefs(bundle)).toEqual([]); |
| 50 | + expect(findUnresolvedRefs({ paint: { color: '$unresolved' } })).toEqual(['paint.color']); |
| 51 | + }); |
| 52 | + |
| 53 | + it('points every source at its theme PMTiles with the release placeholder', () => { |
| 54 | + for (const [, , style] of styles) { |
| 55 | + for (const [name, source] of Object.entries(style.sources)) { |
| 56 | + expect(source.url).toBe(`pmtiles://https://tiles.overturemaps.org/{release}/${name}.pmtiles`); |
| 57 | + } |
| 58 | + for (const layer of style.layers) { |
| 59 | + expect(style.sources).toHaveProperty(layer.source); |
| 60 | + } |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + it('defaults to the first variant that styles the collection', () => { |
| 65 | + for (const [key, c] of entries) { |
| 66 | + const first = Object.keys(variants).find((v) => c.styles[v]); |
| 67 | + expect([key, c.default]).toEqual([key, first]); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + it('sets glyphs and sprite only when given', () => { |
| 72 | + const [, , style] = styles[0]; |
| 73 | + expect(style).not.toHaveProperty('glyphs'); |
| 74 | + expect(style).not.toHaveProperty('sprite'); |
| 75 | + |
| 76 | + const withAssets = buildStyleBundle({ variants, glyphs: 'https://g/{fontstack}/{range}.pbf', sprite: 'https://s/sprite' }); |
| 77 | + for (const c of Object.values(withAssets.collections)) { |
| 78 | + for (const s of Object.values(c.styles)) { |
| 79 | + expect(s.glyphs).toBe('https://g/{fontstack}/{range}.pbf'); |
| 80 | + expect(s.sprite).toBe('https://s/sprite'); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + it('rejects a tiles URL without the release placeholder', () => { |
| 86 | + expect(() => buildStyleBundle({ variants, tilesUrl: 'pmtiles://x/{theme}.pmtiles' })).toThrow('{release}'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('rejects a tiles URL without the theme placeholder', () => { |
| 90 | + expect(() => buildStyleBundle({ variants, tilesUrl: 'pmtiles://x/{release}/tiles.pmtiles' })).toThrow('{theme}'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('does not mutate the app layer specs', () => { |
| 94 | + const before = JSON.stringify(defaultLayerSpecs); |
| 95 | + const b = buildStyleBundle({ variants }); |
| 96 | + b.collections['base/water'].styles.explore.layers[0].paint = {}; |
| 97 | + expect(JSON.stringify(defaultLayerSpecs)).toBe(before); |
| 98 | + }); |
| 99 | +}); |
0 commit comments