use causa_kernel::{
BlockContent, BlockId, BlockMeta, Context, ContextBlock, Replacement, Role, ToolCallPayload,
ToolOutput, ToolResultPayload, ToolResultStatus,
};
use serde_json::json;
use std::ops::Range;
fn id(n: u128) -> BlockId {
BlockId::new(uuid::Uuid::from_u128(n))
}
fn note(id_value: u128, text: &str, source: &str) -> ContextBlock {
let role = match source {
"system" => Role::System,
"assistant" => Role::Assistant,
_ => Role::User,
};
ContextBlock::new(
id(id_value),
BlockContent::text(role, text),
BlockMeta::from_source(source.to_owned()),
)
}
fn call(id_value: u128, name: &str) -> ContextBlock {
ContextBlock::new(
id(id_value),
BlockContent::ToolCall(ToolCallPayload::new(
name.to_owned(),
json!({"round": id_value}),
)),
BlockMeta::default()
.with_source("model".to_owned())
.with_provider_call_id(format!("provider-call-{id_value}")),
)
}
fn result(id_value: u128, call_block_id: BlockId, value: &str) -> ContextBlock {
ContextBlock::new(
id(id_value),
BlockContent::ToolResult(ToolResultPayload::new(
call_block_id,
ToolResultStatus::Succeeded,
ToolOutput::new(json!({"value": value})),
)),
BlockMeta::from_source("tool-result"),
)
}
fn replacement(range: Range<usize>, with: Vec<ContextBlock>) -> Replacement {
Replacement { range, with }
}
fn context(blocks: Vec<ContextBlock>) -> Context {
Context::from_blocks(blocks).unwrap()
}
fn assert_frame_is_current(ctx: &Context) {
let frame = ctx.frame();
assert_eq!(frame.blocks, ctx.blocks());
}
#[test]
fn saved_material_and_full_turn_survive_local_summary_reuse_and_later_edit() {
let original_blocks = vec![
note(1, "research question", "user"),
call(2, "search"),
result(3, id(2), "first source"),
note(4, "intermediate finding", "model"),
call(5, "fetch"),
result(6, id(5), "second source"),
note(7, "current instruction", "user"),
];
let mut ctx = context(original_blocks.clone());
let selected = ctx.blocks()[1..3].to_vec();
let full_turn = ctx.clone();
ctx.apply(
vec![replacement(
1..3,
vec![note(20, "first-round summary", "host")],
)],
Vec::new(),
)
.unwrap();
assert_eq!(ctx.blocks()[0].id(), id(1));
assert_eq!(ctx.blocks()[1].id(), id(20));
assert_eq!(ctx.blocks()[2..], original_blocks[3..]);
assert_eq!(selected, original_blocks[1..3]);
assert_eq!(full_turn.blocks(), original_blocks);
assert_frame_is_current(&ctx);
ctx.apply(vec![replacement(1..2, selected.clone())], Vec::new())
.unwrap();
assert_eq!(ctx.blocks()[1..3], selected);
assert_eq!(selected, original_blocks[1..3]);
ctx.apply(
vec![replacement(
1..3,
vec![note(21, "revised first-round summary", "host")],
)],
Vec::new(),
)
.unwrap();
assert!(ctx.blocks().iter().all(|block| block.id() != id(2)));
assert!(ctx.blocks().iter().all(|block| block.id() != id(3)));
assert_eq!(selected, original_blocks[1..3]);
assert_eq!(full_turn.blocks(), original_blocks);
assert_frame_is_current(&ctx);
}
#[test]
fn generic_edit_accepts_local_tool_material_without_pairing_or_execution_policy() {
let non_call_target = note(1, "ordinary fact", "user");
let mut ctx = context(vec![non_call_target]);
let supplied = vec![
result(2, id(900), "isolated"),
result(3, id(901), "first result"),
result(4, id(901), "second result"),
result(5, id(1), "reference points to Message"),
result(6, id(7), "result appears before declaration"),
call(7, "later-declared-tool"),
];
ctx.apply(vec![replacement(1..1, supplied.clone())], Vec::new())
.unwrap();
let expected_ids = [1, 2, 3, 4, 5, 6, 7].map(id);
assert_eq!(
ctx.blocks()
.iter()
.map(ContextBlock::id)
.collect::<Vec<_>>(),
expected_ids
);
assert_eq!(ctx.blocks()[1..], supplied);
assert_frame_is_current(&ctx);
}
#[test]
fn context_serializes_only_blocks_and_clones_preserve_material() {
let context = context(vec![note(40, "material", "host")]);
let cloned = context.clone();
assert_eq!(cloned.blocks(), context.blocks());
let encoded = serde_json::to_value(&context).unwrap();
let object = encoded.as_object().unwrap();
assert_eq!(object.len(), 1, "unexpected serialized fields: {object:?}");
assert!(object.contains_key("blocks"));
let restored: Context = serde_json::from_value(encoded).unwrap();
assert_eq!(restored.into_blocks(), context.into_blocks());
}