//! The port of the pure half of //! `apps/cotton/__tests__/replica/snapshotStream.test.ts`. use super::*; #[test] fn a_promise_is_digits_and_nothing_else() { assert_eq!(parse_expected_size("12"), Some(12)); assert_eq!(parse_expected_size(" 12 \n"), Some(12)); assert_eq!(parse_expected_size("007"), Some(7)); assert_eq!(parse_expected_size("0"), Some(0)); for refused in [ "", " ", "-1", "+1", "1.0", "1e3", "0x10", "12 bytes", "twelve", ] { assert_eq!(parse_expected_size(refused), None, "{refused:?}"); } } #[test] fn a_promise_bigger_than_a_javascript_integer_is_not_a_promise() { // The sidecar is a file both clients may have written, and a number the // TypeScript could not hold is one the two would disagree about. assert_eq!(parse_expected_size("9007199254740991"), Some(MAX_SAFE_SIZE)); assert_eq!(parse_expected_size("9007199254740992"), None); assert_eq!(parse_expected_size(&"9".repeat(40)), None); } #[test] fn a_download_is_complete_only_on_an_exact_match_of_a_non_zero_promise() { assert!(is_complete_download(100, Some(100))); assert!(!is_complete_download(100, None), "no promise, no adoption"); assert!( !is_complete_download(0, Some(0)), "a zero-byte whole file is not one" ); assert!(!is_complete_download(99, Some(100))); assert!( !is_complete_download(101, Some(100)), "longer than promised is not complete either" ); } #[test] fn the_temp_file_survives_a_launch_that_did_not_finish() { let names = vec![ "replica.db.download".to_string(), "replica.db.download.size".to_string(), ]; assert!( stale_downloads(&names, "replica.db", false).is_empty(), "the real file is not there, so this pair is the resume" ); assert_eq!( stale_downloads(&names, "replica.db", true), names, "the real file arrived, so the pair is rubbish" ); } #[test] fn the_gzip_being_resumed_survives_until_the_real_file_arrives() { let names = vec![ "replica.db.download.gz".to_string(), "replica.db.download.copy".to_string(), ]; assert!( stale_downloads(&names, "replica.db", false).is_empty(), "the real file is not there, so the gzip and its copy are the resume" ); assert_eq!(stale_downloads(&names, "replica.db", true), names); } #[test] fn a_pre_fix_random_suffix_is_always_rubbish() { let names = vec![ "replica.db.download-a1b2c3".to_string(), "replica.db.download-ZZZ".to_string(), ]; // Both, whether or not the live file exists: nothing can work out which // attempt a random name belonged to, so it can never be resumed. assert_eq!(stale_downloads(&names, "replica.db", false), names); assert_eq!(stale_downloads(&names, "replica.db", true), names); } #[test] fn nothing_else_in_the_directory_is_touched() { let names = vec![ "replica.db".to_string(), "replica.db-wal".to_string(), "replica.db-shm".to_string(), "notes.txt".to_string(), ]; assert!(stale_downloads(&names, "replica.db", true).is_empty()); } #[test] fn the_two_names_hang_off_the_replicas_own_path() { let replica = std::path::Path::new("/data/client/ws-1/replica.db"); assert_eq!( download_path(replica), std::path::PathBuf::from("/data/client/ws-1/replica.db.download") ); assert_eq!( expected_size_path(replica), std::path::PathBuf::from("/data/client/ws-1/replica.db.download.size") ); }