adaptative attribution

This commit is contained in:
vcoppe committed 2026-12-04 09:47:48 +01:00
1 parent 4b5b6b8a20
commit 22a269a6dc
5 files changed
+264 -92

No files matched your search

@@ -3,7 +3,14 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { describe, it } from 'node:test';
import { fallbackNeeded, planTile, type CountryTiles } from './open-satellite';
import * as maplibregl from 'maplibre-gl';
import {
attributionsOf,
fallbackNeeded,
planTile,
trackOpenSatelliteAttribution,
type CountryTiles,
} from './open-satellite';
const fixtures = new URL('../../../test/fixtures/satellite-countries/', import.meta.url);
@@ -206,3 +213,97 @@ describe('fallbackNeeded', () => {
assert.equal(await fallbackNeeded(12, 2134, 1430, tiles), true);
});
});
describe('attributionsOf', () => {
const credits = async (tilesInView: [number, number, number][]) => {
const { open, fallback } = await attributionsOf(
tilesInView.map(([z, x, y]) => ({ z, x, y })),
savedTiles().tiles
);
return { open: open.replace(/<[^>]*>/g, ''), fallback: fallback.replace(/<[^>]*>/g, '') };
};
it('credits only the imagery of the whole world up to zoom 12', async () => {
assert.deepEqual(await credits([tile(6.63, 46.52, 12)]), {
open: 'IGN-F/Géoplateforme',
fallback: '',
});
});
it('credits the countries in view, each once', async () => {
// Paris, twice, and Basel
assert.deepEqual(
await credits([tile(2.35, 48.85, 15), tile(2.35, 48.85, 14), [13, 4268, 2861]]),
{ open: 'swisstopo, IGN-F/Géoplateforme', fallback: '© MapTiler' }
);
assert.equal((await credits([tile(-94.6075, 39.11, 13)])).open, 'USDA');
});
it('credits only the fallback far from every country', async () => {
assert.deepEqual(await credits([tile(-0.12, 51.5, 14)]), {
open: '',
fallback: '© MapTiler',
});
});
it('credits the imagery of the whole world for the far tiles of a tilted map', async () => {
assert.deepEqual(await credits([tile(-0.12, 51.5, 14), tile(-0.12, 51.5, 11)]), {
open: 'IGN-F/Géoplateforme',
fallback: '© MapTiler',
});
});
});
describe('trackOpenSatelliteAttribution', () => {
it('changes the credits of the sources with the view, and has the control read them', async () => {
const map = new (class extends maplibregl.Evented {})() as unknown as maplibregl.Map;
const fired: string[] = [];
const sources: Record<string, { tiles: string[]; attribution?: string }> = {};
const add = (id: string, protocol: string) => {
const spec = { type: 'raster', tiles: [`${protocol}://{z}/{x}/{y}`] };
const tell = () =>
map.fire(
new maplibregl.MapSourceDataEvent('sourcedata', {
sourceDataType: 'metadata',
sourceId: id,
source: spec as maplibregl.RasterSourceSpecification,
})
);
sources[id] = Object.assign({}, spec, {
fire: (event: maplibregl.MapSourceDataEvent) => {
assert.equal(event.sourceDataType, 'metadata');
fired.push(id);
tell();
},
});
tell();
};
let view = tile(-0.12, 51.5, 10);
Object.assign(map, {
getSource: (id: string) => sources[id],
coveringTiles: () => [{ canonical: { z: view[0], x: view[1], y: view[2] } }],
});
const settled = () => new Promise((resolve) => setTimeout(resolve, 50));
trackOpenSatelliteAttribution(map);
add('satellite', 'opensat');
add('fallback', 'opensat-fallback');
await settled();
assert.match(sources.satellite.attribution ?? '', /^<a [^>]*>IGN-F\/Géoplateforme<\/a>$/);
assert.equal(sources.fallback.attribution, undefined);
assert.deepEqual(fired, ['satellite']);
// London, only on the fallback
view = tile(-0.12, 51.5, 14);
map.fire('moveend');
await settled();
assert.equal(sources.satellite.attribution, '');
assert.match(sources.fallback.attribution ?? '', /MapTiler/);
// nothing to change
fired.length = 0;
map.fire('moveend');
await settled();
assert.deepEqual(fired, []);
});
});