use std::collections::HashMap;
use dejadb_cal::store_types::SearchHit;
use dejadb_context::{ContextAssembler, FormatPolicy, FormattedContext, MetadataLevel, OutputFormat};
use dejadb_core::error::Hash;
use dejadb_core::format::deserialize::DeserializedGrain;
use dejadb_core::format::header::MgHeader;
use dejadb_core::types::GrainType;
const CREATED_AT: u32 = 1_740_000_000;
fn grain(gt: GrainType, hash_byte: u8, fields: Vec<(&str, serde_json::Value)>) -> DeserializedGrain {
let mut map = HashMap::new();
for (k, v) in fields {
map.insert(k.to_string(), v);
}
DeserializedGrain {
header: MgHeader {
version: 1,
flags: 0,
grain_type: gt.type_byte(),
ns_hash: 0,
created_at_sec: CREATED_AT,
},
grain_type: gt,
fields: map,
hash: Hash::from_bytes(&[hash_byte; 32]),
}
}
fn hit(grain: DeserializedGrain) -> SearchHit {
SearchHit {
hash: grain.hash,
score: 0.85,
grain,
score_breakdown: None,
#[cfg(feature = "rerank")]
rerank_score: None,
#[cfg(feature = "llm-rerank")]
llm_rerank_score: None,
explanation: None,
scope_depth: None,
source_namespace: None,
relative_time: None,
conflict_status: None,
supersession_status: None,
superseded_by_hash: None,
recall_source: None,
}
}
fn fixture_hits() -> Vec<SearchHit> {
vec![
hit(grain(
GrainType::State,
0x11,
vec![(
"context_data",
serde_json::json!({ "label": "planning_phase" }),
)],
)),
hit(grain(
GrainType::Goal,
0x22,
vec![
("description", serde_json::json!("Ship the OSS launch")),
("goal_state", serde_json::json!("active")),
("priority", serde_json::json!("high")),
("progress", serde_json::json!(0.4)),
],
)),
hit(grain(
GrainType::Fact,
0x33,
vec![
("subject", serde_json::json!("john")),
("relation", serde_json::json!("likes")),
("object", serde_json::json!("coffee")),
("confidence", serde_json::json!(0.95)),
],
)),
hit(grain(
GrainType::Tool,
0x44,
vec![
("tool_name", serde_json::json!("search_api")),
("is_error", serde_json::json!(false)),
("tool_content", serde_json::json!("12 results")),
("duration_ms", serde_json::json!(340)),
],
)),
hit(grain(
GrainType::Event,
0x55,
vec![("content", serde_json::json!("User asked about pricing tiers."))],
)),
]
}
fn annotate(ctx: &FormattedContext) -> String {
format!(
"included={} omitted={} truncated={} estimated_tokens={}\n---\n{}",
ctx.included_count, ctx.omitted_count, ctx.truncated, ctx.estimated_tokens, ctx.text,
)
}
#[test]
fn snapshot_sml() {
let assembler = ContextAssembler::new();
let policy = FormatPolicy::new(OutputFormat::Sml).metadata(MetadataLevel::Minimal);
let ctx = assembler.format(&fixture_hits(), &policy);
insta::assert_snapshot!("sml", annotate(&ctx));
}
#[test]
fn snapshot_toon() {
let assembler = ContextAssembler::new();
let policy = FormatPolicy::new(OutputFormat::Toon).metadata(MetadataLevel::Minimal);
let ctx = assembler.format(&fixture_hits(), &policy);
insta::assert_snapshot!("toon", annotate(&ctx));
}
#[test]
fn snapshot_markdown() {
let assembler = ContextAssembler::new();
let policy = FormatPolicy::new(OutputFormat::Markdown).metadata(MetadataLevel::Minimal);
let ctx = assembler.format(&fixture_hits(), &policy);
insta::assert_snapshot!("markdown", annotate(&ctx));
}
#[test]
fn snapshot_json() {
let assembler = ContextAssembler::new();
let policy = FormatPolicy::new(OutputFormat::Json).metadata(MetadataLevel::Minimal);
let ctx = assembler.format(&fixture_hits(), &policy);
insta::assert_snapshot!("json", annotate(&ctx));
}
#[test]
fn snapshot_budget_truncation_sml() {
let assembler = ContextAssembler::new();
let policy = FormatPolicy::new(OutputFormat::Sml)
.metadata(MetadataLevel::Minimal)
.no_grain_type_diversity()
.token_budget(40);
let ctx = assembler.format(&fixture_hits(), &policy);
assert!(ctx.truncated, "tight budget must force truncation");
assert!(ctx.omitted_count > 0, "at least one grain must be omitted");
assert!(ctx.included_count > 0, "at least one grain must survive");
insta::assert_snapshot!("budget_truncation_sml", annotate(&ctx));
}
#[test]
fn snapshot_budget_truncation_json() {
let assembler = ContextAssembler::new();
let policy = FormatPolicy::new(OutputFormat::Json)
.metadata(MetadataLevel::Minimal)
.no_grain_type_diversity()
.token_budget(40);
let ctx = assembler.format(&fixture_hits(), &policy);
assert!(ctx.truncated, "tight budget must force truncation");
assert!(ctx.omitted_count > 0, "at least one grain must be omitted");
insta::assert_snapshot!("budget_truncation_json", annotate(&ctx));
}