forked from OurHike/OurHike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoiLayers.test.ts
More file actions
802 lines (686 loc) · 30 KB
/
Copy pathpoiLayers.test.ts
File metadata and controls
802 lines (686 loc) · 30 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { createExpression, featureFilter } from '@maplibre/maplibre-gl-style-spec'
import type { LayerSpecification } from '@maplibre/maplibre-gl-style-spec'
import { MockMap, resetMapLibreMock } from '../test/mocks/maplibre-gl'
import { POI_TYPES } from '../lib/config'
import { hiddenTypesFrom, onlyType, showAllTypes } from '../lib/waypointVisibility'
import {
buildPoiIcons,
POI_FALLBACK_COLOR,
POI_PIN_SIZE,
poiColor,
poiIconId,
siteMemberCombinations,
UNKNOWN_POI_TYPE,
} from './poiIcons'
import { SITE_ANCHOR_TYPES, SITE_MEMBERS_PROPERTY, siteMembersKey } from './poiSites'
import {
attachPoiFilter,
attachPoiData,
attachPoiIcons,
buildPoiDotLayer,
buildPoiLayer,
poiFeatureCollection,
poiFilter,
POI_DOT_COLOR_EXPRESSION,
POI_DOT_LAYER_ID,
POI_DOT_MIN_ZOOM,
POI_DOT_RADIUS_EXPRESSION,
POI_ICON_EXPRESSION,
POI_ICON_SIZE_EXPRESSION,
POI_ID_PROPERTY,
POI_LAYER_ID,
POI_PIN_MIN_ZOOM,
POI_SORT_KEY_EXPRESSION,
POI_SOURCE_ID,
} from './poiLayers'
import { POI_PRIORITY } from './poiPriority'
// These are EVALUATED rather than shape-asserted wherever MapLibre gives us
// the means to. An expression can have exactly the right array structure and
// still resolve to the wrong image, and a `match` with a missing arm produces
// no error at all - just a pin that never appears.
function evaluate(expression: unknown[], properties: Record<string, unknown>, zoom = 14) {
// The rootKey is only used to place errors in a style document; any stable
// string does.
const compiled = createExpression(expression, 'layers[0].layout.icon-image')
if (compiled.result === 'error') {
throw new Error(compiled.value.map((e) => e.message).join('; '))
}
return compiled.value.evaluate({ zoom }, { properties, type: 'Point' } as never)
}
function poi(type: string, confidence: 'high' | 'low' = 'high') {
return { poi_type: type, confidence }
}
const REGISTERED_ICON_IDS = new Set(buildPoiIcons().map((icon) => icon.id))
describe('the icon expression', () => {
it.each(POI_TYPES)('resolves %s to an image that was actually registered', (type) => {
// The failure this catches is silent: a `match` arm naming an image nobody
// registered draws nothing, logs once per tile, and looks exactly like
// "there are no POIs here".
for (const confidence of ['high', 'low'] as const) {
const resolved = evaluate(POI_ICON_EXPRESSION, poi(type, confidence))
expect(resolved).toBe(poiIconId(type, confidence))
expect(REGISTERED_ICON_IDS.has(resolved as string)).toBe(true)
}
})
it('falls through to the neutral pin for a type this build has never seen', () => {
// A category added upstream reaches the map as a neutral pin rather than
// as nothing, so new data does not wait on a client release to be visible.
const resolved = evaluate(POI_ICON_EXPRESSION, poi('yurt'))
expect(resolved).toBe(poiIconId(UNKNOWN_POI_TYPE, 'high'))
expect(REGISTERED_ICON_IDS.has(resolved as string)).toBe(true)
})
it('treats anything that is not an explicit "high" as unverified', () => {
// Matching lib/trailData.ts, which only counts an explicit 'high' as
// verified. Guessing the other way would vouch for a water source nobody
// has checked.
for (const confidence of ['low', '', 'unknown']) {
expect(evaluate(POI_ICON_EXPRESSION, poi('water', confidence as 'low'))).toBe(
poiIconId('water', 'low'),
)
}
})
})
describe('the dot rank', () => {
it('is a CIRCLE layer, which is the entire mechanism', () => {
// THE test in this file. MapLibre's collision engine is a property of
// symbol layers; a circle participates in no placement pass, so every
// feature renders at every camera. Changed to 'symbol' - which would look
// like a harmless refactor and would typecheck - this layer starts
// colliding, waypoints start disappearing again, and the only symptom is
// that the map is quietly lying once more.
expect(buildPoiDotLayer().type).toBe('circle')
})
it('reaches below the pin seam, because a dot makes no claim a seam protects', () => {
// #603, reversing what this test used to assert. The seam is about whether
// a PIN can say what it is without colliding; a dot says only "something is
// here", so that argument never covered it - and holding both ranks at one
// seam left the opening corridor view drawing the trail line and nothing
// else. features/POI_VISIBILITY.md had this open as a question and it is
// answered yes.
//
// The two ranks now stop in different places, deliberately, which is the
// thing a later reader is most likely to "tidy" back into agreement.
expect(buildPoiDotLayer().minzoom).toBe(POI_DOT_MIN_ZOOM)
expect(POI_DOT_MIN_ZOOM).toBeLessThan(POI_PIN_MIN_ZOOM)
expect(buildPoiLayer().minzoom).toBe(POI_PIN_MIN_ZOOM)
})
it('shrinks to a stipple at the corridor view rather than keeping its seam size', () => {
// What makes the line above honest. At z4 the corridor's waypoints sit
// within a few hundred pixels of trail line, so a dot sized for the seam
// would draw a solid bar and claim the trail is one continuous place.
const ramp = POI_DOT_RADIUS_EXPRESSION
const atDotFloor = ramp[ramp.indexOf(POI_DOT_MIN_ZOOM) + 1] as number
const atSeam = ramp[ramp.indexOf(POI_PIN_MIN_ZOOM) + 1] as number
expect(atDotFloor).toBeLessThan(atSeam)
expect(atDotFloor).toBeGreaterThan(0)
})
it('reads the same source as the pins, which is what makes it site-correct', () => {
// poiFeatureCollection already emits one feature per SITE, so sharing the
// source means a privy riding its shelter's pin does not also get a dot
// 40 m away claiming to be a second place. Nothing else enforces that.
// `source` off the union, which also holds background layers that have
// none - narrowed rather than asserted away, so this still fails if either
// layer ever stops being source-backed.
const sourceOf = (layer: LayerSpecification): string | undefined =>
'source' in layer ? layer.source : undefined
expect(sourceOf(buildPoiDotLayer())).toBe(POI_SOURCE_ID)
expect(sourceOf(buildPoiDotLayer())).toBe(sourceOf(buildPoiLayer()))
})
it('wears its category accent, from the same table the pin uses', () => {
for (const type of POI_TYPES) {
expect(evaluate(POI_DOT_COLOR_EXPRESSION, poi(type))).toBe(poiColor(type))
}
})
it('lands an unknown type on the fallback rather than on nothing', () => {
expect(evaluate(POI_DOT_COLOR_EXPRESSION, poi('yurt'))).toBe(POI_FALLBACK_COLOR)
})
it('stays small enough not to compete with a pin', () => {
const atSeam = evaluate(
POI_DOT_RADIUS_EXPRESSION,
poi('water'),
POI_PIN_MIN_ZOOM,
) as number
// Diameter against the pin's whole 38px. A dot that reads as a small pin
// is worse than no dot: it claims to say what is there, which is exactly
// what it cannot do.
expect(atSeam * 2).toBeLessThan(POI_PIN_SIZE / 2)
})
it('grows with the camera, like the pins do', () => {
const far = evaluate(
POI_DOT_RADIUS_EXPRESSION,
poi('water'),
POI_PIN_MIN_ZOOM,
) as number
const near = evaluate(POI_DOT_RADIUS_EXPRESSION, poi('water'), 16) as number
expect(far).toBeLessThan(near)
})
})
describe('density', () => {
it('draws no pins at all above the whole-corridor view', () => {
// The opening camera frames 2,197 miles. Eight hundred pins on it is a
// texture, not information, and letting the collision engine thin them
// would answer "which of these matters" by geometry.
//
// The seam is MEASURED - pipeline/spike_poi_seam.py - so this asserts the
// floor exists rather than restating the figure, which would be one number
// in two places.
//
// `>= 9` rather than `> 9`: the seam is now 9, the same number as the
// original hard floor but reached from the opposite direction - that floor
// drew nothing below itself, this one hands over to the corridor view. 9
// is the bound either way, because below it the corridor is a texture
// (POI_MIN_ZOOM's own argument, which was right about that).
expect(buildPoiLayer().minzoom).toBe(POI_PIN_MIN_ZOOM)
expect(POI_PIN_MIN_ZOOM).toBeGreaterThanOrEqual(9)
})
it('leaves the collision engine switched on, which is the whole density story', () => {
const layout = buildPoiLayer().layout as Record<string, unknown>
expect(layout['icon-allow-overlap']).toBe(false)
})
it('grows the pins as the hiker zooms in', () => {
const far = evaluate(
POI_ICON_SIZE_EXPRESSION,
poi('water'),
POI_PIN_MIN_ZOOM,
) as number
const near = evaluate(POI_ICON_SIZE_EXPRESSION, poi('water'), 14) as number
expect(far).toBeLessThan(near)
expect(near).toBe(1)
})
it('gives water the best sort key, so it is the pin that survives a collision', () => {
// Not a visual preference. When two pins cannot both be placed, the one
// that stays should be the one a hiker most needs to know about, and
// MapLibre places lower sort keys first.
const keys = [...POI_TYPES, 'yurt'].map(
(type) => [type, evaluate(POI_SORT_KEY_EXPRESSION, poi(type)) as number] as const,
)
const water = keys.find(([type]) => type === 'water')?.[1]
expect(water).toBe(0)
for (const [type, key] of keys) {
if (type !== 'water') expect(key).toBeGreaterThan(water as number)
}
})
it('ranks an unknown type below every known one', () => {
expect(evaluate(POI_SORT_KEY_EXPRESSION, poi('yurt'))).toBeGreaterThan(
Math.max(
...POI_TYPES.map((t) => evaluate(POI_SORT_KEY_EXPRESSION, poi(t)) as number),
),
)
})
it('covers every published POI type in the priority order', () => {
// A type missing here would silently take the fallback rank, which for a
// future water-adjacent category is the wrong answer by default.
for (const type of POI_TYPES) expect(POI_PRIORITY).toContain(type)
})
it('places a vista behind every other category, however many of them there are', () => {
// The densest layer ATC publishes: 1,223 vistas against 2,532 POIs of
// every other kind put together. Ranked anywhere but last, the pins that
// survive a crowded ridge are decided by how many of them there are
// rather than by what a hiker needs - and losing a spring to an overlook
// is exactly the trade this ordering exists to refuse.
const viewpoint = evaluate(POI_SORT_KEY_EXPRESSION, poi('viewpoint')) as number
for (const type of POI_TYPES) {
if (type !== 'viewpoint') {
expect(evaluate(POI_SORT_KEY_EXPRESSION, poi(type))).toBeLessThan(viewpoint)
}
}
})
})
describe('the pin layer', () => {
it('asks for no text, because there is no font to render it with offline', () => {
// The OFFLINE style declares no `glyphs` URL - it cannot, there is no
// network on a mountain. MapLibre draws icons happily without one and
// cannot draw a single character of a label. A `text-field` added here
// would fail at the top of a hill and nowhere else.
//
// The live sheet does declare one, for its own OSM labels, and that is
// exactly why the pin layer must not lean on it: pins are drawn on both
// backgrounds, and a label that renders in town and vanishes on the ridge
// is worse than one that was never there.
const layout = buildPoiLayer().layout as Record<string, unknown>
expect(layout['text-field']).toBeUndefined()
})
it('reads its pins from the POI source', () => {
const layer = buildPoiLayer()
expect(layer.type).toBe('symbol')
expect('source' in layer && layer.source).toBe(POI_SOURCE_ID)
})
})
describe('poiFeatureCollection', () => {
const pois = [
{ id: 'w1', type: 'water', lat: 39.3, lon: -77.1, confidence: 'high' as const },
{ id: 's1', type: 'shelter', lat: 40.1, lon: -76.4, confidence: 'low' as const },
]
it('writes coordinates as [lon, lat], which is the order GeoJSON means', () => {
// Reversed, every pin in the Appalachians lands in the Indian Ocean, and
// nothing in the type system objects - both are numbers.
const [first] = poiFeatureCollection(pois).features
expect(first.geometry.coordinates).toEqual([-77.1, 39.3])
})
it('carries the attributes the style matches on, and the id to look up by', () => {
const [, shelter] = poiFeatureCollection(pois).features
expect(shelter.id).toBe('s1')
expect(shelter.properties).toEqual({
poi_type: 'shelter',
confidence: 'low',
[POI_ID_PROPERTY]: 's1',
// Always present, empty where the pin carries nothing (#524). Asserted
// exactly rather than loosely, which is why this test had to change when
// the property arrived - a `toMatchObject` here would have let a fourth
// property appear unnoticed.
[SITE_MEMBERS_PROPERTY]: '',
})
})
// One pin per site (#524). The mechanism lives in map/poiSites.ts and is
// tested there; what only this file can catch is the source failing to apply
// it, which would leave every member competing for a box exactly as before.
it('resolves every site pin to an image that was actually registered', () => {
// THE FAILURE THIS CATCHES, and the reason it EVALUATES the expression
// rather than reading it: MapLibre draws a missing image as NOTHING, logging
// once per tile. A site pin asking for an id nobody built is a shelter that
// vanishes from the map entirely - strictly worse than the privy problem
// #524 is fixing.
for (const type of SITE_ANCHOR_TYPES) {
for (const members of siteMemberCombinations()) {
for (const confidence of ['high', 'low'] as const) {
const resolved = evaluate(POI_ICON_EXPRESSION, {
...poi(type, confidence),
[SITE_MEMBERS_PROPERTY]: siteMembersKey(members),
})
const label = `${type}/${confidence}/${members.join('+')}`
expect(REGISTERED_ICON_IDS, label).toContain(resolved)
// And it must be the SITE image, not merely A registered one. Asserting
// only "registered" passed while the expression resolved every site pin
// to the PLAIN icon - a shelter carrying a privy drawing a bare shelter
// pin and saying nothing, which is the failure this whole change exists
// to prevent. Caught by mutating the arm, not by reading it.
expect(resolved, label).toBe(poiIconId(type, confidence, members))
expect(resolved, label).not.toBe(poiIconId(type, confidence))
}
}
}
})
it('still resolves a pin carrying nothing to the plain image', () => {
const resolved = evaluate(POI_ICON_EXPRESSION, {
...poi('shelter', 'high'),
[SITE_MEMBERS_PROPERTY]: '',
})
expect(resolved).toBe(poiIconId('shelter', 'high'))
})
it('drops a site member from the source rather than letting it lose a collision', () => {
const collection = poiFeatureCollection([
{
id: 'shelter',
type: 'shelter',
lat: 39,
lon: -77,
confidence: 'high',
siteId: 'site_1',
siteRole: 'anchor',
},
{
id: 'privy',
type: 'privy',
lat: 39.0004,
lon: -77,
confidence: 'high',
siteId: 'site_1',
siteRole: 'member',
},
])
expect(collection.features.map((f) => f.id)).toEqual(['shelter'])
})
it('tells the style what the surviving pin is carrying', () => {
const collection = poiFeatureCollection([
{
id: 'shelter',
type: 'shelter',
lat: 39,
lon: -77,
confidence: 'high',
siteId: 'site_1',
siteRole: 'anchor',
},
{
id: 'privy',
type: 'privy',
lat: 39.0004,
lon: -77,
confidence: 'high',
siteId: 'site_1',
siteRole: 'member',
},
{
id: 'water',
type: 'water',
lat: 39.0005,
lon: -77,
confidence: 'low',
siteId: 'site_1',
siteRole: 'member',
},
])
expect(collection.features[0].properties[SITE_MEMBERS_PROPERTY]).toBe('privy+water')
})
it('puts the POI id somewhere a tap can still read it', () => {
// The gotcha, and the reason the id is duplicated into the properties at
// all: MapLibre runs a string feature id through parseInt (FeatureWrapper,
// maplibre-gl 6), so every id the pipeline publishes reaches a rendered
// feature as NaN. A pin whose id only lived in the GeoJSON `id` field
// could be drawn perfectly and never be identified again.
const published = [
{ id: 'atc_shelters:0f8a-4c11', type: 'shelter', lat: 44, lon: -70 },
].map((poi) => ({ ...poi, confidence: 'high' as const }))
const [feature] = poiFeatureCollection(published).features
expect(Number.parseInt(feature.id, 10)).toBeNaN()
expect(feature.properties[POI_ID_PROPERTY]).toBe('atc_shelters:0f8a-4c11')
})
it('produces a collection every feature of which the icon expression can resolve', () => {
for (const feature of poiFeatureCollection(pois).features) {
expect(
REGISTERED_ICON_IDS.has(
evaluate(POI_ICON_EXPRESSION, feature.properties) as string,
),
).toBe(true)
}
})
it('is empty for no POIs rather than undefined', () => {
expect(poiFeatureCollection([])).toEqual({ type: 'FeatureCollection', features: [] })
})
})
describe('hiding a category', () => {
function passes(hidden: string[], type: string): boolean {
const { filter } = featureFilter(
poiFilter(new Set(hidden)) as never,
'layers[0].filter',
)
return filter(
{ zoom: 14 } as never,
{ properties: poi(type), type: 1 } as never,
null as never,
)
}
it('shows everything when nothing is hidden', () => {
for (const type of POI_TYPES) expect(passes([], type)).toBe(true)
})
it('drops exactly the hidden category and nothing else', () => {
expect(passes(['water'], 'water')).toBe(false)
expect(passes(['water'], 'shelter')).toBe(true)
})
it('hides several categories at once', () => {
expect(passes(['water', 'campsite'], 'water')).toBe(false)
expect(passes(['water', 'campsite'], 'campsite')).toBe(false)
expect(passes(['water', 'campsite'], 'resupply')).toBe(true)
})
it('is stable regardless of the order the hiker tapped the rows in', () => {
// The filter is handed to MapLibre on every toggle; two orderings of the
// same set producing two different filters would re-evaluate every feature
// for no reason.
expect(poiFilter(new Set(['water', 'campsite']))).toEqual(
poiFilter(new Set(['campsite', 'water'])),
)
})
})
// The source and the filter TOGETHER, which is the only place this bug is
// visible (#607). Neither half can catch it alone: the composition can be right
// about which POI carries the pin while the filter takes that pin off the map,
// and the filter can be right about which types survive while the source never
// offered the privy in the first place. What is asserted here is what reaches
// the hiker's screen.
describe('filtering the legend down to one category', () => {
const SITE = [
{
id: 'shelter',
type: 'shelter',
lat: 39,
lon: -77,
confidence: 'high' as const,
siteId: 'site_1',
siteRole: 'anchor',
},
{
id: 'privy',
// 42 m from its shelter, which is the median in features/POI_SITES.md and
// the reason it cannot be drawn below z16 while it competes for a box.
type: 'privy',
lat: 39.0004,
lon: -77,
confidence: 'high' as const,
siteId: 'site_1',
siteRole: 'member',
},
]
function drawnPins(shown: string[]): string[] {
const hiddenTypes = hiddenTypesFrom(shown)
const { filter } = featureFilter(poiFilter(hiddenTypes) as never, 'layers[0].filter')
return poiFeatureCollection(SITE, { hiddenTypes })
.features.filter((feature) =>
filter(
{ zoom: 14 } as never,
{ properties: feature.properties, type: 1 } as never,
null as never,
),
)
.map((feature) => feature.id)
}
it('draws the privy once its shelter has been filtered out', () => {
// THE REGRESSION. Before this, both halves fired at once and the map drew
// NOTHING: the privy was removed from the source as riding a shelter pin,
// and the shelter pin was removed by the filter. Over the real corridor that
// is 284 of 316 privies gone from the one control built to find them.
expect(drawnPins(onlyType('privy'))).toEqual(['privy'])
})
it('still draws the shelter, and only the shelter, when nothing is hidden', () => {
expect(drawnPins(showAllTypes())).toEqual(['shelter'])
})
it('draws the shelter and not the privy when only shelters are asked for', () => {
// The other direction, and it must still fold: the privy is not promoted
// just because a filter is in force, only because the pin it rides has gone.
expect(drawnPins(onlyType('shelter'))).toEqual(['shelter'])
})
it('resolves the promoted pin to an image that was actually registered', () => {
// A promoted member is asked for by an expression built for anchors. A privy
// is not a SITE_ANCHOR_TYPE, so it has no member variants - and an id nobody
// built draws as nothing at all, which would turn this fix into the same
// blank map by another route.
const hiddenTypes = hiddenTypesFrom(onlyType('privy'))
const [promoted] = poiFeatureCollection(SITE, { hiddenTypes }).features
const resolved = evaluate(POI_ICON_EXPRESSION, promoted.properties)
expect(REGISTERED_ICON_IDS).toContain(resolved)
expect(resolved).toBe(poiIconId('privy', 'high'))
})
})
describe('the "Verified?" filter', () => {
function passes(
confidence: 'high' | 'low',
verifiedOnly: boolean,
hidden: string[] = [],
): boolean {
const { filter } = featureFilter(
poiFilter(new Set(hidden), verifiedOnly) as never,
'layers[0].filter',
)
return filter(
{ zoom: 14 } as never,
{ properties: poi('water', confidence), type: 1 } as never,
null as never,
)
}
it('draws both confidences while it is off', () => {
// Off is the default, and deliberately: an unconfirmed spring is still the
// best information anyone has about that spring.
expect(passes('high', false)).toBe(true)
expect(passes('low', false)).toBe(true)
})
it('drops exactly the unverified pins while it is on', () => {
expect(passes('high', true)).toBe(true)
expect(passes('low', true)).toBe(false)
})
it('composes with the hidden categories rather than replacing them', () => {
// Two controls, one filter. A verified spring in a hidden category stays
// hidden - if these were two filters the second write would win and the
// legend's category toggles would silently stop working.
expect(passes('high', true, ['water'])).toBe(false)
expect(passes('high', false, ['water'])).toBe(false)
})
it('keeps one expression shape whether it is on or off', () => {
// Same reasoning as the empty hidden set above: "showing everything" must
// not be a second code path that can drift from the one doing the work.
const off = poiFilter(new Set(), false) as unknown[]
const on = poiFilter(new Set(), true) as unknown[]
expect(off[0]).toBe('all')
expect(on[0]).toBe('all')
expect(off).toHaveLength(on.length)
})
})
describe('pushing all of it onto a live map', () => {
let map: MockMap
beforeEach(() => {
resetMapLibreMock()
map = new MockMap({})
// Both ranks, because the real style carries both (#597) and
// attachPoiFilter waits for both before writing. A stub holding only the
// pin layer would make every filter test here pass by never running.
map.layerIds = [POI_LAYER_ID, POI_DOT_LAYER_ID]
map.sourceIds = [POI_SOURCE_ID]
})
afterEach(() => {
vi.restoreAllMocks()
})
it('registers every pin image once the style is up', () => {
attachPoiIcons(map as never)
map.emit('load')
for (const { id } of buildPoiIcons()) expect(map.images.has(id)).toBe(true)
})
it('registers them at 2x, so a 60px badge is not drawn 60px wide', () => {
attachPoiIcons(map as never)
map.emit('load')
expect(map.imageOptions.get(poiIconId('water', 'high'))).toEqual({ pixelRatio: 2 })
})
it('registers immediately when the style has already loaded', () => {
// A style that finished before this ran will never fire `load` again.
// Waiting on the event alone leaves the map permanently pinless on
// exactly the fast path.
map.styleLoaded = true
attachPoiIcons(map as never)
expect(map.images.size).toBeGreaterThan(0)
})
it('does nothing after detaching, even if the layer arrives late', () => {
map.layerIds = []
const detach = attachPoiIcons(map as never)
detach()
map.layerIds = [POI_LAYER_ID]
map.emit('styledata')
expect(map.images.size).toBe(0)
expect(map.listenerCount('styledata')).toBe(0)
})
it('honours a detach that lands part-way through the style event itself', () => {
// Not hypothetical: MapLibre dispatches to a snapshot of its listeners, so
// an earlier handler unmounting the map screen removes this one from the
// map and cannot remove it from the snapshot. Without the detached check,
// that writes images onto a map React has already torn down.
map.layerIds = []
let detach = () => {}
map.on('styledata', () => detach())
detach = attachPoiIcons(map as never)
map.layerIds = [POI_LAYER_ID]
map.emit('styledata')
expect(map.images.size).toBe(0)
})
it('still lands the POIs when the style is busy at the moment they arrive', () => {
// The bug (#129). The gate asked whether the WHOLE style was loaded and
// waited on `load` when it was not - but `load` fires exactly once, while
// isStyleLoaded() goes false again on every tile fetch, every setData and
// every source reload. POIs arrive from IndexedDB once. One landing in
// such a window registered a listener for an event that had already
// happened, and the pins never appeared at all, for the life of the map,
// while the legend went on listing what was missing.
map.sourceIds = []
map.emit('load')
map.styleLoaded = false
const pois = [
{ id: 'w1', type: 'water', lat: 39.3, lon: -77.1, confidence: 'high' as const },
]
attachPoiData(map as never, pois)
expect(map.sourceData.get(POI_SOURCE_ID)).toBeUndefined()
map.sourceIds = [POI_SOURCE_ID]
map.emit('styledata')
expect(map.sourceData.get(POI_SOURCE_ID)).toEqual(poiFeatureCollection(pois))
})
it('does not re-register images a previous map screen already added', () => {
// Images outlive a style reload and MapLibre throws on a duplicate id.
// Every trip through the More tab builds a new map, so this is the
// ordinary path, not an edge case.
map.styleLoaded = true
attachPoiIcons(map as never)
const addImage = vi.spyOn(map, 'addImage')
attachPoiIcons(map as never)
expect(addImage).not.toHaveBeenCalled()
expect(map.images.size).toBe(buildPoiIcons().length)
})
it('pushes the POIs into the source as GeoJSON', () => {
map.styleLoaded = true
attachPoiData(map as never, [
{ id: 'w1', type: 'water', lat: 39.3, lon: -77.1, confidence: 'high' },
])
expect(map.sourceData.get(POI_SOURCE_ID)).toEqual(
poiFeatureCollection([
{ id: 'w1', type: 'water', lat: 39.3, lon: -77.1, confidence: 'high' },
]),
)
})
it('applies the hidden set as a filter on the pin layer', () => {
map.styleLoaded = true
attachPoiFilter(map as never, new Set(['water']))
expect(map.filters.get(POI_LAYER_ID)).toEqual(poiFilter(new Set(['water'])))
})
it('carries the "Verified?" toggle onto the same layer filter', () => {
map.styleLoaded = true
attachPoiFilter(map as never, new Set(['water']), true)
expect(map.filters.get(POI_LAYER_ID)).toEqual(poiFilter(new Set(['water']), true))
})
it('hides a type on BOTH ranks, so no dot outlives the pin it belonged to', () => {
// The failure this exists for is silent: hide privies, the pins go, and a
// stipple of privy dots stays behind saying the legend is lying. Nothing
// throws, nothing logs, and the only symptom is on a screen.
map.styleLoaded = true
attachPoiFilter(map as never, new Set(['privy']), true)
const expected = poiFilter(new Set(['privy']), true)
expect(map.filters.get(POI_LAYER_ID)).toEqual(expected)
expect(map.filters.get(POI_DOT_LAYER_ID)).toEqual(expected)
})
it('waits for both ranks rather than filtering whichever arrived first', () => {
// A style mid-reload can hold one layer and not the other. Writing to the
// one that exists would leave the two ranks showing different categories
// until something else happened to trigger a re-filter.
map.styleLoaded = true
map.layerIds = [POI_LAYER_ID]
attachPoiFilter(map as never, new Set(['privy']))
expect(map.filters.has(POI_LAYER_ID)).toBe(false)
})
it('keeps the map alive when a write fails, and says so', () => {
// These run inside React effects on the map screen. An exception here
// would take the whole map down over a pin, which is the one outcome
// worse than a missing pin.
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
// A layer that IS there and still refuses the write - a style swapped out
// from under the call. A layer that is merely absent is a different state
// now: it means "not yet", and waiting is the right answer to it.
vi.spyOn(map, 'setFilter').mockImplementation(() => {
throw new Error('style replaced mid-write')
})
expect(() => attachPoiFilter(map as never, new Set(['water']))).not.toThrow()
expect(warn).toHaveBeenCalled()
})
it('leaves no load listener behind when detached before the style loads', () => {
for (const detach of [
attachPoiIcons(map as never),
attachPoiData(map as never, []),
attachPoiFilter(map as never, new Set()),
]) {
detach()
}
expect(map.listenerCount('load')).toBe(0)
})
})