Track space timestamps
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::error::Error;
|
||||
|
||||
use ely_browser_core::{BrowserCore, CoreError, InitialBrowserConfig};
|
||||
use ely_domain::{ProfileId, ProfileKind};
|
||||
use ely_domain::{ArchivePolicy, ProfileId, ProfileKind};
|
||||
|
||||
#[test]
|
||||
fn created_space_binds_current_profile_as_default() -> Result<(), Box<dyn Error>> {
|
||||
@@ -21,6 +21,21 @@ fn created_space_binds_current_profile_as_default() -> Result<(), Box<dyn Error>
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn created_space_records_creation_timestamps() -> Result<(), Box<dyn Error>> {
|
||||
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
|
||||
let research_space_id = core.create_space("Research", "R", 0x9fc9a2)?;
|
||||
let snapshot = core.snapshot()?;
|
||||
let Some(research_space) =
|
||||
snapshot.spaces.iter().find(|space| space.id() == &research_space_id)
|
||||
else {
|
||||
return Err("missing research space".into());
|
||||
};
|
||||
|
||||
assert_eq!(research_space.created_at(), research_space.updated_at());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn space_default_profile_updates_with_profile_validation() -> Result<(), Box<dyn Error>> {
|
||||
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
|
||||
@@ -44,3 +59,33 @@ fn space_default_profile_updates_with_profile_validation() -> Result<(), Box<dyn
|
||||
assert_eq!(error, CoreError::ProfileNotFound { id: missing_profile_id });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn space_settings_refresh_updated_at() -> Result<(), Box<dyn Error>> {
|
||||
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
|
||||
let work_space_id = core.snapshot()?.active_space_id;
|
||||
let before_update = active_space_updated_at(&core, &work_space_id)?;
|
||||
|
||||
core.set_space_archive_policy(&work_space_id, ArchivePolicy::IdleDays(7))?;
|
||||
let after_archive_update = active_space_updated_at(&core, &work_space_id)?;
|
||||
|
||||
let research_profile_id = core.create_profile("Research", 0x9fc9a2, ProfileKind::Standard)?;
|
||||
core.set_space_default_profile(&work_space_id, &research_profile_id)?;
|
||||
let after_profile_update = active_space_updated_at(&core, &work_space_id)?;
|
||||
|
||||
assert!(after_archive_update > before_update);
|
||||
assert!(after_profile_update > after_archive_update);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn active_space_updated_at(
|
||||
core: &BrowserCore,
|
||||
space_id: &ely_domain::SpaceId,
|
||||
) -> Result<std::time::SystemTime, Box<dyn Error>> {
|
||||
let snapshot = core.snapshot()?;
|
||||
let Some(space) = snapshot.spaces.iter().find(|space| space.id() == space_id) else {
|
||||
return Err("missing space".into());
|
||||
};
|
||||
|
||||
Ok(space.updated_at())
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
use crate::{ProfileId, SpaceId};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
@@ -14,6 +16,8 @@ pub struct Space {
|
||||
accent_hex: u32,
|
||||
default_profile_id: ProfileId,
|
||||
archive_policy: ArchivePolicy,
|
||||
created_at: SystemTime,
|
||||
updated_at: SystemTime,
|
||||
}
|
||||
|
||||
impl Space {
|
||||
@@ -24,6 +28,7 @@ impl Space {
|
||||
accent_hex: u32,
|
||||
default_profile_id: ProfileId,
|
||||
) -> Self {
|
||||
let created_at = SystemTime::now();
|
||||
Self {
|
||||
id: SpaceId::new(),
|
||||
name: name.into(),
|
||||
@@ -31,6 +36,8 @@ impl Space {
|
||||
accent_hex,
|
||||
default_profile_id,
|
||||
archive_policy: ArchivePolicy::Manual,
|
||||
created_at,
|
||||
updated_at: created_at,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +68,7 @@ impl Space {
|
||||
|
||||
pub fn set_default_profile_id(&mut self, profile_id: ProfileId) {
|
||||
self.default_profile_id = profile_id;
|
||||
self.record_update();
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
@@ -70,5 +78,22 @@ impl Space {
|
||||
|
||||
pub fn set_archive_policy(&mut self, archive_policy: ArchivePolicy) {
|
||||
self.archive_policy = archive_policy;
|
||||
self.record_update();
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn created_at(&self) -> SystemTime {
|
||||
self.created_at
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn updated_at(&self) -> SystemTime {
|
||||
self.updated_at
|
||||
}
|
||||
|
||||
fn record_update(&mut self) {
|
||||
let now = SystemTime::now();
|
||||
self.updated_at =
|
||||
if now > self.updated_at { now } else { self.updated_at + Duration::from_nanos(1) };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user