use core::convert::Infallible;
use super::{FanError, LaneFan};
use crate::{FanAll, Lane, LaneIdent, LaneRouter, RoutedProbe, ScriptRouter};
static FANNED: &[Lane] = &[
Lane::of(LaneIdent::new("en")).accepting(&[LaneIdent::new("latn")]),
Lane::of(LaneIdent::new("ja")).accepting(&[
LaneIdent::new("hani"),
LaneIdent::new("hira"),
LaneIdent::new("kana"),
]),
];
static SOLE: &[Lane] = &[Lane::of(LaneIdent::new("en")).accepting(&[LaneIdent::new("latn")])];
struct Astray;
impl LaneRouter for Astray {
type Question = str;
type Routed = String;
type Error = Infallible;
fn route(&self, question: &str) -> Result<Vec<RoutedProbe<String>>, Infallible> {
Ok(vec![RoutedProbe::new(
LaneIdent::new("sw"),
question.to_owned(),
)])
}
}
struct Refuses;
#[derive(Debug, PartialEq, Eq)]
struct NotToday;
impl core::fmt::Display for NotToday {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("not today")
}
}
impl core::error::Error for NotToday {}
impl LaneRouter for Refuses {
type Question = str;
type Routed = String;
type Error = NotToday;
fn route(&self, _question: &str) -> Result<Vec<RoutedProbe<String>>, NotToday> {
Err(NotToday)
}
}
struct Silent;
impl LaneRouter for Silent {
type Question = str;
type Routed = String;
type Error = Infallible;
fn route(&self, _question: &str) -> Result<Vec<RoutedProbe<String>>, Infallible> {
Ok(Vec::new())
}
}
#[test]
fn a_router_mounts_and_unmounts() {
let bare = LaneFan::<ScriptRouter>::new(FANNED);
assert!(bare.router().is_none());
assert_eq!(bare.lanes(), FANNED);
let mounted = bare.with_router(ScriptRouter::new(FANNED));
assert!(mounted.router().is_some());
let mut held = mounted;
held.set_router(None);
assert!(held.router().is_none());
}
#[test]
fn a_fanned_corpus_with_no_router_is_refused_at_boot() {
let refusal = LaneFan::<ScriptRouter>::new(FANNED)
.check()
.expect_err("two lanes, no router");
let sentence = refusal.to_string();
assert_eq!(refusal.lanes(), FANNED);
assert!(sentence.contains("2 lanes"), "{sentence}");
assert!(sentence.contains("`en`"), "{sentence}");
assert!(sentence.contains("`ja`"), "{sentence}");
assert!(sentence.contains("ScriptWeightedFan"), "{sentence}");
assert!(sentence.contains("FanAll"), "{sentence}");
}
#[test]
fn a_corpus_with_nothing_to_choose_passes_with_nothing_mounted() {
assert!(LaneFan::<ScriptRouter>::new(SOLE).check().is_ok());
static NONE: &[Lane] = &[];
assert!(LaneFan::<ScriptRouter>::new(NONE).check().is_ok());
}
#[test]
fn a_mounted_router_satisfies_the_boot_check() {
assert!(
LaneFan::new(FANNED)
.with_router(ScriptRouter::new(FANNED))
.check()
.is_ok()
);
assert!(
LaneFan::new(FANNED)
.with_router(FanAll::<str>::new(FANNED))
.check()
.is_ok()
);
}
#[test]
fn a_probe_naming_an_undeclared_lane_is_refused() {
let fan = LaneFan::new(FANNED).with_router(Astray);
let refusal = fan.route("hello").expect_err("`sw` is not a lane here");
match &refusal {
FanError::Unroutable(unroutable) => {
assert_eq!(unroutable.lane(), "sw");
assert_eq!(unroutable.lanes(), FANNED);
let sentence = unroutable.to_string();
assert!(sentence.contains("`sw`"), "{sentence}");
assert!(sentence.contains("`en`"), "{sentence}");
}
other => panic!("expected the unroutable arm, got {other}"),
}
assert!(refusal.to_string().contains("does not hold"));
}
#[test]
fn an_empty_split_is_the_empty_set() {
let fan = LaneFan::new(FANNED).with_router(Silent);
assert!(fan.route("anything").expect("empty is legal").is_empty());
let shipped = LaneFan::new(FANNED).with_router(ScriptRouter::new(FANNED));
assert!(shipped.route("").expect("no runs").is_empty());
}
#[test]
fn a_routers_own_refusal_crosses_untouched() {
let fan = LaneFan::new(FANNED).with_router(Refuses);
match fan.route("hello").expect_err("this router refuses") {
FanError::Router(NotToday) => {}
other => panic!("expected the router arm, got {other}"),
}
}
#[test]
fn routing_with_nothing_mounted_refuses() {
let fan = LaneFan::<ScriptRouter>::new(FANNED);
match fan.route("hello").expect_err("nothing is mounted") {
FanError::Missing(missing) => assert_eq!(missing.lanes(), FANNED),
other => panic!("expected the missing arm, got {other}"),
}
}
#[test]
fn a_guarded_route_passes_the_routers_answer_through() {
let fan = LaneFan::new(FANNED).with_router(ScriptRouter::new(FANNED));
let dispatches = fan.route("日本語 report").expect("both scripts land");
assert_eq!(dispatches.len(), 2);
assert_eq!(dispatches[0].lane(), LaneIdent::new("ja"));
assert_eq!(dispatches[1].lane(), LaneIdent::new("en"));
}
#[test]
fn the_refusal_sum_carries_each_cause() {
let missing: FanError<NotToday> = FanError::Missing(super::RouterMissing::new(FANNED));
let router: FanError<NotToday> = FanError::Router(NotToday);
assert!(core::error::Error::source(&missing).is_some());
assert!(core::error::Error::source(&router).is_some());
assert!(router.to_string().contains("not today"));
}