kcode-k1-web-coding 0.2.2

Orchestrate authorized per-user K1 Web coding operations
Documentation
use super::*;
use kcode_k1_web_package::{AuthorityId, SourceFile};
use semver::Version;

fn tx(byte: u8) -> TxId {
    TxId::from_bytes([byte; 12])
}

fn family(name: &str) -> WebFamily {
    WebFamily::new(AuthorityId::new(tx(1)), name).unwrap()
}

fn package(name: &str, version: u64) -> Arc<SourcePackage> {
    let manifest = format!(
        r#"{{"name":"{name}","version":"{version}.0.0","entry":"index.js","tests":"index.js","dependencies":[]}}"#
    );
    let id = WebId::new(family(name), Version::new(version, 0, 0)).unwrap();
    Arc::new(
        SourcePackage::new(
            id,
            vec![
                SourceFile::new("Documentation.md", name.as_bytes().to_vec()),
                SourceFile::new("index.js", b"app".to_vec()),
                SourceFile::new("k1-web.json", manifest.into_bytes()),
            ],
        )
        .unwrap(),
    )
}

#[test]
fn retained_candidate_is_reconstructed_only_for_its_exact_identifier() {
    let root = tempfile::tempdir().unwrap();
    let cache = UserWebCache::open(root.path(), tx(9), "boot", "schema", "policy").unwrap();
    let candidate = package("app", 1);
    assert!(cache.candidate(candidate.id()).unwrap().is_none());
    cache.materialize(&candidate).unwrap();
    assert_eq!(
        cache.candidate(candidate.id()).unwrap().as_ref(),
        Some(candidate.as_ref())
    );
    let wrong = WebId::new(family("app"), Version::new(2, 0, 0)).unwrap();
    assert!(cache.candidate(&wrong).unwrap().is_none());
}