Spec : tous les messages des deux liaisons (hello/welcome, core/*, session/*, events/*, render/stop ; render/ready, render/failed, render/closed, render/input, render/stop), table Recording (SQRC), codes d'erreur ajoutes ; compatible avec published/0.5.0. Numeros 6 et 7 de studio-core reserves a log/protocol. Generateur : tableaux d'enumerations, encode_<message> et message_<message> en C++, readForCore en TypeScript, client RxJS (emit-client.ts), documentation HTML (emit-docs.ts). Core : protocole binaire seul (liaison choisie par l'identifiant de la premiere trame, texte refuse 1003, jeton/origine refuses 1008 avec le code en raison), tous les messages par CoreReceiver, base64 et FrameKind retires, enregistrement par la table Recording. 3d/wire : codec JSON retire. sequa-render, Studio host, Studio et le faux Core (tools/mock-core.ts) passes au contrat. Docs : specs JSON gelees, README, contract/README, plan de migration (restes : tutoriels, tests de contrat generes, essai 4 sous ASan). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
5.7 KiB
C++
179 lines
5.7 KiB
C++
// SPDX-License-Identifier: MPL-2.0
|
|
//
|
|
// Bout en bout : StudioHost pilote le VRAI sequa-core (spec 0.2 §7, ADR 0006), dans un
|
|
// dossier d'exécution temporaire (jamais celui de l'utilisateur).
|
|
#include "studio_host.hpp"
|
|
|
|
#include "sequa/studio/host/paths.hpp"
|
|
#include "sequa/studio/host/random.hpp"
|
|
#include "sequa/studio/host/run_file.hpp"
|
|
|
|
#include <doctest/doctest.h>
|
|
#include <studio_core_contract.hpp>
|
|
|
|
#include <chrono>
|
|
#include <filesystem>
|
|
#include <memory>
|
|
#include <thread>
|
|
|
|
using namespace sequa::studio::host;
|
|
using namespace std::chrono_literals;
|
|
|
|
namespace {
|
|
|
|
const std::filesystem::path core_exe = SEQUA_TEST_CORE_EXE;
|
|
|
|
struct RunDir {
|
|
RunDir() : path(std::filesystem::temp_directory_path() / ("sequa-host-e2e-" + random_hex(6).value())) {}
|
|
~RunDir()
|
|
{
|
|
std::error_code ignored;
|
|
std::filesystem::remove_all(path, ignored);
|
|
}
|
|
std::filesystem::path path;
|
|
};
|
|
|
|
HostConfig config_for(const RunDir& dir, std::uint16_t port)
|
|
{
|
|
HostConfig config;
|
|
config.core_exe = core_exe;
|
|
config.run_dir = dir.path;
|
|
config.core_port = port;
|
|
config.allowed_origins = {"http://127.0.0.1:9180"};
|
|
return config;
|
|
}
|
|
|
|
// Démarre un host sur le premier port libre d'une plage réservée à ces tests.
|
|
std::unique_ptr<StudioHost> start_host(const RunDir& dir, std::uint16_t& port)
|
|
{
|
|
for (port = 19500; port < 19540; ++port) {
|
|
auto host = std::make_unique<StudioHost>(config_for(dir, port));
|
|
if (host->start().empty()) {
|
|
return host;
|
|
}
|
|
}
|
|
FAIL("aucun port libre entre 19500 et 19539");
|
|
return nullptr;
|
|
}
|
|
|
|
namespace sc = ::sequa::studio_core;
|
|
|
|
std::uint64_t tick_of(StudioHost& host)
|
|
{
|
|
const auto status = host.request(sc::contract::message_session_status_request({}), sc::Message::SessionStatus);
|
|
REQUIRE(status.ok());
|
|
return status.value->AsSessionStatus()->tick.value_or(0);
|
|
}
|
|
|
|
// Entrée du joueur (session/input).
|
|
bool send_input(StudioHost& host, std::uint32_t buttons, std::int16_t move_x)
|
|
{
|
|
sc::SessionInputT input;
|
|
input.buttons = buttons;
|
|
input.move_x = move_x;
|
|
input.move_y = 0;
|
|
return host.request(sc::contract::message_session_input(std::move(input)), sc::Message::InputAccepted).ok();
|
|
}
|
|
|
|
bool wait_dead(std::uint32_t pid, std::chrono::milliseconds timeout)
|
|
{
|
|
const auto deadline = std::chrono::steady_clock::now() + timeout;
|
|
while (is_process_alive(pid)) {
|
|
if (std::chrono::steady_clock::now() >= deadline) {
|
|
return false;
|
|
}
|
|
std::this_thread::sleep_for(50ms);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("StudioHost : lancement puis rechargement par rejeu, session conservee")
|
|
{
|
|
RunDir dir;
|
|
std::uint16_t port = 0;
|
|
auto host = start_host(dir, port);
|
|
REQUIRE(host);
|
|
CHECK_FALSE(host->attached_existing());
|
|
CHECK(host->linked());
|
|
CHECK(host->core_url().find("?token=") != std::string::npos);
|
|
const auto first = host->run();
|
|
CHECK(read_run_file(run_file_path(dir.path, port)) == first);
|
|
CHECK(first.exePath.parent_path() == bin_dir(dir.path)); // lancé depuis une copie fantôme
|
|
|
|
// Un peu de jeu : courir et sauter pendant que la session tourne à 60 Hz.
|
|
REQUIRE(send_input(*host, 0, 32767));
|
|
std::this_thread::sleep_for(300ms);
|
|
REQUIRE(send_input(*host, 1, 32767));
|
|
std::this_thread::sleep_for(200ms);
|
|
const std::uint64_t before = tick_of(*host);
|
|
CHECK(before > 10);
|
|
|
|
const ReloadReport report = host->reload();
|
|
INFO("erreur : " << report.error);
|
|
REQUIRE(report.ok);
|
|
CHECK(report.compared);
|
|
CHECK_FALSE(report.divergence.has_value()); // même code : comportement identique
|
|
CHECK(report.tick >= before);
|
|
|
|
const auto second = host->run();
|
|
CHECK(second.pid != first.pid);
|
|
CHECK(second.instanceId != first.instanceId);
|
|
CHECK(second.token == first.token);
|
|
CHECK(wait_dead(first.pid, 3s)); // l'ancien Core est bien arrêté
|
|
CHECK(read_run_file(run_file_path(dir.path, port)) == second);
|
|
|
|
// La session continue dans le nouveau Core.
|
|
std::this_thread::sleep_for(200ms);
|
|
CHECK(tick_of(*host) > report.tick);
|
|
|
|
host->shutdown();
|
|
CHECK(wait_dead(second.pid, 3s));
|
|
CHECK_FALSE(std::filesystem::exists(run_file_path(dir.path, port)));
|
|
}
|
|
|
|
TEST_CASE("StudioHost : detachement reversible, rattachement par un nouveau Studio")
|
|
{
|
|
RunDir dir;
|
|
std::uint16_t port = 0;
|
|
auto first = start_host(dir, port);
|
|
REQUIRE(first);
|
|
const auto run = first->run();
|
|
REQUIRE(first->detach().empty());
|
|
CHECK_FALSE(first->linked());
|
|
first->shutdown(); // détaché : seule la connexion est fermée
|
|
first.reset();
|
|
|
|
std::this_thread::sleep_for(3500ms); // au-delà du délai de grâce du bail (3 s)
|
|
CHECK(is_process_alive(run.pid));
|
|
|
|
StudioHost second(config_for(dir, port));
|
|
REQUIRE(second.start().empty());
|
|
CHECK(second.attached_existing());
|
|
CHECK(second.run().instanceId == run.instanceId);
|
|
CHECK(second.linked()); // le rattachement rétablit le bail
|
|
|
|
second.shutdown();
|
|
CHECK(wait_dead(run.pid, 3s));
|
|
CHECK_FALSE(std::filesystem::exists(run_file_path(dir.path, port)));
|
|
}
|
|
|
|
TEST_CASE("StudioHost : Studio disparu sans arret propre -> le Core lie s'arrete seul")
|
|
{
|
|
RunDir dir;
|
|
std::uint16_t port = 0;
|
|
auto host = start_host(dir, port);
|
|
REQUIRE(host);
|
|
const auto pid = host->run().pid;
|
|
host.reset(); // comme un plantage : aucune demande d'arrêt
|
|
CHECK(wait_dead(pid, 6s)); // délai de grâce 3 s, puis arrêt (code 3)
|
|
|
|
// Le fichier d'exécution est resté : le Studio suivant le détecte périmé.
|
|
REQUIRE(std::filesystem::exists(run_file_path(dir.path, port)));
|
|
StudioHost next(config_for(dir, port));
|
|
REQUIRE(next.start().empty());
|
|
CHECK_FALSE(next.attached_existing());
|
|
next.shutdown();
|
|
}
|