// M10.5c: the engineering client's remote session (apps/evo-client/remote_session.hpp) // against a loopback region (ADR-0093): the launcher's connect flow, snapshot-fed views, // the camera recentre and interest, intention outcomes (simulated, unknown), reconnects // with the next grant of the bundle, and refusals (scope, superseded, exhausted). #include "evo/client/session_host.hpp" #include "evo/network/dev_credentials.hpp" #include "evo/network/session_grant.hpp" #include "evo/server/region_sessions.hpp" #include "remote_session.hpp" #include #include #include #include #include #include #include #include #include #include #include namespace { using namespace evo; using namespace evo::network; using client::LaunchError; using client::LaunchPhase; using client::OutcomeState; using client_app::RemoteSession; using Clock = std::chrono::steady_clock; void check(bool ok, std::source_location at = std::source_location::current()) { if (!ok) throw std::runtime_error("client remote session line " + std::to_string(at.line())); } #define CHECK(...) check(static_cast(__VA_ARGS__)) constexpr std::string_view name = "dev-region.evolution.test"; constexpr world::WorldIdentity world_id{7, 3}; constexpr std::uint64_t region = 12; constexpr std::uint64_t start = 1'800'000'000; struct Files { std::filesystem::path dir, ca, certificate, key; std::string ca_pem; }; Files credentials() { const auto made = generate_dev_credentials(DeploymentEnvironment::dev, name, 1, std::chrono::system_clock::now()); CHECK(made.has_value()); Files f; f.dir = std::filesystem::temp_directory_path() / "evo-client-remote-session"; std::filesystem::remove_all(f.dir); std::filesystem::create_directories(f.dir); f.ca_pem = made->authority_pem; f.ca = f.dir / "ca.pem"; f.certificate = f.dir / "server.pem"; f.key = f.dir / "server.key"; for (const auto& [path, text] : {std::pair{f.ca, made->authority_pem}, std::pair{f.certificate, made->server_certificate_pem}, std::pair{f.key, made->server_key_pem}}) { std::ofstream out{path, std::ios::binary | std::ios::trunc}; out << text; CHECK(out.good()); } return f; } AccountId account(std::uint8_t tag) { AccountId a; a.bytes.fill(tag); return a; } std::array scalar() { std::array s{}; s.back() = 0x2B; return s; } client::EnvironmentCell cell(std::int64_t x, std::int64_t y, std::uint64_t quantity) { client::EnvironmentCell c; c.voxel = {x, y, -7}; c.chunk_revision = 40; c.habitat = {{100}, {200}, {0}, {1000}, {500}, {10}}; c.volume = {1000ULL * 500 * 10}; c.substrate = {1}; c.terrain_material = {2}; c.terrain_fill = 1000; c.presence[0] = client::FieldPresence::present; c.quantity[0] = quantity; c.presence[1] = client::FieldPresence::missing; c.presence[7] = client::FieldPresence::present; c.quantity[7] = 5; c.moisture_status = world::MoistureStatus::available; // Water is requested. c.moisture = {3, 50}; return c; } // A region on loopback with a 21 x 21 slice (x, y in -10..10 at z -7), one snapshot per tick. struct Region { explicit Region(const Files& f) : files{f} { server::RegionSessionsConfig c; c.transport.certificate_pem_file = f.certificate.string(); c.transport.key_pem_file = f.key.string(); c.control.environment = DeploymentEnvironment::dev; c.control.world = world_id; c.control.region = region; c.control.epoch = epoch; c.control.max_account_attempts = 16; c.issuer_keys.push_back({1, *key.public_key()}); auto made = server::RegionSessions::create(std::move(c)); CHECK(made.has_value()); sessions = std::move(*made); sessions->set_default_interest({3, 3, -7, 7}); } WireGrant grant(const AccountId& who, std::uint64_t serial, std::uint64_t in_region = region) const { GrantClaims c; c.grant_id = 900 + serial; c.account = who; c.serial = serial; c.world = world_id; c.region = in_region; c.issued_at = start; c.expires_at = start + 120; const auto g = key.sign(c); CHECK(g.has_value()); return *g; } // The launcher's material: the endpoint by host name, as a tester types it. client::ConnectMaterial material(std::vector grants) const { return {{"localhost", sessions->port()}, std::string{name}, files.ca_pem, files.ca.string(), std::move(grants)}; } void pump() { sessions->poll({true, epoch, tick}, start + 1, Clock::now()); if (tick != replicated) { replicated = tick; sessions->replicate(tick, [&](const SessionInterest&) { ReplicationSource s; s.identity = world_id; s.field_revision = tick; s.mask = (1U << 0) | (1U << 1) | (1U << 7); for (std::int64_t x = -10; x <= 10; ++x) for (std::int64_t y = -10; y <= 10; ++y) s.environment.push_back(cell(x, y, 30)); return s; }); } static_cast(sessions->take_events()); } const Files& files; GrantSigningKey key = *GrantSigningKey::import(DeploymentEnvironment::dev, 1, scalar()); std::unique_ptr sessions; std::uint64_t epoch{3}, tick{100}, replicated{}; }; struct Client { client::SessionHost host; client::UiState ui; client::Intention intention; // Steps the region and the client together (one region tick every `tick_every` // iterations, 0: none) until `done` or the limit. bool until(Region& r, const std::function& done, int tick_every = 0, std::chrono::milliseconds limit = std::chrono::milliseconds{15000}) { const auto end = Clock::now() + limit; for (int i = 1; Clock::now() < end; ++i) { r.pump(); if (tick_every != 0 && i % tick_every == 0) ++r.tick; host.step(Clock::now(), intention, ui); if (done()) return true; std::this_thread::sleep_for(std::chrono::milliseconds{1}); } return done(); } [[nodiscard]] LaunchPhase phase() const { return host.launcher().phase(); } [[nodiscard]] std::size_t cells() const { const auto* s = host.session(); const auto* e = s ? s->environment().current() : nullptr; return e ? e->cells().size() : 0; } [[nodiscard]] bool has(OutcomeState state) const { const auto* s = host.session(); return s && std::ranges::any_of(s->outcomes().entries(), [&](const auto& e) { return e.state == state; }); } }; void views_interest_and_intentions(Region& r) { Client c; CHECK(c.host.connect( RemoteSession::open(r.material({r.grant(account(1), 1), r.grant(account(1), 2)})))); CHECK(c.until(r, [&] { return c.phase() == LaunchPhase::connected; })); // The default interest (3, 3, radius 7) arrives as the copied view. CHECK(c.until(r, [&] { return c.cells() == 225; }, 20)); auto& s = *c.host.session(); const auto* view = s.environment().current(); CHECK(view != nullptr && view->slice_z() == -7 && std::ranges::all_of(view->cells(), [](const auto& e) { return e.voxel.x >= -4 && e.voxel.x <= 10 && e.voxel.y >= -4 && e.voxel.y <= 10; })); CHECK(s.environment().freshness() == client::Freshness::current); // The camera follows the view: the middle of its square, offered once per change. const auto centre = s.take_recentre().value_or(client::WorldPoint{-1, -1}); CHECK(centre.x_mm == 3500 && centre.y_mm == 3500 && !s.take_recentre()); s.focus(centre); // Where the view put it: still following, no interest sent. static_cast(c.until(r, [] { return false; }, 20, std::chrono::milliseconds{300})); CHECK(!s.status().focused && c.cells() == 225); // 300 ms of ticks: the same view. // The user moves the camera: its centre's voxel becomes the interest (clipped). s.focus({-5500, -5200}); CHECK(c.until(r, [&] { return c.cells() == 144; }, 20)); // x, y in -10..1. const auto status = s.status(); CHECK(status.focused && status.focus_x == -6 && status.focus_y == -6 && status.focus_z == -7 && status.phase == "connected" && status.grants_left == 1 && status.snapshot_bytes > 0); // An intention: sent once per change, admitted, then simulated (never durable). c.intention.move_x = 700; CHECK(c.until(r, [&] { return c.has(OutcomeState::admitted); })); CHECK(c.until(r, [&] { return c.has(OutcomeState::simulated); }, 20)); const auto& first = s.outcomes().entries().front(); CHECK(first.sequence == 1 && first.tick != 0 && s.status().simulated_tick >= first.tick); // A new epoch closes the session: the open intention is unknown, the views are stale, // and the session reconnects with the bundle's next grant after its pause. const auto old_session = s.status().session; c.intention.move_x = -700; CHECK(c.until(r, [&] { return c.has(OutcomeState::admitted) || c.has(OutcomeState::sent); })); r.epoch = 4; CHECK(c.until(r, [&] { return c.has(OutcomeState::unknown); })); CHECK(s.environment().freshness() == client::Freshness::stale); CHECK(s.outcomes().entries().size() == 2 && s.outcomes().entries()[1].sequence == 2 && s.outcomes().entries()[1].state == OutcomeState::unknown); CHECK(c.until(r, [&] { return s.status().phase == "connected"; }, 20)); CHECK(s.status().epoch == 4 && s.status().session != old_session && s.status().grants_left == 0 && c.phase() == LaunchPhase::connected); // Views restart for the new epoch, at the camera's interest (sent again). CHECK(c.until(r, [&] { return c.cells() == 144 && s.status().snapshot_tick > 0; }, 20)); // The current intention is sent again in the new session; the record numbers it on // (session sequences restart) and keeps the earlier entries. CHECK(c.until(r, [&] { return s.outcomes().entries().size() == 3; })); CHECK(s.outcomes().entries()[0].state == OutcomeState::simulated && s.outcomes().entries()[2].sequence == 3); // Another client with a newer grant for the account supersedes this one: no fight back. Client other; CHECK(other.host.connect(RemoteSession::open(r.material({r.grant(account(1), 9)})))); CHECK(other.until(r, [&] { c.host.step(Clock::now(), c.intention, c.ui); return c.phase() == LaunchPhase::failed && other.phase() == LaunchPhase::connected; })); CHECK(c.host.launcher().error() == LaunchError::grant_refused && !c.host.session()); } void refusals(Region& r) { // A grant for another region: refused before any Welcome; the launcher stops. Client wrong; CHECK(wrong.host.connect(RemoteSession::open(r.material({r.grant(account(2), 1, 99)})))); CHECK(wrong.until(r, [&] { return wrong.phase() == LaunchPhase::failed; })); CHECK(wrong.host.launcher().error() == LaunchError::grant_refused); // One grant: once its session ends there is nothing to reconnect with. Client one; CHECK(one.host.connect(RemoteSession::open(r.material({r.grant(account(3), 1)})))); CHECK(one.until(r, [&] { return one.phase() == LaunchPhase::connected; })); r.epoch = 5; CHECK(one.until(r, [&] { return one.phase() == LaunchPhase::failed; })); CHECK(one.host.launcher().error() == LaunchError::grants_exhausted); // Nothing to open: an unresolvable endpoint or an empty bundle. auto bad = r.material({r.grant(account(4), 1)}); bad.endpoint.host = "no-such-host.invalid"; CHECK(!RemoteSession::open(bad)); CHECK(!RemoteSession::open(r.material({}))); } } // namespace int main() try { const auto files = credentials(); { Region r{files}; views_interest_and_intentions(r); refusals(r); } std::filesystem::remove_all(files.dir); std::puts("client remote session tests passed"); return 0; } catch (const std::exception& e) { std::fprintf(stderr, "%s\n", e.what()); return 1; }