require_relative "test_helper" require "tmpdir" require_relative "../lib/maptool/updater" class UpdaterTest < Minitest::Test def setup Maptool::Updater.reset! end def test_latest_is_mains_commit_and_its_ci_state_cached_until_checked_again calls = [] fetch = lambda do |url| calls << url url.end_with?("/branches/main") ? { "commit" => { "id" => "a" * 40 } } : { "state" => "success" } end assert_equal({ sha: "a" * 40, ci: "success" }, Maptool::Updater.latest("https://git", "r/m", fetch: fetch)) assert_equal ["https://git/api/v1/repos/r/m/branches/main", "https://git/api/v1/repos/r/m/commits/#{'a' * 40}/status"], calls Maptool::Updater.latest("https://git", "r/m", fetch: fetch) assert_equal 2, calls.size # cached end def test_gitea_out_of_reach_is_no_answer_not_an_error assert_nil Maptool::Updater.latest("https://git", "r/m", fetch: ->(_) { raise SocketError }) end def test_a_request_holds_a_full_commit_only Dir.mktmpdir do |dir| assert_raises(ArgumentError) { Maptool::Updater.request(dir, "abc; rm -rf /") } Maptool::Updater.request(dir, "b" * 40) assert_equal "b" * 40, JSON.parse(File.read(File.join(dir, "update", "request.json")))["sha"] assert_nil Maptool::Updater.status(dir) end end end