// City Wire native history: canonical reconstruction, bounded corruption handling, and retention. #include "sc2k/ui/notification_persistence.hpp" #include "sc2k/ui/notification_projector.hpp" #include #include #include #include #include #include "test_harness.hpp" using namespace sc2k; static const ui::NotificationParameter* parameter(const ui::Notification& notification, std::string_view name) { for (const ui::NotificationParameter& candidate : notification.parameters) if (candidate.name == name) return &candidate; return nullptr; } template static sim::DomainEvent domain_event(Payload payload, std::uint32_t ordinal = 0) { return {{900, sim::EventPhase::Progression, ordinal}, sim::DomainEventPayload{payload}}; } static std::vector representative_history() { ui::NotificationStore store; CHECK(ui::project_notification_event(store, domain_event(sim::InventionDiscovered{sim::InventionId::NaturalGas}), {100, "Archive City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(ui::project_notification_event(store, domain_event(sim::FirstBuildingOpened{ sim::CityOpeningKind::Airport, 10, 20, xbld::kAirportBldg1}, 1), {101, "Archive City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(ui::project_notification_event(store, domain_event(sim::CityStatusAdvanced{0, 1, 2500}, 2), {102, "Archive City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.size() == 3); std::vector records(store.records().begin(), store.records().end()); CHECK(records[0].lifecycle == ui::NotificationLifecycle::Historical); CHECK(records[1].lifecycle == ui::NotificationLifecycle::Historical); CHECK(records[2].lifecycle == ui::NotificationLifecycle::RewardBound); records[0].unread = false; records[0].autoOpened = true; // Persistence must ignore this forged descriptor and rebuild the catalogue action. records[1].actions[0].command = ui::NotificationActionCommand::AcceptDecision; records[1].actions[0].argument = 999; return ui::encode_notification_history(records); } static void history_round_trips_without_protected_state_or_action_authority() { City city(MapSize::Small); const std::vector bytes = representative_history(); ui::NotificationStore restored; const ui::NotificationHistoryRestoreResult result = ui::restore_notification_history(restored, bytes, 110, city); CHECK(result.recognized && result.restored == 2 && result.discarded == 0); CHECK(restored.size() == 2); const ui::Notification* invention = restored.find( ui::NotificationKey{ui::NotificationKeyDomain::Invention, 0}); CHECK(invention && !invention->unread && invention->autoOpened); CHECK(invention && invention->category == ui::NotificationCategory::Info); CHECK(invention && invention->sourceEvent == std::nullopt); const ui::Notification* opening = restored.find( ui::NotificationKey{ui::NotificationKeyDomain::Opening, static_cast(sim::CityOpeningKind::Airport)}); CHECK(opening && opening->actionCount == 1); CHECK(opening && opening->actions[0].command == ui::NotificationActionCommand::GoTo); CHECK(opening && opening->actions[0].argument == 0); CHECK(opening && opening->actions[0].row == 10 && opening->actions[0].column == 20); CHECK(!restored.find(ui::NotificationKey{ui::NotificationKeyDomain::Reward, xbld::kMayorsHouse})); } static void unknown_and_expired_records_are_discarded_independently() { City city(MapSize::Small); std::vector bytes = representative_history(); constexpr std::string_view known = "citywire.invention.gas.title"; const auto found = std::search(bytes.begin(), bytes.end(), known.begin(), known.end()); CHECK(found != bytes.end()); if (found != bytes.end()) *found = static_cast('x'); ui::NotificationStore partial; const auto partialResult = ui::restore_notification_history(partial, bytes, 110, city); CHECK(partialResult.recognized && partialResult.restored == 1 && partialResult.discarded == 1); CHECK(partial.size() == 1); CHECK(partial.records()[0].titleKey == "desk.open.airport.head"); ui::NotificationStore expired; const auto expiredResult = ui::restore_notification_history( expired, representative_history(), 200, city); CHECK(expiredResult.recognized && expiredResult.restored == 0 && expiredResult.discarded == 2); CHECK(expired.empty()); } static void missing_and_structurally_corrupt_sections_fail_closed() { City city(MapSize::Small); ui::NotificationStore store; const auto missing = ui::restore_notification_history(store, {}, 100, city); CHECK(!missing.recognized && missing.restored == 0 && missing.discarded == 0); std::vector bytes = representative_history(); CHECK(bytes.size() > 12); bytes[8] = 0xff; // first record size exceeds the remaining bounded section bytes[9] = bytes[10] = bytes[11] = 0xff; const auto corrupt = ui::restore_notification_history(store, bytes, 110, city); CHECK(corrupt.recognized && corrupt.restored == 0 && corrupt.discarded == 2); CHECK(store.empty()); } static void encoded_history_never_exceeds_the_native_section_budget() { ui::NotificationStore seed; CHECK(ui::project_notification_event(seed, domain_event(sim::BondIssued{1, 10000, 600, 0, 0, 1, 20000}), {100, "Large Archive"}) == ui::NotificationProjectionOutcome::Upserted); std::vector records; for (std::uint64_t index = 0; index < ui::NotificationStore::kHistoricalSoftCap; ++index) { ui::Notification record = seed.records()[0]; record.parameters.push_back({"sequence", static_cast(index)}); record.parameters.push_back( {"large_a", ui::NotificationLiteral{std::string(4000, 'a')}}); record.parameters.push_back( {"large_b", ui::NotificationLiteral{std::string(4000, 'b')}}); record.parameters.push_back( {"large_c", ui::NotificationLiteral{std::string(4000, 'c')}}); records.push_back(std::move(record)); } const std::vector bytes = ui::encode_notification_history(records); CHECK(bytes.size() <= ui::kMaximumNotificationHistoryBytes); City city(MapSize::Small); ui::NotificationStore restored; const auto result = ui::restore_notification_history(restored, bytes, 110, city); CHECK(result.recognized && result.restored > 0); CHECK(result.restored < ui::NotificationStore::kHistoricalSoftCap); } static void mismatched_stable_identity_cannot_impersonate_protected_state() { ui::NotificationStore seed; CHECK(ui::project_notification_event(seed, domain_event(sim::InventionDiscovered{sim::InventionId::NaturalGas}), {100, "Identity"}) == ui::NotificationProjectionOutcome::Upserted); std::vector records(seed.records().begin(), seed.records().end()); records[0].stableKey = ui::NotificationKey{ui::NotificationKeyDomain::Reward, xbld::kMayorsHouse}; const std::vector bytes = ui::encode_notification_history(records); City city(MapSize::Small); ui::NotificationStore restored; const auto result = ui::restore_notification_history(restored, bytes, 110, city); CHECK(result.recognized && result.restored == 0 && result.discarded == 1); CHECK(restored.empty()); } static void native_debt_history_round_trips_exact_rate_and_maturity_facts() { ui::NotificationStore store; CHECK(ui::project_notification_event(store, domain_event(sim::BondIssued{7, 30000, 575, 120, 329, 2, 50000}), {100, "Archive City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(ui::project_notification_event(store, domain_event(sim::BondMatured{3, 240}, 1), {101, "Archive City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.size() == 2); const std::vector bytes = ui::encode_notification_history(store.records()); City city(MapSize::Small); ui::NotificationStore restored; const auto result = ui::restore_notification_history(restored, bytes, 110, city); CHECK(result.recognized && result.restored == 2 && result.discarded == 0); CHECK(restored.size() == 2); const ui::Notification& issued = restored.records()[0]; const ui::Notification& matured = restored.records()[1]; const ui::NotificationParameter* rate = parameter(issued, "rate"); CHECK(issued.titleKey == "citywire.finance.bond_issued.title"); CHECK(rate && rate->value == ui::NotificationParameterValue{std::int64_t{575}}); CHECK(rate && rate->format == ui::NotificationParameterFormat::PercentBasisPoints); CHECK(matured.titleKey == "citywire.finance.bond_matured.title"); const ui::NotificationParameter* debtId = parameter(matured, "debt_id"); CHECK(debtId && debtId->value == ui::NotificationParameterValue{std::int64_t{3}}); CHECK(matured.actionCount == 1 && matured.actions[0].command == ui::NotificationActionCommand::OpenBonds); } int main() { history_round_trips_without_protected_state_or_action_authority(); unknown_and_expired_records_are_discarded_independently(); missing_and_structurally_corrupt_sections_fail_closed(); encoded_history_never_exceeds_the_native_section_budget(); mismatched_stable_identity_cannot_impersonate_protected_state(); native_debt_history_round_trips_exact_rate_and_maturity_facts(); return failures ? 1 : 0; }