use crate::{
model::{RecordModel, TracingLevel},
tests::{model, whole_row},
};
fn emitted(model: &RecordModel) -> String {
model.emit().to_string()
}
fn observed(level: TracingLevel) -> RecordModel {
let mut model = model("Decode", &[], "id");
model.tracing = Some(level);
model
}
#[test]
fn the_level_const_rides_the_observation_gate() {
let emission = emitted(&observed(TracingLevel::Debug));
assert!(
emission.contains("gate :: if_tracing"),
"the const is not gated: {emission}"
);
assert!(
emission.contains("const TRACING"),
"no const at all: {emission}"
);
assert!(
emission.contains("TracingLevel :: Debug"),
"the declared level did not reach the emission: {emission}"
);
}
#[test]
fn every_level_emits_its_own_variant() {
for level in TracingLevel::ALL {
let emission = emitted(&observed(level));
let variant = level.variant();
assert!(
emission.contains(&format!("TracingLevel :: {variant}")),
"`{word}` emitted no variant: {emission}",
word = level.as_str()
);
}
}
#[test]
fn a_silent_declaration_emits_no_const() {
let emission = emitted(&model("Decode", &[], "id"));
assert!(
!emission.contains("const TRACING"),
"a node that wrote no word states a level: {emission}"
);
assert!(
!emission.contains("if_tracing"),
"a node that wrote no word carries the gate: {emission}"
);
}
#[test]
fn the_query_doors_are_written_once_per_half() {
let emission = emitted(&whole_row());
assert!(emission.contains("if_tracing"), "{emission}");
assert!(emission.contains("if_no_tracing"), "{emission}");
assert_eq!(
emission.matches("async fn get_sqlite").count(),
2,
"the entry is not written once per half: {emission}"
);
assert_eq!(
emission.matches("fn find_sqlite <").count(),
1,
"the builder door was duplicated, and it crosses to nothing: {emission}"
);
}
#[test]
fn the_instrumented_half_opens_a_named_span() {
let emission = emitted(&whole_row());
assert!(emission.contains("\"ingraph.query\""), "{emission}");
assert!(emission.contains("tracing :: Level :: INFO"), "{emission}");
for field in ["record", "entry", "db"] {
assert!(
emission.contains(&format!("{field} =")),
"the span carries no `{field}`: {emission}"
);
}
for entry in ["\"get\"", "\"insert\"", "\"upsert\""] {
assert!(
emission.contains(entry),
"no door named {entry}: {emission}"
);
}
}
#[test]
fn the_declared_level_is_the_emitted_spans_level() {
let mut model = whole_row();
model.tracing = Some(TracingLevel::Trace);
let emission = emitted(&model);
assert!(emission.contains("tracing :: Level :: TRACE"), "{emission}");
assert!(
!emission.contains("tracing :: Level :: INFO"),
"the default is still being spent: {emission}"
);
}
#[test]
fn the_gate_shut_build_names_no_tracing() {
let shut = shut_halves(whole_row().emit());
assert!(!shut.is_empty(), "there is no shut half to check");
assert!(
!shut.contains("tracing"),
"a build with observation off still names tracing: {shut}"
);
assert!(
shut.contains("async fn get_sqlite"),
"the shut half lost the entry it exists to keep: {shut}"
);
}
fn shut_halves(tokens: proc_macro2::TokenStream) -> String {
let mut kept = String::new();
let mut named = false;
for tree in tokens {
match tree {
proc_macro2::TokenTree::Ident(ident) => named = ident == "if_no_tracing",
proc_macro2::TokenTree::Group(group) if named => {
kept.push_str(&group.stream().to_string());
named = false;
}
proc_macro2::TokenTree::Group(group) => kept.push_str(&shut_halves(group.stream())),
_ => {}
}
}
kept
}