Files
randy 53c9a82af2
CI / check (push) Successful in 17s
CI / deploy (push) Successful in 12s
Add a country-based region picker for offline data
Replaces "regenerate for the current viewport" with what Locus/OsmAnd-
style apps give you: tick which countries you want offline (a Natural
Earth-derived checklist, public domain, vendored), Apply rebuilds
offline.pmtiles/terrain.pmtiles to cover the whole selection at once.
Multiple regions can be offline simultaneously (e.g. Slovakia + Austria
for a trip that crosses the border) — the selection itself is the
source of truth (data/offline_regions.json), not a set of per-country
files, so "remove a country" and "refresh a country" are both just
"change the selection, then Apply."

Confirmed against go-pmtiles' own source (not just its docs) before
committing to this shape: `extract --region=<geojson>` treats a
FeatureCollection's features as their union already (no dissolve/union
math needed on our side — pmtiles/region.go's UnmarshalRegion just
concatenates every feature's rings), and `merge` hard-errors on any
overlapping tile id between inputs (merge.go's prepareInputs). That
second fact rules out "extract each country separately, merge them
all" — adjacent countries' region-extracts would share border tiles,
since extraction copies whole tiles rather than clipping vector
content to the polygon. The actual fix: extract per *source archive*
(the Protomaps build, each intersecting Mapterhorn chunk), each call
covering the whole current selection via one --region= file — the
terrain pipeline already extracted-then-merged per source this way, it
just needed a multi-country region instead of a single bbox.
2026-09-11 12:55:56 +02:00

97 lines
3.5 KiB
Ruby

ENV["RACK_ENV"] = "test"
ENV["LIVETRACK_WEBHOOK_SECRET"] ||= "test-webhook-secret"
require "fileutils"
require "tmpdir"
require "minitest/autorun"
require "rack"
require "json"
# A fresh throwaway directory per test run, not the real data/ — every
# GpxStore/LiveTrackStore write app.rb's routes make during the run lands
# here and is gone when the process exits, so running the suite never
# touches (or needs to clean up after itself in) real uploaded data.
ENV["MAPTOOL_DATA_DIR"] = Dir.mktmpdir("maptool-test-data")
Minitest.after_run { FileUtils.remove_entry(ENV["MAPTOOL_DATA_DIR"]) }
# A fixture plugin (test/fixtures/plugins/test-plugin), not the real
# plugins/ — keeps /api/plugins route tests offline/deterministic instead
# of actually running plugins/overpass-pois against the real Overpass API.
ENV["MAPTOOL_PLUGINS_DIR"] = File.join(__dir__, "fixtures", "plugins")
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
require_relative "../app"
# Available in every test via Minitest::Test below — fixture_path/json_body
# are generic enough to be worth having everywhere, not just in app.rb's
# own HTTP-level tests (see AppTestHelper for those).
module TestHelpers
def fixture_path(name)
File.join(__dir__, "fixtures", name)
end
def json_body(response)
JSON.parse(response.body)
end
end
class Minitest::Test
include TestHelpers
end
# Only included where needed (test_app.rb) — drives app.rb's Sinatra routes
# through Rack::MockRequest, the same way every ad-hoc verification during
# development did.
module AppTestHelper
def app
Maptool::App
end
def request
@request ||= Rack::MockRequest.new(app)
end
# Sinatra's session is read from env["rack.session"] (populated by the
# Rack::Session::Cookie middleware on a real request) — setting it
# directly here is equivalent to already having a valid session cookie,
# without needing to drive the actual Gitea OAuth2 login flow.
def logged_in_env(user: "testuser")
{ "rack.session" => { user: user } }
end
# GPX_STORE/LIVETRACK_STORE are process-wide constants pointing at the
# one throwaway MAPTOOL_DATA_DIR (see above) — without this, uploads from
# one test would still be there (and show up in tree/list assertions) in
# the next one, since minitest runs a class's tests in one process.
def teardown
FileUtils.rm_rf(Dir.glob(File.join(Maptool::App::DATA_DIR, "gpx", "*")))
File.write(File.join(Maptool::App::DATA_DIR, "livetrack.json"), "[]")
FileUtils.rm_f(File.join(Maptool::App::DATA_DIR, "offline_regions.json"))
Maptool::PmtilesBuilder.reset!
super
end
def multipart_body(boundary, filename, content, content_type: "application/octet-stream", field_name: "file")
"--#{boundary}\r\n" \
"Content-Disposition: form-data; name=\"#{field_name}\"; filename=\"#{filename}\"\r\n" \
"Content-Type: #{content_type}\r\n\r\n" \
"#{content}\r\n" \
"--#{boundary}--\r\n"
end
# POSTs a single file as a multipart/form-data upload; pass folder in
# `path`'s query string (e.g. "/api/gpx?folder=trips") the same way the
# real upload form does.
def post_file(path, filename:, content:, content_type: "application/octet-stream", env: {})
boundary = "TestBoundary#{rand(1_000_000)}"
body = multipart_body(boundary, filename, content, content_type: content_type)
request.post(
path,
input: body,
"CONTENT_TYPE" => "multipart/form-data; boundary=#{boundary}",
"CONTENT_LENGTH" => body.bytesize.to_s,
**env,
)
end
end