// SPDX-License-Identifier: MIT #include "openpu/storage.hpp" #include "test_util.hpp" #include using namespace openpu; TEST_CASE("official vehicles.json (with BOM) is understood") { const std::string official = "\xEF\xBB\xBF" R"({"VF3AAAAAAAAAAAAA1":{"name":"5008","type":"nac","wave":"path_guide_wave_4"},)" R"("VR3AAAAAAAAAAAAA2":{"name":"e208","type":"rcc","wave":"path_guide_rcc",)" R"("undeletedFiles":{"fw":"C:\\dl\\PSA_rcc.tar","license":"C:\\dl\\license_X.key"}}})"; const auto v = VehicleStore::parse(official); REQUIRE(v.size() == 2); CHECK(v[0].vin == "VF3AAAAAAAAAAAAA1"); CHECK(v[0].type == HeadUnitType::Nac); CHECK(v[0].guideKey == "path_guide_wave_4"); CHECK(v[1].type == HeadUnitType::Rcc); CHECK(v[1].pendingFirmware == "C:\\dl\\PSA_rcc.tar"); CHECK(v[1].pendingLicense == "C:\\dl\\license_X.key"); CHECK(v[1].pendingMap.empty()); } TEST_CASE("vehicle store round trip") { TempDir tmp; VehicleStore store(tmp.path() / "sub" / "vehicles.json"); store.load(); // missing file CHECK(store.all().empty()); Vehicle a; a.vin = "VF3AAAAAAAAAAAAA1"; a.name = "Ma 208"; a.type = HeadUnitType::Nac; a.guideKey = "path_guide_wave_2"; store.upsert(a); Vehicle b = a; b.vin = "VR3AAAAAAAAAAAAA2"; b.type = HeadUnitType::CronyNonav; b.pendingMap = "/dl/map.tar"; store.upsert(b); a.name = "renamed"; store.upsert(a); store.save(); VehicleStore again(store.file()); again.load(); REQUIRE(again.all().size() == 2); REQUIRE(again.find("VF3AAAAAAAAAAAAA1") != nullptr); CHECK(again.find("VF3AAAAAAAAAAAAA1")->name == "renamed"); CHECK(again.find("VR3AAAAAAAAAAAAA2")->type == HeadUnitType::CronyNonav); CHECK(again.find("VR3AAAAAAAAAAAAA2")->pendingMap == "/dl/map.tar"); CHECK(again.remove("VF3AAAAAAAAAAAAA1")); CHECK_FALSE(again.remove("VF3AAAAAAAAAAAAA1")); CHECK(again.all().size() == 1); } TEST_CASE("corrupt vehicles file is an error, not silently emptied") { TempDir tmp; writeAll(tmp.path() / "vehicles.json", "{broken"); VehicleStore store(tmp.path() / "vehicles.json"); CHECK_THROWS_AS(store.load(), StorageError); } TEST_CASE("settings round trip") { TempDir tmp; const auto file = tmp.path() / "config.json"; Settings s = Settings::load(file); CHECK(s.language.empty()); CHECK_FALSE(s.pending.has_value()); CHECK_FALSE(s.effectiveDownloadDir().empty()); s.language = "fr"; s.downloadDir = "/data/dl"; PendingDownload p; p.vin = "VR3AAAAAAAAAAAAA2"; p.firmware = true; p.firmwareFile = "PSA_rcc.tar"; p.licenseFile = "license_X.key"; s.pending = p; s.save(file); const Settings r = Settings::load(file); CHECK(r.language == "fr"); CHECK(r.effectiveDownloadDir() == "/data/dl"); REQUIRE(r.pending.has_value()); CHECK(r.pending->firmware); CHECK_FALSE(r.pending->map); CHECK(r.pending->licenseFile == "license_X.key"); CHECK(r.checkAppUpdates); // default s.checkAppUpdates = false; s.save(file); CHECK_FALSE(Settings::load(file).checkAppUpdates); } TEST_CASE("XDG paths") { // Only checks the shape: paths end with our application directory. #ifdef __HAIKU__ CHECK(paths::configDir().filename() == "OpenPU"); CHECK(paths::defaultDownloadDir().filename() == "OpenPU downloads"); #else CHECK(paths::configDir().filename() == "openpu"); CHECK(paths::defaultDownloadDir().filename() == "OpenPU"); #endif }