// City Wire projection for persisted local civic-unrest episodes. #include "sc2k/ui/notification_projector.hpp" #include #include #include "test_harness.hpp" namespace { using namespace sc2k; sim::DomainEvent unrest_event(sim::CivicUnrestChanged change, std::int32_t tick = 23) { return {{tick, sim::EventPhase::Civic, 0}, sim::DomainEventPayload{change}}; } std::int64_t integer_parameter(const ui::Notification& notification, std::string_view name) { for (const ui::NotificationParameter& parameter : notification.parameters) if (parameter.name == name) { const auto* value = std::get_if(¶meter.value); CHECK(value); return value ? *value : 0; } CHECK(false); return 0; } ui::NotificationKey unrest_key(std::uint64_t candidateId) { return {ui::NotificationKeyDomain::Civic, candidateId}; } sim::CivicSatisfactionState state_with_episode() { const auto made = sim::make_civic_satisfaction_state( 64, 64, 1234, sim::CivicSatisfactionPolicy{}, 0); CHECK(made.has_value()); sim::CivicSatisfactionState state = made.value_or(sim::CivicSatisfactionState{}); state.nextUnrestEpisodeId = 8; state.unrestEpisodes.push_back({7, 3, 99, 0, 2, 2, 2}); CHECK(sim::valid_civic_satisfaction_state(state)); return state; } void start_and_end_share_exact_episode_identity() { City city{MapSize::Tiny}; ui::NotificationStore store; const sim::DomainEvent started = unrest_event( {sim::CivicUnrestTransitionKind::Started, sim::CivicUnrestResolution::None, 7, 3, 99, 0, 2, 2}); CHECK(ui::project_notification_event( store, started, {0, "Civic City", &city}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* notice = store.find(unrest_key(3)); CHECK(notice); if (!notice) return; CHECK(notice->category == ui::NotificationCategory::Danger); CHECK(notice->lifecycle == ui::NotificationLifecycle::ConditionBound); CHECK(notice->titleKey == "citywire.civic.unrest.active.title"); CHECK(notice->autoOpen && !notice->autoOpened); CHECK(notice->actionCount == 2); CHECK(notice->actions[0].command == ui::NotificationActionCommand::GoTo); CHECK(notice->actions[0].row == 2 && notice->actions[0].column == 2); CHECK(notice->actions[1].command == ui::NotificationActionCommand::OpenDataView); CHECK(notice->actions[1].argument == ui::notification_action_argument( ui::NotificationDataView::Satisfaction)); CHECK(integer_parameter(*notice, "episode_id") == 7); CHECK(integer_parameter(*notice, "candidate_id") == 3); CHECK(integer_parameter(*notice, "row") == 2); CHECK(integer_parameter(*notice, "column") == 2); const sim::DomainEvent staleEnd = unrest_event( {sim::CivicUnrestTransitionKind::Ended, sim::CivicUnrestResolution::DurationComplete, 6, 3, 99, 3, 2, 2}, 98); CHECK(ui::project_notification_event( store, staleEnd, {3, "Civic City", &city}) == ui::NotificationProjectionOutcome::Ignored); CHECK(store.find(unrest_key(3))); const sim::DomainEvent ended = unrest_event( {sim::CivicUnrestTransitionKind::Ended, sim::CivicUnrestResolution::DurationComplete, 7, 3, 99, 3, 2, 2}, 98); CHECK(ui::project_notification_event( store, ended, {3, "Civic City", &city}) == ui::NotificationProjectionOutcome::Resolved); CHECK(!store.find(unrest_key(3))); } void load_synchronization_rebuilds_without_interrupting() { City city{MapSize::Tiny}; ui::NotificationStore store; sim::CivicSatisfactionState state = state_with_episode(); const ui::NotificationProjectionContext context{1, "Loaded Civic", &city}; CHECK(ui::synchronize_civic_unrest_notifications( store, state, context) == (ui::NotificationProjectionSummary{1, 0, 0})); const ui::Notification* notice = store.find(unrest_key(3)); CHECK(notice && !notice->autoOpen); CHECK(notice && notice->sourceEvent == std::nullopt); CHECK(notice && integer_parameter(*notice, "episode_id") == 7); CHECK(ui::synchronize_civic_unrest_notifications( store, state, context) == (ui::NotificationProjectionSummary{0, 0, 1})); state.unrestEpisodes.clear(); state.lastProcessedPeriod = 3; CHECK(sim::valid_civic_satisfaction_state(state)); CHECK(ui::synchronize_civic_unrest_notifications( store, state, {3, "Loaded Civic", &city}) == (ui::NotificationProjectionSummary{0, 1, 0})); CHECK(store.empty()); } void malformed_events_and_authority_fail_closed() { City city{MapSize::Tiny}; ui::NotificationStore store; sim::CivicUnrestChanged invalid{ sim::CivicUnrestTransitionKind::Started, sim::CivicUnrestResolution::DurationComplete, 7, 3, 99, 0, 2, 2}; CHECK(ui::project_notification_event(store, unrest_event(invalid), {0, "Invalid", &city}) == ui::NotificationProjectionOutcome::Ignored); invalid.resolution = sim::CivicUnrestResolution::None; invalid.row = 64; CHECK(ui::project_notification_event(store, unrest_event(invalid), {0, "Invalid", &city}) == ui::NotificationProjectionOutcome::Ignored); CHECK(store.empty()); sim::CivicSatisfactionState malformed = state_with_episode(); malformed.randomSalt = 0; CHECK(ui::synchronize_civic_unrest_notifications( store, malformed, {0, "Invalid", &city}) == (ui::NotificationProjectionSummary{0, 0, 1})); CHECK(store.empty()); } } // namespace int main() { start_and_end_share_exact_episode_identity(); load_synchronization_rebuilds_without_interrupting(); malformed_events_and_authority_fail_closed(); if (failures == 0) { std::puts("notification civic unrest: all checks passed"); return 0; } std::printf("notification civic unrest: %d failure(s)\n", failures); return 1; }