summaryrefslogtreecommitdiff
path: root/tests/i2p_functions.rs
blob: b07631adcf50c6f812024afcf24b7cd8cb280be3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use mesk::net::i2p_package::I2PPackage;
use mesk::pkgtoolkit::{Archs, Package};

mod shared;
use shared::create_test_config;

use std::collections::HashMap;
use tempfile::TempDir;
use tokio;

#[cfg(test)]
mod i2p_package_tests {
    use super::*;

    // fn parse_http_response(response_bytes: &[u8]) -> Result<(u16, &[u8]), Box<dyn std::error::Error>> { ... }
    // #[test]
    // fn test_parse_http_response() { ... }
    /*
    #[tokio::test]
    async fn test_i2p_fetch_index_success() -> Result<(), Box<dyn std::error::Error>> {
        let temp_config_dir = TempDir::new()?;
        let config = create_test_config(temp_config_dir.path().to_str().unwrap());
        let mut config_with_mock_url = config.clone();
        config_with_mock_url.repo.repo_url = "http://dummy.i2p/repo";

        let mut mock_session = MockSession::new();
        mock_session
            .expect_connect()
            .with(eq("dummy.i2p"))
            .returning(|_| {
                // Возвращаем мок-поток, который возвращает байты архива
                let index_toml_content = r#"[[packages]] name = "test-pkg" ... "#;
                let mut archive_data = Vec::new();
                // ... заполняем archive_data как в test_http_fetch_index_success ...
                let response_body = format!(
                    "HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n{}",
                    archive_data.len(),
                    std::str::from_utf8(&archive_data).unwrap()
                ).into_bytes();
                Ok(MockStream::new(response_body))
            });


        let mut i2p_pkg = I2PPackage::new_with_session(config_with_mock_url, Box::new(mock_session));

        let result = i2p_pkg.fetch_index().await;

        assert!(result.is_ok());

        Ok(())
    }
    */
    #[tokio::test]
    async fn test_i2p_fetch_package_info_success() -> Result<(), Box<dyn std::error::Error>> {
        let temp_config_dir = TempDir::new()?;
        let config = create_test_config(temp_config_dir.path().to_str().unwrap());

        let mut i2p_pkg = I2PPackage::new(config);
        let mut packages_map = HashMap::new();
        let pkg = Package {
            name: "test-pkg".to_string(),
            version: "1.0.0".to_string(),
            arch: Archs::X86_64,
            descr: Some("Test".to_string()),
            license: Some("MIT".to_string()),
            url: "http://example.i2p/repo/test-pkg.mesk".to_string(),
        };
        packages_map.insert("test-pkg".to_string(), pkg);
        i2p_pkg.index_packages = Some(packages_map);

        let pkg_info = i2p_pkg.fetch_package_info("test-pkg");

        assert!(pkg_info.is_ok());
        assert_eq!(pkg_info.unwrap().name, "test-pkg");

        Ok(())
    }

    #[tokio::test]
    async fn test_i2p_fetch_package_info_not_found() -> Result<(), Box<dyn std::error::Error>> {
        let temp_config_dir = TempDir::new()?;
        let config = create_test_config(temp_config_dir.path().to_str().unwrap());

        let i2p_pkg = I2PPackage::new(config.clone()); // index_packages = None
        let result = i2p_pkg.fetch_package_info("nonexistent-pkg");

        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("Index not loaded"));

        let mut i2p_pkg_empty_index = I2PPackage::new(config);
        i2p_pkg_empty_index.index_packages = Some(HashMap::new());
        let result_empty = i2p_pkg_empty_index.fetch_package_info("nonexistent-pkg");

        assert!(result_empty.is_err());
        let err_msg_empty = result_empty.unwrap_err().to_string();
        assert!(err_msg_empty.contains("not found in index"));

        Ok(())
    }
}