Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177// City Wire notification store: identities, lifecycle, coalescing, expiry, and retention caps.#include "sc2k/ui/notification_store.hpp"#include <cstdio>
#include "test_harness.hpp"
using namespace sc2k;
static ui::NotificationDraft draft(ui::NotificationCategory category, ui::NotificationLifecycle lifecycle, const char* title) { ui::NotificationDraft value; value.category = category; value.lifecycle = lifecycle; value.titleKey = title; value.descriptionKey = "citywire.test.description"; return value;}
static void stable_keys_upsert_and_resolve_without_reusing_identity() { ui::NotificationStore store; const ui::NotificationKey blackout{ui::NotificationKeyDomain::Condition, 1}; ui::NotificationDraft first = draft(ui::NotificationCategory::Warning, ui::NotificationLifecycle::ConditionBound, "citywire.blackout.title"); first.stableKey = blackout; first.parameters.push_back({"affected", std::int64_t{12}}); first.actions[0] = {"citywire.goto", ui::NotificationActionTone::Caution, ui::NotificationActionCommand::GoTo, 0, 10, 20, true}; first.actionCount = 1; const ui::NotificationId id = store.upsert(std::move(first), 100); CHECK(id.value == 1 && store.size() == 1); CHECK(store.find(id) && store.find(id)->createdMonth == 100 && store.find(id)->updatedMonth == 100); CHECK(store.acknowledge(id)); CHECK(!store.rearm_auto_open(id)); CHECK(store.mark_auto_opened(id)); CHECK(!store.rearm_auto_open(id));
ui::NotificationDraft update = draft(ui::NotificationCategory::Danger, ui::NotificationLifecycle::ConditionBound, "citywire.blackout.severe.title"); update.stableKey = blackout; update.parameters.push_back({"affected", std::int64_t{60}}); update.actionCount = 9; // malformed external data is deterministically bounded to the fixed array const ui::NotificationId updated = store.upsert(std::move(update), 101); CHECK(updated == id && store.size() == 1); const ui::Notification* item = store.find(blackout); CHECK(item && item->createdMonth == 100 && item->updatedMonth == 101); CHECK(item && item->category == ui::NotificationCategory::Danger && item->unread); CHECK(item && item->autoOpened && item->actionCount == ui::kMaxNotificationActions);
CHECK(store.resolve(blackout)); CHECK(!store.resolve(blackout) && store.empty()); const ui::NotificationId next = store.upsert( draft(ui::NotificationCategory::Info, ui::NotificationLifecycle::Historical, "citywire.next"), 102); CHECK(next.value == 2);}
static void mandatory_auto_open_can_be_rearmed_after_a_child_workflow() { ui::NotificationStore store; ui::NotificationDraft decision = draft(ui::NotificationCategory::Emergency, ui::NotificationLifecycle::DecisionBound, "decision"); decision.autoOpen = true; const ui::NotificationId id = store.upsert(std::move(decision), 10); CHECK(!store.rearm_auto_open(id)); CHECK(store.mark_auto_opened(id)); CHECK(store.rearm_auto_open(id)); CHECK(store.find(id) && !store.find(id)->autoOpened); CHECK(!store.rearm_auto_open(id)); CHECK(store.mark_auto_opened(id));}
static void key_domains_are_distinct_and_unkeyed_history_does_not_coalesce() { ui::NotificationStore store; ui::NotificationDraft condition = draft(ui::NotificationCategory::Warning, ui::NotificationLifecycle::ConditionBound, "condition"); condition.stableKey = ui::NotificationKey{ui::NotificationKeyDomain::Condition, 4}; ui::NotificationDraft reward = draft(ui::NotificationCategory::Reward, ui::NotificationLifecycle::RewardBound, "reward"); reward.stableKey = ui::NotificationKey{ui::NotificationKeyDomain::Reward, 4}; store.upsert(std::move(condition), 1); store.upsert(std::move(reward), 1); store.upsert(draft(ui::NotificationCategory::Info, ui::NotificationLifecycle::Historical, "history"), 1); store.upsert(draft(ui::NotificationCategory::Info, ui::NotificationLifecycle::Historical, "history"), 1); CHECK(store.size() == 4);}
static void expiry_uses_simulation_months_and_never_removes_protected_items() { ui::NotificationStore store; ui::NotificationDraft comment = draft(ui::NotificationCategory::Comments, ui::NotificationLifecycle::Historical, "comment"); const ui::NotificationKey commentKey{ui::NotificationKeyDomain::Singleton, 20}; comment.stableKey = commentKey; comment.expiryMonths = 3; const ui::NotificationId historical = store.upsert(std::move(comment), 20); ui::NotificationDraft refreshed = draft(ui::NotificationCategory::Comments, ui::NotificationLifecycle::Historical, "comment-updated"); refreshed.stableKey = commentKey; refreshed.expiryMonths = 3; CHECK(store.upsert(std::move(refreshed), 21) == historical);
ui::NotificationDraft decision = draft(ui::NotificationCategory::Danger, ui::NotificationLifecycle::DecisionBound, "decision"); decision.stableKey = ui::NotificationKey{ui::NotificationKeyDomain::Decision, 9}; decision.expiryMonths = 1; const ui::NotificationId protectedId = store.upsert(std::move(decision), 20);
store.prune(23); CHECK(store.find(historical) && store.find(protectedId)); store.prune(24); CHECK(!store.find(historical) && store.find(protectedId));}
static void comments_and_history_caps_use_deterministic_eviction_priority() { ui::NotificationStore comments; ui::NotificationId firstComment; for (int index = 0; index < 7; ++index) { const ui::NotificationId id = comments.upsert(draft(ui::NotificationCategory::Comments, ui::NotificationLifecycle::Historical, "comment"), 0); if (index == 0) firstComment = id; } CHECK(comments.size() == ui::NotificationStore::kCommentsCap); CHECK(!comments.find(firstComment));
ui::NotificationStore history; const ui::NotificationId oldestInfo = history.upsert(draft(ui::NotificationCategory::Info, ui::NotificationLifecycle::Historical, "old-info"), 0); const ui::NotificationId expendableComment = history.upsert(draft(ui::NotificationCategory::Comments, ui::NotificationLifecycle::Historical, "comment"), 1); for (int index = 0; index < 23; ++index) history.upsert(draft(ui::NotificationCategory::Info, ui::NotificationLifecycle::Historical, "info"), index + 2); CHECK(history.size() == ui::NotificationStore::kHistoricalSoftCap); CHECK(history.find(oldestInfo)); CHECK(!history.find(expendableComment));}
static void protected_items_sit_outside_the_history_cap() { ui::NotificationStore store; for (std::uint64_t index = 0; index < 30; ++index) { ui::NotificationDraft item = draft(ui::NotificationCategory::Reward, ui::NotificationLifecycle::RewardBound, "reward"); item.stableKey = ui::NotificationKey{ui::NotificationKeyDomain::Reward, index}; store.upsert(std::move(item), static_cast<int>(index)); } for (int index = 0; index < 30; ++index) store.upsert(draft(ui::NotificationCategory::Info, ui::NotificationLifecycle::Historical, "info"), index); CHECK(store.size() == 30 + ui::NotificationStore::kHistoricalSoftCap); for (std::uint64_t index = 0; index < 30; ++index) CHECK(store.find(ui::NotificationKey{ui::NotificationKeyDomain::Reward, index}));
store.reset(); CHECK(store.empty()); CHECK(store.upsert(draft(ui::NotificationCategory::Info, ui::NotificationLifecycle::Historical, "reset"), 0) .value == 1);}
int main() { stable_keys_upsert_and_resolve_without_reusing_identity(); mandatory_auto_open_can_be_rearmed_after_a_child_workflow(); key_domains_are_distinct_and_unkeyed_history_does_not_coalesce(); expiry_uses_simulation_months_and_never_removes_protected_items(); comments_and_history_caps_use_deterministic_eviction_priority(); protected_items_sit_outside_the_history_cap(); if (failures == 0) { std::printf("notification store: all checks passed\n"); return 0; } std::printf("notification store: %d failure(s)\n", failures); return 1;}