use std::fs;
use std::path::Path;
#[allow(unused_imports)]
use useceleris_client::{
CelerisError, Channel, ChannelEventHandler, ChannelState, Client, ClientOptions,
CredentialFuture, CredentialProvider, CredentialReason, CredentialRequest, Credentials,
MessageMetadata, PayloadCodec, PresenceConnection, PresenceEvent, PresencePage, ProviderError,
RecoveryEvent, Registration, Segment, ServerError, ServerErrorResource, ServerErrorType,
ServerNotice, Subscription, read_text, text_payload,
};
const PUBLIC_ITEMS: [&str; 27] = [
"CelerisError",
"Channel",
"ChannelEventHandler",
"ChannelState",
"Client",
"ClientOptions",
"CredentialFuture",
"CredentialProvider",
"CredentialReason",
"CredentialRequest",
"Credentials",
"MessageMetadata",
"PayloadCodec",
"PresenceConnection",
"PresenceEvent",
"PresencePage",
"ProviderError",
"RecoveryEvent",
"Registration",
"Segment",
"ServerError",
"ServerErrorResource",
"ServerErrorType",
"ServerNotice",
"Subscription",
"read_text",
"text_payload",
];
#[test]
fn the_public_surface_is_exactly_the_pinned_list() {
let source = fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("src/lib.rs"))
.expect("lib.rs is readable");
let mut exported: Vec<String> = source
.split("pub use ")
.skip(1)
.flat_map(|statement| {
let statement = statement.split(';').next().unwrap_or_default();
let names = statement.rsplit("::").next().unwrap_or_default();
names
.trim_matches(|character: char| {
character == '{' || character == '}' || character.is_whitespace()
})
.split(',')
.map(|name| name.trim().to_owned())
.filter(|name| !name.is_empty())
.collect::<Vec<_>>()
})
.collect();
exported.sort();
let mut expected: Vec<String> = PUBLIC_ITEMS.iter().map(|name| (*name).to_owned()).collect();
expected.sort();
assert_eq!(exported, expected);
}
#[test]
fn the_runtime_dependencies_are_exactly_the_reviewed_ones() {
let manifest = fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml"))
.expect("Cargo.toml is readable");
let dependencies = manifest
.split("[dependencies]")
.nth(1)
.and_then(|section| section.split("\n[").next())
.expect("Cargo.toml has a dependencies section");
let names: Vec<&str> = dependencies
.lines()
.filter_map(|line| line.split_once('=').map(|(name, _)| name.trim()))
.filter(|name| !name.is_empty())
.collect();
assert_eq!(
names,
[
"futures-util",
"getrandom",
"rustls",
"rustls-native-certs",
"tokio",
"tokio-tungstenite"
]
);
}
#[test]
fn the_crate_prints_and_logs_nothing() {
let source_directory = Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
let mut offenders = Vec::new();
for entry in fs::read_dir(&source_directory)
.expect("src is readable")
.flatten()
{
let path = entry.path();
if path.extension().is_none_or(|extension| extension != "rs") {
continue;
}
let source = fs::read_to_string(&path).expect("source files are readable");
let code: String = source
.lines()
.filter(|line| !line.trim_start().starts_with("//"))
.collect::<Vec<_>>()
.join("\n");
for forbidden in [
"println!",
"eprintln!",
"print!(",
"eprint!(",
"dbg!(",
"log::",
] {
if code.contains(forbidden) {
offenders.push(format!("{}: {forbidden}", path.display()));
}
}
}
assert!(offenders.is_empty(), "{offenders:?}");
}