saola-kit 0.1.1

Saola core toolkit for Laravel for Rust (contexts, envelope protocol, i18n, masks, modules, Sanctum-compatible tokens)
Documentation
use std::collections::BTreeMap;

use saola_kit::context::{
    ContextError, ContextHosts, ContextLinksRule, ContextSpec, ContextTarget, HrefOptions,
    RouteEntry, RouteIndex,
};

fn sample_index() -> RouteIndex {
    let contexts = vec![
        ContextSpec::new("web")
            .with_hosts(["https://vn360plus.com:8443/path", "www.vn360plus.com", "vn360plus.com"])
            .with_target(ContextTarget::WebHydrate)
            .with_links_rule(ContextLinksRule::Public),
        ContextSpec::new("admin")
            .with_hosts(["admin.vn360plus.com"])
            .with_target(ContextTarget::SpaOnly)
            .with_links_rule(ContextLinksRule::Ability("access-admin".to_owned())),
        ContextSpec::new("internal")
            .with_hosts(["internal.vn360plus.com"])
            .with_links_rule(ContextLinksRule::Private),
    ];

    let routes = vec![
        RouteEntry::new("web.home", "/"),
        RouteEntry::new("web.chat.show", "/tin-nhan/{id?}"),
        RouteEntry::new("web.posts.show", "/blog/{slug}"),
        RouteEntry::new("admin.posts.show", "/posts/{slug}"),
        RouteEntry::new("admin.dashboard", "/dashboard"),
        RouteEntry::new("internal.health", "/health"),
    ];

    RouteIndex::new(contexts, routes, "https://vn360plus.com").expect("valid index")
}

#[test]
fn normalizes_and_deduplicates_hosts_in_order() {
    let hosts = ContextHosts::normalize([
        "  https://App.Example.COM:8080/dashboard ",
        "app.example.com",
        "",
        "   ",
        "http://www.example.com/",
    ]);
    assert_eq!(hosts, vec!["app.example.com", "www.example.com"]);
}

#[test]
fn rejects_duplicate_host_across_contexts() {
    let contexts = vec![
        ContextSpec::new("web").with_hosts(["app.test"]),
        ContextSpec::new("admin").with_hosts(["APP.TEST"]),
    ];
    let err = ContextHosts::table(&contexts).unwrap_err();
    assert_eq!(
        err,
        ContextError::DuplicateHost {
            host: "app.test".to_owned(),
            first: "web".to_owned(),
            second: "admin".to_owned(),
        }
    );
}

#[test]
fn resolves_relative_same_host_and_cross_host_absolute_urls() {
    let index = sample_index();

    let web_opts = HrefOptions {
        request_host: Some("www.vn360plus.com".to_owned()),
        request_scheme: Some("https".to_owned()),
        current_route_name: Some("web.home".to_owned()),
        ..Default::default()
    };

    // Same context & same host -> relative path
    let mut params = BTreeMap::new();
    params.insert("id".to_owned(), "42".to_owned());
    params.insert("tab".to_owned(), "all".to_owned());
    assert_eq!(
        index.href("chat.show", &params, &web_opts).unwrap(),
        "/tin-nhan/42?tab=all"
    );

    // Optional parameter omitted
    assert_eq!(
        index
            .href("chat.show", &BTreeMap::new(), &web_opts)
            .unwrap(),
        "/tin-nhan"
    );

    // Cross-context link -> absolute URL with target's canonical host
    assert_eq!(
        index
            .href("admin.dashboard", &BTreeMap::new(), &web_opts)
            .unwrap(),
        "https://admin.vn360plus.com/dashboard"
    );

    // Explicit absolute on same host preserves request host & non-default dev port
    let dev_opts = HrefOptions {
        request_host: Some("www.vn360plus.com".to_owned()),
        request_scheme: Some("http".to_owned()),
        request_port: Some(8099),
        current_route_name: Some("web.home".to_owned()),
        absolute: true,
        ..Default::default()
    };
    assert_eq!(
        index.href("home", &BTreeMap::new(), &dev_opts).unwrap(),
        "http://www.vn360plus.com:8099/"
    );
}

#[test]
fn detects_ambiguous_and_missing_context_routes() {
    let index = sample_index();

    let no_ctx = index.href("posts.show", &BTreeMap::new(), &HrefOptions::default());
    assert!(matches!(no_ctx, Err(ContextError::MissingContext { .. })));

    let other_ctx = HrefOptions::default().with_context("internal");
    let ambig = index.href("posts.show", &BTreeMap::new(), &other_ctx);
    assert!(matches!(ambig, Err(ContextError::AmbiguousRoute { .. })));

    let full = index
        .href("web.home", &BTreeMap::new(), &HrefOptions::default())
        .unwrap();
    assert_eq!(full, "https://vn360plus.com/");
}

#[test]
fn filters_client_links_by_ability() {
    let index = sample_index();

    let guest_links = index.client_links("web", &[]);
    assert!(!guest_links.links.contains_key("admin.dashboard"));
    assert!(!guest_links.links.contains_key("internal.health"));

    let admin_links = index.client_links("web", &["access-admin"]);
    assert!(admin_links.links.contains_key("admin.dashboard"));
    assert!(!admin_links.links.contains_key("internal.health"));
}