require_relative "test_helper" require "maptool/library" require "maptool/google_saved" require "zip" class GoogleSavedTest < Minitest::Test CSV_TEXT = <<~CSV Title,Note,URL,Tags,Comment ,,,, Reine,"Fishing village, red huts",https://www.google.com/maps/place/Reine/data=!4m2!3m1!1s0x45de3cbf60603ce7:0x82f2b236d153558f,, Somewhere,,"https://www.google.com/maps/search/48.1486,+17.1077",, CSV def setup @dir = Dir.mktmpdir end def teardown FileUtils.remove_entry(@dir) end def test_the_place_id_decodes_near_the_place lng, lat = Maptool::GoogleSaved.s2_lng_lat(0x45de3cbf60603ce7) assert_in_delta 67.932, lat, 0.02 # Reine, Lofoten assert_in_delta 13.089, lng, 0.02 end def test_a_list_becomes_points_with_their_google_id reine, spot = Maptool::GoogleSaved.features(CSV_TEXT) assert_equal "Reine", reine["properties"]["name"] assert_match(/\AFishing village, red huts\n\nGoogle Maps: https:/, reine["properties"]["description"]) assert_equal "google:0x45de3cbf60603ce7:0x82f2b236d153558f", reine["properties"]["source_id"] assert reine["properties"]["approximate"] assert_equal [17.1077, 48.1486], spot["geometry"]["coordinates"] refute spot["properties"].key?("approximate") end def test_importing_a_takeout_again_updates_instead_of_duplicating library = Maptool::Library.new(File.join(@dir, "library.sqlite"), gpx_store: Maptool::GpxStore.new(File.join(@dir, "gpx")), sources_dir: File.join(@dir, "sources")) takeout = File.join(@dir, "takeout") FileUtils.mkdir_p(takeout) write_zip = lambda do |name, text| Zip::File.open(File.join(takeout, name), create: true) { |zip| zip.get_output_stream("Takeout/Saved/Norway 2026.csv") { |io| io.write(text) } } end state = File.join(@dir, "state") not_found = ->(*) {} write_zip.call("takeout-1.zip", CSV_TEXT) assert_equal 2, Maptool::GoogleSaved.import_takeout(takeout, library: library, state_path: state, lookup: not_found) assert_nil Maptool::GoogleSaved.import_takeout(takeout, library: library, state_path: state, lookup: not_found), "same zip twice" points = -> { JSON.parse(library.geojson(library.tree["children"][0]["children"][0]["children"][0]["path"]))["features"] } reine = points.call.find { |f| f["properties"]["name"] == "Reine" } reine["properties"]["color"] = "#ff0000" library.update_feature("points:x", reine["properties"]["id"], reine) somewhere = points.call.find { |f| f["properties"]["name"] == "Somewhere" } somewhere["properties"]["name"] = "Bratislava" somewhere["properties"].delete("imported") # the popup needn't send it back library.update_feature("points:x", somewhere["properties"]["id"], somewhere) sleep 1.1 # a newer export (mtime) with a changed note, now found by name write_zip.call("takeout-2.zip", CSV_TEXT.sub("red huts", "rorbuer").sub("Somewhere,,", "Somewhere,a note,")) Maptool::GoogleSaved.import_takeout(takeout, library: library, state_path: state, lookup: ->(*) { [13.0897, 67.9322] }) features = points.call assert_equal 2, features.size reine = features.find { |f| f["properties"]["name"] == "Reine" } assert_match(/\AFishing village, rorbuer/, reine["properties"]["description"]) assert_equal "#ff0000", reine["properties"]["color"], "own changes kept" assert_equal [13.0897, 67.9322], reine["geometry"]["coordinates"], "approximate spot replaced by the exact one" refute reine["properties"].key?("approximate") renamed = features.find { |f| f["properties"]["source_id"].include?("48.1486") } assert_equal "Bratislava", renamed["properties"]["name"], "a name changed here is kept" assert_equal "a note", renamed["properties"]["description"].lines.first.strip, "an untouched description still updates" end end