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
1 changed files with 149 additions and 127 deletions

View File

@ -113,7 +113,9 @@ impl Tasks {
}
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 start: Option<Timestamp> = None;
for event in events {
@ -126,6 +128,7 @@ impl Tasks {
}
_ => if let Some(stamp) = start {
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 {
info!("Tracking \"{:?}\" from {}", self.position.map(|id| self.get_task_title(&id)), time.to_human_datetime());
self.submit(
build_tracking(self.get_position())
.custom_created_at(time)
)
let pos = self.get_position();
let tracking = build_tracking(pos);
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 {
@ -520,7 +533,7 @@ impl Tasks {
t.children.insert(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 {
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) {
self.sender.clear().into_iter().rev().for_each(|event| {
self.remove(&event)
@ -545,7 +562,7 @@ impl Tasks {
}
}
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); });
}
@ -630,6 +647,10 @@ impl<'a> Iterator for ParentIterator<'a> {
}
}
#[cfg(test)]
mod tasks_test {
use super::*;
fn stub_tasks() -> Tasks {
use std::sync::mpsc;
use nostr_sdk::Keys;
@ -658,8 +679,8 @@ fn test_tracking() {
tasks.move_to(None);
tasks.track_at(Timestamp::from(2));
assert_eq!(tasks.history.values().nth(0).unwrap().len(), 5);
assert_eq!(tasks.time_tracked(zero), 2);
assert_eq!(tasks.get_own_history().unwrap().len(), 3);
assert_eq!(tasks.time_tracked(zero), 1);
}
#[test]
@ -758,3 +779,4 @@ fn test_unknown_task() {
2
);
}
}