11import assert from "node:assert/strict" ;
22import { describe , it } from "node:test" ;
33import { syncLayer } from "../packages/map/src/layer-sync" ;
4- import { createPMTilesStoreLayer } from "../packages/map/src/pmtiles-layer" ;
4+ import {
5+ createPMTilesStoreLayer ,
6+ type PMTilesStoreLayerOptions ,
7+ } from "../packages/map/src/pmtiles-layer" ;
58
6- interface MapCall {
7- method : string ;
8- args : unknown [ ] ;
9- }
9+ /** A MapLibre layer definition as the sync passes it, read by key in the assertions below. */
10+ type AddedLayer = Record < string , unknown > & { id : string } ;
1011
1112/** Enough of a MapLibre map to record what a sync pass adds, starting from an empty style. */
1213function makeMapStub ( ) {
13- const calls : MapCall [ ] = [ ] ;
14+ const addedSources : { id : string ; source : Record < string , unknown > } [ ] = [ ] ;
15+ const addedLayers : AddedLayer [ ] = [ ] ;
1416 const sources = new Set < string > ( ) ;
1517 const layers = new Set < string > ( ) ;
1618 const map = {
1719 getStyle : ( ) => ( { layers : [ ...layers ] . map ( ( id ) => ( { id, type : "fill" } ) ) } ) ,
1820 getLayer : ( id : string ) => ( layers . has ( id ) ? { id, type : "fill" } : undefined ) ,
1921 getSource : ( id : string ) => ( sources . has ( id ) ? { id } : undefined ) ,
20- addSource : ( id : string , source : unknown ) => {
22+ addSource : ( id : string , source : Record < string , unknown > ) => {
2123 sources . add ( id ) ;
22- calls . push ( { method : "addSource" , args : [ id , source ] } ) ;
24+ addedSources . push ( { id, source } ) ;
2325 } ,
24- addLayer : ( layer : { id : string } ) => {
26+ addLayer : ( layer : AddedLayer ) => {
2527 layers . add ( layer . id ) ;
26- calls . push ( { method : "addLayer" , args : [ layer ] } ) ;
28+ addedLayers . push ( layer ) ;
2729 } ,
2830 removeLayer : ( ) => { } ,
2931 removeSource : ( ) => { } ,
@@ -32,69 +34,69 @@ function makeMapStub() {
3234 setPaintProperty : ( ) => { } ,
3335 setLayerZoomRange : ( ) => { } ,
3436 } ;
35- const added = ( method : string ) => calls . filter ( ( call ) => call . method === method ) ;
36- return { map, added } ;
37+ return { map, addedSources, addedLayers } ;
3738}
3839
39- const archive = {
40+ const archive : PMTilesStoreLayerOptions = {
4041 id : "layer-1" ,
4142 name : "Geologic units" ,
4243 url : "https://example.org/units.pmtiles" ,
43- tileType : "vector" as const ,
44+ tileType : "vector" ,
4445 sourceLayers : [ "units" ] ,
4546} ;
4647
4748describe ( "syncing a layer from createPMTilesStoreLayer" , ( ) => {
4849 it ( "adds the archive as a vector source and one MapLibre layer per declared id" , ( ) => {
4950 const layer = createPMTilesStoreLayer ( archive ) ;
50- const { map, added } = makeMapStub ( ) ;
51+ const { map, addedSources , addedLayers } = makeMapStub ( ) ;
5152
5253 syncLayer ( map as never , layer ) ;
5354
54- assert . deepEqual ( added ( "addSource" ) [ 0 ] ?. args , [
55- "layer-1" ,
56- { type : "vector" , url : "pmtiles://https://example.org/units.pmtiles" } ,
57- ] ) ;
55+ assert . deepEqual ( addedSources [ 0 ] , {
56+ id : "layer-1" ,
57+ source : { type : "vector" , url : "pmtiles://https://example.org/units.pmtiles" } ,
58+ } ) ;
5859 // The ids the builder promised are exactly the ones the sync creates: a layer whose
5960 // nativeLayerIds name something else renders nothing while claiming it renders.
6061 assert . deepEqual (
61- added ( "addLayer" ) . map ( ( call ) => ( call . args [ 0 ] as { id : string } ) . id ) ,
62+ addedLayers . map ( ( added ) => added . id ) ,
6263 layer . metadata . nativeLayerIds ,
6364 ) ;
64- for ( const call of added ( "addLayer" ) ) {
65- assert . equal ( ( call . args [ 0 ] as Record < string , unknown > ) [ "source-layer" ] , "units" ) ;
66- assert . equal ( ( call . args [ 0 ] as Record < string , unknown > ) . source , "layer-1" ) ;
65+ for ( const added of addedLayers ) {
66+ assert . equal ( added [ "source-layer" ] , "units" ) ;
67+ assert . equal ( added . source , "layer-1" ) ;
6768 }
6869 } ) ;
6970
7071 it ( "adds a raster archive as a raster source and its single layer" , ( ) => {
7172 const layer = createPMTilesStoreLayer ( { ...archive , tileType : "raster" , sourceLayers : [ ] } ) ;
72- const { map, added } = makeMapStub ( ) ;
73+ const { map, addedSources , addedLayers } = makeMapStub ( ) ;
7374
7475 syncLayer ( map as never , layer ) ;
7576
76- assert . equal ( ( added ( "addSource" ) [ 0 ] ?. args [ 1 ] as { type : string } ) . type , "raster" ) ;
77+ assert . equal ( addedSources [ 0 ] ?. source . type , "raster" ) ;
7778 assert . deepEqual (
78- added ( "addLayer" ) . map ( ( call ) => ( call . args [ 0 ] as { id : string } ) . id ) ,
79+ addedLayers . map ( ( added ) => added . id ) ,
7980 [ "layer-1-raster" ] ,
8081 ) ;
8182 } ) ;
8283
8384 it ( "renders a source layer whose name needs encoding in a layer id" , ( ) => {
8485 const layer = createPMTilesStoreLayer ( { ...archive , sourceLayers : [ "water lines" ] } ) ;
85- const { map, added } = makeMapStub ( ) ;
86+ const { map, addedLayers } = makeMapStub ( ) ;
8687
8788 syncLayer ( map as never , layer ) ;
8889
8990 assert . deepEqual (
90- added ( "addLayer" ) . map ( ( call ) => ( call . args [ 0 ] as { id : string } ) . id ) ,
91+ addedLayers . map ( ( added ) => added . id ) ,
9192 layer . metadata . nativeLayerIds ,
9293 ) ;
9394 // The source-layer keeps the archive's own name; only the id is encoded.
94- assert . equal (
95- ( added ( "addLayer" ) [ 0 ] ?. args [ 0 ] as Record < string , unknown > ) [ "source-layer" ] ,
96- "water lines" ,
97- ) ;
98- assert . equal ( ( layer . metadata . nativeLayerIds as string [ ] ) [ 0 ] , "layer-1-water_20lines-fill" ) ;
95+ assert . equal ( addedLayers [ 0 ] ?. [ "source-layer" ] , "water lines" ) ;
96+ assert . deepEqual ( layer . metadata . nativeLayerIds , [
97+ "layer-1-water_20lines-fill" ,
98+ "layer-1-water_20lines-line" ,
99+ "layer-1-water_20lines-circle" ,
100+ ] ) ;
99101 } ) ;
100102} ) ;
0 commit comments