epoch_forge/lib_efo/tests/workspace.rs

223 lines
8.4 KiB
Rust

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.manifest.next_epoch_id, EpochId::new(2));
assert!(backup.join("prune.json").is_file());
assert_eq!(
workspace.restore_pruned(&backup).expect("restore future"),
1
);
assert!(directory.path().join("epoch/2/epochinfo.json").exists());
assert_eq!(workspace.manifest.next_epoch_id, EpochId::new(3));
}
#[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());
}
#[test]
fn revert_to_worktree_is_reviewable_and_restorable() {
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 Aria");
workspace.commit("Add Aria".into()).expect("commit Aria");
workspace
.revert_to_worktree(EpochId::new(1))
.expect("apply inverse");
assert!(workspace.worktree.world.characters.is_empty());
assert!(workspace.has_unstaged_changes());
assert!(!workspace.has_staged_changes());
workspace.restore_worktree().expect("restore inverse");
assert_eq!(workspace.worktree.world.characters.len(), 1);
}
#[test]
fn hard_reset_discards_transient_state_and_newer_epochs() {
let directory = repository();
let mut workspace = Workspace::open(directory.path()).expect("workspace");
workspace.worktree.world.summary = "committed".into();
workspace.stage_all().expect("stage summary");
workspace.commit("First".into()).expect("first commit");
workspace.worktree.world.summary = "dirty".into();
let removed = workspace.hard_reset(EpochId::new(0)).expect("hard reset");
assert_eq!(removed, vec![EpochId::new(1)]);
assert_eq!(workspace.manifest.epoch_id, EpochId::new(0));
assert_eq!(workspace.manifest.next_epoch_id, EpochId::new(1));
assert!(workspace.worktree.world.summary.is_empty());
assert!(!directory.path().join("epoch/1").exists());
}
#[test]
fn prune_backup_restores_future_tags_and_allocator() {
let directory = repository();
let mut workspace = Workspace::open(directory.path()).expect("workspace");
workspace.worktree.world.summary = "tagged future".into();
workspace.worktree.world.tags.push("future".into());
workspace.stage_all().expect("stage tagged future");
workspace
.commit("Tagged future".into())
.expect("commit future");
workspace.switch(EpochId::new(0)).expect("switch backward");
let backup = workspace.prune_future().expect("prune tagged future");
assert_eq!(workspace.manifest.next_epoch_id, EpochId::new(1));
assert!(!workspace.manifest.tags.contains_key("future"));
workspace
.restore_pruned(&backup)
.expect("restore tagged future");
assert_eq!(workspace.manifest.next_epoch_id, EpochId::new(2));
assert_eq!(
workspace.manifest.tags.get("future"),
Some(&EpochId::new(1))
);
}