clm-kernel 0.0.10

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
use super::*;

    #[test]
    fn test_mount_registers_md_handles() {
        let mut files = HashMap::new();
        files.insert("README.md".to_string(), "# Hello".to_string());
        files.insert("docs/arch.md".to_string(), "# Arch".to_string());
        let mount = OkfMount::mount("/okf", MountMode::Instead, files).unwrap();
        assert!(mount.handles.contains_key("README.md"));
        assert!(mount.handles.contains_key("docs:arch.md"));
        assert!(!mount.handles.contains_key("image.png"));
    }

    #[test]
    fn test_index_md_gluing_manifest() {
        let mut files = HashMap::new();
        files.insert(
            "index.md".to_string(),
            "schema: a_mcard\nb-mcard: okf:README.md\ntype: okf/gluing_manifest\n".to_string(),
        );
        let mount = OkfMount::mount("/okf", MountMode::Instead, files).unwrap();
        let manifest = mount.gluing_manifest.unwrap();
        assert_eq!(manifest.schemas, vec!["a_mcard"]);
        assert_eq!(manifest.b_mcard_expectations, vec!["okf:README.md"]);
        assert_eq!(manifest.declared_types, vec!["okf/gluing_manifest"]);
    }

    #[test]
    fn test_log_md_handle_history() {
        let mut files = HashMap::new();
        files.insert(
            "log.md".to_string(),
            "2026-01-01T00:00:00Z did:key:z1 handle:h1 abc -> def\n".to_string(),
        );
        let mount = OkfMount::mount("/okf", MountMode::Instead, files).unwrap();
        assert_eq!(mount.handle_history.len(), 1);
        let entry = &mount.handle_history[0];
        assert_eq!(entry.handle, "handle:h1");
        assert_eq!(entry.new_hash, "def");
    }

    #[test]
    fn test_unmount_instead_removes_handles() {
        let mut files = HashMap::new();
        files.insert("README.md".to_string(), "# Hello".to_string());
        let mut mount = OkfMount::mount("/okf", MountMode::Instead, files).unwrap();
        mount.unmount().unwrap();
        assert!(mount.handles.is_empty());
    }

    #[test]
    fn test_unmount_before_marks_stale() {
        let mut files = HashMap::new();
        files.insert("README.md".to_string(), "# Hello".to_string());
        let mut mount = OkfMount::mount("/okf", MountMode::Before, files).unwrap();
        let original_hash = mount.handles["README.md"].clone();
        mount.unmount().unwrap();
        assert_eq!(mount.handles["README.md"], format!("stale:{}", original_hash));
    }

    #[test]
    fn test_remount_is_idempotent() {
        let mut files = HashMap::new();
        files.insert("README.md".to_string(), "# Hello".to_string());
        let mount = OkfMount::mount("/okf", MountMode::Instead, files.clone()).unwrap();
        assert!(mount.is_idempotent_with(&files));
    }

    // ── Bidirectional Sync tests (Story 139.6.2) ──

    #[test]
    fn test_sync_disk_to_mcard_creates_new() {
        let mut files = HashMap::new();
        files.insert("README.md".to_string(), "# Hello".to_string());
        let mut mount = OkfMount::mount("/okf", MountMode::Instead, files.clone()).unwrap();

        // Add a new file
        let mut updated_files = files.clone();
        updated_files.insert("docs/new.md".to_string(), "# New".to_string());
        let events = mount.sync_disk_to_mcard(&updated_files).unwrap();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].event_type, SyncEventType::Created);
        assert_eq!(events[0].handle, "docs:new.md");
    }

    #[test]
    fn test_sync_disk_to_mcard_updates_changed() {
        let mut files = HashMap::new();
        files.insert("README.md".to_string(), "# Hello".to_string());
        let mut mount = OkfMount::mount("/okf", MountMode::Instead, files.clone()).unwrap();

        // Modify the file
        let mut updated_files = files.clone();
        updated_files.insert("README.md".to_string(), "# Updated".to_string());
        let events = mount.sync_disk_to_mcard(&updated_files).unwrap();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].event_type, SyncEventType::Updated);
        assert_eq!(events[0].handle, "README.md");
    }

    #[test]
    fn test_sync_disk_to_mcard_deletes_removed() {
        let mut files = HashMap::new();
        files.insert("README.md".to_string(), "# Hello".to_string());
        files.insert("docs/gone.md".to_string(), "# Gone".to_string());
        let mut mount = OkfMount::mount("/okf", MountMode::Instead, files.clone()).unwrap();

        // Remove a file
        let mut updated_files = files.clone();
        updated_files.remove("docs/gone.md");
        let events = mount.sync_disk_to_mcard(&updated_files).unwrap();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].event_type, SyncEventType::Deleted);
        assert_eq!(events[0].handle, "docs:gone.md");
    }

    #[test]
    fn test_sync_mcard_to_disk_detects_diff() {
        let mut files = HashMap::new();
        files.insert("README.md".to_string(), "# Hello".to_string());
        let mount = OkfMount::mount("/okf", MountMode::Instead, files.clone()).unwrap();

        // Simulate MCard handle being updated (not on disk)
        let mut mount2 = mount.clone();
        mount2.handles.insert("README.md".to_string(), sha256_hex("# Different"));

        let events = mount2.sync_mcard_to_disk(&files).unwrap();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].event_type, SyncEventType::Updated);
    }

    #[test]
    fn test_sync_bidirectional_no_changes() {
        let mut files = HashMap::new();
        files.insert("README.md".to_string(), "# Hello".to_string());
        let mut mount = OkfMount::mount("/okf", MountMode::Instead, files.clone()).unwrap();
        let report = mount.sync_bidirectional(&files).unwrap();
        assert!(report.disk_to_mcard_events.is_empty());
        assert!(report.mcard_to_disk_events.is_empty());
        assert!(report.conflicts.is_empty());
    }

    #[test]
    fn test_sync_bidirectional_with_disk_change() {
        let mut files = HashMap::new();
        files.insert("README.md".to_string(), "# Hello".to_string());
        let mut mount = OkfMount::mount("/okf", MountMode::Instead, files.clone()).unwrap();

        // Change on disk
        let mut updated = files.clone();
        updated.insert("README.md".to_string(), "# Updated on disk".to_string());
        let report = mount.sync_bidirectional(&updated).unwrap();
        assert_eq!(report.disk_to_mcard_events.len(), 1);
        assert_eq!(report.disk_to_mcard_events[0].event_type, SyncEventType::Updated);
    }

    #[test]
    fn test_format_log_md() {
        let event = SyncEvent {
            handle: "README.md".to_string(),
            old_hash: "abc".to_string(),
            new_hash: "def".to_string(),
            event_type: SyncEventType::Updated,
            timestamp: "2026-01-01T00:00:00Z".to_string(),
        };
        let log = OkfMount::format_log_md(&[event], "did:key:z123");
        assert!(log.contains("README.md"));
        assert!(log.contains("abc"));
        assert!(log.contains("def"));
        assert!(log.contains("did:key:z123"));
    }

    #[test]
    fn test_round_trip_file_mcard_file() {
        let content = "# Round Trip\nContent here.";
        let hash = sha256_hex(content);
        let mut files = HashMap::new();
        files.insert("concept.md".to_string(), content.to_string());
        let mount = OkfMount::mount("/okf", MountMode::Instead, files.clone()).unwrap();

        // The handle hash should match the file hash
        let handle_hash = mount.handles.get("concept.md").unwrap();
        assert_eq!(*handle_hash, hash);

        // Re-mounting should produce the same hash (idempotent)
        assert!(mount.is_idempotent_with(&files));
    }