proa-cli 0.2.2

CLI for scaffolding, inspecting, formatting, and validating Proa web projects
Documentation
// Copyright Proa Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use axum::{
    body::Body,
    http::{header, Request, StatusCode},
};
use tower::ServiceExt;

use __CRATE_IDENT__ as atlas;

#[tokio::test]
async fn mounted_documents_and_assets_keep_their_public_prefix() {
    for mount in ["/apps/demo", "/user/a%40b/proxy/3000"] {
        let base_path = proa::BasePath::new(mount).unwrap();
        let html = atlas::render_atlas_page_with_base_path(
            None,
            std::sync::Arc::new(atlas::loader::FixtureLoader::github()),
            base_path.clone(),
        )
        .await;
        for path in [
            "/static/favicon.svg",
            "/static/styles.css",
            "/static/proa/proa-ui/proa-free.css",
            "/static/proa/proa-ui/proa-free.js",
            "/static/origin.js",
            "/static/proa-logo.svg",
            "/site/github.com.md",
        ] {
            assert!(
                html.contains(&format!("\"{mount}{path}")),
                "missing {mount}{path}"
            );
        }
        assert!(html.contains(&format!("action=\"{mount}/\"")));
        assert!(html.contains(&format!("href=\"{mount}/?q=")));
        assert!(html.contains(&format!("src:url({mount}/_proa/immutable/fonts/")));
        assert!(html.contains(&format!("{mount}/?q=github.com")));
        for root in [
            "href=\"/static/",
            "src=\"/static/",
            "href=\"/_proa/",
            "src:url(/_proa/",
            "href=\"/site/",
        ] {
            assert!(!html.contains(root), "unprefixed URL: {root}");
        }

        let app = atlas::app_with_base_path(base_path.clone());
        let font = html
            .split(&format!("href=\"{mount}/_proa/immutable/fonts/"))
            .nth(1)
            .unwrap()
            .split('"')
            .next()
            .unwrap();
        let font = format!("/_proa/immutable/fonts/{font}");
        for path in [
            "/static/favicon.svg",
            "/static/styles.css",
            "/static/origin.js",
            font.as_str(),
        ] {
            let public = base_path.url(path).to_string();
            let forwarded = public.strip_prefix(mount).unwrap();
            let response = app
                .clone()
                .oneshot(
                    Request::builder()
                        .uri(forwarded)
                        .body(Body::empty())
                        .unwrap(),
                )
                .await
                .unwrap();
            assert_eq!(response.status(), StatusCode::OK, "{public}");
        }
        // The app serves the RSJS modules under content-hashed names. Their
        // bytes hold no mount: each one imports the runtime by relative path.
        let modules = proa::axum::RsjsAssets::prepare().unwrap();
        assert!(modules.files().len() > 2);
        for module in modules.files() {
            let response = app
                .clone()
                .oneshot(
                    Request::builder()
                        .uri(format!("/{}", module.path))
                        .body(Body::empty())
                        .unwrap(),
                )
                .await
                .unwrap();
            assert_eq!(response.status(), StatusCode::OK, "{}", module.path);
            assert_eq!(
                response.headers()[header::CACHE_CONTROL],
                "public, max-age=31536000, immutable"
            );
            let source = std::str::from_utf8(&module.bytes).unwrap();
            assert!(!source.contains(mount), "{}", module.path);
            if module.path.contains("/rsjs/island/") {
                assert!(source.contains("../runtime."), "{}", module.path);
            }
        }
    }
}

#[tokio::test]
async fn site_markdown_route_serves_the_dossier() {
    let response = __CRATE_IDENT__::app()
        .oneshot(
            Request::builder()
                .uri("/site/cloudflare.com.md")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    assert!(response.headers()[header::CONTENT_TYPE]
        .to_str()
        .unwrap()
        .starts_with("text/markdown"));
}