fix(tasks): test time-tracking including automatic back-tracking

This commit is contained in:
xeruf 2024-08-06 17:52:20 +03:00
parent d950c13098
commit db4f9ee8cb

View file

@ -113,7 +113,9 @@ impl Tasks {
} }
fn time_tracked_for<'a, E>(events: E, ids: &Vec<EventId>) -> u64 fn time_tracked_for<'a, E>(events: E, ids: &Vec<EventId>) -> u64
where E: IntoIterator<Item=&'a Event> { where
E: IntoIterator<Item=&'a Event>,
{
let mut total = 0; let mut total = 0;
let mut start: Option<Timestamp> = None; let mut start: Option<Timestamp> = None;
for event in events { for event in events {
@ -126,6 +128,7 @@ impl Tasks {
} }
_ => if let Some(stamp) = start { _ => if let Some(stamp) = start {
total += (event.created_at - stamp).as_u64(); total += (event.created_at - stamp).as_u64();
start = None;
} }
} }
} }
@ -490,10 +493,20 @@ impl Tasks {
pub(crate) fn track_at(&mut self, time: Timestamp) -> EventId { pub(crate) fn track_at(&mut self, time: Timestamp) -> EventId {
info!("Tracking \"{:?}\" from {}", self.position.map(|id| self.get_task_title(&id)), time.to_human_datetime()); info!("Tracking \"{:?}\" from {}", self.position.map(|id| self.get_task_title(&id)), time.to_human_datetime());
self.submit( let pos = self.get_position();
build_tracking(self.get_position()) let tracking = build_tracking(pos);
.custom_created_at(time) self.get_own_history().map(|events| {
) if let Some(event) = events.pop_last() {
if event.kind.as_u16() == TRACKING_KIND &&
(pos == None && event.tags.is_empty()) ||
event.tags.iter().all(|t| t.content().map(|str| str.to_string()) == pos.map(|id| id.to_string())) {
// Replace last for easier calculation
} else {
events.insert(event);
}
}
});
self.submit(tracking.custom_created_at(time))
} }
fn submit(&mut self, builder: EventBuilder) -> EventId { fn submit(&mut self, builder: EventBuilder) -> EventId {
@ -520,7 +533,7 @@ impl Tasks {
t.children.insert(event.id); t.children.insert(event.id);
}); });
if self.tasks.contains_key(&event.id) { if self.tasks.contains_key(&event.id) {
debug!("Did not insert duplicate event {}", event.id); warn!("Did not insert duplicate event {}", event.id);
} else { } else {
self.tasks.insert(event.id, Task::new(event)); self.tasks.insert(event.id, Task::new(event));
} }
@ -532,6 +545,10 @@ impl Tasks {
}); });
} }
fn get_own_history(&mut self) -> Option<&mut BTreeSet<Event>> {
self.history.get_mut(&self.sender.pubkey())
}
pub(crate) fn undo(&mut self) { pub(crate) fn undo(&mut self) {
self.sender.clear().into_iter().rev().for_each(|event| { self.sender.clear().into_iter().rev().for_each(|event| {
self.remove(&event) self.remove(&event)
@ -545,7 +562,7 @@ impl Tasks {
} }
} }
self.tasks.remove(&event.id); self.tasks.remove(&event.id);
self.history.get_mut(&self.sender.pubkey()).map(|t| t.remove(event)); self.get_own_history().map(|t| t.remove(event));
self.referenced_tasks(event, |t| { t.props.remove(event); }); self.referenced_tasks(event, |t| { t.props.remove(event); });
} }
@ -630,7 +647,11 @@ impl<'a> Iterator for ParentIterator<'a> {
} }
} }
fn stub_tasks() -> Tasks { #[cfg(test)]
mod tasks_test {
use super::*;
fn stub_tasks() -> Tasks {
use std::sync::mpsc; use std::sync::mpsc;
use nostr_sdk::Keys; use nostr_sdk::Keys;
@ -640,10 +661,10 @@ fn stub_tasks() -> Tasks {
keys: Keys::generate(), keys: Keys::generate(),
queue: Default::default(), queue: Default::default(),
}) })
} }
#[test] #[test]
fn test_tracking() { fn test_tracking() {
let mut tasks = stub_tasks(); let mut tasks = stub_tasks();
//let task = tasks.make_task("task"); //let task = tasks.make_task("task");
@ -658,12 +679,12 @@ fn test_tracking() {
tasks.move_to(None); tasks.move_to(None);
tasks.track_at(Timestamp::from(2)); tasks.track_at(Timestamp::from(2));
assert_eq!(tasks.history.values().nth(0).unwrap().len(), 5); assert_eq!(tasks.get_own_history().unwrap().len(), 3);
assert_eq!(tasks.time_tracked(zero), 2); assert_eq!(tasks.time_tracked(zero), 1);
} }
#[test] #[test]
fn test_depth() { fn test_depth() {
let mut tasks = stub_tasks(); let mut tasks = stub_tasks();
let t1 = tasks.make_task("t1"); let t1 = tasks.make_task("t1");
@ -724,10 +745,10 @@ fn test_depth() {
assert_eq!(tasks.current_tasks().len(), 4); assert_eq!(tasks.current_tasks().len(), 4);
tasks.depth = -1; tasks.depth = -1;
assert_eq!(tasks.current_tasks().len(), 2); assert_eq!(tasks.current_tasks().len(), 2);
} }
#[test] #[test]
fn test_empty_task_title_fallback_to_id() { fn test_empty_task_title_fallback_to_id() {
let mut tasks = stub_tasks(); let mut tasks = stub_tasks();
let empty = tasks.make_task(""); let empty = tasks.make_task("");
@ -735,10 +756,10 @@ fn test_empty_task_title_fallback_to_id() {
let empty_id = empty_task.event.id.to_string(); let empty_id = empty_task.event.id.to_string();
assert_eq!(empty_task.get_title(), empty_id); assert_eq!(empty_task.get_title(), empty_id);
assert_eq!(tasks.get_task_path(Some(empty)), empty_id); assert_eq!(tasks.get_task_path(Some(empty)), empty_id);
} }
#[test] #[test]
fn test_unknown_task() { fn test_unknown_task() {
let mut tasks = stub_tasks(); let mut tasks = stub_tasks();
let zero = EventId::all_zeros(); let zero = EventId::all_zeros();
@ -757,4 +778,5 @@ fn test_unknown_task() {
"test toast".split_ascii_whitespace().collect_vec().len(), "test toast".split_ascii_whitespace().collect_vec().len(),
2 2
); );
}
} }