251 lines
9.0 KiB
Rust
251 lines
9.0 KiB
Rust
use std::{
|
|
collections::{BTreeMap, BTreeSet},
|
|
fs,
|
|
path::Path,
|
|
};
|
|
|
|
use lib_efo::{
|
|
entity::Entity,
|
|
epoch::{EpochId, EpochInfo},
|
|
migration::{MigrationError, execute_migration, plan_migration},
|
|
repository_format::{DetectedManifest, LegacyManifest, detect_manifest},
|
|
stats::{EntityType, Role, TypedLink},
|
|
};
|
|
use serde::Deserialize;
|
|
use uuid::Uuid;
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
struct Fixture {
|
|
name: String,
|
|
current_epoch: u64,
|
|
epoch_ids: Vec<u64>,
|
|
tags: BTreeMap<String, u64>,
|
|
feature: String,
|
|
}
|
|
|
|
fn fixtures() -> Vec<Fixture> {
|
|
serde_json::from_str(include_str!("fixtures/v1/catalog.json")).expect("valid fixture catalog")
|
|
}
|
|
|
|
#[test]
|
|
fn version_one_migration_matrix_covers_every_legacy_shape() {
|
|
let fixtures = fixtures();
|
|
let names = fixtures
|
|
.iter()
|
|
.map(|fixture| fixture.name.as_str())
|
|
.collect::<BTreeSet<_>>();
|
|
assert_eq!(
|
|
names,
|
|
BTreeSet::from([
|
|
"empty",
|
|
"legacy-monster",
|
|
"malformed-missing-selected-epoch",
|
|
"populated",
|
|
"relationships",
|
|
"switched-back-with-future",
|
|
"tags",
|
|
])
|
|
);
|
|
|
|
for fixture in fixtures {
|
|
let legacy = LegacyManifest {
|
|
epoch_id: EpochId::new(fixture.current_epoch),
|
|
version: "fixture-v1".to_owned(),
|
|
tags: fixture
|
|
.tags
|
|
.into_iter()
|
|
.map(|(tag, id)| (tag, EpochId::new(id)))
|
|
.collect(),
|
|
};
|
|
let bytes = serde_json::to_vec(&legacy).expect("serialize fixture manifest");
|
|
assert!(matches!(
|
|
detect_manifest(&bytes).expect("detect fixture manifest"),
|
|
DetectedManifest::Legacy(_)
|
|
));
|
|
|
|
let known = fixture.epoch_ids.into_iter().collect::<BTreeSet<_>>();
|
|
if fixture.feature == "malformed" {
|
|
assert!(!known.contains(&fixture.current_epoch));
|
|
} else {
|
|
assert!(
|
|
known.contains(&fixture.current_epoch),
|
|
"{} must contain its selected epoch",
|
|
fixture.name
|
|
);
|
|
}
|
|
|
|
if fixture.feature == "future" {
|
|
assert!(known.iter().any(|id| *id > fixture.current_epoch));
|
|
}
|
|
if fixture.feature == "tags" {
|
|
assert!(!legacy.tags.is_empty());
|
|
}
|
|
|
|
for id in known {
|
|
let mut snapshot = EpochInfo {
|
|
epoch_id: EpochId::new(id),
|
|
summary: fixture.feature.clone(),
|
|
..EpochInfo::default()
|
|
};
|
|
populate_feature(&fixture.feature, &mut snapshot);
|
|
let encoded = serde_json::to_vec(&snapshot).expect("serialize fixture snapshot");
|
|
let decoded: EpochInfo =
|
|
serde_json::from_slice(&encoded).expect("load fixture snapshot");
|
|
assert_eq!(decoded.epoch_id, EpochId::new(id));
|
|
|
|
match fixture.feature.as_str() {
|
|
"populated" => assert_eq!(decoded.characters.len(), 1),
|
|
"relationships" => {
|
|
assert_eq!(decoded.organizations.len(), 1);
|
|
assert_eq!(decoded.towns.len(), 1);
|
|
let value = serde_json::to_value(&decoded.organizations[0])
|
|
.expect("serialize related organisation");
|
|
assert_eq!(
|
|
value["metadata"]["links"]
|
|
.as_array()
|
|
.expect("relationship list")
|
|
.len(),
|
|
1
|
|
);
|
|
}
|
|
"monster" => assert_eq!(decoded.calamities.len(), 1),
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn populate_feature(feature: &str, snapshot: &mut EpochInfo) {
|
|
match feature {
|
|
"populated" => {
|
|
let mut character = Entity::default();
|
|
character.name = "Ada".to_owned();
|
|
character.entity_type = EntityType::Character;
|
|
snapshot.characters.push(character);
|
|
}
|
|
"relationships" => {
|
|
let town_id = Uuid::from_u128(1);
|
|
let mut town = Entity::default();
|
|
town.id = town_id;
|
|
town.name = "Beacon".to_owned();
|
|
town.entity_type = EntityType::Town;
|
|
|
|
let mut organization = Entity::default();
|
|
organization.id = Uuid::from_u128(2);
|
|
organization.name = "Council".to_owned();
|
|
organization.entity_type = EntityType::Organization;
|
|
organization.add_to_links(TypedLink::new(town_id, Role::Capital, EntityType::Town));
|
|
|
|
snapshot.towns.push(town);
|
|
snapshot.organizations.push(organization);
|
|
}
|
|
"monster" => {
|
|
let mut calamity = Entity::default();
|
|
calamity.name = "Old Blight".to_owned();
|
|
calamity.entity_type = EntityType::Calamity;
|
|
snapshot.calamities.push(calamity);
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn legacy_monster_spelling_remains_loadable() {
|
|
let mut entity = lib_efo::entity::Entity::default();
|
|
entity.name = "Old Blight".to_owned();
|
|
entity.entity_type = lib_efo::stats::EntityType::Calamity;
|
|
let mut value = serde_json::to_value(entity).expect("serialize calamity");
|
|
value["entity_type"] = serde_json::Value::String("Monster".to_owned());
|
|
|
|
let decoded: lib_efo::entity::Entity =
|
|
serde_json::from_value(value).expect("load legacy Monster spelling");
|
|
assert_eq!(decoded.entity_type, lib_efo::stats::EntityType::Calamity);
|
|
}
|
|
|
|
#[test]
|
|
fn migration_plan_is_read_only_and_allocates_after_future_history() {
|
|
let directory = tempfile::tempdir().expect("temporary fixture repository");
|
|
materialize_repository(directory.path(), 1, &[0, 1, 2, 3]);
|
|
let manifest_before = fs::read(directory.path().join(".epoch")).expect("legacy manifest");
|
|
|
|
let plan = plan_migration(directory.path()).expect("valid migration plan");
|
|
|
|
assert_eq!(plan.current_epoch(), EpochId::new(1));
|
|
assert_eq!(plan.next_epoch_id(), EpochId::new(4));
|
|
assert_eq!(plan.epoch_count(), 4);
|
|
assert_eq!(
|
|
fs::read(directory.path().join(".epoch")).expect("manifest after plan"),
|
|
manifest_before
|
|
);
|
|
assert!(!directory.path().join(".efo").exists());
|
|
}
|
|
|
|
#[test]
|
|
fn migration_rejects_missing_selected_epoch_without_writing() {
|
|
let directory = tempfile::tempdir().expect("temporary fixture repository");
|
|
materialize_repository(directory.path(), 4, &[0]);
|
|
|
|
let error = plan_migration(directory.path()).expect_err("invalid migration must fail");
|
|
assert!(matches!(error, MigrationError::Invalid(_)));
|
|
assert!(!directory.path().join(".efo").exists());
|
|
}
|
|
|
|
#[test]
|
|
fn migration_backs_up_manifest_and_commits_version_two_last() {
|
|
let directory = tempfile::tempdir().expect("temporary fixture repository");
|
|
materialize_repository(directory.path(), 1, &[0, 1, 2]);
|
|
let legacy_bytes = fs::read(directory.path().join(".epoch")).expect("legacy manifest");
|
|
let plan = plan_migration(directory.path()).expect("valid migration plan");
|
|
|
|
let outcome = execute_migration(&plan).expect("execute migration");
|
|
|
|
assert_eq!(
|
|
fs::read(outcome.backup_directory.join(".epoch")).expect("backup manifest"),
|
|
legacy_bytes
|
|
);
|
|
assert!(outcome.backup_directory.join("checksums.fnv1a64").is_file());
|
|
let manifest_bytes = fs::read(directory.path().join(".epoch")).expect("current manifest");
|
|
let DetectedManifest::Current(manifest) =
|
|
detect_manifest(&manifest_bytes).expect("version two manifest")
|
|
else {
|
|
panic!("migration did not commit version two")
|
|
};
|
|
assert_eq!(manifest.repository_id, outcome.repository_id);
|
|
assert_eq!(manifest.next_epoch_id, EpochId::new(3));
|
|
for layer in ["index.json", "worktree.json"] {
|
|
let snapshot: lib_efo::repository_format::RepositorySnapshot = serde_json::from_slice(
|
|
&fs::read(directory.path().join(".efo").join(layer)).expect("transient snapshot"),
|
|
)
|
|
.expect("decode transient snapshot");
|
|
assert_eq!(snapshot.repository_id, outcome.repository_id);
|
|
assert_eq!(snapshot.base_epoch_id, EpochId::new(1));
|
|
assert_eq!(snapshot.world.epoch_id, EpochId::new(1));
|
|
}
|
|
}
|
|
|
|
fn materialize_repository(root: &Path, current: u64, epochs: &[u64]) {
|
|
let manifest = LegacyManifest {
|
|
epoch_id: EpochId::new(current),
|
|
version: "fixture-v1".to_owned(),
|
|
tags: BTreeMap::new(),
|
|
};
|
|
fs::write(
|
|
root.join(".epoch"),
|
|
serde_json::to_vec_pretty(&manifest).expect("serialize legacy manifest"),
|
|
)
|
|
.expect("write legacy manifest");
|
|
for id in epochs {
|
|
let directory = root.join(format!("epoch/{id}"));
|
|
fs::create_dir_all(&directory).expect("create epoch directory");
|
|
let snapshot = EpochInfo {
|
|
epoch_id: EpochId::new(*id),
|
|
..EpochInfo::default()
|
|
};
|
|
fs::write(
|
|
directory.join("epochinfo.json"),
|
|
serde_json::to_vec_pretty(&snapshot).expect("serialize legacy snapshot"),
|
|
)
|
|
.expect("write legacy snapshot");
|
|
}
|
|
}
|