Files
EscapistsReintroduced/tests/test_server_item_catalog.py
T
jakub 6a667d8deb Capture current mission, AI, map and administration development
Include the current addon, terrain integrations, tools, regression tests and recorded development evidence.
2026-10-13 22:52:53 +02:00

46 lines
3.8 KiB
Python

"""Exercise per-admin catalog navigation; discovery itself is checked natively."""
from policy_harness import ROOT, body, run
core=ROOT/'addons/core/Scripts/Game/ERR/00_Core'
s=body(core/'ERR_AdminSession.c','protected bool ItemCatalogCommand(')
for a,b in [('protected ',''),('array<string> replies','Array<string>& replies'),('array<string> parts','Array<string> parts'),('ERR_ServerItemCatalog catalog','Catalog* catalog'),('GetGame().ERR_ServerItems()','GetCatalog()'),('GetGame().ERR_ItemSelections().','preferences.'),('catalog.','catalog->'),('ERR_CommandArguments.Integer','Integer')]:s=s.replace(a,b)
run(r'''
#include <cassert>
#include <string>
#include <vector>
#include <sstream>
struct string;
template<class T>struct Array:std::vector<T>{using std::vector<T>::vector;bool IsEmpty(){return this->empty();}int Count(){return this->size();}void Insert(T x){this->push_back(x);}};
struct string:std::string{using std::string::string;string(std::string s):std::string(s){}void Split(string,Array<string>& out,bool){std::istringstream in(*this);std::string word;while(in>>word)out.push_back(word);}};
struct Catalog{bool Contains(string a,string i){return a=="loaded-a"&&i=="known-item";}void Start(void*){}bool Find(string s){return s=="loaded-a"||s=="loaded-b";}} catalog;
Catalog* GetCatalog(){return &catalog;}
bool Integer(string s,int& n){try{size_t p;n=std::stoi(s,&p);return p==s.size();}catch(...){return false;}}
struct Preferences{int edits=0;bool Preset(string action,string name){edits++;return action=="save"&&name=="test";}bool Set(string,string,bool){edits++;return true;}bool SetWeight(string,string,int){edits++;return true;}} preferences;
struct Session{void* m_Mode=nullptr;string m_ItemAddon;int m_ItemPage=0;
''' + s + r'''
};
int main(){Session a,b;Array<string> replies;
assert(!a.ItemCatalogCommand("status",replies));
assert(a.ItemCatalogCommand("items list loaded-a 2",replies)&&a.m_ItemAddon=="loaded-a"&&a.m_ItemPage==2);
assert(b.ItemCatalogCommand("items list loaded-b 1",replies)&&b.m_ItemAddon=="loaded-b"&&a.m_ItemAddon=="loaded-a"&&a.m_ItemPage==2);
for(string bad:{"items", "items list unknown 0", "items list loaded-a -1", "items list loaded-a 2731", "items list loaded-a 0junk", "items list loaded-a 0 extra", "items disable loaded-a 0"}){
assert(a.ItemCatalogCommand(bad,replies));assert(a.m_ItemAddon=="loaded-a"&&a.m_ItemPage==2);
}
assert(a.ItemCatalogCommand("items list loaded-a 0",replies)&&a.m_ItemPage==0);
for(string bad:{"items set unknown * 0","items set loaded-a unknown-item 0","items set loaded-a known-item 2","items set loaded-a * 0 extra"})a.ItemCatalogCommand(bad,replies);
assert(preferences.edits==0);
a.ItemCatalogCommand("items set loaded-a known-item 0",replies);assert(preferences.edits==1);
a.ItemCatalogCommand("items set loaded-a * 1",replies);assert(preferences.edits==2);
a.ItemCatalogCommand("items preset save test",replies);assert(preferences.edits==3);
for(string bad:{"items weight unknown * 100","items weight loaded-a unknown-item 100","items weight loaded-a * 101","items weight loaded-a * -1","items weight loaded-a * 100junk"})a.ItemCatalogCommand(bad,replies);
assert(preferences.edits==3);
a.ItemCatalogCommand("items weight loaded-a * 100",replies);assert(preferences.edits==4);
a.ItemCatalogCommand("items weight loaded-a known-item 0",replies);assert(preferences.edits==5);
assert(a.m_ItemAddon=="loaded-a"&&a.m_ItemPage==0);
}
''')
command=body(core/'ERR_AdminSession.c','void Command(')
assert command.index('if (!Authorize()) return;') < command.index('ItemCatalogCommand(arguments, replies)')
assert 'PAGE_SIZE = 12' in (core/'ERR_ServerItemCatalog.c').read_text()
assert 'GetLoadedAddons' in (core/'ERR_ServerItemCatalog.c').read_text()
print('PASS: catalog requests use authenticated sessions, independent admin page selections, loaded-mod allowlisting, bounded pages and allowlisted item mutations')