// Ambient commentary persistence: provenance, bounded validation, corruption fallback, and replay. #include "sc2k/ui/commentary_persistence.hpp" #include #include #include #include #include #include "sc2k/io/wire.hpp" #include "test_harness.hpp" using namespace sc2k; static ui::CommentaryState representative_state() { ui::CommentaryState state = ui::make_commentary_state( ui::CommentaryTrackingProvenance::FromCreation, 100, 0x123456789ABCDEFull); state.emissionSequence = 8; state.lastEvaluatedMonth = 118; state.lastEmittedMonth = 117; state.nextAmbientMonth = 121; state.lastTopicEmissionMonth[static_cast(ui::CommentTopic::General)] = 117; state.lastTopicEmissionMonth[static_cast(ui::CommentTopic::Disaster)] = 110; state.ruleCooldowns = {{{30}, 105}, {{10}, 117}}; state.pendingCues = { {{70}, 112, 118, 122, 3}, {{20}, 115, 115, 120, 1}, }; state.streaks = { {{50}, 116, 120, 5}, {{10}, 119, 120, 2}, }; state.disasterHistory[static_cast(ui::CommentDisasterKind::Earthquake)] = { ui::CommentHistoryKnowledge::Occurred, 2, 110}; return state; } static ui::CommentRuleDefinition rule(std::uint16_t id, std::uint16_t weight) { ui::CommentRuleDefinition definition; definition.id = {id}; definition.weight = weight; definition.variantCount = 3; return definition; } static void state_round_trips_in_canonical_order() { const ui::CommentaryState source = representative_state(); CHECK(ui::validate_commentary_state(source, 120) == ui::CommentaryStateIssue::None); const std::vector bytes = ui::encode_commentary_state(source, 120); CHECK(source.citySalt == 7884945696333623821ull); CHECK(io::crc32c(bytes) == 3520154299u); CHECK(bytes.size() == 292); CHECK(bytes.size() <= ui::kMaximumCommentaryMemoryBytes); CHECK(bytes[0] == 'C' && bytes[1] == 'M' && bytes[2] == 'T' && bytes[3] == '1'); const ui::CommentaryRestoreResult restored = ui::restore_commentary_state(bytes, 120, 999); CHECK(restored.status == ui::CommentaryRestoreStatus::Restored); CHECK(restored.issue == ui::CommentaryStateIssue::None); CHECK(restored.state.citySalt == source.citySalt); CHECK(restored.state.trackingProvenance == ui::CommentaryTrackingProvenance::FromCreation); CHECK(restored.state.ruleCooldowns[0].id.value == 10); CHECK(restored.state.ruleCooldowns[1].id.value == 30); CHECK(restored.state.pendingCues[0].id.value == 20); CHECK(restored.state.pendingCues[1].id.value == 70); CHECK(restored.state.streaks[0].id.value == 10); CHECK(restored.state.streaks[1].id.value == 50); CHECK(restored.state.disasterHistory[static_cast( ui::CommentDisasterKind::Earthquake)] == (ui::CommentOccurrenceHistory{ui::CommentHistoryKnowledge::Occurred, 2, 110})); const std::vector reencoded = ui::encode_commentary_state(restored.state, 120); CHECK(reencoded == bytes); } static void historical_provenance_starts_conservatively() { const ui::CommentaryState fresh = ui::make_commentary_state( ui::CommentaryTrackingProvenance::FromCreation, 0, 0); CHECK(fresh.citySalt != 0 && fresh.trackingStartMonth == 0 && fresh.nextAmbientMonth == 2); CHECK(std::all_of(fresh.disasterHistory.begin(), fresh.disasterHistory.end(), [](const ui::CommentOccurrenceHistory& history) { return history.knowledge == ui::CommentHistoryKnowledge::KnownNever && history.occurrenceCount == 0 && history.lastOccurrenceMonth == ui::kCommentMonthUnset; })); const ui::CommentaryState imported = ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, 45, 0); CHECK(imported.citySalt != 0 && imported.trackingStartMonth == 45 && imported.nextAmbientMonth == 47); CHECK(std::all_of(imported.disasterHistory.begin(), imported.disasterHistory.end(), [](const ui::CommentOccurrenceHistory& history) { return history.knowledge == ui::CommentHistoryKnowledge::Unknown; })); CHECK(ui::validate_commentary_state(fresh, 0) == ui::CommentaryStateIssue::None); CHECK(ui::validate_commentary_state(imported, 45) == ui::CommentaryStateIssue::None); const ui::CommentaryState boundary = ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, std::numeric_limits::max(), 1); CHECK(boundary.trackingStartMonth == std::numeric_limits::max()); CHECK(boundary.nextAmbientMonth == std::numeric_limits::max()); CHECK(ui::validate_commentary_state(boundary, std::numeric_limits::max()) == ui::CommentaryStateIssue::None); } static void missing_and_corrupt_payloads_fail_soft_to_unknown() { const std::array identity{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; const std::uint64_t fallbackSeed = ui::commentary_salt_from_bytes(identity); const ui::CommentaryRestoreResult missing = ui::restore_commentary_state({}, 120, fallbackSeed); CHECK(missing.status == ui::CommentaryRestoreStatus::Missing); CHECK(missing.state.trackingProvenance == ui::CommentaryTrackingProvenance::Unknown); CHECK(missing.state.trackingStartMonth == 120); CHECK(missing.state.citySalt != 0); const std::vector valid = ui::encode_commentary_state(representative_state(), 120); for (std::size_t size = 1; size < valid.size(); ++size) { const auto truncated = ui::restore_commentary_state( std::span(valid).first(size), 120, fallbackSeed); CHECK(truncated.status == ui::CommentaryRestoreStatus::Discarded); CHECK(truncated.state.trackingProvenance == ui::CommentaryTrackingProvenance::Unknown); } std::vector future = valid; future[3] = '2'; CHECK(ui::restore_commentary_state(future, 120, fallbackSeed).status == ui::CommentaryRestoreStatus::Discarded); std::vector trailing = valid; trailing.push_back(0); CHECK(ui::restore_commentary_state(trailing, 120, fallbackSeed).status == ui::CommentaryRestoreStatus::Discarded); std::vector zeroSalt = valid; std::fill(zeroSalt.begin() + 4, zeroSalt.begin() + 12, 0); const auto invalidSalt = ui::restore_commentary_state(zeroSalt, 120, fallbackSeed); CHECK(invalidSalt.status == ui::CommentaryRestoreStatus::Discarded); CHECK(invalidSalt.issue == ui::CommentaryStateIssue::MissingSalt); std::vector badProvenance = valid; badProvenance[20] = 9; const auto invalidProvenance = ui::restore_commentary_state(badProvenance, 120, fallbackSeed); CHECK(invalidProvenance.status == ui::CommentaryRestoreStatus::Discarded); CHECK(invalidProvenance.issue == ui::CommentaryStateIssue::InvalidProvenance); } static void invalid_state_is_omitted_instead_of_poisoning_a_save() { ui::CommentaryState state = representative_state(); state.ruleCooldowns.push_back(state.ruleCooldowns.front()); CHECK(ui::validate_commentary_state(state, 120) == ui::CommentaryStateIssue::DuplicateRuleCooldown); CHECK(ui::encode_commentary_state(state, 120).empty()); state = representative_state(); state.pendingCues[0].expiryMonth = 117; CHECK(ui::validate_commentary_state(state, 120) == ui::CommentaryStateIssue::InvalidPendingCue); CHECK(ui::encode_commentary_state(state, 120).empty()); state = representative_state(); state.streaks[0].lengthMonths = 4; CHECK(ui::validate_commentary_state(state, 120) == ui::CommentaryStateIssue::InvalidStreak); state = representative_state(); state.disasterHistory[0] = { ui::CommentHistoryKnowledge::Unknown, 0, ui::kCommentMonthUnset}; CHECK(ui::validate_commentary_state(state, 120) == ui::CommentaryStateIssue::InvalidDisasterHistory); state = ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, 100, 1); state.disasterHistory[0] = { ui::CommentHistoryKnowledge::KnownNever, 0, ui::kCommentMonthUnset}; CHECK(ui::validate_commentary_state(state, 120) == ui::CommentaryStateIssue::InvalidDisasterHistory); } static void save_reload_preserves_the_next_decision() { ui::CommentaryState uninterrupted = representative_state(); uninterrupted.lastEvaluatedMonth = 118; uninterrupted.lastEmittedMonth = 117; uninterrupted.nextAmbientMonth = 121; const std::array rules{rule(1, 1), rule(2, 3), rule(3, 2)}; ui::CommentaryContext context; context.monthIndex = 121; const ui::CommentaryDecision expected = ui::evaluate_commentary(uninterrupted, context, rules); const std::vector bytes = ui::encode_commentary_state(uninterrupted, 120); const auto restored = ui::restore_commentary_state(bytes, 120, 99); CHECK(restored.status == ui::CommentaryRestoreStatus::Restored); const ui::CommentaryDecision actual = ui::evaluate_commentary(restored.state, context, rules); CHECK(actual == expected); } static void unknown_stable_ids_are_harmless_and_round_trip() { ui::CommentaryState state = ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, 10, 5); state.ruleCooldowns.push_back({{65000}, 10}); state.pendingCues.push_back({{65000}, 10, 10, 12, 1}); state.streaks.push_back({{65000}, 10, 10, 1}); const auto bytes = ui::encode_commentary_state(state, 10); CHECK(!bytes.empty()); const auto restored = ui::restore_commentary_state(bytes, 10, 7); CHECK(restored.status == ui::CommentaryRestoreStatus::Restored); CHECK(restored.state.ruleCooldowns[0].id.value == 65000); const std::array rules{rule(1, 1)}; ui::CommentaryContext context; context.monthIndex = 12; CHECK(ui::evaluate_commentary(restored.state, context, rules).selection.has_value()); } int main() { state_round_trips_in_canonical_order(); historical_provenance_starts_conservatively(); missing_and_corrupt_payloads_fail_soft_to_unknown(); invalid_state_is_omitted_instead_of_poisoning_a_save(); save_reload_preserves_the_next_decision(); unknown_stable_ids_are_harmless_and_round_trip(); if (failures == 0) { std::printf("commentary persistence: all checks passed\n"); return 0; } std::printf("commentary persistence: %d failure(s)\n", failures); return 1; }