// SPDX-License-Identifier: GPL-3.0-only #include "modern/ui/demo_transfer.hpp" #include #include #include #include using namespace bmh::modern_ui; namespace { void require(bool value,const char *message){if(!value)throw std::runtime_error(message);} std::string pid(int team,int i){return "player:transfer-test-"+std::to_string(team)+"-"+std::to_string(i);} struct Fixture { fs::path root;std::shared_ptr world; Fixture(int own_count=24,int precision=0,int foreign_count=30){ root=fs::temp_directory_path()/("openbmh-transfer-ui-"+std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));fs::create_directories(root/"world"); const auto write=[&](const std::string&file,const Json&v){std::ofstream f(root/file);f<{"gk","def","mid","fwd"}[size_t(i)%4];std::string name="Synthetic player "+std::to_string(team)+" "+std::to_string(i);if(team==1&&i==0)name="Änne Müller";if(team==2&&i==1)name="François Öztürk"; Json p={{"id",pid(team,i)},{"team",club},{"name",name},{"positions",{role}},{"squad_order",i},{"nationalities",{"nation:de"}},{"born","1995-01-01"},{"attributes",{{"strength",70+i%15},{"condition",68+i%15},{"technique",72+i%15},{"form",70+i%15},{"corner",0},{"free_kick",0},{"tackling",0},{"passing",0},{"shooting",0},{"speed",0},{"teamplay",0}}}}; if(team==0&&i==1){p.erase("born");p["birth_date"]="1995-01-01";p["attributes"].erase("form");}players.push_back(p); } } write("world/clubs.json",{{"items",clubs}});write("world/players.json",{{"items",players}});world=Catalog::open(root); } ~Fixture(){std::error_code e;fs::remove_all(root,e);} }; Json strip(Json state){state.erase("transfer");return state;} void edit(Json&s,const Json&a,const Catalog&w){auto e=edit_demo_transfer(s,a,w,"club:transfer-test-0");if(!e.empty())throw std::runtime_error("Unexpected transfer rejection "+e+" "+a.dump());require(valid_demo_transfer(s,w,"club:transfer-test-0"),"Accepted action invalidated transfer state");} void reject(Json&s,const Json&a,const Catalog&w){auto before=s;require(!edit_demo_transfer(s,a,w,"club:transfer-test-0").empty()&&s==before,"Rejected transfer action changed state or accepted");} Json quote(const Json&s,const Catalog&w,const char*kind,const std::string&id,bool poach=false){auto q=demo_transfer_quote(s,{{"kind",kind},{"player",id},{"poached",poach}},w,"club:transfer-test-0");if(!q.value("ok",false))throw std::runtime_error("Unexpected quote rejection "+q.dump());return q;} Json order(const Json&q,int factor){Json o={{"kind",q.at("kind")},{"player",q.at("row").at("id")},{"source_club",q.at("source_club")},{"fee",q.at("fee").get()/factor},{"salary",q.at("salary").get()/factor},{"months",q.at("months")},{"bonuses",q.at("bonuses")},{"poached",q.at("poached")}};for(auto&b:o["bonuses"])b=b.get()/factor;return o;} void check_currency(int precision){ Fixture f(24,precision);const auto&w=*f.world;const int factor=precision?100:1;Json state={{"cash",1000000LL*factor},{"date","2026-07-01"},{"contracts",{{"players",{{pid(0,0),{{"salary",12000*factor}}}}}}},{"lineup",{{"order",Json::array()}}},{"unrelated",{1,2,3}}};const auto before=state,world_players=w.rows("players"); require(migrate_demo_transfer(state,w,"club:transfer-test-0")&&valid_demo_transfer(state,w,"club:transfer-test-0")&&strip(state)==before,"Transfer migration failed or altered outside fields"); auto initial=state;auto view=demo_transfer_view(state,w,"club:transfer-test-0");require(view.at("own").size()==24&&view.at("market").size()==12,"Transfer truncated own roster or invented market count");require(view.at("own")[1].at("age").is_null()&&view.at("own")[1].at("form").is_null()&&view.at("own")[0].at("age")==31&&view.at("own")[0].at("skills")[0]==0,"Unavailable fields invented or born/zero data lost"); const auto buy=quote(state,w,"buy",pid(1,0));require(buy.at("fee")==450000*factor&&buy.at("salary")==25000*factor&&buy.at("months")==24,"Pinned Gold demand units or terms changed");const auto sell=quote(state,w,"sell",pid(0,0));require(sell.at("demand_salary")==12000*factor,"Own current contract salary missing");require(quote(state,w,"sell",pid(0,1)).at("demand_salary").is_null(),"Missing salary invented"); auto copied=buy;copied["fee"]=1;require(quote(state,w,"buy",pid(1,0))==buy&&state==initial,"Quote aliases or mutates state"); auto o=order(buy,factor);o["fee"]=99999999;o["bonuses"][6]=100;edit(state,{{"kind","submit"},{"order",o}},w);require(state["transfer"]["orders"][0]["fee"]==99999999LL*factor&&state["transfer"]["orders"][0]["bonuses"][6]==100*factor&&strip(state)==before,"Staging charged cash or converted fee/bonus units incorrectly"); auto loan=order(quote(state,w,"loan",pid(1,0)),factor);edit(state,{{"kind","submit"},{"order",loan}},w);require(state["transfer"]["orders"].size()==2,"Buy/loan were merged into one order");loan["months"]=36;reject(state,{{"kind","submit"},{"order",loan}},w); for(int i=0;i<3;++i)edit(state,{{"kind","submit"},{"order",order(quote(state,w,i==1?"lend":"sell",pid(0,i)),factor)}},w); auto fourth=order(sell,factor);fourth["player"]=pid(0,3);reject(state,{{"kind","submit"},{"order",fourth}},w);require(demo_transfer_quote(state,{{"kind","sell"},{"player",pid(0,3)},{"poached",false}},w,"club:transfer-test-0").at("error")=="transfer_list_full","Quote ignored three-player listing cap"); edit(state,{{"kind","submit"},{"order",order(quote(state,w,"lend",pid(0,0)),factor)}},w);require(state["transfer"]["listed"].size()==3&&state["transfer"]["orders"].size()==5,"Sell/lend replacement duplicated own listing");edit(state,{{"kind","unlist"},{"player",pid(0,0)}},w);require(state["transfer"]["listed"].size()==2&&state["transfer"]["orders"].size()==4,"Unlisting did not remove prospective own order"); auto criteria=state["transfer"]["criteria"];criteria["price_max"]=criteria["price_max"].get()/factor;const auto orders_before=state["transfer"]["orders"],listed_before=state["transfer"]["listed"]; for(const auto&text:{"muller","ÖZTÜRK","francois"}){edit(state,{{"kind","search"},{"query",text}},w);require(!demo_transfer_view(state,w,"club:transfer-test-0").at("market").empty(),"Accent/Unicode search failed");} std::string forty;for(int i=0;i<40;++i)forty+="ü";edit(state,{{"kind","search"},{"query",forty}},w);reject(state,{{"kind","search"},{"query",forty+"ü"}},w); criteria["price_max"]=0;edit(state,{{"kind","criteria"},{"value",criteria},{"positions",15}},w);require(demo_transfer_view(state,w,"club:transfer-test-0").at("market").empty()&&state["transfer"]["orders"]==orders_before&&state["transfer"]["listed"]==listed_before,"Filter changed orders/listings or zero price did not filter"); Json active=Json::array();for(int i=0;i<11;++i)active.push_back(pid(0,i));auto averages=demo_transfer_strengths(state,w,"club:transfer-test-0",active);require(averages.value("ok",false)&&averages.at("active").size()==4&&averages.at("team_average").is_number_integer(),"Lineup-fed strength projection failed"); auto bulk=demo_transfer_roster(state,w,"club:transfer-test-0","club:transfer-test-1");require(bulk.at("rows").size()==30,"Poach roster truncated to Gold twelve fixtures"); state=initial;for(int i=0;i<50;++i){const int team=i<30?1:2,index=i<30?i:i-30;edit(state,{{"kind","submit"},{"order",order(quote(state,w,"buy",pid(team,index),true),factor)}},w);}require(state["transfer"]["orders"].size()==50,"Order list did not retain fifty distinct targets");auto extra=order(quote(state,w,"buy",pid(2,29),true),factor);reject(state,{{"kind","submit"},{"order",extra}},w);edit(state,{{"kind","submit"},{"order",order(quote(state,w,"buy",pid(1,0),true),factor)}},w);require(state["transfer"]["orders"].size()==50,"Replacement blocked or duplicated at cap"); for(const auto&a:std::vector{nullptr,Json::array(),Json::object(),{{"kind",1}},{{"kind","unknown"}},{{"kind","positions"},{"value",0}},{{"kind","positions"},{"value",true}},{{"kind","positions"},{"value",uint64_t(-1)}},{{"kind","search"},{"query",1}},{{"kind","search"},{"query",std::string(1,char(255))}},{{"kind","remove"},{"index",50}},{{"kind","remove"},{"index",-1}},{{"kind","submit"}},{{"kind","search"},{"query",""},{"cash",0}}})reject(state,a,w); auto badorder=extra;badorder["fee"]=uint64_t(-1);reject(state,{{"kind","submit"},{"order",badorder}},w);badorder=extra;badorder["source_club"]="club:transfer-test-0";reject(state,{{"kind","submit"},{"order",badorder}},w);badorder=extra;badorder["salary"]=200001;reject(state,{{"kind","submit"},{"order",badorder}},w);badorder=extra;badorder["bonuses"][0]=101;reject(state,{{"kind","submit"},{"order",badorder}},w); std::vector corrupt;auto bad=state;bad["transfer"]["orders"].push_back(bad["transfer"]["orders"][0]);corrupt.push_back(bad);bad=state;bad["transfer"]["listed"].push_back(pid(0,0));corrupt.push_back(bad);bad=state;bad["transfer"]["criteria"]["traits"]=0;corrupt.push_back(bad);bad=state;bad["transfer"]["club"]="club:transfer-test-1";corrupt.push_back(bad);bad=state;bad["transfer"]["extra"]=true;corrupt.push_back(bad);bad=state;bad["transfer"].erase("orders");corrupt.push_back(bad); for(auto&v:corrupt){const auto keep=v;require(!valid_demo_transfer(v,w,"club:transfer-test-0")&&!migrate_demo_transfer(v,w,"club:transfer-test-0")&&v==keep,"Migration repaired corrupt existing transfer data");reject(v,{{"kind","search"},{"query",""}},w);} if(precision){state["transfer"]["orders"][0]["fee"]=123;auto keep=state;require(valid_demo_transfer(state,w,"club:transfer-test-0")&&migrate_demo_transfer(state,w,"club:transfer-test-0")&&state==keep,"Migration rounded existing cents");} require(valid_demo_transfer(Json::parse(state.dump()),w,"club:transfer-test-0")&&strip(state)==before&&w.rows("players")==world_players,"Roundtrip failed or transfer altered world/other model"); } } // namespace int main()try{check_currency(0);check_currency(2);for(int count:{0,2,20}){Fixture f(count,0,0);Json state={{"date","2026-07-01"},{"cash",0}};require(migrate_demo_transfer(state,*f.world,"club:transfer-test-0")&&demo_transfer_view(state,*f.world,"club:transfer-test-0").at("own").size()==size_t(count)&&demo_transfer_view(state,*f.world,"club:transfer-test-0").at("market").empty(),"Empty/small/full real roster unsafe");}std::cout<<"Modern UI transfer checks passed\n";return 0;}catch(const std::exception&e){std::cerr<