// City Wire finance projector: economy, credit, taxes, budgets, debt, and ordinances. #include "sc2k/ui/notification_projector.hpp" #include "sc2k/ui/notification_persistence.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::Finance) { return {{1200, 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 ui::NotificationParameterFormat parameter_format(const ui::Notification& notification, std::string_view name) { const ui::NotificationParameter* found = parameter(notification, name); CHECK(found); return found ? found->format : ui::NotificationParameterFormat::Plain; } 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 void check_action(const ui::Notification& notification, std::string_view label, ui::NotificationActionCommand command) { CHECK(notification.actionCount == 1); if (notification.actionCount != 1) return; CHECK(notification.actions[0].labelKey == label); CHECK(notification.actions[0].command == command); CHECK(notification.actions[0].closeDialog); } static ui::NotificationKey finance_key(ui::NotificationFinanceKey key) { return {ui::NotificationKeyDomain::Finance, ui::notification_key_value(key)}; } static ui::NotificationKey decision_key(ui::NotificationDecisionKey key) { return {ui::NotificationKeyDomain::Decision, ui::notification_key_value(key)}; } static void economy_regimes_replace_one_stable_state() { static constexpr std::array kTitles = { "desk.econ.depression.head", "desk.econ.recession.head", "desk.econ.recovery.head", "desk.econ.boom.head"}; ui::NotificationStore store; const ui::NotificationKey key = finance_key(ui::NotificationFinanceKey::EconomyRegime); ui::NotificationId originalId; for (int trend = 0; trend < 4; ++trend) { const int oldTrend = trend == 0 ? 3 : trend - 1; const sim::DomainEvent event = domain_event( sim::EconomyRegimeChanged{oldTrend, trend, 4 + trend}, trend, sim::EventPhase::Economy); CHECK(ui::project_notification_event(store, event, {20 + trend, "Economy City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* notification = store.find(key); CHECK(notification); if (!notification) continue; if (trend == 0) originalId = notification->id; else CHECK(notification->id == originalId); CHECK(notification->sourceEvent == event.stamp); CHECK(notification->titleKey == kTitles[trend]); CHECK(notification->descriptionKey == (trend < 2 ? "desk.econ.down.body" : "desk.econ.up.body")); CHECK(notification->category == ui::NotificationCategory::Budget); CHECK(notification->lifecycle == (trend < 2 ? ui::NotificationLifecycle::ConditionBound : ui::NotificationLifecycle::Historical)); CHECK(notification->expiryMonths == (trend < 2 ? 0 : 12)); CHECK(integer_parameter(*notification, "old_trend") == oldTrend); CHECK(integer_parameter(*notification, "trend") == trend); CHECK(integer_parameter(*notification, "rate") == 4 + trend); check_action(*notification, "citywire.action.open_bonds", ui::NotificationActionCommand::OpenBonds); } CHECK(store.size() == 1); const std::array invalid = { sim::EconomyRegimeChanged{-1, 0, 4}, sim::EconomyRegimeChanged{0, 4, 4}, sim::EconomyRegimeChanged{2, 2, 4}, sim::EconomyRegimeChanged{2, 3, -1}}; for (const auto& event : invalid) CHECK(ui::project_notification_event(store, domain_event(event), {24, "Economy City"}) == ui::NotificationProjectionOutcome::Ignored); } static void material_debt_risk_is_protected_until_a_committed_upgrade() { ui::NotificationStore store; const ui::NotificationKey key = finance_key(ui::NotificationFinanceKey::CreditRating); const sim::DomainEvent ordinary = domain_event( sim::CreditRatingChanged{0, 3}, 0, sim::EventPhase::Conditions); CHECK(ui::project_notification_event(store, ordinary, {30, "Credit City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::NotificationId id = store.find(key)->id; CHECK(store.find(key)->category == ui::NotificationCategory::Budget); CHECK(store.find(key)->lifecycle == ui::NotificationLifecycle::Historical); CHECK(literal_parameter(*store.find(key), "grade") == "B"); const sim::DomainEvent junk = domain_event( sim::CreditRatingChanged{3, 4}, 1, sim::EventPhase::Conditions); CHECK(ui::project_notification_event(store, junk, {31, "Credit City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* danger = store.find(key); CHECK(danger && danger->id == id); CHECK(danger && danger->category == ui::NotificationCategory::Warning); CHECK(danger && danger->lifecycle == ui::NotificationLifecycle::ConditionBound); CHECK(danger && !danger->autoOpen && danger->expiryMonths == 0); CHECK(danger && danger->titleKey == "citywire.finance.debt_risk.title"); CHECK(danger && literal_parameter(*danger, "grade") == "C"); CHECK(danger && literal_parameter(*danger, "city") == "Credit City"); check_action(*danger, "citywire.action.open_bonds", ui::NotificationActionCommand::OpenBonds); store.prune(10000); CHECK(store.find(key)); const sim::DomainEvent worse = domain_event( sim::CreditRatingChanged{4, 5}, 2, sim::EventPhase::Conditions); CHECK(ui::project_notification_event(store, worse, {32, "Credit City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.find(key) && store.find(key)->id == id && !store.find(key)->autoOpened); CHECK(store.find(key) && store.find(key)->category == ui::NotificationCategory::Warning); CHECK(store.find(key) && store.find(key)->lifecycle == ui::NotificationLifecycle::ConditionBound); CHECK(store.find(key) && store.find(key)->titleKey == "citywire.finance.debt_risk.title"); CHECK(store.find(key) && literal_parameter(*store.find(key), "grade") == "D"); const sim::DomainEvent junkUpgrade = domain_event( sim::CreditRatingChanged{5, 4}, 3, sim::EventPhase::Conditions); CHECK(ui::project_notification_event(store, junkUpgrade, {33, "Credit City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.find(key) && store.find(key)->titleKey == "citywire.finance.debt_risk.title"); CHECK(store.find(key) && store.find(key)->category == ui::NotificationCategory::Warning); CHECK(store.find(key) && store.find(key)->lifecycle == ui::NotificationLifecycle::ConditionBound); CHECK(store.find(key) && !store.find(key)->autoOpen); const sim::DomainEvent upgrade = domain_event( sim::CreditRatingChanged{4, 2}, 4, sim::EventPhase::Conditions); CHECK(ui::project_notification_event(store, upgrade, {34, "Credit City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* recovered = store.find(key); CHECK(recovered && recovered->id == id); CHECK(recovered && recovered->titleKey == "desk.rating.up.head"); CHECK(recovered && recovered->category == ui::NotificationCategory::Budget); CHECK(recovered && recovered->lifecycle == ui::NotificationLifecycle::Historical); CHECK(recovered && !recovered->autoOpen && recovered->expiryMonths == 12); CHECK(recovered && literal_parameter(*recovered, "grade") == "A"); CHECK(ui::project_notification_event(store, domain_event(sim::CreditRatingChanged{2, 4}, 5, sim::EventPhase::Conditions), {35, "Credit City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.find(key) && store.find(key)->id == id); CHECK(store.find(key) && !store.find(key)->autoOpen); CHECK(ui::project_notification_event(store, domain_event(sim::CreditRatingChanged{4, 2}, 6, sim::EventPhase::Conditions), {36, "Credit City"}) == ui::NotificationProjectionOutcome::Upserted); store.prune(47); CHECK(store.find(key)); store.prune(48); CHECK(!store.find(key)); const std::array invalid = { sim::CreditRatingChanged{-1, 0}, sim::CreditRatingChanged{0, 7}, sim::CreditRatingChanged{2, 2}}; for (const auto& event : invalid) CHECK(ui::project_notification_event(store, domain_event(event), {45, "Credit City"}) == ui::NotificationProjectionOutcome::Ignored); } static void tax_changes_coalesce_and_ignore_service_funding() { ui::NotificationStore store; const ui::NotificationKey key = finance_key(ui::NotificationFinanceKey::PropertyTax); CHECK(ui::project_notification_event(store, domain_event(sim::BudgetRateChanged{0, 7, 8}, 0, sim::EventPhase::Session), {40, "Tax City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::NotificationId id = store.find(key)->id; CHECK(store.find(key)->titleKey == "desk.tax.hike.head"); CHECK(integer_parameter(*store.find(key), "record") == 0); CHECK(integer_parameter(*store.find(key), "rate") == 8); check_action(*store.find(key), "citywire.action.open_budget", ui::NotificationActionCommand::OpenBudget); CHECK(ui::project_notification_event(store, domain_event(sim::BudgetRateChanged{2, 8, 6}, 1, sim::EventPhase::Session), {41, "Tax City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.size() == 1 && store.find(key)->id == id); CHECK(store.find(key)->titleKey == "desk.tax.cut.head"); CHECK(integer_parameter(*store.find(key), "record") == 2); CHECK(integer_parameter(*store.find(key), "old_rate") == 8); CHECK(integer_parameter(*store.find(key), "rate") == 6); const std::array invalid = { sim::BudgetRateChanged{5, 100, 95}, sim::BudgetRateChanged{-1, 7, 8}, sim::BudgetRateChanged{0, 7, 7}, sim::BudgetRateChanged{0, -1, 7}, sim::BudgetRateChanged{0, 7, 21}}; for (const auto& event : invalid) CHECK(ui::project_notification_event(store, domain_event(event), {41, "Tax City"}) == ui::NotificationProjectionOutcome::Ignored); } static void annual_review_is_bounded_but_unresolved_auto_budget_failure_is_deferred() { ui::NotificationStore store; const ui::NotificationKey key = finance_key(ui::NotificationFinanceKey::AnnualBudgetReview); const sim::DomainEvent review = domain_event(sim::YearEndSettled{12000, 9000, true, false}); CHECK(ui::project_notification_event(store, review, {50, "Budget City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* notification = store.find(key); CHECK(notification && notification->titleKey == "citywire.finance.annual_budget.title"); CHECK(notification && notification->category == ui::NotificationCategory::Budget); CHECK(notification && notification->lifecycle == ui::NotificationLifecycle::Historical); CHECK(notification && notification->expiryMonths == 12 && !notification->autoOpen); CHECK(notification && integer_parameter(*notification, "funds_before") == 12000); CHECK(notification && integer_parameter(*notification, "funds_after") == 9000); CHECK(notification && parameter_format(*notification, "funds_before") == ui::NotificationParameterFormat::Money); CHECK(notification && parameter_format(*notification, "funds_after") == ui::NotificationParameterFormat::Money); check_action(*notification, "citywire.action.open_budget", ui::NotificationActionCommand::OpenBudget); CHECK(ui::project_notification_event(store, domain_event(sim::YearEndSettled{9000, -1000, false, true}), {51, "Budget City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(ui::project_notification_event(store, domain_event(sim::YearEndSettled{9000, 10000, false, false}), {51, "Budget City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(store.size() == 1); } static void bond_transactions_are_independent_history() { ui::NotificationStore store; CHECK(ui::project_notification_event(store, domain_event(sim::BondIssued{42, 10000, 650, 0, 0, 3, 25000}, 0, sim::EventPhase::Finance), {60, "Bond City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(ui::project_notification_event(store, domain_event(sim::BondRedeemed{41, 10000, 525, 0, 10000, 2, 15000}, 1, sim::EventPhase::Finance), {60, "Bond City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(ui::project_notification_event(store, domain_event(sim::BondMatured{40, 120}, 2, sim::EventPhase::Finance), {60, "Bond City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(ui::project_notification_event(store, domain_event(sim::DebtServicePaid{120, 12, 173, 185, 1, 14815}, 3, sim::EventPhase::Finance), {60, "Bond City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(store.size() == 3); const ui::Notification& issued = store.records()[0]; const ui::Notification& repaid = store.records()[1]; const ui::Notification& matured = store.records()[2]; CHECK(!issued.stableKey && !repaid.stableKey && !matured.stableKey); CHECK(issued.titleKey == "citywire.finance.bond_issued.title"); CHECK(repaid.titleKey == "citywire.finance.bond_repaid.title"); CHECK(matured.titleKey == "citywire.finance.bond_matured.title"); CHECK(integer_parameter(issued, "rate") == 650); CHECK(integer_parameter(issued, "debt_id") == 42); CHECK(integer_parameter(issued, "principal") == 10000); CHECK(integer_parameter(issued, "outstanding") == 3); CHECK(integer_parameter(issued, "funds") == 25000); CHECK(integer_parameter(repaid, "outstanding") == 2); CHECK(integer_parameter(matured, "debt_id") == 40); CHECK(integer_parameter(matured, "period") == 120); CHECK(parameter_format(issued, "rate") == ui::NotificationParameterFormat::PercentBasisPoints); CHECK(parameter_format(issued, "principal") == ui::NotificationParameterFormat::Money); CHECK(parameter_format(issued, "funds") == ui::NotificationParameterFormat::Money); check_action(issued, "citywire.action.open_bonds", ui::NotificationActionCommand::OpenBonds); check_action(repaid, "citywire.action.open_bonds", ui::NotificationActionCommand::OpenBonds); check_action(matured, "citywire.action.open_bonds", ui::NotificationActionCommand::OpenBonds); const std::array invalidIssued = { sim::BondIssued{0, 10000, 500, 0, 0, 1, 0}, sim::BondIssued{1, 0, 500, 0, 0, 1, 0}, sim::BondIssued{1, 10000, sim::kMaximumAnnualRateBasisPoints + 1, 0, 0, 1, 0}, sim::BondIssued{1, 10000, 500, 12, 0, 1, 0}, sim::BondIssued{1, 10000, 500, 0, 0, 0, 0}, sim::BondIssued{1, 10000, 500, 0, 0, static_cast(sim::kMaximumDebtInstruments) + 1, 0}}; for (const auto& event : invalidIssued) CHECK(ui::project_notification_event(store, domain_event(event), {60, "Bond City"}) == ui::NotificationProjectionOutcome::Ignored); const std::array invalidRedeemed = { sim::BondRedeemed{0, 10000, 500, 0, 10000, 0, 0}, sim::BondRedeemed{1, 0, 500, 0, 0, 0, 0}, sim::BondRedeemed{1, 10000, 500, -1, 9999, 0, 0}, sim::BondRedeemed{1, 10000, 500, 50, 10000, 0, 0}, sim::BondRedeemed{1, 10000, 500, 0, 10000, -1, 0}, sim::BondRedeemed{1, 10000, 500, 0, 10000, static_cast(sim::kMaximumDebtInstruments), 0}}; for (const auto& event : invalidRedeemed) CHECK(ui::project_notification_event(store, domain_event(event), {60, "Bond City"}) == ui::NotificationProjectionOutcome::Ignored); const std::array invalidMatured = { sim::BondMatured{0, 1}, sim::BondMatured{1, -1}}; for (const auto& event : invalidMatured) CHECK(ui::project_notification_event(store, domain_event(event), {60, "Bond City"}) == ui::NotificationProjectionOutcome::Ignored); } static void every_ordinance_uses_a_message_key_and_city_options_stay_quiet() { ui::NotificationStore store; for (std::uint8_t bit = 0; bit < 20; ++bit) { const bool enabled = (bit % 2) == 0; const sim::DomainEvent event = domain_event( sim::OrdinanceChanged{bit, enabled}, bit, sim::EventPhase::Session); CHECK(ui::project_notification_event(store, event, {70, "Civic City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification& notification = store.records().back(); CHECK(!notification.stableKey); CHECK(notification.titleKey == (enabled ? "desk.ordinance.on.head" : "desk.ordinance.off.head")); CHECK(notification.category == ui::NotificationCategory::Info); CHECK(notification.expiryMonths == 12); CHECK(message_parameter(notification, "ord").starts_with("ordinance.")); CHECK(integer_parameter(notification, "bit") == bit); check_action(notification, "citywire.action.open_ordinances", ui::NotificationActionCommand::OpenOrdinances); } CHECK(store.size() == 20); CHECK(ui::project_notification_event(store, domain_event(sim::OrdinanceChanged{20, true}, 20, sim::EventPhase::Session), {70, "Civic City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(ui::project_notification_event(store, domain_event(sim::CityOptionChanged{sim::CityOptionId::AutoBudget, true}, 21, sim::EventPhase::Session), {70, "Civic City"}) == ui::NotificationProjectionOutcome::Ignored); } static void mixed_finance_batch_preserves_source_order() { ui::NotificationStore store; const std::vector events = { domain_event(sim::BondIssued{1, 10000, 600, 0, 0, 1, 20000}, 0, sim::EventPhase::Finance), domain_event(sim::CityOptionChanged{sim::CityOptionId::AutoGoto, true}, 1, sim::EventPhase::Session), domain_event(sim::OrdinanceChanged{0, true}, 2, sim::EventPhase::Session), domain_event(sim::EconomyRegimeChanged{2, 3, 5}, 3, sim::EventPhase::Economy)}; CHECK(ui::project_notification_batch(store, events, {80, "Batch City"}) == (ui::NotificationProjectionSummary{3, 0, 1})); CHECK(store.size() == 3); CHECK(store.records()[0].sourceEvent == events[0].stamp); CHECK(store.records()[1].sourceEvent == events[2].stamp); CHECK(store.records()[2].sourceEvent == events[3].stamp); } static void adverse_finance_state_reconstructs_without_replaying_recovery_edges() { ui::NotificationStore store; sim::CitySignals signals; signals.economyTrend = 0; signals.federalRate = 9; signals.loanRating = 4; sim::MunicipalDebt debt; const auto adverse = ui::synchronize_finance_notifications( store, signals, debt, {500, "Loaded Finance"}); CHECK(adverse.upserted == 2 && store.size() == 2); const ui::Notification* economy = store.find( finance_key(ui::NotificationFinanceKey::EconomyRegime)); const ui::Notification* credit = store.find( finance_key(ui::NotificationFinanceKey::CreditRating)); CHECK(economy && economy->lifecycle == ui::NotificationLifecycle::ConditionBound); CHECK(economy && economy->titleKey == "desk.econ.depression.head"); CHECK(credit && credit->category == ui::NotificationCategory::Warning); CHECK(credit && !credit->autoOpen); signals.economyTrend = 2; signals.loanRating = 2; const auto recovered = ui::synchronize_finance_notifications( store, signals, debt, {501, "Loaded Finance"}); CHECK(recovered.resolved == 2 && store.empty()); CHECK(ui::project_notification_event(store, domain_event(sim::EconomyRegimeChanged{1, 2, 5}), {502, "Loaded Finance"}) == ui::NotificationProjectionOutcome::Upserted); const ui::NotificationId historicalId = store.records()[0].id; const auto steady = ui::synchronize_finance_notifications( store, signals, debt, {503, "Loaded Finance"}); CHECK(steady.resolved == 0 && store.find(historicalId)); CHECK(store.find(historicalId)->lifecycle == ui::NotificationLifecycle::Historical); } static void government_supervision_reconstructs_and_resolves_from_canonical_debt() { ui::NotificationStore store; sim::CitySignals signals; signals.economyTrend = 2; signals.loanRating = 0; sim::MunicipalDebt debt; debt.support.status = sim::FiscalStatus::GovernmentSupervision; debt.support.lastBailoutPeriod = 100; debt.support.bailoutEligibleAgainPeriod = 160; debt.support.lastBailoutAmount = 25000; const ui::NotificationKey key = finance_key(ui::NotificationFinanceKey::GovernmentSupervision); const auto created = ui::synchronize_finance_notifications( store, signals, debt, {120, "Supported City"}); CHECK(created.upserted == 1); const ui::Notification* supervision = store.find(key); CHECK(supervision && supervision->category == ui::NotificationCategory::Budget); CHECK(supervision && supervision->lifecycle == ui::NotificationLifecycle::ConditionBound); CHECK(supervision && !supervision->autoOpen && supervision->expiryMonths == 0); CHECK(supervision && supervision->titleKey == "citywire.finance.supervision.title"); check_action(*supervision, "citywire.action.open_bonds", ui::NotificationActionCommand::OpenBonds); store.prune(10000); CHECK(store.find(key)); const ui::NotificationId id = supervision->id; const auto steady = ui::synchronize_finance_notifications( store, signals, debt, {121, "Supported City"}); CHECK(steady.upserted == 0 && store.find(key) && store.find(key)->id == id); debt.support.status = sim::FiscalStatus::Normal; const auto resolved = ui::synchronize_finance_notifications( store, signals, debt, {160, "Supported City"}); CHECK(resolved.resolved == 1 && !store.find(key)); } static void government_support_decision_is_reconstructed_from_fina_not_history() { City city; city.set_misc_be32(sc2k::misc::kStartYear, 2000); ui::NotificationStore store; sim::CitySignals signals; signals.economyTrend = 2; signals.loanRating = 0; sim::MunicipalDebt debt; debt.support.pending = sim::PendingBailout{120, 25000, -5000, 10000, 40000}; const ui::NotificationKey key = decision_key(ui::NotificationDecisionKey::GovernmentSupport); const auto created = ui::synchronize_finance_notifications( store, signals, debt, {120, "Default City", &city}); CHECK(created.upserted == 1); const ui::Notification* support = store.find(key); CHECK(support); if (!support) return; CHECK(support->category == ui::NotificationCategory::Emergency); CHECK(support->lifecycle == ui::NotificationLifecycle::DecisionBound); CHECK(support->closePolicy == ui::NotificationClosePolicy::ResolutionRequired); CHECK(support->autoOpen && !support->autoOpened && support->expiryMonths == 0); CHECK(support->titleKey == "citywire.finance.support.title"); CHECK(parameter_format(*support, "due") == ui::NotificationParameterFormat::Money); CHECK(parameter_format(*support, "treasury") == ui::NotificationParameterFormat::Money); CHECK(parameter_format(*support, "injection") == ui::NotificationParameterFormat::Money); CHECK(parameter_format(*support, "reserve") == ui::NotificationParameterFormat::Money); CHECK(integer_parameter(*support, "due") == 25000); CHECK(integer_parameter(*support, "treasury") == -5000); CHECK(integer_parameter(*support, "injection") == 40000); CHECK(integer_parameter(*support, "reserve") == 10000); CHECK(integer_parameter(*support, "runway_months") == sim::kBailoutRunwayMonths); CHECK(integer_parameter(*support, "supervision_months") == sim::kGovernmentSupportCooldownMonths); CHECK(message_parameter(*support, "eligible_month") == "date.mon.0"); CHECK(integer_parameter(*support, "eligible_year") == 2015); CHECK(support->actionCount == 2); CHECK(support->actions[0].labelKey == "citywire.action.accept_support"); CHECK(support->actions[0].tone == ui::NotificationActionTone::Positive); CHECK(support->actions[0].command == ui::NotificationActionCommand::AcceptDecision); CHECK(support->actions[1].labelKey == "citywire.action.reject_support"); CHECK(support->actions[1].tone == ui::NotificationActionTone::Critical); CHECK(support->actions[1].command == ui::NotificationActionCommand::DeclineDecision); const std::int64_t argument = static_cast( ui::notification_key_value(ui::NotificationDecisionKey::GovernmentSupport)); CHECK(support->actions[0].argument == argument); CHECK(support->actions[1].argument == argument); const ui::NotificationId supportId = support->id; // A failed document creation cannot consume the request: only a successful presentation calls // mark_auto_opened, so selection returns the same mandatory decision on the next frame. CHECK(ui::next_auto_open_notification(store.records()) == supportId); CHECK(ui::next_auto_open_notification(store.records()) == supportId); store.prune(10000); CHECK(store.find(key)); // NWIR deliberately carries no decision authority. Loading the discardable history produces an // empty store, then canonical FINA state recreates the same protected decision. const std::vector history = ui::encode_notification_history(store.records()); ui::NotificationStore restored; const auto historyResult = ui::restore_notification_history( restored, history, 10000, city); CHECK(historyResult.recognized && historyResult.restored == 0); CHECK(restored.empty()); CHECK(ui::synchronize_finance_notifications( restored, signals, debt, {120, "Default City", &city}) .upserted == 1); CHECK(restored.find(key)); debt.support.pending.reset(); const auto resolved = ui::synchronize_finance_notifications( restored, signals, debt, {121, "Default City", &city}); CHECK(resolved.resolved == 1 && !restored.find(key)); } static void dismissal_outcomes_reconstruct_from_fina_without_duplicates() { struct Expected { sim::DismissalReason reason; const char* title; CityOutcome outcome; }; static constexpr std::array kExpected{ Expected{sim::DismissalReason::BailoutRejected, "citywire.outcome.support_rejected.title", CityOutcome::BailoutRejected}, Expected{sim::DismissalReason::RepeatedDebtDefault, "citywire.outcome.repeated_default.title", CityOutcome::RepeatedDebtDefault}, Expected{sim::DismissalReason::PersistentOperatingCrisis, "citywire.outcome.operating_crisis.title", CityOutcome::PersistentOperatingCrisis}, }; const ui::NotificationKey key{ui::NotificationKeyDomain::Outcome, ui::notification_key_value(ui::NotificationOutcomeKey::Outcome)}; for (const Expected& expected : kExpected) { ui::NotificationStore store; sim::CitySignals signals; signals.economyTrend = 2; sim::MunicipalDebt debt; debt.support.status = sim::FiscalStatus::Dismissed; debt.support.dismissalReason = expected.reason; const auto created = ui::synchronize_finance_notifications( store, signals, debt, {200, "Dismissed City"}); CHECK(created.upserted == 1); const ui::Notification* outcome = store.find(key); CHECK(outcome && outcome->titleKey == expected.title); CHECK(outcome && outcome->category == ui::NotificationCategory::Danger); CHECK(outcome && outcome->autoOpen && outcome->actionCount == 1); CHECK(outcome && integer_parameter(*outcome, "outcome") == static_cast(expected.outcome)); const ui::NotificationId id = outcome ? outcome->id : ui::NotificationId{}; const auto steady = ui::synchronize_finance_notifications( store, signals, debt, {201, "Dismissed City"}); CHECK(steady.upserted == 0 && store.find(key) && store.find(key)->id == id); } } static void operating_decisions_reconstruct_from_authoritative_state() { ui::NotificationStore store; sim::CitySignals signals; signals.economyTrend = 2; sim::MunicipalDebt debt; debt.support.pending = sim::PendingBailout{24, 0, 0, 10000, 10000, sim::BailoutContext::OperatingDistress, 0}; sim::OperatingBudgetState operating; const ui::NotificationKey supportKey = decision_key(ui::NotificationDecisionKey::GovernmentSupport); CHECK(ui::synchronize_finance_notifications(store, signals, debt, {24, "Rationed City"}, &operating) .upserted == 1); const ui::Notification* support = store.find(supportKey); CHECK(support && support->titleKey == "citywire.finance.operating_support.title"); CHECK(support && integer_parameter(*support, "due") == 0); CHECK(support && integer_parameter(*support, "injection") == 10000); debt.support.pending.reset(); operating.pendingDecisionPeriod = 25; const ui::NotificationKey cutsKey = decision_key(ui::NotificationDecisionKey::OperatingCuts); const auto changed = ui::synchronize_finance_notifications(store, signals, debt, {25, "Rationed City"}, &operating); CHECK(changed.resolved == 1 && changed.upserted == 1); CHECK(!store.find(supportKey)); const ui::Notification* cuts = store.find(cutsKey); CHECK(cuts && cuts->titleKey == "citywire.finance.operating_cuts.title"); CHECK(cuts && cuts->closePolicy == ui::NotificationClosePolicy::ResolutionRequired); CHECK(cuts && cuts->actionCount == 2); CHECK(cuts && cuts->actions[0].command == ui::NotificationActionCommand::AcceptDecision); CHECK(cuts && cuts->actions[0].argument == static_cast(ui::notification_key_value(ui::NotificationDecisionKey::OperatingCuts))); CHECK(cuts && cuts->actions[1].command == ui::NotificationActionCommand::OpenBudget); operating.pendingDecisionPeriod.reset(); CHECK(ui::synchronize_finance_notifications(store, signals, debt, {25, "Rationed City"}, &operating) .resolved == 1); CHECK(!store.find(cutsKey)); } static void operating_rationing_reports_escalate_and_resolve() { City city; for (int record = 5; record < 16; ++record) city.set_budget_rate(record, 0); city.set_budget_rate(5, 100); city.set_budget_rate(7, 100); city.set_budget_rate(10, 100); ui::NotificationStore store; sim::CitySignals signals; signals.economyTrend = 2; sim::MunicipalDebt debt; sim::OperatingBudgetState operating; operating.coverageBasisPoints = 7500; static constexpr std::array kKeys{ ui::NotificationFinanceKey::OperatingPublicSafety, ui::NotificationFinanceKey::OperatingCommunityServices, ui::NotificationFinanceKey::OperatingInfrastructure, }; const auto rationed = ui::synchronize_finance_notifications(store, signals, debt, {30, "Rationed City", &city}, &operating); CHECK(rationed.upserted == 3 && store.size() == 3); std::array ids{}; for (std::size_t index = 0; index < kKeys.size(); ++index) { const ui::Notification* report = store.find(finance_key(kKeys[index])); CHECK(report && report->category == ui::NotificationCategory::Warning); CHECK(report && report->lifecycle == ui::NotificationLifecycle::ConditionBound); CHECK(report && !report->autoOpen && report->expiryMonths == 0); CHECK(report && integer_parameter(*report, "coverage") == 7500); CHECK(report && parameter_format(*report, "coverage") == ui::NotificationParameterFormat::PercentBasisPoints); CHECK(report && literal_parameter(*report, "city") == "Rationed City"); if (report) { ids[index] = report->id; check_action(*report, "citywire.action.open_budget", ui::NotificationActionCommand::OpenBudget); } } operating.coverageBasisPoints = 4000; operating.consecutiveDistressMonths = 1; const auto critical = ui::synchronize_finance_notifications(store, signals, debt, {31, "Rationed City", &city}, &operating); CHECK(critical.upserted == 3 && store.size() == 3); for (std::size_t index = 0; index < kKeys.size(); ++index) { const ui::Notification* report = store.find(finance_key(kKeys[index])); CHECK(report && report->id == ids[index]); CHECK(report && report->category == ui::NotificationCategory::Danger); CHECK(report && integer_parameter(*report, "coverage") == 4000); CHECK(report && integer_parameter(*report, "distress_months") == 1); } operating.coverageBasisPoints = 0; operating.consecutiveDistressMonths = 2; const auto shutdown = ui::synchronize_finance_notifications(store, signals, debt, {32, "Rationed City", &city}, &operating); CHECK(shutdown.upserted == 3 && store.size() == 3); for (std::size_t index = 0; index < kKeys.size(); ++index) { const ui::Notification* report = store.find(finance_key(kKeys[index])); CHECK(report && report->id == ids[index]); CHECK(report && integer_parameter(*report, "coverage") == 0); CHECK(report && integer_parameter(*report, "distress_months") == 2); } operating.coverageBasisPoints = 10000; operating.consecutiveDistressMonths = 0; const auto recovered = ui::synchronize_finance_notifications(store, signals, debt, {33, "Rationed City", &city}, &operating); CHECK(recovered.resolved == 3 && store.empty()); } static void active_disaster_precedes_the_support_decision() { City city; city.set_misc_be32(sc2k::misc::kStartYear, 2000); ui::NotificationStore store; sim::CitySignals signals; signals.economyTrend = 2; sim::MunicipalDebt debt; debt.support.pending = sim::PendingBailout{12, 1000, 0, 10000, 11000}; CHECK(ui::synchronize_finance_notifications( store, signals, debt, {12, "Priority City", &city}) .upserted == 1); const ui::NotificationId support = store.find( decision_key(ui::NotificationDecisionKey::GovernmentSupport)) ->id; const sim::DomainEvent disaster = domain_event( sim::DisasterStarted{7, sim::dtype::kFire, 5, 6}, 0, sim::EventPhase::Disaster); CHECK(ui::project_notification_event(store, disaster, {12, "Priority City", &city}) == ui::NotificationProjectionOutcome::Upserted); const ui::NotificationId disasterId = store.find( ui::NotificationKey{ui::NotificationKeyDomain::Disaster, 7}) ->id; CHECK(ui::next_auto_open_notification(store.records()) == disasterId); CHECK(disasterId != support); } int main() { economy_regimes_replace_one_stable_state(); material_debt_risk_is_protected_until_a_committed_upgrade(); tax_changes_coalesce_and_ignore_service_funding(); annual_review_is_bounded_but_unresolved_auto_budget_failure_is_deferred(); bond_transactions_are_independent_history(); every_ordinance_uses_a_message_key_and_city_options_stay_quiet(); mixed_finance_batch_preserves_source_order(); adverse_finance_state_reconstructs_without_replaying_recovery_edges(); government_supervision_reconstructs_and_resolves_from_canonical_debt(); government_support_decision_is_reconstructed_from_fina_not_history(); dismissal_outcomes_reconstruct_from_fina_without_duplicates(); operating_decisions_reconstruct_from_authoritative_state(); operating_rationing_reports_escalate_and_resolve(); active_disaster_precedes_the_support_decision(); if (failures == 0) { std::printf("notification finance: all checks passed\n"); return 0; } std::printf("notification finance: %d failure(s)\n", failures); return 1; }