122 lines
4.8 KiB
Rust
122 lines
4.8 KiB
Rust
//! Mandatory tests 3 and 4: medals recomputed give byte-identical results; an imported Goodreads history earns
|
|
//! book-count medals and no day medals; the reminder never fires when it is off or the goal is met.
|
|
|
|
mod common;
|
|
use common::*;
|
|
use liber_habit::{Reminder, medals, next_reminder, unseen};
|
|
use liber_library::Op;
|
|
use liber_library::library::{Field, Outcome};
|
|
use serde_json::json;
|
|
|
|
fn earned(lib: &liber_library::Library) -> Vec<(String, String)> {
|
|
medals(lib).into_iter().filter_map(|m| m.earned.map(|e| (m.id.to_owned(), e))).collect()
|
|
}
|
|
|
|
#[test]
|
|
fn recomputing_gives_byte_identical_medals() {
|
|
let mut log = Log::new();
|
|
let m = monday();
|
|
for d in 0..40 {
|
|
log.read_day(m.plus(d));
|
|
}
|
|
log.push(Op::ReadStart { book: "b".into(), read: "r1".into(), date: Some("2026-03-02".into()) });
|
|
log.push(Op::ReadEnd { read: "r1".into(), date: Some("2026-03-20".into()), outcome: Outcome::Read });
|
|
let a = serde_json::to_vec(&medals(&log.lib())).unwrap();
|
|
let b = serde_json::to_vec(&medals(&log.lib())).unwrap();
|
|
assert_eq!(a, b);
|
|
// Rebuilt from the same events in another order: the same bytes.
|
|
let mut rev = log.events.clone();
|
|
rev.reverse();
|
|
assert_eq!(serde_json::to_vec(&medals(&liber_library::Library::from_events(&rev))).unwrap(), a);
|
|
let e = earned(&log.lib());
|
|
assert!(e.contains(&("days-7".into(), m.plus(6).date())));
|
|
assert!(e.contains(&("days-30".into(), m.plus(29).date())));
|
|
assert!(e.contains(&("first-book".into(), "2026-03-20".into())));
|
|
assert!(!e.iter().any(|(id, _)| id == "days-100"));
|
|
}
|
|
|
|
#[test]
|
|
fn an_imported_history_earns_book_medals_and_no_day_medals() {
|
|
// Goodreads: twelve books read, one of them twice, one abandoned; no reading sessions at all.
|
|
let mut csv =
|
|
String::from("Book Id,Title,Author,ISBN,ISBN13,My Rating,Date Read,Date Added,Exclusive Shelf,Read Count\n");
|
|
for i in 0..12 {
|
|
csv.push_str(&format!(
|
|
"{i},Kniha {i},Autor {i},,,4,2024/0{}/1{i},2024/01/01,read,{}\n",
|
|
1 + i % 9,
|
|
if i == 3 { 2 } else { 1 }
|
|
));
|
|
}
|
|
csv.push_str("99,Opuštěná,Autor,,,0,,2024/01/01,read,1\n");
|
|
let mut n = 0;
|
|
let (ops, report) = liber_library::import::import_csv(
|
|
csv.as_bytes(),
|
|
&Default::default(),
|
|
&mut || {
|
|
n += 1;
|
|
format!("id{n}")
|
|
},
|
|
0,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(report.imported, 13);
|
|
let mut log = Log::new();
|
|
for op in ops {
|
|
log.push(op);
|
|
}
|
|
// StoryGraph-style: one did-not-finish.
|
|
log.push(Op::ReadStart { book: "b".into(), read: "dnf".into(), date: None });
|
|
log.push(Op::ReadEnd { read: "dnf".into(), date: Some("2024-05-01".into()), outcome: Outcome::DidNotFinish });
|
|
let lib = log.lib();
|
|
let e = earned(&lib);
|
|
let ids: Vec<&str> = e.iter().map(|(i, _)| i.as_str()).collect();
|
|
assert!(ids.contains(&"first-book"), "{ids:?}");
|
|
assert!(ids.contains(&"books-10"));
|
|
assert!(ids.contains(&"first-reread"));
|
|
assert!(ids.contains(&"let-go"));
|
|
assert!(!ids.iter().any(|i| i.starts_with("days-")), "no fake days: {ids:?}");
|
|
assert_eq!(liber_habit::habit(&lib, monday(), 1).total_days, 0);
|
|
}
|
|
|
|
#[test]
|
|
fn five_moods_and_seen_medals() {
|
|
let mut log = Log::new();
|
|
log.push(Op::BookSet {
|
|
book: "b".into(),
|
|
field: Field::Moods,
|
|
value: json!(["dark", "tense", "sad", "funny", "hopeful"]),
|
|
base: None,
|
|
});
|
|
log.push(Op::ReadStart { book: "b".into(), read: "r".into(), date: None });
|
|
log.push(Op::ReadEnd { read: "r".into(), date: Some("2026-01-05".into()), outcome: Outcome::Read });
|
|
let lib = log.lib();
|
|
assert!(earned(&lib).contains(&("five-moods".into(), "2026-01-05".into())));
|
|
assert_eq!(unseen(&lib).len(), 2, "first book and five moods");
|
|
log.push(Op::MedalSeen { medal: "first-book".into() });
|
|
let lib = log.lib();
|
|
assert_eq!(unseen(&lib).iter().map(|m| m.id).collect::<Vec<_>>(), vec!["five-moods"]);
|
|
assert!(
|
|
medals(&lib).iter().filter(|m| !m.available).all(|m| m.earned.is_none()),
|
|
"later medals cannot be earned yet"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn the_reminder_is_quiet_when_off_or_when_the_goal_is_met() {
|
|
let today = monday();
|
|
let off = Reminder { enabled: false, minute: 20 * 60 };
|
|
let on = Reminder { enabled: true, minute: 20 * 60 };
|
|
for met in [false, true] {
|
|
for now in [0u16, 600, 1199, 1200, 1439] {
|
|
assert_eq!(next_reminder(&off, today, now, met), None);
|
|
let next = next_reminder(&on, today, now, met).unwrap();
|
|
if met {
|
|
assert_ne!(next.0, today, "never today when today's goal is met");
|
|
}
|
|
assert_eq!(next.1, 1200);
|
|
}
|
|
}
|
|
assert_eq!(next_reminder(&on, today, 600, false), Some((today, 1200)));
|
|
assert_eq!(next_reminder(&on, today, 1300, false), Some((today.plus(1), 1200)));
|
|
}
|