wrolpi/app/cypress/e2e/map-standalone.cy.js
lrnselfreliance 177d0dba4b
The standalone map viewer draws overlays, as the map does (#966)
The standalone viewer (port 8084) drew basemaps only: a map file which is
not a basemap was skipped.  It now offers each overlay in its layer panel,
hidden until turned on, with a checkbox per layer, draws it with the style
beside it when there is one, and shows a feature's attributes on a click.

The overlay logic is the map viewer's own.  It moved to dependency-free
modules (app/src/map-shared) which a browser can load as they are; the
standalone viewer, which has no build step, loads copies of them from
modules/map/static/js.  A test fails while a copy differs from its module
(`npm run sync:map-shared` makes the copies).  What differs between the
viewers is passed in: where a map file and its style are fetched from, and
whether a layer can be checked before MapLibre is given it.

The standalone viewer cannot check a layer first, so it adds an overlay's
layers to the map one at a time once the style has loaded: MapLibre refuses a
whole style over one layer it cannot draw, but only that layer when it is
added by itself.

The API also names a style by its path within the map directory, which is
what the standalone viewer is served.

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-13 17:28:41 -07:00

127 lines
5.5 KiB
JavaScript

/*
* The standalone map viewer (port 8084) and overlays: map files which are not a basemap.
*
* The standalone viewer is plain HTML with no build step; it loads copies of the map viewer's
* overlay modules. This runs against the live stack with a real map file: the map:* tasks write
* a small overlay (no tiles) and a style beside it into the media directory's map directory.
*/
const VIEWER = 'https://localhost:8084/';
const LABEL = 'Cypress Overlay';
const METADATA = {
name: LABEL,
description: 'An overlay for the standalone viewer test',
type: 'overlay',
vector_layers: [
{id: 'towers', fields: {name: 'String'}, minzoom: 0, maxzoom: 8},
{id: 'power_lines', fields: {}, minzoom: 0, maxzoom: 8},
{id: 'parks', fields: {}, minzoom: 0, maxzoom: 8},
],
tilestats: {
layers: [
{layer: 'towers', geometry: 'Point', count: 1},
{layer: 'power_lines', geometry: 'LineString', count: 1},
{layer: 'parks', geometry: 'Polygon', count: 1},
],
},
};
// Written for some other map: its own source name and URL, and one layer MapLibre cannot draw.
const STYLE = {
version: 8,
sources: {features: {type: 'vector', url: 'https://example.com/features.json'}},
layers: [
{id: 'background', type: 'background'},
{id: 'lines', type: 'line', source: 'features', 'source-layer': 'power_lines', paint: {'line-color': '#c60'}},
{id: 'bad', type: 'line', source: 'features', 'source-layer': 'parks', paint: {'line-width': 'wide'}},
{
id: 'towers', type: 'symbol', source: 'features', 'source-layer': 'towers',
layout: {'text-field': ['get', 'name'], 'text-font': ['Roboto Bold']},
},
],
};
const overlayRow = () => cy.contains('.overlay-file', LABEL);
const overlayBox = () => overlayRow().find('input[type=checkbox]');
const layerBox = (name) => cy.contains('.overlay-layer', name).find('input[type=checkbox]');
const openPanel = () => cy.get('#overlay-section', {timeout: 20000}).should('be.visible');
// Requires the full stack (API, Caddy on 8084, real media directory); CI runs only the React dev server.
(Cypress.env('CI') ? describe.skip : describe)('Standalone map viewer: overlays', () => {
afterEach(() => {
cy.task('map:cleanup');
});
it('offers an overlay by its own name, hidden, with a checkbox per layer', () => {
cy.task('map:seed-overlay', {name: 'generic', metadata: METADATA});
cy.visit(VIEWER);
openPanel();
overlayBox().should('not.be.checked');
overlayRow().find('.overlay-zooms').should('have.text', 'z0\u20138');
overlayRow().find('.overlay-expand').click();
// Every layer of the file, each with the color it is drawn in.
cy.get('.overlay-layer').should('have.length', 3);
cy.get('.overlay-swatch').should('have.length', 3);
// The layers cannot be chosen while the file is off.
layerBox('towers').should('be.disabled');
overlayBox().check();
layerBox('towers').should('be.enabled').and('be.checked');
layerBox('towers').uncheck();
cy.contains('.overlay-all-none button', 'None').click();
layerBox('power_lines').should('not.be.checked');
cy.contains('.overlay-all-none button', 'All').click();
layerBox('power_lines').should('be.checked');
layerBox('towers').should('be.checked');
});
it('remembers what was turned on', () => {
cy.task('map:seed-overlay', {name: 'remembered', metadata: METADATA});
cy.visit(VIEWER);
openPanel();
overlayBox().check();
overlayRow().find('.overlay-expand').click();
layerBox('parks').uncheck();
cy.reload();
openPanel();
overlayBox().should('be.checked');
overlayRow().find('.overlay-expand').click();
layerBox('parks').should('not.be.checked');
layerBox('towers').should('be.checked');
});
it('draws an overlay with the style beside it, without the layer MapLibre refuses', () => {
cy.intercept('GET', '**/data/cypress-map/styled.style.json').as('style');
cy.task('map:seed-overlay', {name: 'styled', metadata: METADATA, style: STYLE});
cy.visit(VIEWER);
// The style is fetched from the map directory this viewer is served.
cy.wait('@style').its('response.statusCode').should('eq', 200);
openPanel();
overlayRow().find('.overlay-expand').click();
// Only the layers the style draws, and no generic colors.
cy.get('.overlay-layer').should('have.length', 3);
cy.get('.overlay-swatch').should('have.length', 0);
// This viewer cannot check a layer before MapLibre does: the bad one is refused there.
cy.contains('.overlay-note', '1 style layer skipped');
// The panel is rebuilt by a change, so the checkbox is found again.
overlayBox().check();
overlayBox().should('be.checked');
});
it('does not offer an ignored file', () => {
cy.task('map:seed-overlay', {name: 'ignored', metadata: METADATA}).then((mapPath) => {
cy.request('POST', 'https://localhost:8084/api/map/files/visible', {map_path: mapPath, visible: false});
cy.visit(VIEWER);
cy.get('#layer-content', {timeout: 20000}).contains('Labels');
cy.contains('.overlay-file', LABEL).should('not.exist');
// Deleting the file forgets that it was ignored.
cy.request('DELETE', `https://localhost:8084/api/map/files/${mapPath}`);
});
});
});