clm-kernel 0.0.10

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

    #[test]
    fn test_detect_platform() {
        let platform = detect_platform();
        assert!(!platform.is_empty());
        assert!(platform.contains('-'));
    }

    #[test]
    fn test_verify_hash_match() {
        let dir = std::env::temp_dir().join("clm_manifest_test_hash_match");
        let _ = fs::create_dir_all(&dir);
        let test_file = dir.join("test_binary.bin");
        fs::write(&test_file, b"hello world").unwrap();

        let mut hasher = Sha256::new();
        hasher.update(b"hello world");
        let expected = format!("{:x}", hasher.finalize());

        assert!(verify_hash(&test_file, &expected).is_ok());

        let _ = fs::remove_file(&test_file);
        let _ = fs::remove_dir(&dir);
    }

    #[test]
    fn test_verify_hash_mismatch() {
        let dir = std::env::temp_dir().join("clm_manifest_test_hash_mismatch");
        let _ = fs::create_dir_all(&dir);
        let test_file = dir.join("test_binary_mismatch.bin");
        fs::write(&test_file, b"hello world").unwrap();

        let wrong_hash = "0".repeat(64);
        let result = verify_hash(&test_file, &wrong_hash);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Hash mismatch"));

        let _ = fs::remove_file(&test_file);
        let _ = fs::remove_dir(&dir);
    }

    #[test]
    fn test_compute_file_hash() {
        let dir = std::env::temp_dir().join("clm_manifest_test_compute_hash");
        let _ = fs::create_dir_all(&dir);
        let test_file = dir.join("compute_hash_test.bin");
        fs::write(&test_file, b"test content for hashing").unwrap();

        let hash = compute_file_hash(&test_file).unwrap();
        assert_eq!(hash.len(), 64);
        assert!(hash.chars().all(|c| c.is_ascii_hexdigit()));

        let _ = fs::remove_file(&test_file);
        let _ = fs::remove_dir(&dir);
    }

    #[test]
    fn test_local_provider_found() {
        let dir = std::env::temp_dir().join("clm_manifest_test_local_provider");
        let _ = fs::create_dir_all(&dir);
        let source = dir.join("source.bin");
        fs::write(&source, b"binary content").unwrap();

        let dest = dir.join("dest.bin");
        let provider = Provider {
            provider_type: "local".to_string(),
            url: None,
            path: Some(source.to_string_lossy().to_string()),
        };

        let result = fetch_from_provider(&provider, "/tmp", &dest);
        assert!(result.is_ok());
        assert!(dest.exists());

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn test_local_provider_not_found() {
        let provider = Provider {
            provider_type: "local".to_string(),
            url: None,
            path: Some("/nonexistent/path/to/binary".to_string()),
        };

        let result = fetch_from_provider(&provider, "/tmp", Path::new("/tmp/dest.bin"));
        assert!(result.is_err());
    }

    #[test]
    fn test_provider_chain_fallback() {
        let dir = std::env::temp_dir().join("clm_manifest_test_chain");
        let _ = fs::create_dir_all(&dir);
        let source = dir.join("source_chain.bin");
        fs::write(&source, b"chain content").unwrap();

        let dest = dir.join("dest_chain.bin");
        let providers = vec![
            Provider {
                provider_type: "local".to_string(),
                url: None,
                path: Some("/nonexistent/path".to_string()),
            },
            Provider {
                provider_type: "local".to_string(),
                url: None,
                path: Some(source.to_string_lossy().to_string()),
            },
        ];

        let result = fetch_from_providers(&providers, "/tmp", &dest);
        assert!(result.is_ok());
        assert!(dest.exists());

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn test_cache_hit() {
        let dir = std::env::temp_dir().join("clm_manifest_test_cache_hit");
        let _ = fs::create_dir_all(&dir);
        let source = dir.join("source_cache.bin");
        fs::write(&source, b"cacheable content").unwrap();

        let mut hasher = Sha256::new();
        hasher.update(b"cacheable content");
        let hash = format!("{:x}", hasher.finalize());

        // First call caches the binary
        let cached1 = cache_binary(&source, &hash).unwrap();
        assert!(cached1.exists());

        // Second call should be a cache hit
        let cached2 = cache_binary(&source, &hash).unwrap();
        assert_eq!(cached1, cached2);

        let _ = fs::remove_dir_all(&dir);
        // Clean up cache
        let cache_dir = dirs_cache_dir().unwrap();
        let _ = fs::remove_file(cache_dir.join(&hash));
    }

    #[test]
    fn test_cache_miss_then_hit() {
        let dir = std::env::temp_dir().join("clm_manifest_test_cache_miss");
        let _ = fs::create_dir_all(&dir);
        let source = dir.join("source_miss.bin");
        fs::write(&source, b"miss then hit content").unwrap();

        let mut hasher = Sha256::new();
        hasher.update(b"miss then hit content");
        let hash = format!("{:x}", hasher.finalize());

        let cache_dir = dirs_cache_dir().unwrap();
        let cache_path = cache_dir.join(&hash);
        let _ = fs::remove_file(&cache_path);

        // Cache miss — should copy
        let cached = cache_binary(&source, &hash).unwrap();
        assert!(cached.exists());

        // Clean up
        let _ = fs::remove_dir_all(&dir);
        let _ = fs::remove_file(&cache_path);
    }

    #[test]
    fn test_manifest_deserialization() {
        let json = r#"{
            "name": "test_binary",
            "version": "1.0.0",
            "platforms": {
                "darwin-arm64": {
                    "path": "/usr/local/bin/test",
                    "hash": "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
                    "size": 1024,
                    "format": "raw",
                    "providers": [
                        {"type": "local", "path": "/usr/local/bin/test"}
                    ]
                }
            }
        }"#;
        let manifest: ExecutableManifest = serde_json::from_str(json).unwrap();
        assert_eq!(manifest.name.as_deref(), Some("test_binary"));
        assert_eq!(manifest.platforms.len(), 1);
        let entry = manifest.platforms.get("darwin-arm64").unwrap();
        assert_eq!(entry.format, ArchiveFormat::Raw);
        assert_eq!(entry.providers.len(), 1);
    }