use super::{Block, block, usage};
use serde_json::{Value, json};
fn counters(v: &Value) -> Result<super::Usage, String> {
usage(v.as_object().expect("an object"))
}
#[test]
fn the_three_known_blocks_read_back_as_themselves() {
assert_eq!(
block(&json!({"kind": "text", "text": "on it"})),
Ok(Block::Text("on it".to_owned()))
);
assert_eq!(
block(&json!({"kind": "thinking", "text": "weighing two seams"})),
Ok(Block::Thinking("weighing two seams".to_owned()))
);
assert_eq!(
block(&json!({"kind": "tool-use", "id": "tu-1", "name": "read",
"input": "src/reply.rs"})),
Ok(Block::ToolUse {
id: "tu-1".to_owned(),
name: "read".to_owned(),
input: "src/reply.rs".to_owned(),
})
);
for (kind, expected) in [
(json!({"kind": "text", "text": ""}), "text"),
(json!({"kind": "thinking", "text": ""}), "thinking"),
(
json!({"kind": "tool-use", "id": "", "name": "", "input": ""}),
"tool-use",
),
] {
assert_eq!(block(&kind).expect("a block").label(), expected);
}
}
#[test]
fn an_unknown_block_kind_keeps_its_word() {
let read = block(&json!({"kind": "citation", "text": "…"})).expect("a block");
assert_eq!(read, Block::Unknown("citation".to_owned()));
assert_eq!(read.label(), "citation");
}
#[test]
fn a_block_that_will_not_read_refuses() {
assert_eq!(
block(&json!("text")),
Err("block: not an object".to_owned())
);
let refusal = block(&json!({"kind": "tool-use", "id": "tu-1"})).expect_err("no name");
assert!(refusal.contains("\"name\""), "{refusal}");
}
#[test]
fn counters_ride_under_the_provider_s_own_names() {
let read = counters(&json!({"usage": {
"input_tokens": 1841, "output_tokens": 96, "a_counter_added_later": 7,
}}))
.expect("counters");
assert_eq!(read.get("a_counter_added_later"), Some(&7));
assert_eq!(read.len(), 3);
}
#[test]
fn an_entry_that_reported_no_counters_says_so_with_an_empty_object() {
assert!(
counters(&json!({"usage": {}}))
.expect("counters")
.is_empty()
);
let refusal = counters(&json!({})).expect_err("absent");
assert!(refusal.contains("\"usage\""), "{refusal}");
let wrong = counters(&json!({"usage": {"input_tokens": "many"}})).expect_err("not a count");
assert!(wrong.contains("not a count"), "{wrong}");
}