//! The two transcript formats, and the two usage records, pinned to fixtures.//!//! THE FIXTURES ARE HAND-WRITTEN from the agents' published formats, not//! recorded from a session — a recorded transcript of the owner's own work is//! exactly the thing that must never enter this tree. What they pin is the//! SHAPE: `type`/`message.content` for Claude, `{timestamp,type,payload}` with//! `response_item` messages for Codex, and the two rate-limit records.//!//! A version bump that changes either format fails HERE, loudly, naming the//! parser — never by quietly reading as "no turn" on the phone.use cotton_session_host::agents::{claude::Claude, codex::Codex, Adapter};use cotton_session_host::protocol::{Provider, TurnRole};const CLAUDE_TRANSCRIPT: &str = include_str!("fixtures/claude-transcript.jsonl");const CODEX_ROLLOUT: &str = include_str!("fixtures/codex-rollout.jsonl");const CLAUDE_STATUSLINE: &str = include_str!("fixtures/claude-statusline.json");const NOW: &str = "2026-09-18T09:01:01Z";#[test]fn claude_s_last_turn_is_the_assistant_s_text_with_the_other_blocks_left_out() {let turn = Claude .read_last_turn(CLAUDE_TRANSCRIPT) .expect("the fixture has an assistant turn"); assert_eq!(turn.role, TurnRole::Assistant); assert_eq!( turn.text,"I added two: one for the bound and one for the replay tail." ); assert!( !turn.text.contains("the ring is the file"),"a thinking block is not what the session said" );}#[test]fn claude_s_earlier_records_are_still_readable_when_the_tail_is_cut_back() {let only_the_user = CLAUDE_TRANSCRIPT.lines().next().expect("a first line");let turn = Claude .read_last_turn(only_the_user) .expect("a user record is a turn too"); assert_eq!(turn.role, TurnRole::User); assert_eq!(turn.text, "Add a test for the scrollback ring.");}#[test]fn a_claude_transcript_with_nothing_in_it_is_no_turn_rather_than_an_empty_one() { assert_eq!(Claude.read_last_turn(""), None); assert_eq!(Claude.read_last_turn("not json\n"), None); assert_eq!(Claude.read_last_turn("{\"type\":\"system\",\"subtype\":\"x\"}\n"),None );}#[test]fn codex_s_last_turn_is_the_assistant_s_output_text_out_of_the_rollout() {let turn = Codex .read_last_turn(CODEX_ROLLOUT) .expect("the fixture has an assistant turn"); assert_eq!(turn.role, TurnRole::Assistant); assert_eq!( turn.text,"I added two: one for the bound and one for the replay tail." );}#[test]fn a_codex_rollout_with_no_message_record_is_no_turn() {let meta = CODEX_ROLLOUT.lines().next().expect("a first line"); assert_eq!(Codex.read_last_turn(meta), None); assert_eq!(Codex.read_last_turn(""), None);}#[test]fn neither_parser_reads_the_other_s_format_so_a_swap_is_loud_rather_than_empty() { assert_eq!(Claude.read_last_turn(CODEX_ROLLOUT), None); assert_eq!(Codex.read_last_turn(CLAUDE_TRANSCRIPT), None);}/// RULE: A usage window's reset time that Claude Code sends as a number of epoch seconds is read as the instant it names, in RFC 3339 UTC.////// Todo 2ced68f2: the status line sends `resets_at` as a NUMBER, and this/// parser read only strings — every real reading arrived with no reset time./// The fixture carries the numeric shape Claude Code actually sends.#[test]fn claude_s_usage_comes_off_the_status_line_with_both_windows_as_instants() {let payload: serde_json::Value = serde_json::from_str(CLAUDE_STATUSLINE).expect("the fixture is JSON");let usage = Claude .read_usage(&payload, NOW) .expect("the fixture carries rate_limits"); assert_eq!(usage.provider, Provider::Anthropic); assert_eq!(usage.five_hour_pct, Some(61.0)); assert_eq!( usage.five_hour_resets_at.as_deref(),Some("2026-09-18T13:00:00Z") ); assert_eq!(usage.weekly_pct, Some(18.5)); assert_eq!( usage.weekly_resets_at.as_deref(),Some("2026-09-22T00:00:00Z") );}#[test]fn a_reset_time_written_as_an_rfc_3339_string_is_still_read_as_that_instant() {let payload = serde_json::json!({"rate_limits": {"five_hour": { "used_percentage": 12.0, "resets_at": "2026-09-18T15:00:00+02:00" },"seven_day": { "used_percentage": 3.0, "resets_at": "1790035200" } } });let usage = Claude .read_usage(&payload, NOW) .expect("the payload carries rate_limits"); assert_eq!( usage.five_hour_resets_at.as_deref(),Some("2026-09-18T13:00:00Z"),"normalised to UTC" ); assert_eq!( usage.weekly_resets_at.as_deref(),Some("2026-09-22T00:00:00Z"),"epoch seconds written as text" );}#[test]fn a_reset_time_that_is_neither_a_number_nor_an_instant_is_no_reset_time() {let payload = serde_json::json!({"rate_limits": { "five_hour": { "used_percentage": 12.0, "resets_at": "soon" } } });let usage = Claude .read_usage(&payload, NOW) .expect("the percentage is still a reading"); assert_eq!(usage.five_hour_pct, Some(12.0)); assert_eq!(usage.five_hour_resets_at, None);}#[test]fn a_status_line_before_the_first_api_response_is_no_reading_at_all() {let early = serde_json::json!({"model": {"display_name": "claude-opus-5"}}); assert_eq!(Claude.read_usage(&early, NOW), None);let empty = serde_json::json!({"rate_limits": {}}); assert_eq!(Claude.read_usage(&empty, NOW),None,"an empty rate_limits is not two zeroes" );}#[test]fn codex_s_usage_comes_off_a_token_count_record_and_becomes_instants() {let record: serde_json::Value = CODEX_ROLLOUT .lines() .rev() .find_map(|line| {let value: serde_json::Value = serde_json::from_str(line).ok()?; (value["payload"]["type"] == "token_count").then_some(value) }) .expect("the fixture has a token_count record");let usage = Codex .read_usage(&record, NOW) .expect("the record carries rate_limits"); assert_eq!(usage.provider, Provider::Openai); assert_eq!(usage.five_hour_pct, Some(37.5)); assert_eq!( usage.five_hour_resets_at.as_deref(),Some("2026-09-18T11:01:01Z"),"two hours after the instant the event arrived, not two hours after the wall clock" ); assert_eq!(usage.weekly_pct, Some(12.25)); assert_eq!( usage.weekly_resets_at.as_deref(),Some("2026-09-21T09:01:01Z") ); assert_eq!(usage.email, None, "a rollout record names no account");}#[test]fn a_codex_payload_forwarded_on_its_own_reads_the_same_as_the_whole_record() {let record: serde_json::Value = CODEX_ROLLOUT .lines() .rev() .find_map(|line| {let value: serde_json::Value = serde_json::from_str(line).ok()?; (value["payload"]["type"] == "token_count").then_some(value) }) .expect("the fixture has a token_count record"); assert_eq!(Codex.read_usage(&record["payload"], NOW),Codex.read_usage(&record, NOW) );}#[test]fn a_codex_record_with_no_rate_limits_is_no_reading() {let plain = serde_json::json!({"type": "event_msg", "payload": {"type": "token_count"}}); assert_eq!(Codex.read_usage(&plain, NOW), None);}#[test]fn neither_adapter_reads_the_other_s_usage_record() {let claude_payload: serde_json::Value = serde_json::from_str(CLAUDE_STATUSLINE).expect("the fixture is JSON"); assert_eq!(Codex.read_usage(&claude_payload, NOW),None,"Claude names its windows five_hour/seven_day, Codex primary/secondary" );}/// **A TRANSCRIPT DOES NOT END ON THE ASSISTANT, AND THE CARD MUST STILL SAY/// WHAT THE SESSION SAID.**////// Claude writes a `user` record for every tool result and for its own hook/// output, so the last readable record is very often one of those. Measured on/// a real session 2026-09-19: `cotton_sessions.last_line` read back the words/// the person had just typed, on a card whose whole job is to say what the/// session said.#[test]fn the_last_assistant_word_wins_over_a_user_record_written_after_it() {let transcript = concat!(r#"{"type":"user","message":{"role":"user","content":[{"type":"text","text":"Reply with ok."}]}}"#,"\n",r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"ok"}]}}"#,"\n",r#"{"type":"user","message":{"role":"user","content":[{"type":"text","text":"a tool result, written after the answer"}]}}"#,"\n", );let turn = Claude .read_last_turn(transcript) .expect("there is an assistant turn in there"); assert_eq!(turn.role, TurnRole::Assistant); assert_eq!(turn.text, "ok");}