Key changes:

- Add MapLibre GL JS viewer with light/dark themes, terrain layer,
  and embedded static map page for coordinate sharing
- Add map pin support with persistent map_pins.yaml config
- Add map region subscriptions and downloader with progress tracking,
  sourcing pre-extracted PMTiles from a cold CDN
- Add map export to PNG and GPS coordinate copy via context menu
- Add MapViewer React component alongside reworked Map page
- Add map catalog and region-building script (build_map_regions.py)
- Remove OSM import/render scripts, leaflet files, Apache configs,
  renderd service, and osm-map-dumper Docker container
- Update Caddyfile to serve PMTiles and map static assets
- Update build/install/repair scripts for Protomaps dependencies
- Drop map_file table, add/drop map_subscription table (alembic)
- Adding button to skip primary drive mounting during onboarding.
This commit is contained in:
Roland 2026-03-29 18:58:31 -06:00
parent 0a5fe7eebe
commit b8a532c8d9
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
102 changed files with 5033 additions and 2712 deletions

View file

@ -1,135 +1,130 @@
import json
from http import HTTPStatus
from itertools import zip_longest
import mock
import pytest
async def assert_map_file_status(async_client, expected, status_code=HTTPStatus.OK):
@pytest.mark.asyncio
async def test_get_files_empty(async_client, test_directory):
"""An empty map directory returns no files."""
request, response = await async_client.get('/api/map/files')
assert response.status_code == status_code
for pbf, expected_ in zip_longest(response.json['files'], expected):
imported = 'to be imported' if expected_['imported'] else 'to NOT be imported'
if not pbf['imported'] == expected_['imported']:
raise AssertionError(f'Expected {pbf["path"]} {imported}')
assert pbf['path'].endswith(expected_['path'])
assert response.json['pending'] is None
assert response.status_code == HTTPStatus.OK
assert response.json['files'] == []
@pytest.mark.asyncio
@mock.patch('modules.map.lib.is_pbf_file', lambda i: True)
@mock.patch('modules.map.lib.is_dump_file', lambda i: True)
async def test_status_and_import(async_client, test_session, make_files_structure, mock_run_command):
"""PBF files can be imported, and the status of the import can be monitored."""
pbf1, pbf2, dump1 = make_files_structure([
'map/pbf/some-country.osm.pbf',
'map/pbf/other-country.osm.pbf',
'map/dump/dumpy.dump',
async def test_get_files(async_client, test_directory, make_files_structure):
"""PMTiles files in the map directory are listed."""
make_files_structure([
'map/usa.pmtiles',
'map/oregon.pmtiles',
'map/not-a-map.txt',
])
# The PBF files can be found, but have not yet been imported.
expected = [
{'imported': False, 'path': 'map/dump/dumpy.dump'},
{'imported': False, 'path': 'map/pbf/other-country.osm.pbf'},
{'imported': False, 'path': 'map/pbf/some-country.osm.pbf'},
]
await assert_map_file_status(async_client, expected)
body = {'files': [str(pbf1), str(pbf2)]}
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
assert response.status_code == HTTPStatus.NO_CONTENT
# Can import multiple pbf files.
expected = [
{'imported': False, 'path': 'map/dump/dumpy.dump'},
{'imported': True, 'path': 'map/pbf/other-country.osm.pbf'},
{'imported': True, 'path': 'map/pbf/some-country.osm.pbf'},
]
await assert_map_file_status(async_client, expected)
body = {'files': [str(pbf1), ], }
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
assert response.status_code == HTTPStatus.NO_CONTENT
# Importing one pbf resets the import statuses.
expected = [
{'imported': False, 'path': 'map/dump/dumpy.dump'},
{'imported': False, 'path': 'map/pbf/other-country.osm.pbf'},
{'imported': True, 'path': 'map/pbf/some-country.osm.pbf'},
]
await assert_map_file_status(async_client, expected)
body = {'files': [str(dump1), ]}
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
assert response.status_code == HTTPStatus.NO_CONTENT
# Importing a dump also resets statuses.
expected = [
{'imported': True, 'path': 'map/dump/dumpy.dump'},
{'imported': False, 'path': 'map/pbf/other-country.osm.pbf'},
{'imported': True, 'path': 'map/pbf/some-country.osm.pbf'},
]
await assert_map_file_status(async_client, expected)
request, response = await async_client.get('/api/map/files')
assert response.status_code == HTTPStatus.OK
files = response.json['files']
names = [f['name'] for f in files]
assert 'oregon.pmtiles' in names
assert 'usa.pmtiles' in names
assert 'not-a-map.txt' not in names
@pytest.mark.asyncio
@mock.patch('modules.map.lib.is_pbf_file', lambda i: True)
@mock.patch('modules.map.lib.is_dump_file', lambda i: True)
async def test_multiple_import(async_client, test_session, test_directory, make_files_structure, mock_run_command):
"""Multiple PBFs can be imported. Importing a second time overwrites the previous imports."""
pbf1, pbf2, pbf3, dump = make_files_structure([
'map/pbf/country1.osm.pbf',
'map/pbf/country2.osm.pbf',
'map/pbf/country3.osm.pbf',
'map/dumpy.dump',
])
async def test_delete_file(async_client, test_directory, make_files_structure):
"""A PMTiles file can be deleted."""
pmtiles_file, = make_files_structure(['map/oregon.pmtiles'])
body = {'files': [str(pbf2), str(pbf1)]}
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
# File exists.
request, response = await async_client.get('/api/map/files')
assert len(response.json['files']) == 1
# Delete the file.
request, response = await async_client.delete('/api/map/files/oregon.pmtiles')
assert response.status_code == HTTPStatus.NO_CONTENT
# Two PBFs can be merged and imported.
expected = [
{'imported': False, 'path': 'map/dumpy.dump'},
{'imported': True, 'path': 'map/pbf/country1.osm.pbf'},
{'imported': True, 'path': 'map/pbf/country2.osm.pbf'},
{'imported': False, 'path': 'map/pbf/country3.osm.pbf'},
]
await assert_map_file_status(async_client, expected)
body = {'files': [str(pbf3), ]}
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
assert response.status_code == HTTPStatus.NO_CONTENT
# New imports overwrite old imports.
expected = [
{'imported': False, 'path': 'map/dumpy.dump'},
{'imported': False, 'path': 'map/pbf/country1.osm.pbf'},
{'imported': False, 'path': 'map/pbf/country2.osm.pbf'},
{'imported': True, 'path': 'map/pbf/country3.osm.pbf'},
]
await assert_map_file_status(async_client, expected)
# Can't mix PBF and dump.
body = {'files': [str(dump), ]}
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
assert response.status_code == HTTPStatus.NO_CONTENT
# Dump does not overwrite.
expected = [
{'imported': True, 'path': 'map/dumpy.dump'},
{'imported': False, 'path': 'map/pbf/country1.osm.pbf'},
{'imported': False, 'path': 'map/pbf/country2.osm.pbf'},
{'imported': True, 'path': 'map/pbf/country3.osm.pbf'},
]
await assert_map_file_status(async_client, expected)
# File is gone.
request, response = await async_client.get('/api/map/files')
assert len(response.json['files']) == 0
@pytest.mark.asyncio
async def test_empty_import(async_client, test_session, test_directory):
"""Some files must be requested."""
body = {'files': []}
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
async def test_delete_nonexistent_file(async_client, test_directory):
"""Deleting a nonexistent file returns 404."""
request, response = await async_client.delete('/api/map/files/nonexistent.pmtiles')
assert response.status_code == HTTPStatus.NOT_FOUND
@pytest.mark.asyncio
async def test_delete_path_traversal(async_client, test_directory, make_files_structure):
"""Path traversal attempts are rejected."""
request, response = await async_client.delete('/api/map/files/..%2F..%2Fetc%2Fpasswd')
# Path traversal is blocked — either 400 (caught by validation) or 404 (file not found).
assert response.status_code in (HTTPStatus.BAD_REQUEST, HTTPStatus.NOT_FOUND)
@pytest.mark.asyncio
async def test_get_subscriptions_empty(async_client, test_session, test_directory):
"""Catalog and empty subscriptions can be fetched."""
request, response = await async_client.get('/api/map/subscribe')
assert response.status_code == HTTPStatus.OK
assert 'catalog' in response.json
assert 'subscriptions' in response.json
assert len(response.json['catalog']) > 0
assert len(response.json['subscriptions']) == 0
@pytest.mark.asyncio
async def test_subscribe_and_unsubscribe(async_client, test_session, test_directory):
"""A map region can be subscribed to and unsubscribed from."""
body = json.dumps({'name': 'Alaska', 'region': 'us-alaska'})
request, response = await async_client.post('/api/map/subscribe', content=body)
assert response.status_code == HTTPStatus.CREATED
# Subscription appears in the list.
request, response = await async_client.get('/api/map/subscribe')
assert response.status_code == HTTPStatus.OK
regions = [s['region'] for s in response.json['subscriptions']]
assert 'us-alaska' in regions
# Unsubscribe.
request, response = await async_client.delete('/api/map/subscribe/us-alaska')
assert response.status_code == HTTPStatus.NO_CONTENT
# Subscription is gone.
request, response = await async_client.get('/api/map/subscribe')
assert len(response.json['subscriptions']) == 0
@pytest.mark.asyncio
async def test_subscribe_multiple_regions(async_client, test_session, test_directory):
"""Multiple regions share a single Download record."""
body = json.dumps({'name': 'Alaska', 'region': 'us-alaska'})
request, response = await async_client.post('/api/map/subscribe', content=body)
assert response.status_code == HTTPStatus.CREATED
body = json.dumps({'name': 'United States (West)', 'region': 'us-west'})
request, response = await async_client.post('/api/map/subscribe', content=body)
assert response.status_code == HTTPStatus.CREATED
request, response = await async_client.get('/api/map/subscribe')
regions = [s['region'] for s in response.json['subscriptions']]
assert 'us-alaska' in regions
assert 'us-west' in regions
# Unsubscribe one — the other remains.
request, response = await async_client.delete('/api/map/subscribe/us-alaska')
assert response.status_code == HTTPStatus.NO_CONTENT
request, response = await async_client.get('/api/map/subscribe')
regions = [s['region'] for s in response.json['subscriptions']]
assert 'us-alaska' not in regions
assert 'us-west' in regions
@pytest.mark.asyncio
async def test_subscribe_invalid_region(async_client, test_session, test_directory):
"""Subscribing to an invalid region returns an error."""
body = json.dumps({'name': 'Atlantis', 'region': 'atlantis'})
request, response = await async_client.post('/api/map/subscribe', content=body)
assert response.status_code == HTTPStatus.BAD_REQUEST
assert 'validate' in response.json['message']

View file

View file

@ -6,98 +6,159 @@ from modules.map import lib
from wrolpi.common import get_wrolpi_config
def test_get_map_paths(test_directory, make_files_structure):
pbf, dump = make_files_structure([
'map/pbf/map.osm.pbf',
'map/pbf/map.dump',
def test_get_pmtiles_files_empty(test_directory):
"""No files returns an empty list."""
assert lib.get_pmtiles_files() == []
def test_get_pmtiles_files(test_directory, make_files_structure):
"""PMTiles files are found, other files are ignored."""
make_files_structure([
'map/usa.pmtiles',
'map/oregon.pmtiles',
'map/old-data.osm.pbf',
'map/readme.txt',
])
with mock.patch('modules.map.lib.subprocess.check_output') as mock_check_output:
mock_check_output.return_value = b'OpenStreetMap Protocolbuffer Binary Format'
assert lib.get_map_paths() == [pbf, ]
files = lib.get_pmtiles_files()
names = [f['name'] for f in files]
assert 'oregon.pmtiles' in names
assert 'usa.pmtiles' in names
assert 'old-data.osm.pbf' not in names
assert 'readme.txt' not in names
with mock.patch('modules.map.lib.subprocess.check_output') as mock_check_output:
mock_check_output.return_value = b'PostgreSQL custom database dump'
assert lib.get_map_paths() == [dump, ]
# Each file has expected keys.
for f in files:
assert 'name' in f
assert 'path' in f
assert 'size' in f
assert 'mtime' in f
def test_delete_pmtiles_file(test_directory, make_files_structure):
"""A PMTiles file can be deleted."""
pmtiles_file, = make_files_structure(['map/oregon.pmtiles'])
assert pmtiles_file.is_file()
assert lib.delete_pmtiles_file('oregon.pmtiles') is True
assert not pmtiles_file.is_file()
def test_delete_nonexistent_file(test_directory):
"""Deleting a nonexistent file returns False."""
assert lib.delete_pmtiles_file('nonexistent.pmtiles') is False
def test_delete_path_traversal(test_directory, make_files_structure):
"""Path traversal is rejected."""
make_files_structure(['map/safe.pmtiles'])
with pytest.raises(ValueError):
lib.delete_pmtiles_file('../../etc/passwd')
def test_get_map_catalog(test_directory):
"""Catalog returns predefined regions."""
catalog = lib.get_map_catalog()
assert len(catalog) > 0
names = [r['name'] for r in catalog]
assert 'Alaska' in names # Verify a known region exists
for region in catalog:
assert 'name' in region
assert 'region' in region
assert 'size_estimate' in region
@pytest.mark.asyncio
async def test_run_import_command(test_directory, mock_run_command, run_command_bad_result):
"""
Import map files. Files to import are checked for validity before importing. Any errors returned by
asyncio.create_subprocess_shell are caught and reported.
"""
foo_pbf = test_directory / 'foo.osm.pbf'
bar_pbf = test_directory / 'bar.osm.pbf'
async def test_subscribe(async_client, test_session, test_directory):
"""A subscription adds a region to the download settings."""
await lib.subscribe(test_session, 'Alaska', 'us-alaska')
subs = lib.get_map_subscriptions(test_session)
regions = [s['region'] for s in subs]
assert 'us-alaska' in regions
@pytest.mark.asyncio
async def test_subscribe_multiple(async_client, test_session, test_directory):
"""Multiple subscriptions share one Download."""
await lib.subscribe(test_session, 'Alaska', 'us-alaska')
await lib.subscribe(test_session, 'United States (West)', 'us-west')
subs = lib.get_map_subscriptions(test_session)
regions = [s['region'] for s in subs]
assert 'us-alaska' in regions
assert 'us-west' in regions
@pytest.mark.asyncio
async def test_subscribe_duplicate(async_client, test_session, test_directory):
"""Subscribing to the same region twice doesn't duplicate."""
await lib.subscribe(test_session, 'Alaska', 'us-alaska')
await lib.subscribe(test_session, 'Alaska', 'us-alaska')
subs = lib.get_map_subscriptions(test_session)
assert len([s for s in subs if s['region'] == 'us-alaska']) == 1
@pytest.mark.asyncio
async def test_subscribe_invalid(async_client, test_session, test_directory):
"""Invalid region name raises ValueError."""
with pytest.raises(ValueError):
await lib.run_import_command(foo_pbf)
foo_pbf.touch()
with pytest.raises(ValueError):
await lib.run_import_command(foo_pbf)
dump_file = test_directory / 'dumpy.dump'
dump_file.touch()
with mock.patch('modules.map.lib.is_pbf_file') as mock_is_pbf_file:
# Run the import, it succeeds.
mock_is_pbf_file.return_value = True
await lib.run_import_command(foo_pbf)
# Can import more than one pbf.
with mock.patch('modules.map.lib.is_pbf_file') as mock_is_pbf_file:
mock_is_pbf_file.return_value = True
await lib.run_import_command(foo_pbf, bar_pbf)
with mock.patch('modules.map.lib.is_pbf_file') as mock_is_pbf_file:
# Run the import, it fails.
mock_run_command(run_command_bad_result)
mock_is_pbf_file.return_value = True
with pytest.raises(ValueError):
await lib.run_import_command(foo_pbf)
with pytest.raises(ValueError) as e:
await lib.run_import_command()
assert 'Must import a file' in str(e)
# Can't import more than one dump.
with pytest.raises(ValueError) as e:
await lib.run_import_command(dump_file, dump_file)
assert 'more than one' in str(e)
await lib.subscribe(test_session, 'Atlantis', 'atlantis')
@pytest.mark.parametrize('size,expected', [
(0, 0),
(-1, 0),
(17737381, 101),
(63434267, 361),
(87745484, 500),
(116318111, 662),
(136372996, 777),
(1936075318, 29032),
(2676094489, 46705),
(3392375001, 67202),
(11346305075, 519006),
])
def test_seconds_to_import_rpi4(size, expected):
assert lib.seconds_to_import(size) == expected
@pytest.mark.asyncio
async def test_unsubscribe(async_client, test_session, test_directory):
"""Unsubscribing removes a region."""
await lib.subscribe(test_session, 'Alaska', 'us-alaska')
await lib.subscribe(test_session, 'United States (West)', 'us-west')
await lib.unsubscribe(test_session, 'us-alaska')
subs = lib.get_map_subscriptions(test_session)
regions = [s['region'] for s in subs]
assert 'us-alaska' not in regions
assert 'us-west' in regions
@pytest.mark.parametrize('size,expected', [
(0, 0),
(-1, 0),
(17737381, 130),
(63434267, 466),
(87745484, 645),
(116318111, 855),
(136372996, 1002),
(1936075318, 10873),
(2676094489, 18313),
(3392375001, 27253),
(11346305075, 241574),
])
def test_seconds_to_import_rpi5(size, expected):
assert lib.seconds_to_import(size, True) == expected
@pytest.mark.asyncio
async def test_unsubscribe_last_deletes_download(async_client, test_session, test_directory):
"""Unsubscribing the last region deletes the Download."""
await lib.subscribe(test_session, 'Alaska', 'us-alaska')
await lib.unsubscribe(test_session, 'us-alaska')
assert lib.get_map_subscriptions(test_session) == []
assert lib._get_map_download(test_session) is None
@pytest.mark.asyncio
async def test_subscribe_renews_download(async_client, test_session, test_directory):
"""Subscribing renews the download so it runs immediately."""
await lib.subscribe(test_session, 'Alaska', 'us-alaska')
download = lib._get_map_download(test_session)
assert download.status == 'new'
# Simulate the download completing.
download.status = 'complete'
test_session.commit()
# Subscribe to another region — download should be renewed.
await lib.subscribe(test_session, 'United States (West)', 'us-west')
download = lib._get_map_download(test_session)
assert download.status == 'new'
@pytest.mark.asyncio
async def test_subscribe_saves_config(async_client, test_session, test_directory):
"""Subscribing triggers a download config save."""
with mock.patch('modules.map.lib.save_downloads_config') as mock_save:
await lib.subscribe(test_session, 'Alaska', 'us-alaska')
mock_save.activate_switch.assert_called_once()
@pytest.mark.asyncio
async def test_unsubscribe_saves_config(async_client, test_session, test_directory):
"""Unsubscribing triggers a download config save."""
await lib.subscribe(test_session, 'Alaska', 'us-alaska')
with mock.patch('modules.map.lib.save_downloads_config') as mock_save:
await lib.unsubscribe(test_session, 'us-alaska')
mock_save.activate_switch.assert_called_once()
def test_get_custom_map_directory(async_client, test_directory, test_wrolpi_config):