use blyte_cli::surface_start::STRIPPED_ENV_VARS;
use blyte_cli::usage_log::{self, surface_from, Entry, SURFACE_ID_ENV, SURFACE_NAME_ENV};
fn sample_entry(surface: Option<&str>, surface_name: Option<&str>) -> Entry {
Entry {
ts: 1_700_000_000,
repo: "/repo/a".into(),
session: Some("sess-1".into()),
surface: surface.map(str::to_string),
surface_name: surface_name.map(str::to_string),
dispatch: Some("disp-1".into()),
research: false,
model: "m1".into(),
outcome: "verified".into(),
turns: 1,
input: 100,
cached: 0,
output: 50,
thinking: 0,
cost_usd: Some(0.10),
}
}
#[test]
fn surface_from_normalizes_id_and_name() {
assert_eq!(
surface_from(Some("s1".into()), Some("alpha".into())),
(Some("s1".into()), Some("alpha".into()))
);
assert_eq!(surface_from(None, Some("alpha".into())), (None, None));
assert_eq!(
surface_from(Some(" ".into()), Some("alpha".into())),
(None, None)
);
assert_eq!(
surface_from(Some("s1".into()), None),
(Some("s1".into()), None)
);
assert_eq!(
surface_from(Some("s1".into()), Some("".into())),
(Some("s1".into()), None)
);
}
#[test]
fn entry_with_surface_round_trips_through_disk() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("usage.jsonl");
let entry = sample_entry(Some("s1"), Some("alpha"));
usage_log::append(&path, &entry).unwrap();
let raw = std::fs::read_to_string(&path).unwrap();
assert!(raw.contains("\"surface\":\"s1\""), "raw line: {raw}");
assert!(
raw.contains("\"surface_name\":\"alpha\""),
"raw line: {raw}"
);
let read_back = usage_log::read(&path);
assert_eq!(read_back, vec![entry]);
}
#[test]
fn entry_without_surface_omits_surface_keys() {
let entry = sample_entry(None, None);
let json = serde_json::to_string(&entry).unwrap();
assert!(!json.contains("surface"), "unexpected surface key: {json}");
}
#[test]
fn pre_change_line_reads_with_none_surface() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("usage.jsonl");
let line = r#"{"ts":1700000000,"repo":"/repo/a","session":"sess-1","dispatch":"disp-1","research":false,"model":"m1","outcome":"verified","turns":1,"input":100,"cached":0,"output":50,"thinking":0,"cost_usd":0.1}"#;
std::fs::write(&path, format!("{line}\n")).unwrap();
let entries = usage_log::read(&path);
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].surface, None);
assert_eq!(entries[0].surface_name, None);
}
#[test]
fn stripped_env_vars_contains_surface_env_names() {
assert!(STRIPPED_ENV_VARS.contains(&SURFACE_ID_ENV));
assert!(STRIPPED_ENV_VARS.contains(&SURFACE_NAME_ENV));
}