Include the current addon, terrain integrations, tools, regression tests and recorded development evidence.
88 lines
6.2 KiB
Python
88 lines
6.2 KiB
Python
from pathlib import Path
|
|
p=Path('/tmp/erri-config-review-1509/tests/test_profile_config.py');s=p.read_text().replace('from policy_harness import ROOT, body, run','import os\nfrom pathlib import Path\nfrom policy_harness import ROOT, body, run').replace("p = ROOT/'addons/core/Scripts/Game/ERR/00_Core/ERR_ProfileConfig.c'","p = Path(os.environ.get('ERR_PROFILE_CONFIG_SOURCE', ROOT/'addons/core/Scripts/Game/ERR/00_Core/ERR_ProfileConfig.c'))")
|
|
# Existing transaction cases model well-formed sections; specialized validation is below.
|
|
s=s.replace(' static string Encode(Map values)', ' static bool ContentsValid(Map){return true;}\n static string Encode(Map values)')
|
|
s += r'''
|
|
# Execute actual shared Save and Commit, with a small token document store.
|
|
# JSON lexing and section parsers are explicit doubles: malformed terrain is a
|
|
# valid JSON object rejected by the real TerrainPreference.Parse predicate.
|
|
# No native filesystem atomicity or full JsonLoadContext implementation claim.
|
|
def translate(method):
|
|
for before, after in [
|
|
('map<string, string>', 'Map'), ('new Map()', 'Map()'),
|
|
('FileHandle file','FileHandle* file'), ('file.','file->'),
|
|
('FileIO.','FileIO::'), ('FileMode.','FileMode::'), ('Replication.','Replication::'),
|
|
('JsonSaveContext json','JsonSaveContext& json')]:
|
|
method=method.replace(before,after)
|
|
method=method.replace('foreach (string key, string value : changed)', 'for (auto [key,value] : changed)')
|
|
method=method.replace('foreach (string path : LegacyPaths())', 'for (string path : LegacyPaths())')
|
|
return method
|
|
helper='static bool ContentsValid(Map) {return true;}'
|
|
if 'static bool ContentsValid(' in p.read_text(): helper=translate(body(p,'static bool ContentsValid('))
|
|
run(r"""
|
|
#include <cassert>
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
struct string:std::string{using std::string::string;string(std::string s):std::string(s){}bool IsEmpty(){return empty();}void TrimInPlace(){}};
|
|
struct Map:std::map<string,string>{using std::map<string,string>::map;bool Contains(string key){return count(key);}string Get(string key){return at(key);}void Set(string key,string value){(*this)[key]=value;}};
|
|
const string PATH="Escapists.json";
|
|
std::map<string,string> files;
|
|
std::map<string,Map> documents;
|
|
int serial=0,copies=0;
|
|
namespace Replication{bool server=true;bool IsServer(){return server;}}
|
|
namespace FileMode{int WRITE=1;}
|
|
struct FileHandle{string path;void Write(string data){files[path]=data;}void Close(){}};
|
|
namespace FileIO{
|
|
FileHandle* OpenFile(string path,int){static FileHandle h;h.path=path;return &h;}
|
|
bool FileExists(string path){return files.count(path);}
|
|
bool DeleteFile(string path){return files.erase(path);}
|
|
bool CopyFile(string from,string to){if(!files.count(from))return false;copies++;files[to]=files[from];return true;}
|
|
}
|
|
struct JsonSaveContext{string token;string SaveToString(){return token;}};
|
|
struct Store{
|
|
static std::vector<string> LegacyPaths(){return {"mission","mods","factions","terrain","items","voice"};}
|
|
static string Section(string path){return path;}
|
|
static bool Ensure(){return true;}
|
|
static string Encode(Map values){string id="doc"+std::to_string(++serial);documents[id]=values;return id;}
|
|
static bool Split(string text,Map& values){if(!documents.count(text))return false;values=documents[text];return true;}
|
|
static bool Valid(string section,string text){Map fields;if(!Split(text,fields))return false;return section!="terrain"||(fields.Contains("scenario")&&fields.Get("scenario")!="malformed");}
|
|
static bool Document(string path,Map& values){return files.count(path)&&Split(files[path],values)&&values.Contains("configVersion")&&values.Get("configVersion")=="1";}
|
|
static bool Same(Map a,Map b){return a==b;}
|
|
""" + helper + translate(body(p,'static bool Commit(')) + translate(body(p,'static bool Save(')) + r"""
|
|
};
|
|
int main(){
|
|
auto validTerrain=Store::Encode({{"scenario","valid-saved-terrain"},{"futureTerrainKey","keep"}});
|
|
auto invalidTerrain=Store::Encode({{"scenario","malformed"},{"futureTerrainKey","keep"}});
|
|
auto mission=Store::Encode({{"timer","10"},{"unknownMissionKey","keep"}});
|
|
auto changedMission=Store::Encode({{"timer","20"}});
|
|
Map good={{"configVersion","1"},{"mission",mission},{"terrain",validTerrain},{"unknownRoot","keep"}},current=good,read;
|
|
current.Set("terrain",invalidTerrain);string originalGood=Store::Encode(good),originalBad=Store::Encode(current);
|
|
files[PATH]=originalBad;files[PATH+".bak"]=originalGood;
|
|
// Owner would reject current terrain and accept backup. An unrelated save must
|
|
// not destroy the only recovery document or report success with invalid data.
|
|
assert(!Store::Valid("terrain",current.Get("terrain"))&&Store::Valid("terrain",good.Get("terrain")));
|
|
JsonSaveContext change{changedMission};
|
|
assert(!Store::Save("mission",change));
|
|
assert(files[PATH]==originalBad&&files[PATH+".bak"]==originalGood&&copies==0);
|
|
// Repair the section through its recovered menu value. Preserve the only good
|
|
// backup; merge unknown fields and allow subsequent ordinary menu persistence.
|
|
JsonSaveContext repair{validTerrain};assert(Store::Save("terrain",repair));
|
|
assert(files[PATH+".bak"]==originalGood&&Store::Document(PATH,read));
|
|
assert(read.Get("unknownRoot")=="keep");Map fields;assert(Store::Split(read.Get("terrain"),fields)&&fields.Get("futureTerrainKey")=="keep");
|
|
string repaired=files[PATH];assert(Store::Save("mission",change));assert(files[PATH+".bak"]==repaired);
|
|
assert(Store::Document(PATH,read)&&Store::Split(read.Get("mission"),fields));assert(fields.Get("timer")=="20"&&fields.Get("unknownMissionKey")=="keep");
|
|
// Missing optional sections continue to use their owner defaults; unknown roots
|
|
// are not subject to this version's validators.
|
|
Map sparse={{"configVersion","1"},{"unrecognized","opaque"}};assert(Store::Commit(sparse));
|
|
// Existing defaults and each known section are checked; no invalid first write.
|
|
files.clear();copies=0;assert(!Store::Commit(current)&&files.empty()&&copies==0);
|
|
// Updating an already healthy document retains ordinary last-valid backup.
|
|
files[PATH]=originalGood;assert(Store::Save("mission",change));assert(files[PATH+".bak"]==originalGood);
|
|
}
|
|
""")
|
|
print('PASS: actual Save/Commit preserves semantically valid backup across unrelated menu edits and repairs; unknown fields/default sections retained; parsers and disk doubled')
|
|
'''
|
|
p.write_text(s)
|