fn config_id(primary: &str, extra: &str) -> String {
format!(
"packs=[kg];db=:memory:;embed={primary};extra=[{extra}];fresh_tail=true;\
blob_hydration_bytes=268435456;backend=Sqlite;outbound=[];git_write=policy;\
brain=readers;telemetry=default;display_tz=UTC"
)
}
#[test]
fn an_available_extra_embedder_does_not_block_daemon_reuse() {
let client = config_id("p", "");
let daemon = config_id("p", "m");
assert!(super::config_ids_compatible(&client, &daemon));
}
#[test]
fn a_missing_requested_extra_embedder_is_named_and_refused() {
let client = config_id("p", "m");
let daemon = config_id("p", "");
assert!(!super::config_ids_compatible(&client, &daemon));
assert_eq!(
super::first_config_mismatch_field(&client, Some(&daemon)),
"extra"
);
}
#[test]
fn extra_embedder_order_and_duplicates_do_not_block_daemon_reuse() {
let client = config_id("p", "a,b,a");
let daemon = config_id("p", "b,a,c,b");
assert!(super::config_ids_compatible(&client, &daemon));
}
#[test]
fn a_primary_repeated_as_an_extra_remains_available_to_the_client() {
let client = config_id("AllMiniLmL6V2", "");
let daemon = config_id("AllMiniLmL6V2", "AllMiniLmL6V2");
assert!(super::config_ids_compatible(&client, &daemon));
assert_eq!(
super::config_id_extra_embedder_exclusions(&client, &daemon),
Some(vec![]),
"the request may still use its primary model"
);
let client_with_primary_extra = config_id("AllMiniLmL6V2", "AllMiniLmL6V2");
let daemon_without_extra = config_id("AllMiniLmL6V2", "");
assert!(super::config_ids_compatible(
&client_with_primary_extra,
&daemon_without_extra
));
}
#[test]
fn a_daemon_extra_matching_the_primary_is_not_hidden_with_other_extras() {
let client = config_id("AllMiniLmL6V2", "BgeSmallEnV15");
let daemon = config_id("AllMiniLmL6V2", "AllMiniLmL6V2,BgeSmallEnV15");
assert!(super::config_ids_compatible(&client, &daemon));
assert_eq!(
super::config_id_extra_embedder_exclusions(&client, &daemon),
Some(vec![])
);
}
#[test]
fn a_legacy_exact_match_refusal_names_an_extra_superset() {
let client = config_id("p", "");
let daemon = config_id("p", "m");
assert!(super::config_ids_compatible(&client, &daemon));
assert_eq!(
super::first_config_mismatch_field(&client, Some(&daemon)),
"extra"
);
}
#[tokio::test]
async fn dispatch_serves_a_client_whose_extra_embedders_the_daemon_covers() {
let covering_calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let covering = MockDispatch {
namespace: "local".to_string(),
config_id: config_id("p", "m"),
dispatch_calls: Arc::clone(&covering_calls),
pool: None,
dispatch_err: None,
};
let served = round_trip(covering, &base_request_frame(&config_id("p", ""))).await;
assert!(
served.ok && !served.config_mismatch,
"a daemon extra-embedder superset must be served: {served:?}"
);
assert_eq!(
covering_calls.load(std::sync::atomic::Ordering::SeqCst),
1,
"the covered request must reach dispatch"
);
let lacking_calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let lacking = MockDispatch {
namespace: "local".to_string(),
config_id: config_id("p", ""),
dispatch_calls: Arc::clone(&lacking_calls),
pool: None,
dispatch_err: None,
};
let refused = round_trip(lacking, &base_request_frame(&config_id("p", "m"))).await;
assert!(
!refused.ok && refused.config_mismatch,
"a requested extra the daemon lacks must be refused: {refused:?}"
);
assert_eq!(
lacking_calls.load(std::sync::atomic::Ordering::SeqCst),
0,
"a refused request must not reach dispatch"
);
}
#[test]
fn a_different_primary_embedder_is_refused_even_with_extra_superset() {
let client = config_id("p", "a");
let daemon = config_id("q", "a,b");
assert!(!super::config_ids_compatible(&client, &daemon));
assert_eq!(
super::first_config_mismatch_field(&client, Some(&daemon)),
"embed"
);
}
const ROUTING: &str = ";backends=[main:Sqlite:/d:wal_ceiling_bytes=0];pack_backends=[kg=main]";
fn with_disk_policy(id: &str, reserve: u64) -> String {
format!("{id};sqlite_disk_guard=[[\"main\",{reserve},2000]]")
}
#[test]
fn a_disk_policy_only_difference_is_named_by_its_own_field() {
let plain = config_id("p", "");
let routed = format!("{plain}{ROUTING}");
for base in [plain, routed] {
let client = with_disk_policy(&base, 1_073_741_824);
let daemon = with_disk_policy(&base, 2_147_483_648);
assert!(!super::config_ids_compatible(&client, &daemon));
assert_eq!(
super::first_config_mismatch_field(&client, Some(&daemon)),
"sqlite_disk_guard"
);
}
}
#[test]
fn a_missing_disk_policy_segment_is_a_disk_policy_mismatch() {
let client = config_id("p", "");
let daemon = with_disk_policy(&client, 1_073_741_824);
assert!(!super::config_ids_compatible(&client, &daemon));
assert_eq!(
super::first_config_mismatch_field(&client, Some(&daemon)),
"sqlite_disk_guard"
);
}
#[test]
fn the_disk_policy_segment_is_parsed_apart_from_the_fields_before_it() {
let legacy = config_id("p", "m");
let fields = super::parse_config_id(&legacy).expect("legacy id parses");
assert_eq!(fields.sqlite_disk_guard, None);
assert_eq!(fields.display_timezone, "UTC");
assert_eq!(fields.pack_backends, None);
let plain = with_disk_policy(&legacy, 7);
let fields = super::parse_config_id(&plain).expect("id with policy parses");
assert_eq!(fields.sqlite_disk_guard, Some("[[\"main\",7,2000]]"));
assert_eq!(fields.display_timezone, "UTC");
let routed = with_disk_policy(&format!("{legacy}{ROUTING}"), 7);
let fields = super::parse_config_id(&routed).expect("routed id with policy parses");
assert_eq!(fields.sqlite_disk_guard, Some("[[\"main\",7,2000]]"));
assert_eq!(fields.display_timezone, "UTC");
assert_eq!(fields.pack_backends, Some("kg=main"));
}
fn with_engine_peers(id: &str, hash: char) -> String {
id.replacen(
";fresh_tail=",
&format!(";engine_peers={};fresh_tail=", hash.to_string().repeat(64)),
1,
)
}
#[test]
fn different_peer_fingerprints_cannot_use_legacy_extra_subset_compatibility() {
let client = with_engine_peers(&config_id("p", "a"), 'a');
let daemon = with_engine_peers(&config_id("p", "a,b"), 'b');
assert!(!super::config_ids_compatible(&client, &daemon));
assert_eq!(
super::first_config_mismatch_field(&client, Some(&daemon)),
"engine_peers"
);
assert_eq!(
super::config_id_extra_embedder_exclusions(&client, &daemon),
None
);
let same_hash_different_projection = with_engine_peers(&config_id("p", "a,b"), 'a');
assert!(!super::config_ids_compatible(
&client,
&same_hash_different_projection
));
assert_eq!(
super::config_id_extra_embedder_exclusions(&client, &same_hash_different_projection),
None
);
}
#[test]
fn missing_peer_fingerprints_refuse_cross_version_daemon_reuse() {
let legacy = config_id("p", "a");
let modern = with_engine_peers(&legacy, 'a');
for (client, daemon) in [(&legacy, &modern), (&modern, &legacy)] {
assert!(!super::config_ids_compatible(client, daemon));
assert_eq!(
super::first_config_mismatch_field(client, Some(daemon)),
"engine_peers"
);
assert_eq!(
super::config_id_extra_embedder_exclusions(client, daemon),
None
);
}
}
#[test]
fn peer_fingerprint_is_separate_from_fresh_tail_and_trailing_policies() {
let modern = with_engine_peers(&config_id("p", "a"), 'a');
let routed = with_disk_policy(&format!("{modern}{ROUTING}"), 7);
let hash = "a".repeat(64);
for id in [&modern, &routed] {
let fields = super::parse_config_id(id).expect("ordered peer id parses");
assert_eq!(fields.engine_peers, Some(hash.as_str()));
assert_eq!(fields.display_timezone, "UTC");
let changed = id.replace(";fresh_tail=true;", ";fresh_tail=false;");
assert!(!super::config_ids_compatible(id, &changed));
assert_eq!(
super::first_config_mismatch_field(id, Some(&changed)),
"fresh_tail"
);
}
let malformed = modern.replace(";fresh_tail=", ";unexpected=value;fresh_tail=");
assert!(super::parse_config_id(&malformed).is_none());
}
fn with_node_channel(id: &str, hash: char) -> String {
format!(
"{id};visibility_receipts=disabled;node_channel={}",
hash.to_string().repeat(64)
)
}
#[test]
fn node_channel_digest_is_parsed_with_and_without_routing() {
let hash = "a".repeat(64);
let plain = with_node_channel(&config_id("p", ""), 'a');
let routed = format!("{plain}{ROUTING}");
for id in [&plain, &routed] {
let fields = super::parse_config_id(id).expect("node channel id parses");
assert_eq!(fields.node_channel, Some(hash.as_str()));
}
let legacy = config_id("p", "");
let fields = super::parse_config_id(&legacy).expect("legacy id parses");
assert_eq!(fields.node_channel, None);
}
#[test]
fn a_malformed_node_channel_digest_is_not_a_config_id() {
let short = format!("{};node_channel={}", config_id("p", ""), "a".repeat(63));
let upper = format!("{};node_channel={}", config_id("p", ""), "A".repeat(64));
let non_hex = format!("{};node_channel={}", config_id("p", ""), "g".repeat(64));
for id in [short, upper, non_hex] {
assert!(super::parse_config_id(&id).is_none(), "{id}");
}
}
#[test]
fn a_node_channel_digest_followed_by_a_trailing_policy_still_parses() {
for tail in [
";blob_file_transfers=true",
";disabled_verbs={\"comm\":[\"comm.send\"]}",
] {
let client = format!("{}{tail}", with_node_channel(&config_id("p", ""), 'a'));
let daemon = format!("{}{tail}", with_node_channel(&config_id("p", "m"), 'a'));
assert!(super::parse_config_id(&client).is_some(), "{client}");
assert!(super::config_ids_compatible(&client, &daemon), "{tail}");
let other_tail = with_node_channel(&config_id("p", "m"), 'a');
assert!(
!super::config_ids_compatible(&client, &other_tail),
"{tail}"
);
}
}
#[test]
fn the_same_node_channel_digest_is_compatible() {
let client = with_node_channel(&config_id("p", ""), 'a');
let daemon = with_node_channel(&config_id("p", ""), 'a');
assert!(super::config_ids_compatible(&client, &daemon));
}
#[test]
fn a_different_node_channel_digest_refuses_daemon_reuse() {
let client = with_node_channel(&config_id("p", ""), 'a');
let daemon = with_node_channel(&config_id("p", ""), 'b');
assert!(!super::config_ids_compatible(&client, &daemon));
assert_eq!(
super::first_config_mismatch_field(&client, Some(&daemon)),
"node_channel"
);
}
#[test]
fn a_node_channel_on_only_one_side_refuses_daemon_reuse() {
let plain = format!("{};visibility_receipts=disabled", config_id("p", ""));
let with_channel = with_node_channel(&config_id("p", ""), 'a');
for (client, daemon) in [(&plain, &with_channel), (&with_channel, &plain)] {
assert!(!super::config_ids_compatible(client, daemon));
assert_eq!(
super::first_config_mismatch_field(client, Some(daemon)),
"node_channel"
);
}
}
fn with_receipts(id: &str, value: &str) -> String {
format!("{id};visibility_receipts={value}")
}
fn receipt_ring(seed: &str) -> String {
let declaration =
format!(r#"[[["{seed}","SigningKey","env",null,null]],[["{seed}-id","{seed}",true]]]"#);
super::visibility_receipt_policy_fingerprint(declaration.as_bytes())
}
#[test]
fn the_ringless_receipt_fingerprint_hashes_an_empty_declaration() {
let no_ring = super::no_receipt_ring_fingerprint();
assert_eq!(
no_ring,
super::visibility_receipt_policy_fingerprint(b"[[],null]")
);
assert_eq!(no_ring.len(), 64);
assert!(no_ring
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)));
}
#[test]
fn a_ringless_client_is_served_by_a_daemon_with_a_receipt_ring() {
let client = with_receipts(&config_id("p", ""), super::no_receipt_ring_fingerprint());
let daemon = with_receipts(&config_id("p", ""), &receipt_ring("a"));
assert!(super::config_ids_compatible(&client, &daemon));
assert_eq!(
super::first_config_mismatch_field(&client, Some(&daemon)),
"unknown"
);
}
#[test]
fn a_client_with_a_receipt_ring_is_refused_by_a_ringless_daemon() {
let client = with_receipts(&config_id("p", ""), &receipt_ring("a"));
let daemon = with_receipts(&config_id("p", ""), super::no_receipt_ring_fingerprint());
assert!(!super::config_ids_compatible(&client, &daemon));
assert_eq!(
super::first_config_mismatch_field(&client, Some(&daemon)),
"visibility_receipts"
);
}
#[test]
fn different_receipt_rings_refuse_daemon_reuse_in_both_directions() {
let ring_a = with_receipts(&config_id("p", ""), &receipt_ring("a"));
let ring_b = with_receipts(&config_id("p", ""), &receipt_ring("b"));
for (client, daemon) in [(&ring_a, &ring_b), (&ring_b, &ring_a)] {
assert!(!super::config_ids_compatible(client, daemon));
assert_eq!(
super::first_config_mismatch_field(client, Some(daemon)),
"visibility_receipts"
);
}
}
#[test]
fn a_ringless_client_is_refused_by_a_daemon_id_without_the_receipt_field() {
let legacy = config_id("p", "");
let ringless = with_receipts(&legacy, super::no_receipt_ring_fingerprint());
for (client, daemon) in [(&ringless, &legacy), (&legacy, &ringless)] {
assert!(!super::config_ids_compatible(client, daemon));
assert_eq!(
super::first_config_mismatch_field(client, Some(daemon)),
"visibility_receipts"
);
}
}
#[test]
fn a_receipt_ring_with_no_keys_is_not_treated_as_no_ring() {
let empty_ring = super::visibility_receipt_policy_fingerprint(b"[[],[]]");
assert_ne!(empty_ring, super::no_receipt_ring_fingerprint());
let client = with_receipts(&config_id("p", ""), &empty_ring);
let daemon = with_receipts(&config_id("p", ""), &receipt_ring("a"));
assert!(!super::config_ids_compatible(&client, &daemon));
assert_eq!(
super::first_config_mismatch_field(&client, Some(&daemon)),
"visibility_receipts"
);
}
#[test]
fn trailing_policy_segments_must_match_for_a_ringless_client() {
let tail = ";blob_file_transfers=true";
let client = with_receipts(
&config_id("p", ""),
&format!("{}{tail}", super::no_receipt_ring_fingerprint()),
);
let same_tail = with_receipts(&config_id("p", ""), &format!("{}{tail}", receipt_ring("a")));
assert!(super::config_ids_compatible(&client, &same_tail));
let no_tail = with_receipts(&config_id("p", ""), &receipt_ring("a"));
assert!(!super::config_ids_compatible(&client, &no_tail));
assert_eq!(
super::first_config_mismatch_field(&client, Some(&no_tail)),
"visibility_receipts"
);
}
#[test]
fn another_differing_field_is_still_named_for_a_ringless_client() {
let client = with_receipts(&config_id("p", ""), super::no_receipt_ring_fingerprint());
let daemon = with_receipts(
&config_id("p", "").replace(";telemetry=default;", ";telemetry=other;"),
&receipt_ring("a"),
);
assert!(!super::config_ids_compatible(&client, &daemon));
assert_eq!(
super::first_config_mismatch_field(&client, Some(&daemon)),
"telemetry"
);
}