frame-state 0.2.0

Content-addressed state layer — haematite integration, entities, branching, cross-component references
Documentation
use frame_core::capability::{Capability, CheckVerdict};
use frame_core::component::ComponentId;

use super::{TestDirectory, TestResult, component, install};
use crate::handle::ResolutionChecker;
use crate::{ComponentStateStore, CrossComponentRef};

struct AllowedChecker(ComponentId);

impl ResolutionChecker for AllowedChecker {
    fn component_id(&self) -> ComponentId {
        self.0
    }

    fn check(
        &self,
        _capability: &Capability,
    ) -> Result<CheckVerdict, frame_core::capability::CapabilityCheckError> {
        Ok(CheckVerdict::Allowed)
    }
}

#[test]
fn reinstall_resolves_the_fresh_namespace_while_archive_stays_recoverable() -> TestResult {
    let directory = TestDirectory::new("cross-ref-reinstall")?;
    let store = ComponentStateStore::open(directory.path())?;
    let reader = component("reinstall-reader");
    let target = component("reinstall-target");
    let reader_handle = install(&store, reader)?;
    let old_target = install(&store, target)?;
    let entity = old_target.put(b"generation zero")?;
    old_target.commit()?;
    let reference = CrossComponentRef::new(target, entity);
    let encoded = reference.to_bytes();
    assert_eq!(encoded[0], 1);
    assert_eq!(&encoded[1..33], target.as_bytes());
    assert_eq!(&encoded[33..], entity.as_bytes());

    let archive = store.archive_storage(target)?;
    let fresh_target = install(&store, target)?;
    assert_eq!(fresh_target.incarnation(), 1);
    assert_eq!(
        reader_handle.resolve_cross_component_with(&AllowedChecker(reader), reference)?,
        None
    );
    assert_eq!(
        store.get_archived(target, archive.generation, entity)?,
        Some(b"generation zero".to_vec())
    );
    Ok(())
}