use std::fs; use lib_efo::{ entity::{Entity, EntityIdentifier}, epoch::{EpochId, EpochInfo}, stats::EntityType, workspace::{Workspace, WorkspaceError}, }; fn repository() -> tempfile::TempDir { let directory = tempfile::tempdir().expect("temporary repository"); Workspace::initialize(directory.path(), "fixture").expect("initialize repository"); directory } #[test] fn all_layers_begin_clean_and_complete() { let directory = repository(); let workspace = Workspace::open(directory.path()).expect("workspace"); assert!(!workspace.has_staged_changes()); assert!(!workspace.has_unstaged_changes()); assert_eq!(workspace.head.epoch_id, EpochId::new(0)); } #[test] fn unfinished_journal_blocks_layer_reads_until_recovery() { let directory = repository(); fs::write(directory.path().join(".efo/transaction.json"), "{}").expect("journal"); let error = Workspace::open(directory.path()).expect_err("journal must block readers"); assert!(matches!(error, WorkspaceError::RecoveryRequired)); } #[test] fn time_advance_commits_even_an_empty_world() { let directory = repository(); let mut workspace = Workspace::open(directory.path()).expect("workspace"); assert_eq!( workspace.advance_and_commit().expect("advance"), EpochId::new(1) ); assert_eq!(workspace.head.commit_message, "Advance time"); } #[test] fn entity_staging_and_both_restore_directions_are_isolated() { let directory = repository(); let mut workspace = Workspace::open(directory.path()).expect("workspace"); let mut aria = Entity::default(); aria.id = uuid::Uuid::from_u128(1); aria.name = "Aria".into(); aria.entity_type = EntityType::Character; workspace.worktree.world.characters.push(aria); workspace.save_worktree("test edit").expect("save worktree"); let mut workspace = Workspace::open(directory.path()).expect("changed workspace"); assert!(workspace.has_unstaged_changes()); assert!(!workspace.has_staged_changes()); workspace .stage_entities( EntityType::Character, &[EntityIdentifier::Name("Aria".into())], ) .expect("stage Aria"); assert!(workspace.has_staged_changes()); assert!(!workspace.has_unstaged_changes()); workspace.restore_index().expect("unstage all"); assert!(!workspace.has_staged_changes()); assert!(workspace.has_unstaged_changes()); workspace.restore_worktree().expect("discard worktree"); assert!(!workspace.has_unstaged_changes()); assert!(workspace.worktree.world.characters.is_empty()); } #[test] fn commit_creates_immutable_history_and_advances_all_layers() { let directory = repository(); let mut workspace = Workspace::open(directory.path()).expect("workspace"); let mut aria = Entity::default(); aria.id = uuid::Uuid::from_u128(1); aria.name = "Aria".into(); aria.entity_type = EntityType::Character; workspace.worktree.world.characters.push(aria); workspace.save_worktree("test edit").expect("worktree"); let mut workspace = Workspace::open(directory.path()).expect("changed workspace"); workspace.stage_all().expect("stage"); let id = workspace.commit("Add Aria".into()).expect("commit"); assert_eq!(id, EpochId::new(1)); assert_eq!(workspace.manifest.next_epoch_id, EpochId::new(2)); assert!(!workspace.has_staged_changes()); assert!(!workspace.has_unstaged_changes()); let committed: EpochInfo = serde_json::from_slice( &fs::read(directory.path().join("epoch/1/epochinfo.json")).expect("epoch 1"), ) .expect("committed epoch"); assert_eq!(committed.parent_epoch, Some(EpochId::new(0))); assert_eq!(committed.commit_message, "Add Aria"); let original: EpochInfo = serde_json::from_slice( &fs::read(directory.path().join("epoch/0/epochinfo.json")).expect("epoch 0"), ) .expect("original epoch"); assert!(original.characters.is_empty()); let reverted = workspace.revert(EpochId::new(1)).expect("revert epoch 1"); assert_eq!(reverted, EpochId::new(2)); assert!(workspace.head.characters.is_empty()); assert_eq!(workspace.head.parent_epoch, Some(EpochId::new(1))); workspace .switch(EpochId::new(1)) .expect("switch to epoch 1"); assert_eq!(workspace.head.characters.len(), 1); assert_eq!(workspace.manifest.next_epoch_id, EpochId::new(3)); assert_eq!( workspace.future_epochs().expect("future"), vec![EpochId::new(2)] ); let backup = workspace.prune_future().expect("prune future"); assert!(!directory.path().join("epoch/2").exists()); assert_eq!( workspace.restore_pruned(&backup).expect("restore future"), 1 ); assert!(directory.path().join("epoch/2/epochinfo.json").exists()); } #[test] fn revert_aborts_when_later_edits_overlap_the_target_change() { let directory = repository(); let mut workspace = Workspace::open(directory.path()).expect("workspace"); let mut aria = Entity::default(); aria.id = uuid::Uuid::from_u128(1); aria.name = "Aria".into(); aria.entity_type = EntityType::Character; workspace.worktree.world.characters.push(aria); workspace.stage_all().expect("stage addition"); workspace.commit("Add Aria".into()).expect("first commit"); workspace.worktree.world.characters[0].name = "Aria Changed".into(); workspace.stage_all().expect("stage edit"); workspace .commit("Rename Aria".into()) .expect("second commit"); let error = workspace .revert(EpochId::new(1)) .expect_err("overlap must conflict"); assert!(matches!(error, WorkspaceError::Conflict(_))); assert_eq!(workspace.manifest.epoch_id, EpochId::new(2)); assert!(!directory.path().join("epoch/3").exists()); }