use super::*;
const SHAPES: &str = CODE_EDGE_SHAPES;
#[test]
fn a_real_export_projection_validates_against_the_shipped_shapes() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("a.rs"), "pub fn leaf() {}\n").unwrap();
std::fs::write(
dir.path().join("b.rs"),
"use crate::a::leaf;\nfn mid() { leaf(); }\n",
)
.unwrap();
std::fs::write(
dir.path().join("README.md"),
"# Guide\n\nThe `leaf` function is the entry point.\n",
)
.unwrap();
let turtle = crate::export::to_turtle(dir.path(), "demo").expect("export ran");
assert!(
turtle.contains("bobbin:calls"),
"fixture must emit a call edge"
);
let v = validate(&turtle, SHAPES).expect("validation ran");
assert!(
v.conforms,
"real export output must validate against the shipped shapes; violations: {:?}",
v.violations
);
}
#[test]
fn empty_bearer_token_is_unset_not_a_credential() {
assert_eq!(normalize_token(None), None);
assert_eq!(normalize_token(Some(String::new())), None);
assert_eq!(
normalize_token(Some("sekrit".into())),
Some("sekrit".to_string())
);
}
const CONFORMING: &str = r#"
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix bobbin: <http://aegis.gastown.local/ontology/> .
bobbin:code_mod a bobbin:CodeModule ;
rdfs:label "m.rs" ; bobbin:filePath "m.rs" ;
bobbin:repo "fixture" ; bobbin:language "rust" .
bobbin:code_x a bobbin:CodeSymbol ;
rdfs:label "x" ; bobbin:name "x" ; bobbin:hasTier "lsp" ;
bobbin:definedIn bobbin:code_mod ;
bobbin:calls bobbin:code_y .
"#;
const VIOLATING: &str = r#"
@prefix bobbin: <http://aegis.gastown.local/ontology/> .
bobbin:code_bad a bobbin:CodeSymbol ;
bobbin:calls "not-an-iri" ;
bobbin:hasTier "vibes" .
"#;
#[test]
fn conforming_projection_validates() {
let v = validate(CONFORMING, SHAPES).expect("validation ran");
assert!(v.conforms, "expected conformance, got {:?}", v.violations);
assert!(v.violations.is_empty());
}
#[test]
fn violating_projection_is_refused_with_reasons() {
let v = validate(VIOLATING, SHAPES).expect("validation ran");
assert!(!v.conforms, "a malformed projection must not conform");
assert!(
!v.violations.is_empty(),
"a refusal must always carry at least one reason"
);
}
#[test]
fn a_refusal_never_reads_as_empty_success() {
let empty_nonconformance = parse_report("[] a sh:ValidationReport ; sh:conforms false .");
assert!(!empty_nonconformance.conforms);
assert!(!empty_nonconformance.violations.is_empty());
}
#[test]
fn promote_refuses_without_writing_when_invalid() {
let out = promote("http://127.0.0.1:1", VIOLATING, "test").expect("no write attempted");
match out {
Promotion::Refused(vs) => assert!(!vs.is_empty()),
Promotion::Wrote(_) => panic!("wrote invalid facts to Quipu"),
}
}
fn synthetic_turtle(blocks: usize, block_bytes: usize) -> String {
let header = "@prefix bobbin: <http://aegis.gastown.local/ontology/> .";
let mut t = String::from(header);
for i in 0..blocks {
let pad = "x".repeat(block_bytes.saturating_sub(60));
t.push_str(&format!(
"\n\nbobbin:code_{i} a bobbin:CodeSymbol ;\n rdfs:label \"{pad}\" ."
));
}
t
}
#[test]
fn under_limit_turtle_is_a_single_untouched_chunk() {
let t = synthetic_turtle(3, 100);
let chunks = chunk_turtle(&t, 1_000_000).expect("chunked");
assert_eq!(chunks.len(), 1);
assert_eq!(chunks[0], t, "single-chunk path must be byte-identical");
}
#[test]
fn oversized_turtle_splits_on_block_boundaries_preserving_every_block() {
let t = synthetic_turtle(40, 300);
let chunks = chunk_turtle(&t, 2_000).expect("chunked");
assert!(chunks.len() > 1, "expected a real split");
for c in &chunks {
assert!(c.len() <= 2_000, "chunk over limit: {} bytes", c.len());
assert!(
c.starts_with("@prefix bobbin:"),
"every chunk must carry the prefix header"
);
}
let stitched: Vec<&str> = chunks
.iter()
.flat_map(|c| c.split("\n\n").skip(1))
.collect();
let original: Vec<&str> = t.split("\n\n").skip(1).collect();
assert_eq!(stitched, original, "blocks lost, duplicated, or reordered");
}
#[test]
fn a_contiguous_edge_section_chunks_at_statement_boundaries() {
let header = "@prefix bobbin: <http://aegis.gastown.local/ontology/> .";
let mut t = String::from(header);
t.push_str("\n\n");
let edges: Vec<String> = (0..200)
.map(|i| format!("<http://x/a{i}> bobbin:calls <http://x/b{i}> ."))
.collect();
t.push_str(&edges.join("\n"));
let chunks = chunk_turtle(&t, 2_000).expect("edge section must chunk, not error");
assert!(chunks.len() > 1, "expected a real split");
let stitched: Vec<String> = chunks
.iter()
.flat_map(|c| c.lines())
.filter(|l| l.contains("bobbin:calls"))
.map(str::to_string)
.collect();
assert_eq!(
stitched, edges,
"edge statements lost, duplicated, or reordered"
);
for c in &chunks {
assert!(c.len() <= 2_000, "chunk over limit: {} bytes", c.len());
assert!(c.starts_with("@prefix"), "chunk missing prefix header");
}
}
#[test]
fn a_block_bigger_than_the_limit_errors_loudly() {
let t = synthetic_turtle(2, 5_000);
let err = chunk_turtle(&t, 1_000).expect_err("unsplittable block must error");
let msg = format!("{err}");
assert!(
msg.contains("cannot split below statement granularity"),
"error must name the cause, got: {msg}"
);
}
#[test]
fn multi_chunk_report_names_the_chunk_count() {
let wrote = Promotion::Wrote(WriteSummary {
count: 9329,
tx_ids: vec![801, 802, 803],
chunks: 3,
});
let mut out = Vec::new();
wrote.report(&mut out).unwrap();
let s = String::from_utf8(out).unwrap();
assert!(s.contains("9329 triples"), "{s}");
assert!(s.contains("tx 801..803"), "{s}");
assert!(s.contains("in 3 chunks"), "{s}");
}