require_relative "test_helper" require "maptool/library" require "stringio" class LibraryTest < Minitest::Test def setup @dir = Dir.mktmpdir @store = Maptool::GpxStore.new(File.join(@dir, "gpx")) end def teardown FileUtils.remove_entry(@dir) end def test_a_fresh_library_migrates_each_old_file_into_a_subfolder_of_its_own @store.save(folder: "logs", filename: "ride.gpx", io: File.open(fixture_path("sample.gpx"))) @store.append_feature(feature: note("Gate", "n1"), folder: "", name: "My Notes") library = open_library tree = library.tree assert_equal ["logs", "My Notes"], tree["children"].map { |c| c["name"] } ride = tree["children"][0]["children"].first assert_equal ["ride", "folder"], ride.values_at("name", "type") layer = ride["children"].first assert_equal ["ride.gpx", %w[waypoint track]], layer.values_at("name", "kinds") assert_equal %w[waypoint track], JSON.parse(library.geojson(layer["path"]))["features"].map { |f| f["properties"]["kind"] } # Running it again (or reopening) doesn't duplicate anything. library.migrate assert_equal tree, open_library.tree end def test_a_layers_notes_are_still_edited_in_its_old_file @store.append_feature(feature: note("Gate", "n1"), folder: "", name: "My Notes") library = open_library key = library.tree["children"].first["children"].first["path"] library.update_feature(key, "n1", note("Wide gate", "n1")) assert_equal ["Wide gate"], JSON.parse(library.geojson(key))["features"].map { |f| f["properties"]["name"] } library.delete_feature(key, "n1") assert_empty JSON.parse(library.geojson(key))["features"] end def test_every_write_bumps_rev_and_deletes_are_tombstones library = open_library before = library.rev key = library.add_note(note("Gate"), "Van") id = JSON.parse(library.geojson(key))["features"].first["properties"]["id"] library.delete_feature(key, id) assert_operator library.rev, :>, before row = library.send(:connect) { |db| db.get_first_row("SELECT deleted_at, rev FROM items WHERE id = ?", [id]) } refute_nil row[0] assert_equal library.rev, row[1] assert_empty JSON.parse(library.geojson(key))["features"] end def test_the_same_feature_is_deduped_per_folder_only library = open_library library.add_note(note("Gate"), "a") library.add_note(note("Gate"), "a") library.add_note(note("Gate"), "b") counts = library.tree["children"].map { |folder| folder["children"].first["name"] } assert_equal ["Points (1)", "Points (1)"], counts end def test_moving_a_folders_points_returns_their_new_key library = open_library key = library.add_note(note("Gate"), "a") moved = library.move_node(key, "b/c") refute_equal key, moved assert_equal ["Gate"], JSON.parse(library.geojson(moved))["features"].map { |f| f["properties"]["name"] } assert_raises(Maptool::Library::NotFound) { library.move_node(key, "b") } end def test_deleting_a_folder_takes_its_subfolders_and_items library = open_library key = library.add_note(note("Gate"), "a/b") library.delete_folder("a") assert_empty library.tree["children"] assert_empty JSON.parse(library.geojson(key))["features"] assert_raises(Maptool::Library::InvalidPath) { library.create_folder("../x") } end def test_renaming_and_recoloring_a_track_and_a_layer @store.save(folder: "", filename: "ride.gpx", io: File.open(fixture_path("sample.gpx"))) library = open_library layer = library.tree["children"].first["children"].first["path"] track = library.import(folder: "t", filename: "ride.gpx", io: File.open(fixture_path("sample.gpx"))).first library.update_node(track, name: "Morning ride", color: "#00ff00") library.update_node(layer, color: "#0000ff") nodes = library.tree["children"].flat_map { |f| f["children"].flat_map { |c| c["children"] || [c] } } assert_includes nodes.map { |n| n.values_at("name", "color") }, ["Morning ride", "#00ff00"] lines = JSON.parse(library.geojson(layer))["features"].reject { |f| f["geometry"]["type"] == "Point" } assert_equal ["#0000ff"], lines.map { |f| f["properties"]["color"] }.uniq library.update_node(track, color: "") assert_nil JSON.parse(library.geojson(track))["features"].first["properties"]["color"] assert_raises(Maptool::Library::InvalidValue) { library.update_node(track, color: "red") } end def test_splitting_a_layer_turns_it_into_items_and_keeps_note_ids @store.append_feature(feature: note("Gate", "n1"), folder: "", name: "My Notes") library = open_library folder = library.tree["children"].first key = folder["children"].first["path"] keys = library.split_layer(key) assert_equal ["Points (1)"], library.tree["children"].first["children"].map { |c| c["name"] } assert_equal ["n1"], JSON.parse(library.geojson(keys.first))["features"].map { |f| f["properties"]["id"] } assert_equal ["My Notes.geojson"], Dir.glob("*/*", base: File.join(@dir, "sources")).map { |f| File.basename(f) } assert_raises(Maptool::Library::NotFound) { library.geojson(key) } end def test_renaming_and_moving_folders library = open_library key = library.add_note(note("Gate"), "a/b") library.create_folder("c") assert_equal "a/x", library.rename_folder("a/b", "x") assert_equal "c/x", library.move_folder("a/x", "c") assert_equal ["a", "c"], library.tree["children"].map { |f| f["name"] } assert_equal ["Gate"], JSON.parse(library.geojson(key))["features"].map { |f| f["properties"]["name"] } assert_raises(Maptool::Library::InvalidPath) { library.move_folder("c", "c/x") } library.create_folder("a/x") assert_raises(Maptool::Library::InvalidValue) { library.move_folder("c/x", "a") } end def test_a_saved_route_is_a_route_node_and_editing_replaces_it route = { "type" => "Feature", "geometry" => { "type" => "LineString", "coordinates" => [[17.1, 48.1], [17.2, 48.2]] }, "properties" => { "name" => "To the lake", "kind" => "route", "via" => [[17.1, 48.1], [17.2, 48.2]] } } library = open_library key = library.add_note(route, "Routes") node = library.tree["children"].first["children"].first assert_equal [key, ["route"]], node.values_at("path", "kinds") id = JSON.parse(library.geojson(key))["features"].first["properties"]["id"] edited = route.merge("properties" => route["properties"].merge("name" => "Lake, scenic", "via" => [[17.1, 48.1], [17.15, 48.3], [17.2, 48.2]])) library.update_feature(key, id, edited) features = JSON.parse(library.geojson(key))["features"] assert_equal [["Lake, scenic", 3]], features.map { |f| [f["properties"]["name"], f["properties"]["via"].size] } end def test_changes_lists_everything_after_a_rev_tombstones_included library = open_library start = library.rev key = library.add_note(note("Gate"), "Van") id = JSON.parse(library.geojson(key))["features"].first["properties"]["id"] library.delete_feature(key, id) changes = library.changes(start) assert_equal library.rev, changes["rev"] assert_equal ["Van"], changes["folders"].map { |f| f["name"] } assert_equal [[id, true, "Point"]], changes["items"].map { |i| [i["id"], i["deleted"], i["geometry"]["type"]] } assert_empty library.changes(library.rev)["items"] assert_raises(Maptool::Library::InvalidValue) { library.changes("x") } end def test_export_for_locus_and_osmand library = open_library library.import(folder: "Van/CZ", filename: "ride.gpx", io: File.open(fixture_path("sample.gpx"))) library.add_note(note("Gate"), "Van/CZ") library.add_note(note("Gate", nil).tap { |f| f["geometry"]["coordinates"] = [17.3, 48.3] }, "Van/CZ") locus = Maptool::Gpx.to_geojson(write_tmp(library.export_gpx("Van/CZ")))["features"] assert_equal 2, locus.count { |f| f["properties"]["name"] == "Gate" } assert(locus.any? { |f| f["properties"]["kind"] == "track" }) files = library.export_osmand("Van/CZ") assert_equal ["favourites.gpx", "tracks/Van/CZ/Morning Loop.gpx"], files.keys.sort favourites = Maptool::Gpx.to_geojson(write_tmp(files["favourites.gpx"]))["features"] assert_equal [["Gate", "Van / CZ"], ["Gate (2)", "Van / CZ"]], favourites.map { |f| f["properties"].values_at("name", "category") }.sort assert_raises(Maptool::Library::NotFound) { library.export_gpx("nope") } end private def write_tmp(content) File.join(@dir, "export-#{SecureRandom.hex(4)}.gpx").tap { |path| File.write(path, content) } end def open_library Maptool::Library.new(File.join(@dir, "library.sqlite"), gpx_store: @store, sources_dir: File.join(@dir, "sources")) end def note(name, id = nil) { "type" => "Feature", "properties" => { "name" => name, "category" => "Note", "id" => id }.compact, "geometry" => { "type" => "Point", "coordinates" => [17.1, 48.1] } } end end