From 7c7e20365e6ea5a4b5723cc3f343446df54a6a3b Mon Sep 17 00:00:00 2001 From: Daniel Pechersky Date: Thu, 18 Sep 2025 21:44:54 +0700 Subject: [PATCH] Snapshot test update_memory_state --- Cargo.lock | 36 ++++++++++++ Cargo.toml | 1 + rslib/.gitignore | 1 + rslib/Cargo.toml | 2 + rslib/src/scheduler/fsrs/memory_state.rs | 72 ++++++++++++++++++++++++ 5 files changed, 112 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index fe88eb3ab..499d9a5b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -117,6 +117,7 @@ dependencies = [ "hyper 1.7.0", "id_tree", "inflections", + "insta", "itertools 0.14.0", "nom 8.0.0", "num_cpus", @@ -1192,6 +1193,18 @@ dependencies = [ "ninja_gen", ] +[[package]] +name = "console" +version = "0.15.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "054ccb5b10f9f2cbf51eb355ca1d05c2d279ce1804688d0db74b4733a5aeafd8" +dependencies = [ + "encode_unicode", + "libc", + "once_cell", + "windows-sys 0.59.0", +] + [[package]] name = "constant_time_eq" version = "0.3.1" @@ -1965,6 +1978,12 @@ dependencies = [ "winreg 0.55.0", ] +[[package]] +name = "encode_unicode" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" + [[package]] name = "encoding_rs" version = "0.8.35" @@ -3369,6 +3388,17 @@ dependencies = [ "libc", ] +[[package]] +name = "insta" +version = "1.43.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46fdb647ebde000f43b5b53f773c30cf9b0cb4300453208713fa38b2c70935a0" +dependencies = [ + "console", + "once_cell", + "similar", +] + [[package]] name = "intl-memoizer" version = "0.5.3" @@ -5948,6 +5978,12 @@ version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" +[[package]] +name = "similar" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa" + [[package]] name = "siphasher" version = "0.3.11" diff --git a/Cargo.toml b/Cargo.toml index 2e9489cb8..04cfcb4f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -87,6 +87,7 @@ htmlescape = "0.3.1" hyper = "1" id_tree = "1.8.0" inflections = "1.1.1" +insta = "1.43.2" intl-memoizer = "0.5.3" itertools = "0.14.0" junction = "1.2.0" diff --git a/rslib/.gitignore b/rslib/.gitignore index 51613a3d0..981c4b7da 100644 --- a/rslib/.gitignore +++ b/rslib/.gitignore @@ -1,3 +1,4 @@ Cargo.lock .build target +tests/support/large diff --git a/rslib/Cargo.toml b/rslib/Cargo.toml index 9be9e8d87..c3143a4ab 100644 --- a/rslib/Cargo.toml +++ b/rslib/Cargo.toml @@ -35,6 +35,8 @@ syn.workspace = true async-stream.workspace = true reqwest = { workspace = true, features = ["native-tls"] } wiremock.workspace = true +tempfile.workspace = true +insta.workspace = true [dependencies] criterion = { workspace = true, optional = true } diff --git a/rslib/src/scheduler/fsrs/memory_state.rs b/rslib/src/scheduler/fsrs/memory_state.rs index 420ead5a3..39ca9cc8d 100644 --- a/rslib/src/scheduler/fsrs/memory_state.rs +++ b/rslib/src/scheduler/fsrs/memory_state.rs @@ -504,4 +504,76 @@ mod tests { ); Ok(()) } + + mod update_memory_state { + use std::{fs::File, io::BufReader}; + + use tempfile::{NamedTempFile, TempPath}; + use zstd::stream::copy_decode; + + use crate::{browser_table::Column, collection::CollectionBuilder, search::SortMode}; + + use super::*; + + const TEST_COLLECTION_PATH: &str = "tests/support/large/collection.anki21b"; + + fn assert_file_size_hasnt_changed() { + insta::assert_snapshot!(std::fs::metadata(TEST_COLLECTION_PATH).unwrap().len(), @"50563219"); + } + + fn open_temp_collection() -> (Collection, TempPath) { + assert_file_size_hasnt_changed(); + + let mut test_collection = BufReader::new(File::open(TEST_COLLECTION_PATH).unwrap()); + let mut temp_file = NamedTempFile::new().unwrap(); + copy_decode(&mut test_collection, temp_file.as_file_mut()).unwrap(); + let temp_path = temp_file.into_temp_path(); + let collection = CollectionBuilder::new(temp_path.to_path_buf()) + .build() + .unwrap(); + (collection, temp_path) + } + + pub static PARAMS: [f32; 21] = [ + 0.212, 1.2931, 2.3065, 8.2956, 6.4133, 0.8334, 3.0194, 0.001, 1.8722, 0.1666, 0.796, + 1.4835, 0.0614, 0.2629, 1.6483, 0.6014, 1.8729, 0.5425, 0.0912, 0.0658, 0.1542, + ]; + + #[test] + #[ignore = "the test collection isn't yet automatically downloaded."] + fn snapshot_req() { + let (mut collection, _temp) = open_temp_collection(); + + let deck_name = "Main::English"; + let deck_id = collection.get_deck_id(deck_name).unwrap().unwrap(); + let search = SearchNode::DeckIdWithChildren(deck_id); + + collection + .update_memory_state(vec![UpdateMemoryStateEntry { + req: Some(UpdateMemoryStateRequest { + params: PARAMS.to_vec(), + preset_desired_retention: 0.9, + historical_retention: 0.9, + max_interval: 36_500, + reschedule: true, + deck_desired_retention: [(deck_id, 0.9)].into_iter().collect(), + }), + search: search.clone(), + ignore_before: TimestampMillis(0), + }]) + .unwrap(); + + let cards_in_deck = collection + .all_cards_for_search_in_order( + search, + SortMode::Builtin { + column: Column::SortField, + reverse: false, + }, + ) + .unwrap(); + + insta::assert_debug_snapshot!(cards_in_deck); + } + } }