#![cfg(feature = "module")]
mod common;
use std::collections::BTreeMap;
use laravel::container::Registrar;
use laravel::prelude::*;
use laravel::testing::prelude::*;
use saola_kit::SaolaServiceProvider;
use saola_kit::context::{
ContextRoutes, ContextSpec, ContextTarget, HrefOptions, RouteIndex, current_context,
};
use saola_kit::module::{ModuleRegistry, SaolaModule};
struct ChatModule;
impl SaolaModule for ChatModule {
fn name(&self) -> &'static str {
"chat"
}
fn priority(&self) -> i32 {
10
}
fn register(&self, app: &mut Registrar) {
app.instance("chat-ready".to_owned());
}
fn web_routes(&self, route: &mut Router) {
route.get("/tin-nhan", async |msg: Inject<String>| (*msg).clone());
}
fn api_routes(&self, route: &mut Router) {
route.get("/chat/ping", async || "pong");
}
}
struct AlphaModule;
impl SaolaModule for AlphaModule {
fn name(&self) -> &'static str {
"alpha"
}
fn context(&self) -> &'static str {
"admin"
}
fn priority(&self) -> i32 {
1
}
}
struct BetaModule;
impl SaolaModule for BetaModule {
fn name(&self) -> &'static str {
"beta"
}
fn priority(&self) -> i32 {
1
}
}
#[test]
fn module_registry_sorts_by_priority_desc_then_name() {
let mut registry = ModuleRegistry::new();
registry.add(BetaModule).add(AlphaModule).add(ChatModule);
let names: Vec<&'static str> = registry.metadata().into_iter().map(|m| m.name).collect();
assert_eq!(names, vec!["chat", "alpha", "beta"]);
}
#[tokio::test]
async fn saola_service_provider_registers_module_services_and_routes() {
let provider = SaolaServiceProvider::new().with_module(ChatModule);
let app = common::configure()
.with_providers(move |providers| {
providers.add(provider);
})
.create();
app.get("/tin-nhan")
.await
.assert_ok()
.assert_see("chat-ready");
app.get("/api/chat/ping")
.await
.assert_ok()
.assert_see("pong");
}
struct MultiContextDemoModule;
impl SaolaModule for MultiContextDemoModule {
fn name(&self) -> &'static str {
"demo"
}
fn routes(&self, ctx: &mut ContextRoutes<'_>) {
if ctx.is("web") {
ctx.module("demo", "/", |m| {
m.get("/demo", async || {
current_context().unwrap_or_default()
})
.name("home");
});
} else if ctx.is("admin") {
ctx.module("demo", "/demo", |m| {
m.get("/", async || current_context().unwrap_or_default())
.name("dashboard");
});
} else if ctx.is("api") {
ctx.module("demo", "/demo", |m| {
m.get("/ping", async || {
current_context().unwrap_or_default()
})
.name("ping");
});
} else if ctx.is("hostadmin") {
ctx.module("demo", "/", |m| {
m.get("/panel", async || {
current_context().unwrap_or_default()
})
.name("panel");
});
}
}
}
#[tokio::test]
async fn saola_module_routes_across_multiple_contexts_and_domains() -> laravel::Result<()> {
let provider = SaolaServiceProvider::new()
.with_context(
ContextSpec::new("web")
.with_prefix("/")
.with_target(ContextTarget::WebHydrate)
.with_priority(0),
)
.with_context(
ContextSpec::new("admin")
.with_prefix("/admin")
.with_target(ContextTarget::WebHydrate)
.with_priority(5),
)
.with_context(
ContextSpec::new("api")
.with_prefix("/api")
.with_target(ContextTarget::Api)
.with_priority(10),
)
.with_context(
ContextSpec::new("hostadmin")
.with_hosts(["admin.example.test"])
.with_target(ContextTarget::SpaOnly)
.with_priority(20),
)
.with_module(MultiContextDemoModule);
let app = common::configure()
.with_providers(move |providers| {
providers.add(provider);
})
.create();
app.get("/demo").await.assert_ok().assert_see("web");
app.get("/admin/demo").await.assert_ok().assert_see("admin");
app.get("/api/demo/ping").await.assert_ok().assert_see("api");
app.get("/panel")
.header("host", "admin.example.test")
.await
.assert_ok()
.assert_see("hostadmin");
app.get("/panel")
.header("host", "other.example.test")
.await
.assert_not_found();
app.scope(async || {
let index = laravel::resolve::<RouteIndex>().await?;
let params = BTreeMap::new();
let in_ctx = |ctx: &str| HrefOptions {
request_host: Some("localhost".to_owned()),
context: Some(ctx.to_owned()),
..HrefOptions::default()
};
assert_eq!(index.href("web.demo.home", ¶ms, &in_ctx("web"))?, "/demo");
assert_eq!(
index.href("admin.demo.dashboard", ¶ms, &in_ctx("admin"))?,
"/admin/demo"
);
assert_eq!(
index.href("api.demo.ping", ¶ms, &in_ctx("api"))?,
"/api/demo/ping"
);
assert_eq!(
index.href("hostadmin.demo.panel", ¶ms, &in_ctx("web"))?,
"http://admin.example.test/panel"
);
Ok::<_, laravel::Error>(())
})
.await?;
Ok(())
}