epoch_forge/lib_efo/tests/workspace.rs

105 lines
3.9 KiB
Rust

use std::{collections::BTreeMap, fs};
use lib_efo::{
entity::{Entity, EntityIdentifier},
epoch::{EpochId, EpochInfo},
migration::{execute_migration, plan_migration},
repository_format::LegacyManifest,
stats::EntityType,
workspace::Workspace,
};
fn migrated_repository() -> tempfile::TempDir {
let directory = tempfile::tempdir().expect("temporary repository");
let epoch_directory = directory.path().join("epoch/0");
fs::create_dir_all(&epoch_directory).expect("epoch directory");
let manifest = LegacyManifest {
epoch_id: EpochId::new(0),
version: "fixture".into(),
tags: BTreeMap::new(),
};
fs::write(
directory.path().join(".epoch"),
serde_json::to_vec_pretty(&manifest).expect("manifest JSON"),
)
.expect("manifest");
fs::write(
epoch_directory.join("epochinfo.json"),
serde_json::to_vec_pretty(&EpochInfo::default()).expect("epoch JSON"),
)
.expect("epoch");
let plan = plan_migration(directory.path()).expect("migration plan");
execute_migration(&plan).expect("migration");
directory
}
#[test]
fn all_layers_begin_clean_and_complete() {
let directory = migrated_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 entity_staging_and_both_restore_directions_are_isolated() {
let directory = migrated_repository();
let mut workspace = Workspace::open(directory.path()).expect("workspace");
let mut aria = Entity::default();
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 = migrated_repository();
let mut workspace = Workspace::open(directory.path()).expect("workspace");
let mut aria = Entity::default();
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());
}