#[path = "common/harness.rs"]
mod harness;
use harness::TestHarness;
use macrame::prelude::*;
const TS: &str = "2026-01-01T00:00:00.000000Z";
const OPEN: &str = "9999-12-31T23:59:59.999999Z";
const ENDED: &str = "2026-01-05T00:00:00.000000Z";
const AT: &str = "2026-01-06T00:00:00.000000Z";
const TERM: &str = "zygomorphic";
const TITLE: &str = "N";
const CORPUS: [&str; 6] = ["gone", "ended", "live0", "live1", "live2", "live3"];
fn model() -> ModelName {
ModelName::new("vis_v1").unwrap()
}
fn v(rank: usize) -> Vec<f32> {
let theta = (rank as f32) * std::f32::consts::PI / 16.0;
vec![theta.cos(), theta.sin()]
}
fn query_vec() -> Vec<f32> {
v(0)
}
fn content(rank: usize) -> String {
let mut tokens = vec![TERM; 8 - rank];
tokens.extend(std::iter::repeat_n("filler", rank));
tokens.join(" ")
}
async fn fixture(harness: &TestHarness) -> Database {
let db = Database::open(&harness.db_path).await.unwrap();
let mut concepts = vec![ConceptUpsert::new("root", "Root").valid_from(TS)];
concepts.extend(CORPUS.iter().enumerate().map(|(rank, id)| {
ConceptUpsert::new(*id, TITLE)
.content(content(rank))
.valid_from(TS)
}));
db.write_concepts(concepts).await.unwrap();
db.register_model(&model(), 2).await.unwrap();
let rows: Vec<(String, Vec<f32>)> = CORPUS
.iter()
.enumerate()
.map(|(rank, id)| (id.to_string(), v(rank)))
.collect();
db.upsert_embeddings(&model(), rows).await.unwrap();
let edges: Vec<EdgeAssertion> = CORPUS
.iter()
.map(|id| {
EdgeAssertion::new("root", *id, "LINKS")
.valid_from(TS)
.valid_to(OPEN)
})
.collect();
db.write_bulk_atomic(edges).await.unwrap();
db
}
async fn every_surface(
db: &Database,
at: Option<&str>,
k: usize,
) -> Vec<(&'static str, Vec<String>)> {
let vector = search_vector(db.read_conn(), &query_vec(), &model(), k, at, None)
.await
.unwrap()
.into_iter()
.map(|r| r.concept_id)
.collect();
let keyword = keyword_search(db.read_conn(), &escape_fts5_query(TERM), k, at, None)
.await
.unwrap()
.into_iter()
.map(|(id, _)| id)
.collect();
let mut hybrid = HybridSearch::new(model(), TERM, query_vec()).top_k(k);
if let Some(t) = at {
hybrid = hybrid.as_of_valid(t);
}
let hybrid = hybrid
.execute(db.read_conn())
.await
.unwrap()
.into_iter()
.map(|h| h.concept_id)
.collect();
let mut walk = TraversalBuilder::new("root").max_depth(1);
if let Some(t) = at {
walk = walk.as_of_valid(t);
}
let base = FilteredVectorSearch::new(model(), query_vec(), walk).top_k(k);
let now = at.unwrap_or(TS);
let mut out = vec![
("search_vector", vector),
("keyword_search", keyword),
("hybrid_search", hybrid),
];
for (name, strategy) in [
(
"search_filtered/PostFilter",
VectorFilterStrategy::PostFilter,
),
(
"search_filtered/PreFilterCTE",
VectorFilterStrategy::PreFilterCTE,
),
] {
let hits = base
.clone()
.strategy(strategy)
.execute(db.read_conn(), now)
.await
.unwrap()
.into_iter()
.map(|r| r.concept_id)
.collect();
out.push((name, hits));
}
out
}
#[tokio::test]
async fn every_search_surface_reads_the_same_visibility() {
let harness = TestHarness::new();
let db = fixture(&harness).await;
let leading: Vec<String> = CORPUS[..3].iter().map(|s| s.to_string()).collect();
for (surface, got) in every_surface(&db, None, 3).await {
assert_eq!(
got, leading,
"fixture is wrong: {surface} was supposed to lead with the two \
concepts this test then hides, and rank them the way every other \
surface does"
);
}
db.write_concepts(vec![
ConceptUpsert::new("gone", TITLE)
.content(content(0))
.valid_from(TS)
.retired(true),
ConceptUpsert::new("ended", TITLE)
.content(content(1))
.valid_from(TS)
.valid_to(ENDED),
])
.await
.unwrap();
let expected: Vec<String> = CORPUS[2..5].iter().map(|s| s.to_string()).collect();
for (surface, got) in every_surface(&db, Some(AT), 3).await {
assert_eq!(
got, expected,
"{surface} disagrees with the ledger about what is visible, or \
stopped treating top_k as a count"
);
}
db.close().await.unwrap();
}
#[tokio::test]
async fn an_unstated_instant_reads_the_corpus_on_every_surface() {
let harness = TestHarness::new();
let db = fixture(&harness).await;
db.write_concepts(vec![
ConceptUpsert::new("gone", TITLE)
.content(content(0))
.valid_from(TS)
.retired(true),
ConceptUpsert::new("ended", TITLE)
.content(content(1))
.valid_from(TS)
.valid_to(ENDED),
])
.await
.unwrap();
let expected: Vec<String> = CORPUS[1..4].iter().map(|s| s.to_string()).collect();
for (surface, got) in every_surface(&db, None, 3).await {
assert_eq!(
got, expected,
"{surface} bounded valid time without being asked to, or lost a \
retirement that has nothing to do with the instant"
);
}
db.close().await.unwrap();
}