#include "settlement/core/voxel_navigation.hpp" #include "settlement/core/worker_pool.hpp" #include #include #include #include #include #include #include #include #include #include namespace { using namespace settlement; using Status = VoxelNavigationStatus; using Void::Testing::Test; void require(bool condition, const char* message) { if (!condition) throw std::runtime_error(message); } Void::Uuid::Uuid epoch(std::uint8_t value = 1) { Void::Uuid::Uuid result; result.bytes.back() = value; return result; } template void install(VoxelRegion& world, ChunkCoordinate chunk, Material material) { std::array cells{}; for (std::uint8_t z = 0; z < 32; ++z) for (std::uint8_t y = 0; y < 32; ++y) for (std::uint8_t x = 0; x < 32; ++x) { const auto p = join_voxel(chunk, {x, y, z}); if (p && world.descriptor().bounds.contains(*p)) cells[*voxel_index({x, y, z})] = material(*p); } auto candidate = world.prepare_restored(chunk, 1, cells); require(candidate.has_value(), "valid fixture page refused"); require(world.activate(std::span(&*candidate, 1)).has_value(), "valid fixture activation refused"); } void edit(VoxelRegion& world, VoxelPosition p, MaterialId material) { const VoxelEdit change{p, material, world.sample(p).stamp.revision}; require(world.edit(std::span(&change, 1)).has_value(), "fixture edit refused"); } VoxelRegion flat(VoxelBounds bounds = {{0, 0, 0}, {8, 8, 4}}) { VoxelRegion world({1, 42, 1, bounds}); const auto low = split_voxel({static_cast(bounds.lower.x), static_cast(bounds.lower.y), static_cast(bounds.lower.z)}).chunk; const auto high = split_voxel({static_cast(bounds.upper.x - 1), static_cast(bounds.upper.y - 1), static_cast(bounds.upper.z - 1)}).chunk; for (auto z = low.z; z <= high.z; ++z) for (auto y = low.y; y <= high.y; ++y) for (auto x = low.x; x <= high.x; ++x) { install(world, {x, y, z}, [](auto p) { return p.z == 0 ? MaterialId::Stone : MaterialId::Air; }); } return world; } VoxelPathRequest request(const VoxelRegion& world, VoxelPosition start, std::vector goals) { return {world.descriptor().id, epoch(), start, world.descriptor().bounds, std::move(goals), {}}; } VoxelRegion stairs(bool closed) { VoxelRegion world({7, 42, 1, {{28, 0, 0}, {38, 3, 8}}}); for (int x : {0, 1}) install(world, {x, 0, 0}, [closed](auto p) { const auto height = std::clamp(p.x - 31, 0, 3); if (p.z <= height) return MaterialId::Stone; if ((p.y == 0 || p.y == 2) && p.z <= height + 2) return MaterialId::Stone; if (p.x == 30 && p.y == 1 && (p.z == 1 || p.z == 2)) return closed ? MaterialId::DoorClosed : MaterialId::DoorOpen; return MaterialId::Air; }); return world; } void require_route(const VoxelRegion& world, const VoxelPathRequest& query, const VoxelPathResult& result) { require(result.status == Status::Reachable && result.goal.has_value(), "expected a reachable goal"); require(result.cost == result.route.size(), "cost differs from actual route"); auto from = query.start; for (auto to : result.route) { require(voxel_transition(world, query.session, from, to).status == Status::Reachable, "returned route crosses invalid transition"); from = to; } require(from == *result.goal && result.consulted.current(world, query.session), "route target or read stamps invalid"); require(std::is_sorted(result.consulted.chunks.begin(), result.consulted.chunks.end(), [](const auto& a, const auto& b) { return a.coordinate < b.coordinate; }), "read stamps not canonical"); } struct [[= Void::Testing::name("VoxelNavigationTests")]] VoxelNavigationTests : Void::Testing::Suite { [[= Void::Testing::tag("voxel-navigation")]] Test doorway_stairs_and_upper_work_contact(); [[= Void::Testing::tag("voxel-navigation")]] Test support_edit_invalidates_route_before_next_step(); [[= Void::Testing::tag("voxel-navigation")]] Test body_headroom_and_step_sweep_are_required(); [[= Void::Testing::tag("voxel-navigation")]] Test missing_support_page_and_negative_answer_are_dependencies(); [[= Void::Testing::tag("voxel-navigation")]] Test known_blockers_do_not_require_unrelated_missing_data(); [[= Void::Testing::tag("voxel-navigation")]] Test reachable_missing_frontier_waits_for_actual_gateway(); [[= Void::Testing::tag("voxel-navigation")]] Test unrepresentable_vector_capacity_is_a_bounded_refusal(); [[= Void::Testing::tag("voxel-navigation")]] Test interaction_contacts_outside_window_need_frontier_evidence(); [[= Void::Testing::tag("voxel-navigation")]] Test unchanged_chunks_and_old_snapshots_remain_usable(); [[= Void::Testing::tag("voxel-navigation")]] Test bounded_search_has_distinct_incomplete_and_error_results(); [[= Void::Testing::tag("voxel-navigation")]] Test invalid_same_goal_and_extreme_coordinate_windows(); [[= Void::Testing::tag("voxel-navigation")]] Test goals_and_neighbors_have_stable_shortest_ties(); [[= Void::Testing::tag("voxel-navigation")]] Test shortest_costs_match_exhaustive_small_grid(); [[= Void::Testing::tag("voxel-navigation")]] Test real_parallel_queries_preserve_route_and_readsets(); }; Test VoxelNavigationTests::doorway_stairs_and_upper_work_contact() { auto world = stairs(true); auto query = request(world, {29, 1, 1}, {{36, 1, 4}}); auto blocked = find_voxel_path(world, query); require(blocked.status == Status::Unreachable, "closed two-cell door was passable"); const std::array changes{VoxelEdit{{30, 1, 1}, MaterialId::DoorOpen, 1}, VoxelEdit{{30, 1, 2}, MaterialId::DoorOpen, 1}}; require(world.edit(changes).has_value(), "door opening failed"); require(!blocked.consulted.current(world, epoch()), "door change retained negative result"); auto found = find_voxel_path(world, query); require_route(world, query, found); require(found.cost == 7 && std::any_of(found.route.begin(), found.route.end(), [](auto p) { return p.x == 32 && p.z == 2; }), "path failed actual border stair"); edit(world, {37, 1, 4}, MaterialId::Stone); auto work = find_voxel_interaction_path(world, query, {37, 1, 4}); require_route(world, query, work); require(work.goal == VoxelPosition{36, 1, 4}, "upper work position lacks direct face contact"); } Test VoxelNavigationTests::support_edit_invalidates_route_before_next_step() { auto world = stairs(false); const auto query = request(world, {32, 1, 2}, {{36, 1, 4}}); auto found = find_voxel_path(world, query); require_route(world, query, found); const auto held = world; edit(world, {33, 1, 2}, MaterialId::Air); require(!found.consulted.current(world, epoch()), "support change did not expire route"); require(voxel_standing(world, epoch(), query.start).status == Status::Reachable, "future support edit invalidated current foot"); require(voxel_transition(world, epoch(), query.start, found.route.front()).status == Status::Unreachable, "unsafe stored next step still allowed"); require(find_voxel_path(world, query).status == Status::Unreachable, "missing stair allowed a two-cell rise"); require(found.consulted.current(held, epoch()), "retained immutable geometry changed"); edit(world, {33, 1, 2}, MaterialId::Stone); require_route(world, query, find_voxel_path(world, query)); } Test VoxelNavigationTests::body_headroom_and_step_sweep_are_required() { auto world = flat(); require(voxel_standing(world, epoch(), {2, 2, 1}).status == Status::Reachable, "ordinary stance refused"); edit(world, {3, 2, 1}, MaterialId::Stone); require(voxel_transition(world, epoch(), {2, 2, 1}, {3, 2, 2}).status == Status::Reachable, "single stair refused"); edit(world, {2, 2, 3}, MaterialId::Stone); require(voxel_transition(world, epoch(), {2, 2, 1}, {3, 2, 2}).status == Status::Unreachable, "step rose through source ceiling"); require(voxel_transition(world, epoch(), {3, 2, 2}, {2, 2, 1}).status == Status::Unreachable, "step descended through swept ceiling"); edit(world, {2, 2, 3}, MaterialId::Air); edit(world, {3, 2, 3}, MaterialId::DoorClosed); require(voxel_standing(world, epoch(), {3, 2, 2}).status == Status::Unreachable, "head entered closed upper door"); edit(world, {3, 2, 3}, MaterialId::DoorOpen); require(voxel_transition(world, epoch(), {2, 2, 1}, {3, 2, 2}).status == Status::Reachable, "open door blocked head"); require(voxel_transition(world, epoch(), {2, 2, 1}, {3, 3, 1}).status == Status::Unreachable, "diagonal corner cutting accepted"); require(voxel_transition(world, epoch(), {2, 2, 1}, {2, 2, 2}).status == Status::Unreachable, "unsupported vertical movement accepted"); } Test VoxelNavigationTests::missing_support_page_and_negative_answer_are_dependencies() { VoxelRegion world({1, 42, 1, {{-3, 0, -1}, {3, 3, 4}}}); install(world, {-1, 0, 0}, [](auto) { return MaterialId::Air; }); install(world, {0, 0, 0}, [](auto) { return MaterialId::Air; }); auto check = voxel_standing(world, epoch(), {-1, 1, 0}); require(check.status == Status::Unavailable, "unloaded support treated as empty or solid"); require(std::any_of(check.consulted.chunks.begin(), check.consulted.chunks.end(), [](const auto& s) { return s.coordinate == ChunkCoordinate{-1, 0, -1} && s.availability == VoxelAvailability::Unavailable; }), "missing support page omitted from reads"); install(world, {-1, 0, -1}, [](auto) { return MaterialId::Stone; }); require(!check.consulted.current(world, epoch()), "missing-to-loaded support kept stale result"); require(voxel_standing(world, epoch(), {-1, 1, 0}).status == Status::Reachable, "new support still unavailable"); auto query = request(world, {-1, 1, 0}, {{1, 1, 0}}); auto negative = find_voxel_path(world, query); require(negative.status == Status::Unavailable, "missing route support was proven unreachable"); install(world, {0, 0, -1}, [](auto) { return MaterialId::Stone; }); require(!negative.consulted.current(world, epoch()), "negative path omitted queried missing chunk"); require_route(world, query, find_voxel_path(world, query)); } Test VoxelNavigationTests::known_blockers_do_not_require_unrelated_missing_data() { VoxelRegion world({1, 42, 1, {{0, 0, -1}, {64, 8, 4}}}); install(world, {0, 0, 0}, [](auto p) { return p.z == 0 || (p.x == 2 && p.z <= 2) ? MaterialId::Stone : MaterialId::Air; }); auto query = request(world, {1, 1, 1}, {{5, 5, 1}}); auto result = find_voxel_path(world, query); require(result.status == Status::Unreachable, "solid floor and closed wall unnecessarily wait for missing lower page"); require(find_voxel_interaction_path(world, query, {5, 5, 1}).status == Status::Unreachable, "loaded interaction behind known wall unnecessarily waits for missing lower page"); require(std::any_of(result.consulted.chunks.begin(), result.consulted.chunks.end(), [](const auto& s) { return s.coordinate == ChunkCoordinate{0, 0, -1} && s.availability == VoxelAvailability::Unavailable; }), "conclusively blocked reads lost their actual dependency stamps"); query.goals = {{35, 5, 1}}; result = find_voxel_path(world, query); require(result.status == Status::Unreachable, "disconnected unknown goal manufactured reachable frontier"); require(std::any_of(result.consulted.chunks.begin(), result.consulted.chunks.end(), [](const auto& s) { return s.coordinate == ChunkCoordinate{1, 0, 0} && s.availability == VoxelAvailability::Unavailable; }), "unknown goal reads were not retained"); query.goals = {{5, 5, 1}}; query.search_bounds.upper = {8, 6, 4}; require(find_voxel_path(world, query).status == Status::SearchBoundary, "actual reachable window exit was replaced by irrelevant missing data"); install(world, {0, 0, -1}, [](auto) { return MaterialId::Stone; }); query.search_bounds = world.descriptor().bounds; require(find_voxel_path(world, query).status == Status::Unreachable, "lower-page activation changed proven blocked reachability"); for (int x : {0, 1}) for (int z : {1, 2}) edit(world, {x, 3, z}, MaterialId::Stone); query.goals = {{1, 5, 1}}; query.search_bounds.upper.x = 2; require(find_voxel_path(world, query).status == Status::Unreachable, "known blocked transitions beyond search window manufactured an exit"); } Test VoxelNavigationTests::reachable_missing_frontier_waits_for_actual_gateway() { VoxelRegion world({1, 42, 1, {{0, 0, -1}, {64, 8, 4}}}); const auto floor = [](auto p) { return p.z == 0 ? MaterialId::Stone : MaterialId::Air; }; install(world, {0, 0, 0}, floor); auto blocked_goal = request(world, {1, 1, 1}, {{5, 5, 1}}); edit(world, {5, 5, 2}, MaterialId::Stone); require(find_voxel_path(world, blocked_goal).status == Status::Unreachable, "missing branch elsewhere manufactured access to conclusively blocked goal"); edit(world, {5, 5, 2}, MaterialId::Air); auto query = request(world, {1, 1, 1}, {{35, 5, 1}}); auto pending = find_voxel_path(world, query); require(pending.status == Status::Unavailable, "reachable missing gateway was proven unreachable"); install(world, {1, 0, 0}, floor); require(!pending.consulted.current(world, epoch()), "gateway activation did not invalidate pending answer"); auto result = find_voxel_path(world, query); require_route(world, query, result); require(result.cost == 38, "loaded gateway changed shortest path cost"); } Test VoxelNavigationTests::unrepresentable_vector_capacity_is_a_bounded_refusal() { auto world = flat(); auto query = request(world, {1, 1, 1}, {{5, 5, 1}}); query.limits.max_dependencies = std::vector{}.max_size() + 1; query.limits.max_scratch_bytes = std::numeric_limits::max(); require(find_voxel_path(world, query).status == Status::LimitExceeded, "unrepresentable read reserve escaped path API"); require(find_voxel_interaction_path(world, query, {5, 5, 1}).status == Status::LimitExceeded, "unrepresentable read reserve escaped interaction API"); require(voxel_standing(world, epoch(), query.start, query.limits).status == Status::LimitExceeded, "unrepresentable read reserve escaped stance API"); require(voxel_transition(world, epoch(), query.start, {2, 1, 1}, query.limits).status == Status::LimitExceeded, "unrepresentable read reserve escaped transition API"); } Test VoxelNavigationTests::interaction_contacts_outside_window_need_frontier_evidence() { auto world = flat(); edit(world, {3, 3, 1}, MaterialId::Stone); for (auto blocker : {VoxelPosition{2, 3, 2}, VoxelPosition{3, 2, 2}, VoxelPosition{3, 4, 2}, VoxelPosition{3, 3, 3}}) { edit(world, blocker, MaterialId::Stone); } auto query = request(world, {1, 1, 1}, {}); query.search_bounds.upper.x = 6; auto complete = find_voxel_interaction_path(world, query, {3, 3, 1}); require_route(world, query, complete); require(complete.goal == VoxelPosition{4, 3, 1}, "expanded window did not reach sole valid contact"); query.search_bounds.upper.x = 4; require(find_voxel_interaction_path(world, query, {3, 3, 1}).status == Status::SearchBoundary, "clipped valid interaction contact was classified globally unreachable"); // A valid omitted goal cannot manufacture an exit from an enclosed start. for (auto p : {VoxelPosition{0, 1, 1}, VoxelPosition{1, 0, 1}, VoxelPosition{1, 2, 1}, VoxelPosition{2, 1, 1}}) { edit(world, p, MaterialId::Stone); edit(world, {p.x, p.y, 2}, MaterialId::Stone); } require(find_voxel_interaction_path(world, query, {3, 3, 1}).status == Status::Unreachable, "omitted contact manufactured an escape from a known enclosed start"); // The only useful contact is beyond x=31, in an unavailable page. VoxelRegion pending({1, 42, 1, {{0, 0, 0}, {40, 8, 4}}}); install(pending, {0, 0, 0}, [](auto p) { if (p.z == 0 || p == VoxelPosition{31, 3, 1}) return MaterialId::Stone; if (p == VoxelPosition{30, 3, 2} || p == VoxelPosition{31, 2, 2} || p == VoxelPosition{31, 4, 2} || p == VoxelPosition{31, 3, 3}) return MaterialId::Stone; return MaterialId::Air; }); auto partial = request(pending, {29, 1, 1}, {}); partial.search_bounds.upper.x = 32; const auto waiting = find_voxel_interaction_path(pending, partial, {31, 3, 1}); require(waiting.status == Status::Unavailable, "unresolved interaction exit was proven unreachable"); install(pending, {1, 0, 0}, [](auto p) { return p.z == 0 ? MaterialId::Stone : MaterialId::Air; }); require(!waiting.consulted.current(pending, epoch()), "missing omitted contact was absent from dependencies"); require(find_voxel_interaction_path(pending, partial, {31, 3, 1}).status == Status::SearchBoundary, "loaded omitted contact failed to expose reachable window exit"); partial.search_bounds.upper.x = 36; auto reached = find_voxel_interaction_path(pending, partial, {31, 3, 1}); require_route(pending, partial, reached); require(reached.goal == VoxelPosition{32, 3, 1}, "expanded gateway did not expose actual contact"); } Test VoxelNavigationTests::unchanged_chunks_and_old_snapshots_remain_usable() { auto world = flat({{0, 0, 0}, {100, 8, 4}}); auto query = request(world, {1, 1, 1}, {{5, 5, 1}}); query.search_bounds.upper.x = 8; auto route = find_voxel_path(world, query); require_route(world, query, route); edit(world, {97, 2, 1}, MaterialId::Stone); require(route.consulted.current(world, epoch()), "unconsulted page invalidated a path"); require(!route.consulted.current(world, epoch(2)), "old session accepted same numeric revisions"); VoxelRegion other({2, 42, 1, world.descriptor().bounds}); require(!route.consulted.current(other, epoch()), "another region accepted old route"); } Test VoxelNavigationTests::bounded_search_has_distinct_incomplete_and_error_results() { auto world = flat(); auto query = request(world, {1, 1, 1}, {{5, 5, 1}}); query.limits.max_vertices = 1; require(find_voxel_path(world, query).status == Status::LimitExceeded, "vertex budget fabricated unreachable"); query.limits = {}; query.limits.max_scratch_bytes = 8; require(find_voxel_path(world, query).status == Status::LimitExceeded, "scratch budget ignored"); query.limits = {}; query.limits.max_steps = 1; require(find_voxel_path(world, query).status == Status::LimitExceeded, "traversal budget fabricated unreachable"); query.limits = {}; query.limits.max_samples = 2; require(find_voxel_path(world, query).status == Status::LimitExceeded, "material query budget ignored"); query.limits = {}; query.limits.max_dependencies = 0; require(find_voxel_path(world, query).status == Status::LimitExceeded, "read-set bound ignored"); query.limits = {}; query.limits.max_goals = 0; require(find_voxel_path(world, query).status == Status::LimitExceeded, "goal bound ignored"); query.limits = {}; std::stop_source cancelled; cancelled.request_stop(); require(find_voxel_path(world, query, cancelled.get_token()).status == Status::Cancelled, "cancellation fabricated unreachable"); auto clipped_interaction = query; clipped_interaction.search_bounds.upper = {2, 2, 4}; require(find_voxel_interaction_path(world, clipped_interaction, {6, 6, 1}).status == Status::SearchBoundary, "interaction with all feet clipped fabricated invalid input"); clipped_interaction.region = 2; require(find_voxel_interaction_path(world, clipped_interaction, {6, 6, 1}).status == Status::InvalidInput, "clipped interaction bypassed region identity"); query.search_bounds.upper.x = 4; query.goals = {{3, 5, 1}}; for (int y = 0; y < 8; ++y) { edit(world, {2, y, 1}, MaterialId::Stone); edit(world, {2, y, 2}, MaterialId::Stone); } // The accessible edge at x=0 has no escape (region bound), but y edges do // not either. Make a window smaller than the region along y as well. query.search_bounds.upper.y = 6; require(find_voxel_path(world, query).status == Status::SearchBoundary, "clipped search claimed global unreachability"); VoxelRegion sparse({1, 42, 1, {{std::numeric_limits::min(), 0, 0}, {std::int64_t{std::numeric_limits::max()} + 1, 8, 4}}}); auto huge = request(sparse, {0, 1, 1}, {{1, 1, 1}}); require(find_voxel_path(sparse, huge).status == Status::LimitExceeded, "sparse huge extent allocated densely"); } Test VoxelNavigationTests::invalid_same_goal_and_extreme_coordinate_windows() { auto world = flat(); auto query = request(world, {1, 1, 1}, {{1, 1, 1}}); auto same = find_voxel_path(world, query); require_route(world, query, same); require(same.route.empty() && same.cost == 0, "already-at-goal result wrong"); edit(world, {1, 1, 2}, MaterialId::Stone); require(find_voxel_path(world, query).status == Status::Unreachable, "blocked start==goal bypassed geometry"); query.session = {}; require(find_voxel_path(world, query).status == Status::InvalidInput, "nil session accepted"); for (auto x : {std::numeric_limits::min(), std::numeric_limits::max() - 3}) { const VoxelBounds b{{x, 0, 0}, {std::int64_t{x} + 4, 3, 4}}; auto edge = flat(b); auto edge_query = request(edge, {x, 1, 1}, {{static_cast(std::int64_t{x} + 3), 1, 1}}); const auto route = find_voxel_path(edge, edge_query); require_route(edge, edge_query, route); require(route.cost == 3, "extreme coordinate route wrapped"); } } Test VoxelNavigationTests::goals_and_neighbors_have_stable_shortest_ties() { auto world = flat(); auto query = request(world, {3, 3, 1}, {{4, 3, 1}, {2, 3, 1}, {3, 4, 1}}); auto result = find_voxel_path(world, query); require(result.goal == VoxelPosition{2, 3, 1} && result.cost == 1, "equal goals depend on request order"); std::reverse(query.goals.begin(), query.goals.end()); require(find_voxel_path(world, query).route == result.route, "goal order changed route"); query.goals = {{4, 4, 1}}; result = find_voxel_path(world, query); require(result.route == std::vector{{3, 4, 1}, {4, 4, 1}}, "equal paths violated coordinate neighbor order"); } Test VoxelNavigationTests::shortest_costs_match_exhaustive_small_grid() { for (std::uint32_t pattern = 0; pattern < 16; ++pattern) { std::array walls{}; for (std::size_t i = 0; i < walls.size(); ++i) walls[i] = ((i * 13 + pattern * 7) % 17) < 4; walls[8] = false; walls[40] = false; auto world = flat({{0, 0, 0}, {7, 7, 4}}); std::vector changes; for (int y = 0; y < 7; ++y) for (int x = 0; x < 7; ++x) if (walls[static_cast(x + 7 * y)]) { changes.push_back({{x, y, 1}, MaterialId::Stone, 1}); changes.push_back({{x, y, 2}, MaterialId::Stone, 1}); } require(world.edit(changes).has_value(), "small oracle fixture failed"); std::array distance; distance.fill(-1); distance[8] = 0; std::queue queue; queue.push(8); while (!queue.empty()) { const auto v = queue.front(); queue.pop(); for (auto d : std::array, 4>{{{-1, 0}, {0, -1}, {0, 1}, {1, 0}}}) { const int x = v % 7 + d[0], y = v / 7 + d[1]; if (x < 0 || x >= 7 || y < 0 || y >= 7) continue; const auto n = static_cast(x + 7 * y); if (walls[n] || distance[n] >= 0) continue; distance[n] = distance[static_cast(v)] + 1; queue.push(static_cast(n)); } } auto query = request(world, {1, 1, 1}, {{5, 5, 1}}); auto result = find_voxel_path(world, query); if (distance[40] < 0) require(result.status == Status::Unreachable, "oracle blocked path found"); else { require_route(world, query, result); require(result.cost == static_cast(distance[40]), "Void graph route differs from exhaustive grid cost"); } } } Test VoxelNavigationTests::real_parallel_queries_preserve_route_and_readsets() { const auto world = stairs(false); const auto query = request(world, {29, 1, 1}, {{36, 1, 4}}); const auto expected = find_voxel_path(world, query); require_route(world, query, expected); for (std::size_t workers : {2U, 6U, 12U}) { WorkerPool pool(workers); std::vector> futures; for (int i = 0; i < 12; ++i) futures.push_back(pool.submit([&] { return find_voxel_path(world, query); })); std::reverse(futures.begin(), futures.end()); for (auto& future : futures) { auto result = future.get(); require(result.status == expected.status && result.route == expected.route && result.goal == expected.goal && result.consulted.chunks == expected.consulted.chunks, "worker completion order altered navigation"); } } } } // namespace