use clap::Parser as _;
use super::{Cli, Ticket, TokenPolicy, qr, token_line, token_policy, undialable};
const NAMES_NOWHERE: &str = "pipeadlvvgabqkyqvn6vjp7nhslea45a5yls6pnkmizfv4bbu2hxa5iruaaauhlp2na";
const NAMES_BOTH: &str = "pipeadlvvgabqkyqvn6vjp7nhslea45a5yls6pnkmizfv4bbu2hxa5iruaqaaangq5duobztulzpojswyylzfzsxqylnobwgkltdn5ws6aiaa3akqaihcfiqbrp5xr4q";
const NAMES_ONE_ADDRESS: &str = "pipeadlvvgabqkyqvn6vjp7nhslea45a5yls6pnkmizfv4bbu2hxa5iruaicaajcaainxaaaaaaaaaaaaaaaaaaach4qaabstehw";
#[test]
fn the_flag_surface_is_internally_consistent() {
use clap::CommandFactory;
Cli::command().debug_assert();
}
#[test]
fn a_supplied_token_wins_and_absence_generates_one() {
assert!(matches!(
token_policy(Some("k".to_owned()), None, false).unwrap(),
TokenPolicy::Supplied(t) if t == "k"
));
assert!(matches!(
token_policy(None, None, false).unwrap(),
TokenPolicy::Generate
));
assert!(matches!(
token_policy(None, None, true).unwrap(),
TokenPolicy::InsecureNoAuth
));
}
#[test]
fn a_token_file_is_read_and_its_trailing_newline_trimmed() {
let dir = std::env::temp_dir().join(format!("modelpipe-cli-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
for (contents, expected) in [
("secret", "secret"),
("secret\n", "secret"),
("secret\r\n", "secret"),
("secret\n\n", "secret"),
] {
let path = dir.join("token");
std::fs::write(&path, contents).unwrap();
assert!(
matches!(
token_policy(None, Some(path.clone()), false).unwrap(),
TokenPolicy::Supplied(t) if t == expected
),
"{contents:?} should yield {expected:?}"
);
}
let path = dir.join("token");
std::fs::write(&path, "\n").unwrap();
assert!(token_policy(None, Some(path), false).is_err());
let missing = dir.join("does-not-exist");
assert!(token_policy(None, Some(missing), false).is_err());
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn the_network_flags_parse_on_both_subcommands() {
let serve = Cli::try_parse_from([
"modelpipe",
"serve",
"http://127.0.0.1:11434",
"--no-portmap",
"--no-discovery",
"--relay-only",
])
.expect("serve accepts all three");
assert!(matches!(
serve.command,
super::Command::Serve {
no_portmap: true,
no_discovery: true,
relay_only: true,
..
}
));
let connect = Cli::try_parse_from([
"modelpipe",
"connect",
"pipeticket",
"--relay",
"https://relay.example.com/",
"--no-portmap",
"--no-discovery",
"--relay-only",
])
.expect("connect accepts all four");
assert!(matches!(
connect.command,
super::Command::Connect {
no_portmap: true,
no_discovery: true,
relay_only: true,
relay: Some(ref r),
..
} if r == "https://relay.example.com/"
));
}
#[test]
fn the_network_flags_are_all_off_by_default() {
let serve =
Cli::try_parse_from(["modelpipe", "serve", "http://127.0.0.1:11434"]).expect("no flags");
assert!(matches!(
serve.command,
super::Command::Serve {
no_portmap: false,
no_discovery: false,
relay_only: false,
..
}
));
}
#[test]
fn the_qr_encodes_an_uppercased_ticket_that_still_parses() {
let text = "pipeadlvvgabqkyqvn6vjp7nhslea45a5yls6pnkmizfv4bbu2hxa5iruaaauhlp2na";
let ticket: Ticket = text.parse().expect("a normative vector");
let upper = ticket.to_string().to_uppercase();
assert_eq!(
upper.parse::<Ticket>().expect("the scan must parse"),
ticket,
"an upcased ticket is the same ticket"
);
assert!(qr(&ticket).is_some(), "and it must fit a QR code");
}
#[test]
fn an_empty_supplied_token_is_refused_rather_than_enforced() {
for empty in ["", " ", "\t", "\n"] {
assert!(
token_policy(Some(empty.to_owned()), None, false).is_err(),
"{empty:?} must not become a credential"
);
}
assert!(matches!(
token_policy(Some(" sk-real ".to_owned()), None, false).unwrap(),
TokenPolicy::Supplied(t) if t == " sk-real "
));
}
#[test]
fn the_help_text_never_renders_the_environment_token() {
use clap::CommandFactory;
let serve = Cli::command();
let serve = serve
.get_subcommands()
.find(|c| c.get_name() == "serve")
.expect("serve");
let token = serve
.get_arguments()
.find(|a| a.get_id() == "token")
.expect("--token");
assert!(
token.is_hide_env_values_set(),
"`--help` would print the value of MODELPIPE_TOKEN"
);
}
#[test]
fn verbosity_is_accepted_on_either_side_of_the_subcommand() {
let leading = Cli::try_parse_from(["modelpipe", "-vv", "serve", "http://127.0.0.1:11434"])
.expect("-vv before the subcommand");
let trailing = Cli::try_parse_from(["modelpipe", "serve", "http://127.0.0.1:11434", "-vv"])
.expect("-vv after the subcommand");
assert_eq!(leading.verbose, 2);
assert_eq!(trailing.verbose, 2);
}
#[test]
fn verbosity_counts_what_was_typed() {
let none = Cli::try_parse_from(["modelpipe", "serve", "http://127.0.0.1:11434"]).expect("none");
let one =
Cli::try_parse_from(["modelpipe", "serve", "http://127.0.0.1:11434", "-v"]).expect("one");
let spelled = Cli::try_parse_from([
"modelpipe",
"serve",
"http://127.0.0.1:11434",
"--verbose",
"--verbose",
"--verbose",
])
.expect("the long form, thrice");
assert_eq!((none.verbose, one.verbose, spelled.verbose), (0, 1, 3));
}
#[test]
fn a_supplied_token_is_named_rather_than_printed() {
let line = token_line(true, Some("sk-secret".to_owned())).expect("auth is on");
assert_eq!(line, "token: (supplied)");
assert!(
!line.contains("sk-secret"),
"the supplied credential must not reach stdout: {line}"
);
}
#[test]
fn a_generated_token_is_printed_in_full() {
assert_eq!(
token_line(false, Some("sk-minted".to_owned())).expect("auth is on"),
"token: sk-minted"
);
}
#[test]
fn serving_open_produces_no_token_line() {
assert!(token_line(false, None).is_none());
assert!(token_line(true, None).is_none());
}
#[test]
fn a_ticket_that_names_nowhere_is_refused_with_the_cause_that_emptied_it() {
let empty: Ticket = NAMES_NOWHERE.parse().expect("a normative vector");
let under_relay_only = undialable(&empty, true).expect("nothing could dial this");
assert!(
under_relay_only.contains("--relay-only"),
"the switch that removed the addresses has to be named: {under_relay_only}"
);
assert!(
under_relay_only.contains("no relay in 10s"),
"and so does the wait that found no relay: {under_relay_only}"
);
let without = undialable(&empty, false).expect("nothing could dial this either");
assert!(
!without.contains("--relay-only"),
"a flag nobody passed must not be blamed: {without}"
);
assert!(
without.contains("no address of its own"),
"the local half has to be named too: {without}"
);
}
#[test]
fn a_ticket_that_names_anywhere_at_all_is_printed() {
for vector in [NAMES_BOTH, NAMES_ONE_ADDRESS] {
let ticket: Ticket = vector.parse().expect("a normative vector");
for relay_only in [true, false] {
assert_eq!(
undialable(&ticket, relay_only),
None,
"{vector} names somewhere and must not be refused"
);
}
}
}
#[test]
fn the_token_line_aligns_with_the_ticket_line() {
let token = token_line(false, Some("x".to_owned())).expect("auth is on");
let ticket = "ticket: pipeabc";
assert_eq!(
token.find('x'),
ticket.find('p'),
"the value columns must agree: {token:?} vs {ticket:?}"
);
}