Files
maptool/test/test_search.rb
T
randy 3f973a26d7
CI / check (push) Successful in 26s
CI / deploy (push) Successful in 33s
Search the offline map and Wikipedia, nearest results first
Every named OSM object in each country's Geofabrik extract goes into a
SQLite FTS5 index (data/places/<CODE>.sqlite, lib/maptool/places.rb),
built as the last offline-build step or on its own with
rake places:index. Kiwix full-text search is another source; clicking a
hit opens the article. Results from every source are sorted by distance
from the map center (settlements weighted closer) and show it; each
source is queried separately so a slow one doesn't hold up the list.
2026-11-12 12:13:13 +01:00

65 lines
2.5 KiB
Ruby

require_relative "test_helper"
require "maptool/gpx_store"
require "maptool/search"
require "stringio"
class SearchTest < Minitest::Test
def setup
@data_dir = Dir.mktmpdir
@store = Maptool::GpxStore.new(File.join(@data_dir, "gpx"))
camp = { "type" => "FeatureCollection", "features" => [
point("Kemp Košice", 21.26, 48.72, "pitná voda"),
{ "type" => "Feature", "properties" => { "kind" => "track" },
"geometry" => { "type" => "LineString", "coordinates" => [[17.0, 48.0, 150], [18.0, 49.0, 300]] } },
] }
@store.save(folder: "trips", filename: "camps.geojson", io: StringIO.new(JSON.generate(camp)))
trails = File.join(@data_dir, "hiking_trails")
FileUtils.mkdir_p(trails)
File.write(File.join(trails, "SK.geojson"),
JSON.generate("type" => "FeatureCollection", "features" => [point("Cesta hrdinov SNP", 19.0, 48.8)]))
end
def test_finds_across_sources_ignoring_case_and_diacritics_and_says_where
layer = search("kosice")
assert_equal [["Kemp Košice", "layers", "trips/camps.geojson"]], layer.map { |r| r.values_at(:name, :source, :ref) }
trail = search("snp hrdinov").first
assert_equal ["hiking_trails", "SK", "Hiking trails SK"], trail.values_at(:source, :ref, :source_label)
end
def test_matches_description_and_falls_back_to_file_name_with_bbox
assert_equal ["Kemp Košice"], search("voda").map { |r| r[:name] }
track = search("camps").first
assert_equal [17.0, 48.0, 18.0, 49.0], track[:bbox]
end
def test_with_a_map_center_results_come_nearest_first_with_their_distance
camps = { "type" => "FeatureCollection", "features" => [
point("Kemp far", 21.0, 48.7), point("Kemp near", 17.1, 48.15),
] }
@store.save(folder: "", filename: "more.geojson", io: StringIO.new(JSON.generate(camps)))
results = search("kemp", center: [48.14, 17.10])
assert_equal ["Kemp near", "Kemp far", "Kemp Košice"], results.map { |r| r[:name] }
assert_in_delta 1.3, results.first[:distance_km], 0.2
end
def test_source_filter_and_blank_query
assert_empty search("snp", sources: ["layers"])
assert_empty search(" ")
end
private
def search(text, **opts)
Maptool::Search.query(text, gpx_store: @store, data_dir: @data_dir, **opts)
end
def point(name, lng, lat, description = nil)
{ "type" => "Feature", "geometry" => { "type" => "Point", "coordinates" => [lng, lat] },
"properties" => { "name" => name, "description" => description }.compact }
end
end