//! Official JP synchronization planning. use bat_adapters::official::yostar_jp::{ verified_official_platforms, PatchPlatform, YostarJpResourceDiscoveryPlan, YostarJpResourceEndpointChange, YostarJpSyncDelta, YostarJpSyncSnapshot, }; /// High-level official update decision. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum OfficialSyncDecision { /// No material change was detected. UpToDate, /// Official state changed and content should be downloaded and verified. DownloadAndVerify, /// Official state changed enough that the published client snapshot must be updated. DownloadVerifyAndPublish, } /// Comparison result used by schedulers and manual actions. #[derive(Debug, Clone, PartialEq, Eq)] pub struct OfficialSyncPlan { /// Observed official snapshot. pub snapshot: YostarJpSyncSnapshot, /// Difference from the previous stored snapshot. pub delta: YostarJpSyncDelta, /// Whether the sync should be executed now. pub decision: OfficialSyncDecision, } impl OfficialSyncPlan { /// Returns true when a download should start. pub fn should_download(&self) -> bool { matches!( self.decision, OfficialSyncDecision::DownloadAndVerify | OfficialSyncDecision::DownloadVerifyAndPublish ) } /// Returns true when the result may be pushed to clients after verification. pub fn should_publish(&self) -> bool { matches!( self.decision, OfficialSyncDecision::DownloadVerifyAndPublish ) } } /// Builds a sync plan from an observed snapshot and an optional previous snapshot. pub fn build_official_sync_plan( snapshot: YostarJpSyncSnapshot, previous: Option<&YostarJpSyncSnapshot>, ) -> OfficialSyncPlan { let delta = snapshot.diff(previous); let decision = classify_sync_decision(&delta); OfficialSyncPlan { snapshot, delta, decision, } } /// Classifies the sync decision from a snapshot delta. pub fn classify_sync_decision(delta: &YostarJpSyncDelta) -> OfficialSyncDecision { if delta.is_initial { return OfficialSyncDecision::DownloadVerifyAndPublish; } if !delta.has_changes() { return OfficialSyncDecision::UpToDate; } if delta.addressables_root_changed || delta.root_token_changed || !delta.endpoint_changes.is_empty() { return OfficialSyncDecision::DownloadVerifyAndPublish; } if delta.bundle_version_changed { return OfficialSyncDecision::DownloadAndVerify; } OfficialSyncDecision::UpToDate } /// Returns the ordered platform set used by the official JP downloader. pub fn default_official_platforms() -> Vec { verified_official_platforms() } /// Returns all endpoints that should be fetched before any patch publish. pub fn stable_discovery_endpoints(plan: &YostarJpResourceDiscoveryPlan) -> Vec { plan.endpoints .iter() .map(|endpoint| endpoint.url.clone()) .collect() } /// Returns endpoint URLs that changed compared with a previous sync snapshot. pub fn changed_endpoint_urls(delta: &YostarJpSyncDelta) -> Vec { delta .endpoint_changes .iter() .map(|change| match change { YostarJpResourceEndpointChange::Added(endpoint) => endpoint.url.clone(), YostarJpResourceEndpointChange::Removed(endpoint) => endpoint.url.clone(), YostarJpResourceEndpointChange::Updated { current, .. } => current.url.clone(), }) .collect() } #[cfg(test)] mod tests { use super::*; use bat_adapters::official::yostar_jp::{ YostarJpResourceDiscoveryPlan, YostarJpResourceEndpoint, YostarJpResourceEndpointKind, }; fn snapshot() -> YostarJpSyncSnapshot { YostarJpSyncSnapshot { connection_group_name: "Prod-Audit".to_string(), app_version: "1.70.0".to_string(), bundle_version: Some("s8tloc7lo3".to_string()), addressables_root: "https://prod-clientpatch.bluearchiveyostar.com/r93_token".to_string(), endpoints: vec![ YostarJpResourceEndpoint { kind: YostarJpResourceEndpointKind::TableCatalog, platform: None, url: "https://prod-clientpatch.bluearchiveyostar.com/r93_token/TableBundles/TableCatalog.bytes".to_string(), }, YostarJpResourceEndpoint { kind: YostarJpResourceEndpointKind::AddressablesCatalog, platform: Some(PatchPlatform::Windows), url: "https://prod-clientpatch.bluearchiveyostar.com/r93_token/Windows_PatchPack/catalog_StandaloneWindows64.zip".to_string(), }, ], } } fn discovery_plan() -> YostarJpResourceDiscoveryPlan { YostarJpResourceDiscoveryPlan { connection_group_name: "Prod-Audit".to_string(), app_version: "1.70.0".to_string(), bundle_version: Some("s8tloc7lo3".to_string()), addressables_root: "https://prod-clientpatch.bluearchiveyostar.com/r93_token" .to_string(), endpoints: snapshot().endpoints, } } #[test] fn classifies_initial_snapshot_as_publishable() { let plan = build_official_sync_plan(snapshot(), None); assert!(plan.delta.is_initial); assert_eq!( plan.decision, OfficialSyncDecision::DownloadVerifyAndPublish ); assert!(plan.should_download()); assert!(plan.should_publish()); } #[test] fn classifies_identical_snapshot_as_up_to_date() { let current = snapshot(); let plan = build_official_sync_plan(current.clone(), Some(¤t)); assert_eq!(plan.decision, OfficialSyncDecision::UpToDate); assert!(!plan.should_download()); assert!(!plan.should_publish()); } #[test] fn classifies_bundle_version_only_change_as_download_only() { let current = snapshot(); let previous = YostarJpSyncSnapshot { bundle_version: Some("older".to_string()), ..current.clone() }; let delta = current.diff(Some(&previous)); assert!(delta.bundle_version_changed); assert_eq!( classify_sync_decision(&delta), OfficialSyncDecision::DownloadAndVerify ); } #[test] fn exposes_changed_endpoint_urls() { let mut current = snapshot(); current.endpoints[0].url = "https://prod-clientpatch.bluearchiveyostar.com/r93_token/TableBundles/TableCatalog.bytes?v=2".to_string(); let delta = current.diff(Some(&snapshot())); let urls = changed_endpoint_urls(&delta); assert_eq!(urls.len(), 1); assert!(urls[0].contains("v=2")); } #[test] fn keeps_discovery_endpoint_urls_stable() { let plan = discovery_plan(); assert_eq!(stable_discovery_endpoints(&plan).len(), 2); } }