Files
Dreierpack-Manager/tests/modern_other_read_tests.cpp
T

52 lines
5.3 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
// Authored cup records exercise the existing Modern overview, without original data.
#include "modern/content/ui_contract.hpp"
#include "modern/content/projections.hpp"
#include "modern/game/cup.hpp"
#include "formats/executable_layout.hpp"
#include "formats/textd.hpp"
#include <iostream>
#include <stdexcept>
using namespace bmh;
namespace {
using J=cli::Json;using O=J::Object;
const J& at(const J& j,const char* k){return j.object()->at(k);}
void check(bool yes,const char* why){if(!yes)throw std::runtime_error(why);}
}
int main() try {
modern::ModernState g;g.manager_teams={0};g.manager_records.resize(0xb0c);g.display_start_year=2026;
g.memory.add_block(layout::CupTable,modern::kCupEntries*modern::kCupEntrySize);
g.memory.add_block(layout::ActiveSelectors,8);
g.memory.add_block(layout::SyntheticCupParticipantsBegin,0x100);
g.memory.add_block(layout::SyntheticCupResultsBegin,0x100);
for(int i=0;i<modern::kCupEntries;++i){g.memory.set16(modern::cup_entry_address(i)+0x18,0xffff);g.memory.set8(modern::cup_entry_address(i)+0x15,0xff);}
for(int i=0;i<8;++i)g.memory.set8(layout::ActiveSelectors+i,0x14);
g.memory.set8(layout::ActiveSelectors,0);g.memory.set8(layout::ActiveSelectors+1,1);
g.club_ids.resize(4);g.club_names.resize(8);g.world_club_id.resize(4);g.world_club_name.resize(4);
for(int i=0;i<4;++i){g.club_ids[i]=i*2;g.club_names[i*2]='A'+i;g.world_club_id[i]="club:"+std::to_string(i);g.world_club_name[i]="Synthetic "+std::to_string(i);g.memory.set16(layout::SyntheticCupParticipantsBegin+i*2,i);}
g.squad_slots.resize(4000,kEmptySlot);g.team_morale.resize(500);
const auto descriptor=[&](int entry,int comp,int teams,int rounds,int flags){auto a=modern::cup_entry_address(entry);g.memory.set_pointer(a,layout::SyntheticCupParticipantsBegin);g.memory.set_pointer(a+0x0c,layout::SyntheticCupResultsBegin+entry*16);g.memory.set16(a+0x18,comp);g.memory.set8(a+0x14,flags);g.memory.set8(a+0x1a,teams);g.memory.set8(a+0x1b,rounds);};
descriptor(0,0,4,2,4);descriptor(1,1,2,1,5);
g.memory.set16(layout::SyntheticCupResultsBegin,0x41);g.memory.set16(layout::SyntheticCupResultsBegin+2,0xcccc);
g.memory.set16(layout::SyntheticCupResultsBegin+16,0x400|0x21);
g.memory.set_pointer(modern::cup_entry_address(1)+0x10,layout::SyntheticCupResultsBegin+32);g.memory.set16(layout::SyntheticCupResultsBegin+32,0x800|0x62);g.memory.set8(modern::cup_entry_address(1)+0x1d,1);
TextDb texts;texts.strings.resize(1200);for(size_t i=567;i<620;++i)texts.strings[i]="Synthetic round";g.texts=&texts;
modern::UiContext c;c.manager=0;c.state_revision=9;
const auto read=[&](const char* view,int round){return modern::ui_read(g,c,O{{"kind","other-fixtures"},{"viewId",view},{"round",round}});};
const auto index=modern::ui_read(g,c,O{{"kind","other-index"}});check(at(at(index,"data"),"views").array()->size()==2,"active cup index differs");
const auto first=read("cup-view:0:0",1);const auto &m=*at(at(first,"data"),"matches").array();check(m.size()==2,"full first round missing");check(*at(m[0],"status").string()=="played"&&*at(m[0],"result").array()->at(0).integer()==2,"score changed");check(std::holds_alternative<std::nullptr_t>(at(m[1],"result").value),"open fixture has invented score");
const auto future=read("cup-view:0:0",2);check(!std::get<bool>(at(at(future,"data"),"available").value)&&at(at(future,"data"),"matches").array()->empty(),"future draw invented");
const auto legs=read("cup-view:1:0",1);const auto &leg=at(at(legs,"data"),"matches").array()->at(0);check(*at(leg,"phase").integer()==1&&*at(leg,"secondLegPhase").integer()==2,"cup phases lost");check(*at(leg,"secondLeg").array()->at(0).integer()==2&&*at(leg,"secondLeg").array()->at(1).integer()==3,"second leg not oriented to displayed sides");
check(*at(read("cup-view:0:0",0),"messageType").string()=="error"&&*at(read("cup-view:0:0",3),"messageType").string()=="error","invalid cup rounds accepted");
check(*at(read("cup-view:99:0",1),"messageType").string()=="error","foreign cup accepted");
g.memory.add_block(layout::CalendarState_FormatError_RuntimeField3,2);g.memory.set16(layout::CalendarState_FormatError_RuntimeField3,10);
descriptor(2,12,2,1,4);g.memory.set8(layout::ActiveSelectors+2,12);
g.memory.set_pointer(modern::cup_entry_address(2),0);g.memory.set_pointer(modern::cup_entry_address(2)+0x0c,0);
const auto metadata=modern::ui_read(g,c,O{{"kind","other-index"}});check(at(at(metadata,"data"),"views").array()->size()==3,"metadata index accessed uninitialised row arrays");
g.memory.set_pointer(modern::cup_entry_address(2),layout::SyntheticCupParticipantsBegin);g.memory.set_pointer(modern::cup_entry_address(2)+0x0c,layout::SyntheticCupResultsBegin+32);g.memory.set16(layout::SyntheticCupResultsBegin+32,5);
const auto scheduled=read("cup-view:12:0",1);const auto &planned=at(at(scheduled,"data"),"matches").array()->at(0);
check(*at(planned,"status").string()=="scheduled"&&std::holds_alternative<std::nullptr_t>(at(planned,"result").value)&&at(planned,"date").string(),"scheduled offset was decoded as score");
check(g.memory.u16(layout::SyntheticCupResultsBegin)==0x41&&g.memory.u8(layout::ActiveSelectors)==0,"pure read changed source bytes");
std::cout<<"cup index, null results, future draws, phases and reversed legs passed\n";return 0;
} catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}