// City Wire decision and terminal-event projection: military offers and scenario outcomes. #include "sc2k/sim/military.hpp" #include "sc2k/ui/notification_projector.hpp" #include "sc2k/ui/notification_selection.hpp" #include #include #include #include #include #include "test_harness.hpp" using namespace sc2k; template static sim::DomainEvent domain_event(Payload payload, std::uint32_t ordinal = 0, sim::EventPhase phase = sim::EventPhase::Session) { return {{1700, phase, ordinal}, sim::DomainEventPayload{payload}}; } 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; } static std::int64_t integer_parameter(const ui::Notification& notification, std::string_view name) { const ui::NotificationParameter* found = parameter(notification, name); const auto* value = found ? std::get_if(&found->value) : nullptr; CHECK(value); return value ? *value : 0; } static std::string_view literal_parameter(const ui::Notification& notification, std::string_view name) { const ui::NotificationParameter* found = parameter(notification, name); const auto* value = found ? std::get_if(&found->value) : nullptr; CHECK(value); return value ? std::string_view(value->value) : std::string_view{}; } static std::string_view message_parameter(const ui::Notification& notification, std::string_view name) { const ui::NotificationParameter* found = parameter(notification, name); const auto* value = found ? std::get_if(&found->value) : nullptr; CHECK(value); return value ? std::string_view(value->value) : std::string_view{}; } static ui::NotificationKey military_key() { return {ui::NotificationKeyDomain::Decision, ui::notification_key_value(ui::NotificationDecisionKey::MilitaryBase)}; } static ui::NotificationKey scenario_key() { return {ui::NotificationKeyDomain::Outcome, ui::notification_key_value(ui::NotificationOutcomeKey::Outcome)}; } static void check_action(const ui::Notification& notification, std::size_t index, std::string_view label, ui::NotificationActionCommand command, std::int64_t argument = 0, int row = -1, int column = -1) { CHECK(index < notification.actionCount); if (index >= notification.actionCount) return; const ui::NotificationAction& action = notification.actions[index]; CHECK(action.labelKey == label); CHECK(action.command == command && action.argument == argument); CHECK(action.row == row && action.column == column && action.closeDialog); } static void military_offer_is_protected_until_a_committed_outcome_replaces_it() { City city; ui::NotificationStore store; const sim::DomainEvent offered = domain_event( sim::MilitaryBaseOffered{}, 0, sim::EventPhase::Progression); CHECK(ui::project_notification_event(store, offered, {100, "Metro", &city}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* notification = store.find(military_key()); CHECK(notification); if (!notification) return; const ui::NotificationId id = notification->id; CHECK(notification->sourceEvent == offered.stamp); CHECK(notification->titleKey == "citywire.military.offer.title"); CHECK(notification->descriptionKey == "citywire.military.offer.description"); CHECK(notification->category == ui::NotificationCategory::Reward); CHECK(notification->lifecycle == ui::NotificationLifecycle::DecisionBound); CHECK(notification->expiryMonths == 0 && notification->autoOpen); CHECK(literal_parameter(*notification, "city") == "Metro"); CHECK(notification->actionCount == 2); const auto decisionArgument = static_cast( ui::notification_key_value(ui::NotificationDecisionKey::MilitaryBase)); check_action(*notification, 0, "citywire.action.decline_military", ui::NotificationActionCommand::DeclineDecision, decisionArgument); check_action(*notification, 1, "citywire.action.grant_military", ui::NotificationActionCommand::AcceptDecision, decisionArgument); store.prune(10000); CHECK(store.find(military_key())); CHECK(ui::project_notification_event(store, offered, {10001, "Metro", &city}) == ui::NotificationProjectionOutcome::Ignored); city.set_military_base(int(sim::MilitaryType::AirForce)); const sim::DomainEvent placed = domain_event(sim::MilitaryBaseDecisionResolved{ sim::MilitaryBaseDecisionOutcome::Placed, static_cast(sim::MilitaryType::AirForce), 40, 50}, 1); CHECK(ui::project_notification_event(store, placed, {10001, "Metro", &city}) == ui::NotificationProjectionOutcome::Upserted); notification = store.find(military_key()); CHECK(notification && notification->id == id); CHECK(notification && notification->sourceEvent == placed.stamp); CHECK(notification && notification->titleKey == "citywire.military.placed.title"); CHECK(notification && notification->category == ui::NotificationCategory::Info); CHECK(notification && notification->lifecycle == ui::NotificationLifecycle::Historical); CHECK(notification && notification->expiryMonths == 18 && !notification->autoOpen); CHECK(notification && message_parameter(*notification, "kind") == "military.kind.airforce"); CHECK(notification && integer_parameter(*notification, "base_type") == static_cast(sim::MilitaryType::AirForce)); CHECK(notification && integer_parameter(*notification, "center_row") == 40); CHECK(notification && integer_parameter(*notification, "center_column") == 50); CHECK(notification && notification->actionCount == 1); if (notification) check_action(*notification, 0, "citywire.action.goto", ui::NotificationActionCommand::GoTo, 0, 40, 50); } static void military_negative_outcomes_are_distinct_bounded_history() { struct Expected { sim::MilitaryBaseDecisionOutcome outcome; const char* title; }; static constexpr std::array kExpected = { Expected{sim::MilitaryBaseDecisionOutcome::Declined, "citywire.military.declined.title"}, Expected{sim::MilitaryBaseDecisionOutcome::NoSuitableSite, "citywire.military.no_site.title"}, }; for (const Expected& expected : kExpected) { ui::NotificationStore store; CHECK(ui::project_notification_event(store, domain_event(sim::MilitaryBaseOffered{}, 0, sim::EventPhase::Progression), {20, "Decision City"}) == ui::NotificationProjectionOutcome::Upserted); const sim::DomainEvent resolved = domain_event(sim::MilitaryBaseDecisionResolved{ expected.outcome, static_cast(sim::MilitaryType::Declined), -1, -1}, 1); CHECK(ui::project_notification_event(store, resolved, {21, "Decision City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* notification = store.find(military_key()); CHECK(notification && notification->titleKey == expected.title); CHECK(notification && notification->category == ui::NotificationCategory::Info); CHECK(notification && notification->lifecycle == ui::NotificationLifecycle::Historical); CHECK(notification && notification->expiryMonths == 12 && notification->actionCount == 0); CHECK(notification && integer_parameter(*notification, "outcome") == static_cast(expected.outcome)); store.prune(32); CHECK(store.find(military_key())); store.prune(33); CHECK(!store.find(military_key())); } } static void every_city_outcome_has_terminal_copy_priority_and_retention() { struct Expected { CityOutcome outcome; const char* slug; ui::NotificationCategory category; std::uint8_t actions; ui::NotificationActionCommand command; }; static constexpr std::array kExpected = { Expected{CityOutcome::ScenarioWon, "scenario.won", ui::NotificationCategory::Info, 0, ui::NotificationActionCommand::Acknowledge}, Expected{CityOutcome::ScenarioTimeExpired, "scenario.time_expired", ui::NotificationCategory::Danger, 1, ui::NotificationActionCommand::OpenScenarioObjectives}, Expected{CityOutcome::Insolvent, "scenario.bankruptcy", ui::NotificationCategory::Danger, 1, ui::NotificationActionCommand::OpenBudget}, Expected{CityOutcome::BailoutRejected, "outcome.support_rejected", ui::NotificationCategory::Danger, 1, ui::NotificationActionCommand::OpenBonds}, Expected{CityOutcome::RepeatedDebtDefault, "outcome.repeated_default", ui::NotificationCategory::Danger, 1, ui::NotificationActionCommand::OpenBonds}, }; for (const Expected& expected : kExpected) { ui::NotificationStore store; const sim::DomainEvent completed = domain_event( sim::CityOutcomeReached{expected.outcome}, 0, sim::EventPhase::Scenario); CHECK(ui::project_notification_event(store, completed, {300, "Scenario City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* notification = store.find(scenario_key()); CHECK(notification); if (!notification) continue; const std::string base = std::string("citywire.") + expected.slug; CHECK(notification->sourceEvent == completed.stamp); CHECK(notification->titleKey == base + ".title"); CHECK(notification->descriptionKey == base + ".description"); CHECK(notification->category == expected.category); CHECK(notification->lifecycle == ui::NotificationLifecycle::Historical); CHECK(notification->expiryMonths == 36 && notification->autoOpen); CHECK(notification->actionCount == expected.actions); CHECK(literal_parameter(*notification, "city") == "Scenario City"); CHECK(integer_parameter(*notification, "outcome") == static_cast(expected.outcome)); CHECK(ui::next_auto_open_notification(store.records()) == notification->id); CHECK(ui::notification_interrupts_existing_dialog(notification->category) == (expected.category == ui::NotificationCategory::Danger)); CHECK(ui::notification_detail_pause_policy() == ui::SimulationPausePolicy::PauseSimulation); if (expected.actions) CHECK(notification->actions[0].command == expected.command); CHECK(ui::project_notification_event(store, completed, {301, "Scenario City"}) == ui::NotificationProjectionOutcome::Ignored); store.prune(335); CHECK(store.find(scenario_key())); store.prune(336); CHECK(!store.find(scenario_key())); } } static void malformed_decisions_and_nonterminal_scenarios_are_ignored() { City decidedCity; decidedCity.set_military_base(int(sim::MilitaryType::Declined)); ui::NotificationStore store; const std::array events = { domain_event(sim::MilitaryBaseDecisionResolved{sim::MilitaryBaseDecisionOutcome::Placed, static_cast(sim::MilitaryType::Declined), 10, 10}), domain_event(sim::MilitaryBaseDecisionResolved{sim::MilitaryBaseDecisionOutcome::Placed, static_cast(sim::MilitaryType::Army), -1, 10}), domain_event(sim::MilitaryBaseDecisionResolved{sim::MilitaryBaseDecisionOutcome::Declined, static_cast(sim::MilitaryType::Army), -1, -1}), domain_event(sim::MilitaryBaseDecisionResolved{sim::MilitaryBaseDecisionOutcome::NoSuitableSite, static_cast(sim::MilitaryType::Declined), 0, 0}), domain_event(sim::MilitaryBaseDecisionResolved{ static_cast(255), 1, -1, -1}), domain_event(sim::CityOutcomeReached{CityOutcome::Playing}, 0, sim::EventPhase::Scenario), }; CHECK(ui::project_notification_event(store, domain_event(sim::MilitaryBaseOffered{}, 0, sim::EventPhase::Progression), {0, "Decided", &decidedCity}) == ui::NotificationProjectionOutcome::Ignored); CHECK(ui::project_notification_batch(store, events, {0, "Invalid"}) == (ui::NotificationProjectionSummary{0, 0, events.size()})); CHECK(store.empty()); } static void mixed_batch_transitions_the_offer_in_committed_order() { ui::NotificationStore store; const std::vector events = { domain_event(sim::MilitaryBaseOffered{}, 0, sim::EventPhase::Progression), domain_event(sim::MilitaryBaseDecisionResolved{sim::MilitaryBaseDecisionOutcome::Declined, static_cast(sim::MilitaryType::Declined), -1, -1}, 1), domain_event(sim::CityOutcomeReached{CityOutcome::ScenarioTimeExpired}, 2, sim::EventPhase::Scenario), }; CHECK(ui::project_notification_batch(store, events, {500, "Ordered"}) == (ui::NotificationProjectionSummary{3, 0, 0})); CHECK(store.size() == 2); CHECK(store.records()[0].stableKey == military_key()); CHECK(store.records()[0].sourceEvent->ordinal == 1); CHECK(store.records()[0].titleKey == "citywire.military.declined.title"); CHECK(store.records()[1].stableKey == scenario_key()); CHECK(store.records()[1].sourceEvent->ordinal == 2); } static void pending_military_offer_reconstructs_and_clears_from_persisted_city_state() { City city; city.set_city_status(4); ui::NotificationStore store; const ui::NotificationProjectionContext context{800, "Reloaded", &city}; CHECK(ui::synchronize_military_base_notification(store, city, context) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* notification = store.find(military_key()); CHECK(notification && !notification->sourceEvent); CHECK(notification && notification->lifecycle == ui::NotificationLifecycle::DecisionBound); CHECK(notification && notification->autoOpen); CHECK(notification && literal_parameter(*notification, "city") == "Reloaded"); CHECK(ui::synchronize_military_base_notification(store, city, context) == ui::NotificationProjectionOutcome::Ignored); city.set_military_base(int(sim::MilitaryType::Declined)); CHECK(ui::synchronize_military_base_notification(store, city, context) == ui::NotificationProjectionOutcome::Resolved); CHECK(!store.find(military_key())); city.set_city_status(3); city.set_military_base(int(sim::MilitaryType::None)); CHECK(ui::synchronize_military_base_notification(store, city, context) == ui::NotificationProjectionOutcome::Ignored); } int main() { military_offer_is_protected_until_a_committed_outcome_replaces_it(); military_negative_outcomes_are_distinct_bounded_history(); every_city_outcome_has_terminal_copy_priority_and_retention(); malformed_decisions_and_nonterminal_scenarios_are_ignored(); mixed_batch_transitions_the_offer_in_committed_order(); pending_military_offer_reconstructs_and_clears_from_persisted_city_state(); if (failures == 0) { std::printf("notification decisions: all checks passed\n"); return 0; } std::printf("notification decisions: %d failure(s)\n", failures); return 1; }