Files
Dreierpack-Manager/tests/modern_career_reports_tests.cpp

77 lines
7.9 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-License-Identifier: GPL-3.0-only
#include "modern/ui/demo_career_reports.hpp"
#include <chrono>
#include <iostream>
#include <set>
#include <stdexcept>
using namespace bmh::modern_ui;
namespace {
void require(bool ok,const char *message){if(!ok)throw std::runtime_error(message);}
std::string id(int i){return "club:career-test-"+std::to_string(i);}
struct Fixture {
fs::path root;std::shared_ptr<const Catalog> world;
explicit Fixture(int count) {
root=fs::temp_directory_path()/("openbmh-career-reports-"+std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));fs::create_directories(root/"world");
const auto write=[&](const std::string &name,const Json &value){bmh::editor::write_atomic(root/name,value.dump());};
write("manifest.json",{{"format","openbmh-world"},{"kind","world"},{"format_version","1.1"},{"id","world:synthetic-career-reports"},{"start_date","2026-07-01"},{"requires",{"editor:world-v1"}},{"x-editor",{{"profile","editor-world-v1"}}}});
write("world/nations.json",{{"items",{{{"id","nation:de"},{"name","Synthetic nation"}}}}});
write("world/competitions.json",{{"items",{{{"id","competition:career-test"},{"type","league"},{"nation","nation:de"},{"name","Synthetic league"}}}}});
Json clubs=Json::array();for(int i=0;i<count;++i)clubs.push_back({{"id",id(i)},{"name",{{"default","Synthetic club "+std::to_string(i)}}},{"nation","nation:de"},{"competition","competition:career-test"},{"crest","asset:synthetic-crest-"+std::to_string(i)}});
clubs.push_back({{"id","nt:synthetic"},{"name","Synthetic national side"},{"type","national"}});
write("world/clubs.json",{{"items",clubs}});write("world/players.json",{{"items",Json::array()}});world=Catalog::open(root);
}
~Fixture(){std::error_code ec;fs::remove_all(root,ec);}
};
Json state(const std::string &club) {return {{"date","2027-07-07"},{"managers",{{{"name","Synthetic first manager"},{"club",club}},{{"name","Must not be used"},{"club",id(0)}}}},{"cash",123456},{"reports",{{"league","foreign"},{"filter",4}}}};}
void test_full() {
Fixture f(12);auto s=state(id(4));const auto original=s;const auto clubs=f.world->rows("clubs");
const auto current=demo_achievements(*f.world,s,0);require(current.at("ok")==true&&current.at("available")==true&&current.at("fictional")==true,"Career report unavailable for actual first-manager club");
require(current.at("club")==id(4)&&current.at("manager")=="Synthetic first manager","Career projection silently selected another manager or club");
require(current.at("first_year")==2019&&current.at("last_year")==2026&&current.at("range")=="2019/20 – 2026/27","Career window does not end at the last completed season");
require(current.at("charts").size()==3&&current.at("history").size()==12,"Expected three charts and twelve historical seasons");
for(int page=0;page<=4;++page) {
const auto view=demo_achievements(*f.world,s,page);require(view.at("last_year")==2026-page&&view.at("first_year")==2019-page,"Page must shift by one season, not eight");
require(view.at("history")==current.at("history"),"Club history changed when paging chart window");
for(const auto &chart:view.at("charts")) {
require(chart.at("seasons").size()==8,"Chart does not contain eight seasons");
for(size_t i=0;i<8;++i)require(chart.at("seasons")[i].at("year")==2019-page+int(i),"Chart years not chronological");
require(chart.at("latest")==chart.at("seasons")[7],"Latest summary disagrees with visible last season");
}
}
const auto older=demo_achievements(*f.world,s,4);std::set<int> tiers;
for(const auto &r:older.at("charts")[0].at("seasons"))tiers.insert(r.at("tier").get<int>());
require(tiers==std::set<int>({1,2,3,4}),"Four league bands were collapsed");
const auto &league=current.at("charts")[0].at("latest");require(league.at("value")==1&&league.at("winner")==true&&league.at("label")=="1. Platz"&&league.at("tier")==1,"Gold championship sample or title highlighting changed");
require(current.at("charts")[1].at("latest").at("label")=="Sieger"&&current.at("charts")[2].at("latest").at("label")=="Viertelfinale","Cup/Europe result labels changed");
bool zero=false;for(const auto &r:current.at("history"))for(const auto &value:r.at("results"))if(value.at("value")==0){zero=true;require(value.at("label")=="Nicht teilgenommen"&&value.at("caption")=="–"&&value.at("winner")==false,"Nonparticipation was fabricated into a played result or title");}
require(zero,"Historical demo lost its nonparticipation fixture");
require(demo_achievements(*f.world,s,-1).value("ok",true)==false&&demo_achievements(*f.world,s,5).value("ok",true)==false,"Out-of-range career page accepted");
const auto scores=demo_highscores(*f.world,s);require(scores.at("rows").size()==8,"Highscore must show at most eight existing clubs");std::set<std::string> seen;
for(size_t i=0;i<8;++i) {
const auto &r=scores.at("rows")[i];const auto club=r.at("club").get<std::string>();require(f.world->find("clubs",club)&&club!="nt:synthetic"&&seen.insert(club).second,"Unknown, national or duplicated club in highscore");
require(r.at("rank")==i+1&&r.at("points")==18200-int(i)*1575&&r.at("championships")==8-int(i%5)&&r.at("cups")==6-int(i%4)&&r.at("europe")==3-int(i%3)&&r.at("fictional")==true,"Highscore order/totals differ from deterministic Gold preview");
}
require(scores.at("rows")[0].at("club")==id(4)&&scores.at("rows")[0].at("manager")=="Synthetic first manager"&&scores.at("rows")[1].at("manager")=="Chris Winter","First manager or explicitly fictional manager fixtures changed");
auto other=s;other["reports"]={{"league","elsewhere"},{"filter",0}};other["cash"]=0;
require(demo_achievements(*f.world,other,0)==current&&demo_highscores(*f.world,other)==scores,"Career projection depends on unrelated filters or wallet");
other=s;other["managers"][0]["club"]=id(7);other["managers"][0]["name"]="Changed snapshot name";
require(demo_achievements(*f.world,other).at("club")==id(7)&&demo_highscores(*f.world,other).at("rows")[0].at("manager")=="Changed snapshot name","Manager switch retained a stale identity");
other=s;other["date"]="2027-06-30";require(demo_achievements(*f.world,other).at("last_year")==2025,"June incorrectly treated the current season as completed");
other["date"]="2028-02-29";require(demo_achievements(*f.world,other).at("last_year")==2026,"Valid leap date was rejected");
other["date"]="2027-02-29";require(demo_achievements(*f.world,other).at("last_year")==2025,"Invalid date did not use the deterministic manifest reference");
require(s==original&&f.world->rows("clubs")==clubs&&demo_achievements(*f.world,s)==current&&demo_highscores(*f.world,s)==scores,"Read-only projections mutated source/catalogue or were nondeterministic");
}
void test_short(int n) {
Fixture f(n);auto s=state(n?id(n-1):"club:missing");const auto before=s;
const auto a=demo_achievements(*f.world,s),h=demo_highscores(*f.world,s);
require(a.at("available")==bool(n)&&h.at("available")==bool(n)&&h.at("rows").size()==size_t(n),"Short catalogue invented extra clubs");
if(!n)require(a.at("charts").empty()&&a.at("history").empty()&&a.at("club").is_null(),"Empty catalogue invented an own club or historical identity");
if(n){s["managers"][0]["name"]="";require(demo_highscores(*f.world,s).at("rows")[0].at("manager")=="—","Missing current manager received a fictional name");s=before;}
for(const auto &bad:std::vector<Json>{nullptr,Json::array(),Json::object(),Json{{"managers",Json::array()}},state("club:missing"),state("nt:synthetic")}) {
const auto aa=demo_achievements(*f.world,bad),hh=demo_highscores(*f.world,bad);require(aa.at("ok")==true&&aa.at("available")==false&&aa.at("history").empty()&&hh.at("rows").empty(),"Missing/invalid first-manager identity silently fell back to another club");
}
require(s==before,"Short catalogue projection changed state");
}
} // namespace
int main()try{test_full();test_short(0);test_short(1);test_short(3);std::cout<<"Modern career report checks passed\n";return 0;}catch(const std::exception &e){std::cerr<<e.what()<<'\n';return 1;}