Files
maptool/test/test_pmtiles_builder.rb
T
randy bbe9f38bda
CI / check (push) Successful in 18s
CI / deploy (push) Successful in 13s
Show build progress instead of a static "Building…"
terrain.pmtiles can take minutes (tiles fetched one at a time) with no
way to tell a slow build from a stuck one. Now reports tiles_done/
tiles_total into the same status Hash the frontend already polls, and
the panel shows elapsed time (both files) plus tile counts (terrain
only) so the numbers visibly move every ~3s poll instead of sitting on
one static string.
2026-09-11 11:57:43 +02:00

158 lines
6.0 KiB
Ruby

require_relative "test_helper"
require "maptool/pmtiles_builder"
require "tmpdir"
require "fileutils"
# Network calls (the protomaps build index, individual Terrarium tile
# fetches) are never exercised here — same "keep this offline/deterministic"
# reasoning test_helper.rb already gives for /api/plugins staying off the
# real Overpass API — build_offline/build_terrain take fetch_build_url/
# fetch_tile precisely so tests can swap those out. A fake `pmtiles` CLI
# (PMTILES_BIN) stands in for the real go-pmtiles binary.
class PmtilesBuilderTest < Minitest::Test
def setup
@data_dir = Dir.mktmpdir("maptool-pmtiles-test-data")
@bin_dir = Dir.mktmpdir("maptool-pmtiles-test-bin")
# $3 is the output path either way: `extract SOURCE OUTPUT --bbox=...
# --maxzoom=...` and `convert INPUT OUTPUT` both put it 3rd, even though
# it's not the *last* arg in the extract case (the --flags follow it).
@pmtiles_ok = File.join(@bin_dir, "pmtiles-ok")
File.write(@pmtiles_ok, "#!/bin/sh\necho fake > \"$3\"\n")
File.chmod(0o755, @pmtiles_ok)
@pmtiles_fail = File.join(@bin_dir, "pmtiles-fail")
File.write(@pmtiles_fail, "#!/bin/sh\necho 'boom' >&2\nexit 1\n")
File.chmod(0o755, @pmtiles_fail)
@original_pmtiles_bin = ENV["PMTILES_BIN"]
Maptool::PmtilesBuilder.reset!
end
def teardown
FileUtils.remove_entry(@data_dir)
FileUtils.remove_entry(@bin_dir)
ENV["PMTILES_BIN"] = @original_pmtiles_bin
Maptool::PmtilesBuilder.reset!
end
def test_offline_and_terrain_status_start_idle
assert_equal({ state: "idle" }, Maptool::PmtilesBuilder.offline_status)
assert_equal({ state: "idle" }, Maptool::PmtilesBuilder.terrain_status)
end
def test_build_offline_writes_the_file_and_reports_done
ENV["PMTILES_BIN"] = @pmtiles_ok
thread = Maptool::PmtilesBuilder.build_offline(
[1, 2, 3, 4],
data_dir: @data_dir,
fetch_build_url: -> { "https://example.com/fake.pmtiles" },
)
thread.join
assert_path_exists File.join(@data_dir, "offline.pmtiles")
refute_path_exists File.join(@data_dir, "offline.pmtiles.tmp")
assert_equal "done", Maptool::PmtilesBuilder.offline_status[:state]
end
def test_build_offline_reports_an_error_status_on_non_zero_exit
ENV["PMTILES_BIN"] = @pmtiles_fail
thread = Maptool::PmtilesBuilder.build_offline(
[1, 2, 3, 4],
data_dir: @data_dir,
fetch_build_url: -> { "https://example.com/fake.pmtiles" },
)
thread.join
refute_path_exists File.join(@data_dir, "offline.pmtiles")
status = Maptool::PmtilesBuilder.offline_status
assert_equal "error", status[:state]
assert_match(/exited with status/, status[:message])
end
def test_build_offline_reports_a_clear_error_when_the_binary_is_missing
ENV["PMTILES_BIN"] = File.join(@bin_dir, "does-not-exist")
thread = Maptool::PmtilesBuilder.build_offline(
[1, 2, 3, 4],
data_dir: @data_dir,
fetch_build_url: -> { "https://example.com/fake.pmtiles" },
)
thread.join
status = Maptool::PmtilesBuilder.offline_status
assert_equal "error", status[:state]
assert_match(/go-pmtiles installed/, status[:message])
end
# fetch_build_url blocks on a Queue so the build is deterministically
# still "running" when the second call happens, rather than racing a real
# one to see which state wins.
def test_build_offline_raises_when_one_is_already_running
ENV["PMTILES_BIN"] = @pmtiles_ok
gate = Queue.new
thread = Maptool::PmtilesBuilder.build_offline([1, 2, 3, 4], data_dir: @data_dir, fetch_build_url: -> { gate.pop })
assert_raises(Maptool::PmtilesBuilder::BuildError) do
Maptool::PmtilesBuilder.build_offline([1, 2, 3, 4], data_dir: @data_dir, fetch_build_url: -> { "unused" })
end
ensure
gate.push("https://example.com/fake.pmtiles")
thread&.join
end
# A tiny real-world bbox (a few blocks of central Bratislava) keeps the
# z0..15 tile_range small at every zoom level, so this stays fast even
# though it iterates all 16 levels.
def test_build_terrain_packs_fetched_tiles_and_converts
ENV["PMTILES_BIN"] = @pmtiles_ok
thread = Maptool::PmtilesBuilder.build_terrain(
[48.140, 17.100, 48.141, 17.101],
data_dir: @data_dir,
fetch_tile: ->(_z, _x, _y) { "not a real png, write_terrarium_mbtiles just stores opaque bytes" },
)
thread.join
assert_path_exists File.join(@data_dir, "terrain.pmtiles")
status = Maptool::PmtilesBuilder.terrain_status
assert_equal "done", status[:state]
assert_operator status[:tiles_total], :>, 0
assert_equal status[:tiles_total], status[:tiles_done]
end
# fetch_tile blocks after the very first call so the "running" progress
# can be observed mid-flight, deterministically, rather than only ever
# seeing the final done/total once the (very fast, faked) build finishes.
def test_build_terrain_reports_progress_while_running
ENV["PMTILES_BIN"] = @pmtiles_ok
gate = Queue.new
first_call = true
fetch_tile = lambda do |_z, _x, _y|
gate.pop if first_call
first_call = false
"bytes"
end
thread = Maptool::PmtilesBuilder.build_terrain([48.140, 17.100, 48.141, 17.101], data_dir: @data_dir, fetch_tile: fetch_tile)
20.times { break if Maptool::PmtilesBuilder.terrain_status[:tiles_total]; sleep 0.01 }
status = Maptool::PmtilesBuilder.terrain_status
assert_equal "running", status[:state]
assert_operator status[:tiles_total], :>, 0
assert_equal 0, status[:tiles_done]
ensure
gate.push(nil)
thread&.join
end
def test_build_terrain_still_succeeds_when_some_tiles_are_missing
ENV["PMTILES_BIN"] = @pmtiles_ok
thread = Maptool::PmtilesBuilder.build_terrain(
[48.140, 17.100, 48.141, 17.101],
data_dir: @data_dir,
fetch_tile: ->(_z, _x, _y) { nil }, # every tile "missing" — should skip, not fail, the same way HillshadeControl's own source tolerates gaps
)
thread.join
assert_equal "done", Maptool::PmtilesBuilder.terrain_status[:state]
end
end