tauri-plugin-compmd 0.13.0

The Tauri adapter of CompMD's hosting: embeds the bundle @compmd/vite writes, serves it on a scheme of the app's own, and gives the page its origin. In development.
Documentation
use compmd_host::Bundle;
use compmd_host::registry::{LocalRegistry, ServedPack};
use tauri::http::{Request, StatusCode};
use tauri::test::{MockRuntime, mock_builder, mock_context, noop_assets};

use super::*;

const HASH: &str = "1df336e75685de8c";

/// A bundle built for `source`, with a frame document and a pack's file.
pub fn bundle(source: Option<&str>) -> Bundle {
    let source = source.map_or_else(String::new, |source| format!(r#", "source": "{source}""#));
    let manifest = format!(
        r#"{{"format": 1, "frames": {{"hash": "{HASH}"}}{source}, "packs": [{{"pack": "@kit/bars", "version": "1.0.0", "trust": "strict"}}]}}"#
    );
    Bundle::from_files([
        ("manifest.json".to_owned(), manifest.into_bytes()),
        (
            format!("frames/{HASH}/strict.html"),
            b"<!doctype html>".to_vec(),
        ),
        (
            "packs/@kit/bars@1.0.0/package.json".to_owned(),
            br#"{"name": "@kit/bars"}"#.to_vec(),
        ),
    ])
    .expect("a bundle")
}

/// An app on Tauri's mock runtime with `plugin`.
fn app(plugin: TauriPlugin<MockRuntime>) -> tauri::App<MockRuntime> {
    mock_builder()
        .plugin(plugin)
        .build(mock_context(noop_assets()))
        .expect("the app builds")
}

/// A webview's `GET` of `path` on the scheme.
fn get(path: &str) -> Request<Vec<u8>> {
    Request::get(format!("compmd://localhost{path}"))
        .body(Vec::new())
        .expect("a request")
}

#[test]
fn the_bundle_is_served_on_its_scheme_through_the_contract() {
    let app = app(init(bundle(Some("compmd://localhost/"))));
    let compmd = app.compmd();
    assert_eq!(compmd.origin(), Some(origin::origin_of("compmd").as_str()));
    let frame = compmd.respond(&get(&format!("/frames/{HASH}/strict.html")));
    assert_eq!(frame.status(), StatusCode::OK);
    assert_eq!(frame.body(), b"<!doctype html>");
    assert_eq!(
        frame.headers()["content-security-policy"],
        "sandbox allow-scripts"
    );
    let pack = compmd.respond(&get("/packs/@kit/bars@1.0.0/package.json"));
    assert_eq!(pack.status(), StatusCode::OK);
    assert_eq!(
        compmd.respond(&get("/manifest.json")).status(),
        StatusCode::NOT_FOUND
    );
}

/// A bundle for `compmd://localhost/` whose configuration fetches `@kit` from `registry`, `fetch` by whom.
fn bundle_fetching(registry: &str, fetch: &str) -> Bundle {
    let manifest = format!(
        r#"{{"format": 1, "frames": {{"hash": "{HASH}"}}, "source": "compmd://localhost/", "registries": {{"@kit": "{registry}"}}, "fetch": "{fetch}", "maxPackSize": 100000}}"#
    );
    Bundle::from_files([("manifest.json".to_owned(), manifest.into_bytes())]).expect("a bundle")
}

#[test]
fn registry_is_the_proxy_of_the_registries_the_host_fetches_from() {
    let notes = ServedPack::new("@kit/notes").version("1.0.0", &[("package.json", "{}")]);
    let registry = LocalRegistry::serve(&[notes]).expect("served");
    let app = app(init(bundle_fetching(registry.url(), "host")));
    let metadata = app.compmd().respond(&get("/registry/@kit%2fnotes"));
    assert_eq!(metadata.status(), StatusCode::OK);
    assert_eq!(metadata.headers()["access-control-allow-origin"], "*");
    let packument: serde_json::Value = serde_json::from_slice(metadata.body()).expect("JSON");
    let tarball = format!(
        "{}/registry/-/tarball/@kit/notes/1.0.0",
        origin::origin_of("compmd")
    );
    assert_eq!(
        packument["versions"]["1.0.0"]["dist"]["tarball"],
        tarball.as_str()
    );
    let bytes = app
        .compmd()
        .respond(&get("/registry/-/tarball/@kit/notes/1.0.0"));
    assert_eq!(bytes.status(), StatusCode::OK);
    assert!(bytes.body().starts_with(&[0x1f, 0x8b]), "gzip");
    let unknown = app.compmd().respond(&get("/registry/@acme%2fother"));
    assert_eq!(unknown.status(), StatusCode::NOT_FOUND);
}

#[test]
fn without_fetching_through_the_host_there_is_no_proxy() {
    let registry = LocalRegistry::serve(&[]).expect("served");
    let direct = app(init(bundle_fetching(registry.url(), "direct")));
    let refused = direct.compmd().respond(&get("/registry/@kit%2fnotes"));
    assert_eq!(refused.status(), StatusCode::NOT_FOUND);
    let none = app(init(bundle(Some("compmd://localhost/"))));
    let refused = none.compmd().respond(&get("/registry/@kit%2fnotes"));
    assert_eq!(refused.status(), StatusCode::NOT_FOUND);
    assert_eq!(registry.asked(), Vec::<String>::new());
}

#[test]
fn the_store_is_in_the_apps_data_folder() {
    let app = app(init(bundle(Some("compmd://localhost/"))));
    let releases = app
        .compmd()
        .store()
        .releases(&"@kit/other".parse().expect("a name"));
    assert_eq!(releases, []);
}

#[test]
fn without_a_data_folder_the_store_holds_nothing() {
    let store = store_in(None);
    assert_eq!(store.releases(&"@kit/bars".parse().expect("a name")), []);
}

#[test]
fn an_app_without_a_bundle_serves_nothing_and_says_why() {
    let app = app(init(None));
    let compmd = app.compmd();
    assert!(compmd.bundle().is_none());
    assert_eq!(compmd.origin(), None);
    let refused = compmd.respond(&get(&format!("/frames/{HASH}/strict.html")));
    assert_eq!(refused.status(), StatusCode::NOT_FOUND);
    assert!(String::from_utf8_lossy(refused.body()).contains("@compmd/vite"));
    // The contract's refusal headers, so the page can read why from the frame's origin.
    assert_eq!(refused.headers()["access-control-allow-origin"], "*");
    assert_eq!(
        refused.headers()["content-type"],
        "text/plain; charset=utf-8"
    );
}

#[cfg(feature = "processor")]
#[test]
fn the_processor_gets_the_packs_the_frames_load() {
    let app = app(init(bundle(Some("compmd://localhost/"))));
    assert!(app.compmd().packs().is_some());
    assert!(self::app(init(None)).compmd().packs().is_none());
}

#[test]
fn the_page_gets_the_origin_from_a_script_as_createsandbox_reads_it() {
    assert_eq!(
        origin::init_script("compmd://localhost"),
        "globalThis.__COMPMD__ = Object.freeze({ origin: \"compmd://localhost\" });"
    );
}

#[test]
fn the_origin_is_the_schemes_as_the_platform_serves_it() {
    assert_eq!(origin::origin_on("frames", true), "http://frames.localhost");
    assert_eq!(origin::origin_on("frames", false), "frames://localhost");
    let over_http = cfg!(any(windows, target_os = "android"));
    assert_eq!(
        origin::origin_of("frames"),
        origin::origin_on("frames", over_http)
    );
}

#[test]
fn a_schemes_name_is_lowercase_letters_digits_and_plus_minus_dot() {
    for good in ["compmd", "my-frames", "app+frames", "a.b", "x1"] {
        assert!(origin::is_scheme(good), "{good}");
    }
    for bad in ["", "1app", "App", "my frames", "a/b", "a:b", "-a"] {
        assert!(!origin::is_scheme(bad), "{bad}");
    }
}

#[test]
#[should_panic(expected = "built without a `source`")]
fn a_bundle_built_for_http_is_a_mistake_in_the_app() {
    drop(init::<MockRuntime>(bundle(None)));
}

#[test]
#[should_panic(expected = "isn't a scheme's name")]
fn a_bad_scheme_is_a_mistake_in_the_app() {
    drop(init::<MockRuntime>(bundle(Some("Not+A://localhost/"))));
}

#[test]
fn the_embedded_files_are_a_bundle_or_none() {
    static FILES: &[(&str, &[u8])] = &[(
        "manifest.json",
        br#"{"format": 1, "frames": {"hash": "1df336e75685de8c"}, "source": "compmd://localhost/"}"#,
    )];
    assert!(embedded(Some(FILES)).is_some());
    assert!(embedded(None).is_none());
}

#[test]
#[should_panic(expected = "the embedded bundle can't be served: ")]
fn embedded_files_that_arent_a_bundle_are_a_mistake_in_the_app_that_says_why() {
    drop(embedded(Some(&[])));
}