//! Session logs for tests: records written as `loopd` writes them. Do not edit. #![allow(dead_code)] // each test file uses a different part of this module use std::path::{Path, PathBuf}; use proto::{CallId, DataClass, LogRecord, Timestamp, ToolCall}; pub fn at(minute: u32) -> Timestamp { Timestamp::parse(&format!("2026-09-20T14:{minute:02}:00.000Z")).unwrap() } pub fn user(minute: u32, content: &str) -> LogRecord { LogRecord::User { time: at(minute), content: content.to_string(), } } pub fn assistant(minute: u32, content: Option<&str>, calls: &[(&str, &str)]) -> LogRecord { LogRecord::Assistant { time: at(minute), content: content.map(str::to_string), reasoning_content: Some("thinking about it".to_string()), tool_calls: calls .iter() .enumerate() .map(|(i, (name, arguments))| ToolCall { id: format!("c{i}"), name: name.to_string(), arguments: arguments.to_string(), }) .collect(), } } pub fn result(minute: u32, content: &str, class: DataClass, untrusted: bool) -> LogRecord { LogRecord::ToolResult { time: at(minute), call: CallId(1), tool_call_id: "c0".to_string(), content: content.to_string(), class, untrusted, truncated: false, } } /// Write `records` as `sessions//0.jsonl` under `home`. pub fn write_log(home: &Path, session: &str, records: &[LogRecord]) -> PathBuf { let dir = home.join("sessions").join(session); std::fs::create_dir_all(&dir).unwrap(); let path = dir.join("0.jsonl"); let text: String = records .iter() .map(|r| format!("{}\n", serde_json::to_string(r).unwrap())) .collect(); std::fs::write(&path, text).unwrap(); path } /// Append one record to a log. pub fn append(path: &Path, record: &LogRecord) { use std::io::Write; let mut file = std::fs::OpenOptions::new().append(true).open(path).unwrap(); writeln!(file, "{}", serde_json::to_string(record).unwrap()).unwrap(); }