72 lines
3.0 KiB
Rust
72 lines
3.0 KiB
Rust
mod common;
|
|
use common::info;
|
|
use liber_library::library::{GoalKind, Outcome};
|
|
use liber_library::stats::{active_ms, stats};
|
|
use liber_library::{Clock, Event, Library, Op};
|
|
use serde_json::json;
|
|
|
|
#[test]
|
|
fn yearly_and_all_time() {
|
|
let mut c = Clock::default();
|
|
let mut n = 0;
|
|
let mut e = |op: Op| {
|
|
n += 1;
|
|
Event { id: format!("e{n}"), hlc: c.tick(n), device: "d".into(), op }
|
|
};
|
|
let evs = vec![
|
|
e(Op::BookAdd { book: "a".into(), info: info(0), restore: false }),
|
|
e(Op::BookAdd { book: "b".into(), info: info(1), restore: false }),
|
|
e(Op::BookAdd { book: "c".into(), info: info(2), restore: false }),
|
|
e(Op::BookSet {
|
|
book: "a".into(),
|
|
field: liber_library::library::Field::Moods,
|
|
value: json!(["dark", "tense"]),
|
|
base: None,
|
|
}),
|
|
e(Op::ReadStart { book: "a".into(), read: "r1".into(), date: Some("2025-01-02".into()) }),
|
|
e(Op::ReadEnd { read: "r1".into(), date: Some("2025-02-01".into()), outcome: Outcome::Read }),
|
|
e(Op::ReadStart { book: "b".into(), read: "r2".into(), date: Some("2025-03-01".into()) }),
|
|
e(Op::ReadEnd { read: "r2".into(), date: Some("2025-04-01".into()), outcome: Outcome::Read }),
|
|
e(Op::ReadStart { book: "c".into(), read: "r3".into(), date: None }),
|
|
e(Op::ReadEnd { read: "r3".into(), date: Some("2024-05-01".into()), outcome: Outcome::DidNotFinish }),
|
|
e(Op::ReadStart { book: "a".into(), read: "r4".into(), date: Some("2026-01-01".into()) }),
|
|
e(Op::ReadEnd { read: "r4".into(), date: Some("2026-01-20".into()), outcome: Outcome::Read }),
|
|
e(Op::Session {
|
|
offset_min: None,
|
|
id: "s1".into(),
|
|
book: "a".into(),
|
|
start: 0,
|
|
end: 3_600_000,
|
|
active_ms: 1_800_000,
|
|
pages: 30,
|
|
date: "2025-01-10".into(),
|
|
}),
|
|
e(Op::Session {
|
|
offset_min: None,
|
|
id: "s2".into(),
|
|
book: "b".into(),
|
|
start: 0,
|
|
end: 3_600_000,
|
|
active_ms: 9_999_999_999,
|
|
pages: 60,
|
|
date: "2025-03-10".into(),
|
|
}),
|
|
e(Op::Goal { year: 2025, kind: GoalKind::Books, target: 12 }),
|
|
];
|
|
let lib = Library::from_events(&evs);
|
|
let s = stats(&lib, Some(2025));
|
|
assert_eq!((s.books, s.did_not_finish, s.pages), (2, 0, 280 + 604));
|
|
assert_eq!(s.active_minutes, 30 + 60, "a session's active time is capped at its length");
|
|
assert_eq!(s.pace, Some(60.0));
|
|
assert_eq!(s.longest, Some(("b".into(), 604)));
|
|
assert_eq!(s.shortest, Some(("a".into(), 280)));
|
|
assert_eq!(s.moods.get("dark"), Some(&1));
|
|
assert_eq!(s.genres.get("classic"), Some(&2));
|
|
let g = s.goal.unwrap();
|
|
assert_eq!((g.target, g.done), (12, 2));
|
|
let all = stats(&lib, None);
|
|
assert_eq!((all.books, all.did_not_finish), (3, 1), "a re-read counts again");
|
|
assert_eq!(stats(&lib, Some(2024)).did_not_finish, 1);
|
|
assert_eq!(active_ms(10 * 60_000), 2 * 60_000, "two minutes per page at most");
|
|
}
|