// City Wire disaster lifecycle, ticker suppression, and critical-dialog priority. #include "sc2k/sim/disaster_types.hpp" #include "sc2k/ui/notification_projector.hpp" #include "sc2k/ui/notification_selection.hpp" #include #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) { return {{1400, sim::EventPhase::Disaster, 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 ui::NotificationKey disaster_key(std::uint64_t instanceId) { return {ui::NotificationKeyDomain::Disaster, instanceId}; } static ui::NotificationDraft draft(ui::NotificationCategory category, ui::NotificationLifecycle lifecycle, const char* title, bool autoOpen = false) { ui::NotificationDraft value; value.category = category; value.lifecycle = lifecycle; value.titleKey = title; value.descriptionKey = "citywire.test.description"; value.autoOpen = autoOpen; return value; } static void every_disaster_type_transitions_to_its_bounded_outcome() { static constexpr std::array kActiveSlugs = { "fire", "flood", "riots", "fire2", "aircrash", "quake", "tornado", "monster", "meltdown", "microwave", "volcano", "firestorm", "riots", "flood", "firestorm", "hurricane", "calamity", "aircrash"}; static constexpr std::array kOutcomeSlugs = { "fire", "flood", "riots", "fire", "air_crash", "earthquake", "tornado", "monster", "meltdown", "microwave", "volcano", "firestorm", "riots", "flood", "firestorm", "hurricane", "calamity", "air_crash"}; static constexpr std::array kExpiry = { 18, 18, 18, 18, 18, 18, 18, 18, 36, 18, 36, 24, 18, 18, 24, 24, 18, 18}; ui::NotificationStore store; std::array ids{}; for (std::size_t index = 0; index < kActiveSlugs.size(); ++index) { const std::uint8_t kind = static_cast(index + 1); const std::uint64_t instanceId = 100 + index; const int row = 10 + static_cast(index); const int column = 40 + static_cast(index); const sim::DomainEvent event = domain_event( sim::DisasterStarted{instanceId, kind, row, column}, static_cast(index)); CHECK(ui::project_notification_event(store, event, {10, "Disaster City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* notification = store.find(disaster_key(instanceId)); CHECK(notification); if (!notification) continue; ids[index] = notification->id; const std::string activeBase = std::string("citywire.disaster.") + kActiveSlugs[index] + ".active"; CHECK(notification->sourceEvent == event.stamp); CHECK(notification->titleKey == activeBase + ".title"); CHECK(notification->descriptionKey == activeBase + ".description"); CHECK(notification->category == ui::NotificationCategory::Emergency); CHECK(notification->lifecycle == ui::NotificationLifecycle::ConditionBound); CHECK(notification->expiryMonths == 0 && notification->autoOpen && !notification->autoOpened); CHECK(notification->actionCount == 1); CHECK(notification->actions[0].labelKey == "citywire.action.goto"); CHECK(notification->actions[0].command == ui::NotificationActionCommand::GoTo); CHECK(notification->actions[0].row == row && notification->actions[0].column == column); CHECK(notification->actions[0].closeDialog); CHECK(literal_parameter(*notification, "city") == "Disaster City"); CHECK(integer_parameter(*notification, "kind") == kind); CHECK(integer_parameter(*notification, "focus_row") == row); CHECK(integer_parameter(*notification, "focus_column") == column); CHECK(ui::notification_is_active_disaster(*notification)); CHECK(ui::notification_interrupts_existing_dialog(notification->category)); CHECK(ui::notification_detail_pause_policy() == ui::SimulationPausePolicy::PauseSimulation); } CHECK(store.size() == 18); store.prune(10000); CHECK(store.size() == 18); for (std::size_t index = 0; index < kOutcomeSlugs.size(); ++index) { const std::uint8_t kind = static_cast(index + 1); const std::uint64_t instanceId = 100 + index; const sim::DomainEvent event = domain_event( sim::DisasterEnded{instanceId, kind}, static_cast(index)); CHECK(ui::project_notification_event(store, event, {11, "Disaster City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* notification = store.find(disaster_key(instanceId)); CHECK(notification && notification->id == ids[index]); if (!notification) continue; const std::string outcomeBase = std::string("citywire.disaster.") + kOutcomeSlugs[index] + ".outcome"; CHECK(notification->sourceEvent == event.stamp); CHECK(notification->titleKey == outcomeBase + ".title"); CHECK(notification->descriptionKey == outcomeBase + ".description"); CHECK(notification->category == ui::NotificationCategory::Info); CHECK(notification->lifecycle == ui::NotificationLifecycle::Historical); CHECK(notification->createdMonth == 10 && notification->updatedMonth == 11); CHECK(notification->expiryMonths == kExpiry[index]); CHECK(!notification->autoOpen && notification->actionCount == 0); CHECK(literal_parameter(*notification, "city") == "Disaster City"); CHECK(integer_parameter(*notification, "kind") == kind); CHECK(!ui::notification_is_active_disaster(*notification)); } store.prune(28); CHECK(store.size() == 18); store.prune(29); CHECK(store.size() == 5); store.prune(35); CHECK(store.size() == 2); store.prune(47); CHECK(store.empty()); } static void focus_validation_omits_unavailable_actions_without_inventing_a_location() { ui::NotificationStore store; CHECK(ui::project_notification_event(store, domain_event(sim::DisasterStarted{1, sim::dtype::kFire, -1, -1}), {20, "No Focus"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* noFocus = store.find(disaster_key(1)); CHECK(noFocus && noFocus->actionCount == 0); CHECK(noFocus && integer_parameter(*noFocus, "focus_row") == -1); CHECK(noFocus && integer_parameter(*noFocus, "focus_column") == -1); const std::array invalid = { sim::DisasterStarted{2, sim::dtype::kFlood, -1, 10}, sim::DisasterStarted{3, sim::dtype::kFlood, 10, -1}, sim::DisasterStarted{4, sim::dtype::kFlood, -2, -2}}; for (const auto& event : invalid) CHECK(ui::project_notification_event(store, domain_event(event), {20, "No Focus"}) == ui::NotificationProjectionOutcome::Ignored); City city; CHECK(ui::project_notification_event(store, domain_event(sim::DisasterStarted{5, sim::dtype::kTornado, 500, 500}), {20, "No Focus", &city}) == ui::NotificationProjectionOutcome::Ignored); CHECK(ui::project_notification_event(store, domain_event(sim::DisasterStarted{6, sim::dtype::kTornado, 500, 500}), {20, "No Focus"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.find(disaster_key(6)) && store.find(disaster_key(6))->actions[0].row == 500); } static void identity_conflicts_duplicates_and_orphaned_ends_are_safe() { ui::NotificationStore store; const sim::DomainEvent started = domain_event( sim::DisasterStarted{50, sim::dtype::kFire, 20, 21}); CHECK(ui::project_notification_event(store, started, {30, "Identity City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::NotificationId id = store.find(disaster_key(50))->id; CHECK(ui::project_notification_event(store, started, {31, "Identity City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(ui::project_notification_event(store, domain_event(sim::DisasterStarted{50, sim::dtype::kFlood, 20, 21}), {31, "Identity City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(ui::project_notification_event(store, domain_event(sim::DisasterEnded{50, sim::dtype::kFlood}), {31, "Identity City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(store.find(disaster_key(50)) && store.find(disaster_key(50))->id == id); CHECK(store.find(disaster_key(50)) && ui::notification_is_active_disaster(*store.find(disaster_key(50)))); CHECK(ui::project_notification_event(store, domain_event(sim::DisasterEnded{50, sim::dtype::kFire}), {32, "Identity City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.find(disaster_key(50)) && store.find(disaster_key(50))->id == id); CHECK(store.find(disaster_key(50)) && store.find(disaster_key(50))->lifecycle == ui::NotificationLifecycle::Historical); CHECK(ui::project_notification_event(store, domain_event(sim::DisasterEnded{50, sim::dtype::kFire}), {33, "Identity City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(ui::project_notification_event(store, started, {33, "Identity City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(ui::project_notification_event(store, domain_event(sim::DisasterEnded{51, sim::dtype::kFlood}), {33, "Identity City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.find(disaster_key(51)) && store.find(disaster_key(51))->createdMonth == 33); CHECK(store.find(disaster_key(51)) && store.find(disaster_key(51))->titleKey == "citywire.disaster.flood.outcome.title"); const std::array invalidStarts = { sim::DisasterStarted{0, sim::dtype::kFire, 1, 1}, sim::DisasterStarted{60, 0, 1, 1}, sim::DisasterStarted{61, 19, 1, 1}}; for (const auto& event : invalidStarts) CHECK(ui::project_notification_event(store, domain_event(event), {33, "Identity City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(ui::project_notification_event(store, domain_event(sim::DisasterEnded{0, sim::dtype::kFire}), {33, "Identity City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(ui::project_notification_event(store, domain_event(sim::DisasterEnded{62, 19}), {33, "Identity City"}) == ui::NotificationProjectionOutcome::Ignored); } static void ticker_suppression_and_auto_open_priority_are_deterministic() { ui::NotificationStore store; const ui::NotificationId history = store.upsert( draft(ui::NotificationCategory::Info, ui::NotificationLifecycle::Historical, "history"), 40); ui::NotificationDraft dangerDraft = draft( ui::NotificationCategory::Danger, ui::NotificationLifecycle::ConditionBound, "danger", true); const ui::NotificationId danger = store.upsert(std::move(dangerDraft), 40); const ui::NotificationId reward = store.upsert( draft(ui::NotificationCategory::Reward, ui::NotificationLifecycle::RewardBound, "reward", true), 40); const ui::NotificationKey emergencyKey{ui::NotificationKeyDomain::Condition, 500}; ui::NotificationDraft emergencyDraft = draft( ui::NotificationCategory::Emergency, ui::NotificationLifecycle::ConditionBound, "emergency", true); emergencyDraft.stableKey = emergencyKey; const ui::NotificationId emergency = store.upsert(std::move(emergencyDraft), 40); CHECK(ui::project_notification_event(store, domain_event(sim::DisasterStarted{70, sim::dtype::kFire, 10, 10}, 0), {40, "Priority City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(ui::project_notification_event(store, domain_event(sim::DisasterStarted{71, sim::dtype::kFlood, 11, 11}, 1), {40, "Priority City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::NotificationId disasterOne = store.find(disaster_key(70))->id; const ui::NotificationId disasterTwo = store.find(disaster_key(71))->id; CHECK(ui::notification_ticker_candidates(store.records()) == (std::vector{disasterOne, disasterTwo})); CHECK(ui::next_auto_open_notification(store.records()) == disasterOne); CHECK(store.mark_auto_opened(disasterOne)); CHECK(ui::next_auto_open_notification(store.records()) == disasterTwo); CHECK(store.mark_auto_opened(disasterTwo)); CHECK(ui::next_auto_open_notification(store.records()) == emergency); CHECK(store.mark_auto_opened(emergency)); CHECK(ui::next_auto_open_notification(store.records()) == danger); CHECK(store.mark_auto_opened(danger)); CHECK(ui::next_auto_open_notification(store.records()) == reward); CHECK(store.mark_auto_opened(reward)); CHECK(!ui::next_auto_open_notification(store.records())); CHECK(ui::project_notification_event(store, domain_event(sim::DisasterEnded{70, sim::dtype::kFire}, 2), {41, "Priority City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(ui::notification_ticker_candidates(store.records()) == (std::vector{disasterTwo})); CHECK(ui::project_notification_event(store, domain_event(sim::DisasterEnded{71, sim::dtype::kFlood}, 3), {41, "Priority City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(ui::notification_ticker_candidates(store.records()) == (std::vector{emergency})); CHECK(store.resolve(emergencyKey)); CHECK(ui::notification_ticker_candidates(store.records()) == (std::vector{danger, reward, disasterTwo, disasterOne, history})); CHECK(ui::notification_interrupts_existing_dialog(ui::NotificationCategory::Emergency)); CHECK(ui::notification_interrupts_existing_dialog(ui::NotificationCategory::Danger)); CHECK(!ui::notification_interrupts_existing_dialog(ui::NotificationCategory::Warning)); CHECK(!ui::notification_interrupts_existing_dialog(ui::NotificationCategory::Reward)); CHECK(!ui::notification_interrupts_existing_dialog(ui::NotificationCategory::Info)); CHECK(ui::notification_detail_pause_policy() == ui::SimulationPausePolicy::PauseSimulation); } static void mixed_batch_preserves_disaster_transition_order() { ui::NotificationStore store; const std::vector events = { domain_event(sim::DisasterStarted{80, sim::dtype::kFire, 5, 6}, 0), domain_event(sim::DisasterStarted{81, sim::dtype::kFlood, 7, 8}, 1), domain_event(sim::DisasterEnded{80, sim::dtype::kFire}, 2)}; CHECK(ui::project_notification_batch(store, events, {50, "Batch City"}) == (ui::NotificationProjectionSummary{3, 0, 0})); CHECK(store.size() == 2); CHECK(store.records()[0].sourceEvent == events[2].stamp); CHECK(store.records()[0].category == ui::NotificationCategory::Info); CHECK(store.records()[1].sourceEvent == events[1].stamp); CHECK(store.records()[1].category == ui::NotificationCategory::Emergency); CHECK(ui::notification_ticker_candidates(store.records()) == (std::vector{store.records()[1].id})); } static void feed_order_and_urgent_filter_are_deterministic() { ui::NotificationStore store; const ui::NotificationId oldInfo = store.upsert( draft(ui::NotificationCategory::Info, ui::NotificationLifecycle::Historical, "old-info"), 10); const ui::NotificationId warning = store.upsert( draft(ui::NotificationCategory::Warning, ui::NotificationLifecycle::Historical, "warning"), 11); const ui::NotificationId comment = store.upsert( draft(ui::NotificationCategory::Comments, ui::NotificationLifecycle::Historical, "comment"), 12); ui::NotificationDraft protectedInfo = draft( ui::NotificationCategory::Info, ui::NotificationLifecycle::ConditionBound, "protected-info"); const ui::NotificationId condition = store.upsert(std::move(protectedInfo), 9); const ui::NotificationId danger = store.upsert( draft(ui::NotificationCategory::Danger, ui::NotificationLifecycle::Historical, "danger"), 8); const ui::NotificationId emergency = store.upsert( draft(ui::NotificationCategory::Emergency, ui::NotificationLifecycle::ConditionBound, "emergency"), 7); CHECK(ui::notification_feed_candidates(store.records()) == (std::vector{emergency, danger, warning, condition, comment, oldInfo})); CHECK(ui::notification_feed_candidates(store.records(), true) == (std::vector{emergency, danger, warning, condition})); } int main() { every_disaster_type_transitions_to_its_bounded_outcome(); focus_validation_omits_unavailable_actions_without_inventing_a_location(); identity_conflicts_duplicates_and_orphaned_ends_are_safe(); ticker_suppression_and_auto_open_priority_are_deterministic(); mixed_batch_preserves_disaster_transition_order(); feed_order_and_urgent_filter_are_deterministic(); if (failures == 0) { std::printf("notification disasters: all checks passed\n"); return 0; } std::printf("notification disasters: %d failure(s)\n", failures); return 1; }