Files
EscapistsReintroduced/tests/test_welcome_lifecycle.py
T

76 lines
5.1 KiB
Python

"""Exercise the production client tick with HTTP/audio substituted, across worlds."""
from policy_harness import ROOT, body, run
p = ROOT/'addons/core/Scripts/Game/ERR/00_Core/ERR_WelcomeVoice.c'
url = body(p, 'static bool URL(')
connection = 'class Connection { bool m_Attempted=false; public: ' + '\n'.join(body(p,s) for s in ['void MainMenu()', 'bool Claim()', 'bool Attempted()']) + '};'
tick = body(p, 'void Tick(')
for old,new in [('m_Mode.', 'm_Mode->'), ('m_Connection.', 'm_Connection->'), ('m_Callback.', 'm_Callback->'),
('context.', 'context->'), ('RestContext context', 'RestContext* context'),
('GetGame().GetRestApi().GetContext', 'game.api.GetContext'),
('null', 'nullptr'), ('ERR_WelcomeVoiceRules.URL', 'Rules::URL'), ('ERR_WelcomeVoiceRules.HTTPReady', 'Rules::HTTPReady'), ('ERR_WelcomeVoiceRules.Source', 'Rules::Source'), ('ERR_BakedSpeech.Graph', 'ERR_BakedSpeech::Graph'), ('ResourceName', 'string'),
('FileIO.DeleteFile', 'FileIO::DeleteFile'), ('LogLevel.WARNING','0')]:
tick=tick.replace(old,new)
run(r'''
#include <cassert>
#include <string>
struct string:std::string { using std::string::string; string(std::string s):std::string(s){}
bool IsEmpty(){return empty();} int Length(){return size();}
int IndexOf(string s){auto p=find(s);return p==npos?-1:int(p);}
};
struct ERR_BakedSpeech { static string Graph(string value){return value=="baked:welcome"?"bundled.acp":"";} };
struct Mode {bool enabled=true;bool ERR_RecordingsEnabled(){return enabled;}} mode;
bool httpReady=true;
struct Rules { static bool HTTPReady(){return httpReady;} static bool Source(string value){return URL(value)||!ERR_BakedSpeech::Graph(value).IsEmpty();} '''+url+r''' };
'''+connection+r'''
struct RestCallback { void SetOnSuccess(int){} void SetOnError(int){} };
int requests=0;
struct RestContext { void SetTimeout(int){} void GET(RestCallback*,string){requests++;} } context;
struct Api { RestContext* GetContext(string){return &context;} };
struct Game { Api api; } game;
struct FileIO { static void DeleteFile(string){} };
void Print(string,int=0){}
struct ERR_VoiceTransfer {};
struct Client {
ERR_VoiceTransfer* m_Transfer=nullptr; string m_DownloadURL,m_BakedGraph;
void RequestPart(){requests++;}
Mode* m_Mode=&mode;void Stop(){m_Stopped=true;m_Downloaded=false;}
Connection* m_Connection; bool m_Stopped=false,m_Downloaded=false,m_Started=false,current=true;
float m_Wait=0; string m_Path,m_Text; RestCallback* m_Callback=nullptr; int Downloaded=0,Failed=0,plays=0;
Client(Connection* c):m_Connection(c){} ~Client(){delete m_Callback;}
bool permit=true; bool Permit(){return permit;} bool Current(){return current;} void Play(){plays++;}
'''+tick+r'''
};
int main(){
Connection mutedConnection;Client muted(&mutedConnection);mode.enabled=false;
muted.Tick(60,true,"baked:welcome","");assert(!mutedConnection.Attempted()&&!muted.m_Downloaded&&requests==0);
mode.enabled=true;muted.Tick(3,true,"baked:welcome","");assert(muted.m_Downloaded);
mode.enabled=false;muted.Tick(1,true,"baked:welcome","");assert(muted.m_Stopped&&muted.plays==0);
Connection liveConnection;Client live(&liveConnection);live.Tick(3,true,"http://voice/clip","");assert(requests==1);requests=0;
mode.enabled=true;
Connection local;Client baked(&local);httpReady=false;int before=requests;
baked.Tick(3,true,"baked:welcome","Generic welcome");assert(requests==before&&baked.m_Downloaded);
baked.Tick(1,false,"baked:welcome","");assert(baked.plays==0);
baked.Tick(1,true,"baked:welcome","");assert(baked.plays==1&&requests==before);httpReady=true;
Connection connection;
Client first(&connection);
httpReady=false; first.Tick(60,true,"http://voice/clip", ""); assert(requests==0 && !connection.Attempted()); httpReady=true;
first.Tick(60,true,"", ""); assert(requests==0 && !connection.Attempted());
first.Tick(60,false,"http://voice/clip", "hello"); assert(requests==0);
first.permit=false; first.Tick(60,true,"http://voice/clip", "hello"); assert(requests==0 && !connection.Attempted()); first.permit=true; first.m_Wait=0;
first.Tick(2,true,"http://voice/clip", "hello"); assert(requests==0);
first.Tick(1,true,"http://voice/clip", "hello"); assert(requests==1);
first.m_Downloaded=true;
first.Tick(1,false,"http://voice/clip", "changed"); assert(first.plays==0);
first.Tick(1,true,"http://voice/clip", "changed"); assert(first.plays==1 && first.m_Text=="hello");
for(int i=0;i<100;i++)first.Tick(60,true,"http://voice/new", "changed");
assert(first.plays==1 && requests==1);
Client nextWorld(&connection); nextWorld.Tick(60,true,"http://voice/new", "new terrain"); assert(requests==1);
first.m_Stopped=true; first.m_Downloaded=true; first.Tick(60,true,"http://voice/new", ""); assert(first.plays==1);
connection.MainMenu();
Client reconnect(&connection); reconnect.Tick(3,true,"http://voice/new", "new terrain"); assert(requests==2);
// A failed request is still consumed for this connection, including a reload.
Client afterFailure(&connection); afterFailure.Tick(60,true,"http://voice/new", ""); assert(requests==2);
}
''')
print('PASS: actual welcome tick waits for URL/readiness/server permit, preserves local once-only state across worlds and refuses stopped callbacks')