use super::*;
fn attachment_line(inner: &str) -> String {
let filler = "x".repeat(2000);
format!(
concat!(
r#"{{"parentUuid":"p-1","isSidechain":false,"attachment":{{"type":"edited_text_file",{}"#,
r#""snippet":"{}"}},"type":"attachment","uuid":"u-1","#,
r#""timestamp":"2026-06-07T05:00:00.000Z","cwd":"/w","version":"2.1.258"}}"#
),
inner, filler
)
}
#[test]
fn a_payload_uuid_never_shadows_the_record_own_uuid() {
let line = attachment_line(r#""uuid":"NOT-THE-RECORD","#);
let rec = spine_record(line.as_bytes()).expect("a spine row");
assert_eq!(rec.uuid.as_deref(), Some("u-1"));
assert_eq!(rec.parent_uuid.as_deref(), Some("p-1"));
assert_eq!(rec.r#type.as_deref(), Some("attachment"));
assert_eq!(rec.timestamp.as_deref(), Some("2026-06-07T05:00:00.000Z"));
assert_eq!(rec.is_sidechain, Some(false));
}
#[test]
fn a_payload_type_written_after_the_record_own_type_never_wins() {
let filler = "y".repeat(2000);
let line = format!(
concat!(
r#"{{"parentUuid":"p-2","isSidechain":false,"type":"system","subtype":"api_error","#,
r#""content":{{"pad":"{}","error":{{"type":"overloaded_error"}},"uuid":"NOPE"}},"#,
r#""timestamp":"2026-06-07T05:01:00.000Z","uuid":"u-2"}}"#
),
filler
);
let rec = spine_record(line.as_bytes()).expect("a spine row");
assert_eq!(rec.r#type.as_deref(), Some("system"));
assert_eq!(rec.subtype.as_deref(), Some("api_error"));
assert_eq!(rec.uuid.as_deref(), Some("u-2"));
assert_eq!(rec.timestamp.as_deref(), Some("2026-06-07T05:01:00.000Z"));
}
#[test]
fn a_line_of_an_unwanted_type_yields_no_spine_row() {
let filler = "z".repeat(4000);
let line = format!(
r#"{{"parentUuid":null,"snapshot":{{"blob":"{filler}"}},"type":"file-history-snapshot","uuid":"s-1"}}"#
);
assert!(spine_record(line.as_bytes()).is_none());
}
#[test]
fn a_reserialized_line_reads_the_same_as_the_compact_one() {
let compact = attachment_line("");
let value: serde_json::Value = serde_json::from_str(&compact).expect("valid");
let spaced = serde_json::to_string_pretty(&value).expect("re-serializable");
let a = spine_record(compact.as_bytes()).expect("compact");
let b = spine_record(spaced.as_bytes()).expect("spaced");
assert_eq!(a.uuid, b.uuid);
assert_eq!(a.parent_uuid, b.parent_uuid);
assert_eq!(a.timestamp, b.timestamp);
assert_eq!(a.r#type, b.r#type);
}
#[test]
fn a_torn_line_is_no_record() {
let filler = "q".repeat(2000);
let line = format!(r#"{{"parentUuid":"p","attachment":{{"snippet":"{filler}"#);
assert!(spine_record(line.as_bytes()).is_none());
}
const FULL: &str = r#"{"parentUuid":"p-1","isSidechain":false,"type":"attachment","uuid":"u-1","timestamp":"2026-06-07T05:00:00.000Z","attachment":{"type":"hook_additional_context","content":["ctx"]}}"#;
const BOUNDARY: &str = r#"{"type":"system","subtype":"compact_boundary","uuid":"b-1","parentUuid":null,"logicalParentUuid":"a-9","timestamp":"2026-06-07T05:10:00.000Z","compactMetadata":{"trigger":"auto","preTokens":900}}"#;
const LAST_PROMPT: &str =
r#"{"type":"last-prompt","leafUuid":"a-9","explicit":true,"rewound":false,"lastPrompt":"go"}"#;
const UNWANTED: &str = r#"{"type":"file-history-snapshot","messageId":"m-1"}"#;
const TYPELESS: &str = r#"{"uuid":"u-2"}"#;
const SPACED_BOOL: &str = r#"{"type":"last-prompt","leafUuid":"a-9","explicit":true ,"rewound":false ,"lastPrompt":"go"}"#;
const DUPLICATE_KEY: &str = r#"{"type":"user","uuid":"u-1","uuid":"u-2","parentUuid":"p-1"}"#;
fn both(line: &str) -> (Option<Record>, String, Option<Record>) {
let walked = spine_record(line.as_bytes());
let (census, probed) = line_type_and_spine(line.as_bytes())
.expect("valid json")
.expect("non-blank");
(walked, census, probed)
}
fn same(a: Option<&Record>, b: Option<&Record>) {
match (a, b) {
(None, None) => {}
(Some(x), Some(y)) => {
assert_eq!(x.r#type, y.r#type);
assert_eq!(x.subtype, y.subtype);
assert_eq!(x.uuid, y.uuid);
assert_eq!(x.parent_uuid, y.parent_uuid);
assert_eq!(x.logical_parent_uuid, y.logical_parent_uuid);
assert_eq!(x.leaf_uuid, y.leaf_uuid);
assert_eq!(x.timestamp, y.timestamp);
assert_eq!(x.is_sidechain, y.is_sidechain);
assert_eq!(x.explicit, y.explicit);
assert_eq!(x.rewound, y.rewound);
assert_eq!(x.compact_metadata, y.compact_metadata);
}
_ => panic!("one extractor produced a spine row and the other did not"),
}
}
#[test]
fn the_two_extractors_agree_on_every_admitted_shape() {
for line in [FULL, BOUNDARY, LAST_PROMPT, UNWANTED, TYPELESS, SPACED_BOOL] {
let (walked, _, probed) = both(line);
same(walked.as_ref(), probed.as_ref());
}
}
#[test]
fn the_probe_reports_the_census_type_and_gates_the_spine_on_it() {
let (_, census, spine) = both(FULL);
assert_eq!(census, "attachment");
let s = spine.expect("an attachment is on the chain");
assert_eq!(s.uuid.as_deref(), Some("u-1"));
assert_eq!(s.parent_uuid.as_deref(), Some("p-1"));
assert_eq!(s.is_sidechain, Some(false));
assert!(s.message.is_none());
let (_, census, spine) = both(UNWANTED);
assert_eq!(census, "file-history-snapshot", "censused all the same");
assert!(
spine.is_none(),
"a line the loader never admits gets no row"
);
let (_, census, spine) = both(TYPELESS);
assert_eq!(census, "(untyped)");
assert!(spine.is_none());
let (_, _, spine) = both(BOUNDARY);
let s = spine.expect("a boundary is the chain's cut");
assert_eq!(s.logical_parent_uuid.as_deref(), Some("a-9"));
assert!(s.compact_metadata.is_some());
assert!(s.parent_uuid.is_none(), "an explicit null reads as absent");
}
#[test]
fn a_value_of_an_unexpected_json_type_reads_as_absent_in_both() {
let line = r#"{"type":"user","uuid":7,"parentUuid":"p-1","isSidechain":"no"}"#;
let (walked, census, probed) = both(line);
assert_eq!(census, "user");
same(walked.as_ref(), probed.as_ref());
let s = probed.expect("still a chain row");
assert!(s.uuid.is_none());
assert!(s.is_sidechain.is_none());
assert_eq!(s.parent_uuid.as_deref(), Some("p-1"));
}
#[test]
fn a_torn_interior_is_malformed_for_the_probe_and_no_row_for_the_walk() {
let line = br#"{"type":"attachment","uuid":"u-1","attachment":{"conte"#;
assert!(line_type_and_spine(line).is_err());
assert!(spine_record(line).is_none());
assert!(matches!(line_type_and_spine(b" "), Ok(None)));
}
#[test]
fn a_bool_followed_by_whitespace_reads_the_same_in_both() {
let (walked, census, probed) = both(SPACED_BOOL);
assert_eq!(census, "last-prompt");
same(walked.as_ref(), probed.as_ref());
let w = walked.expect("a last-prompt row");
assert_eq!(w.explicit, Some(true));
assert_eq!(w.rewound, Some(false));
assert_eq!(w.leaf_uuid.as_deref(), Some("a-9"));
}
#[test]
fn demoting_a_parsed_record_lands_on_the_same_row_as_walking_its_line() {
for line in [FULL, BOUNDARY, LAST_PROMPT, SPACED_BOOL] {
let parsed = parse_line(line.as_bytes())
.expect("valid json")
.expect("a record");
let demoted = spine_from_record(&parsed);
same(spine_record(line.as_bytes()).as_ref(), Some(&demoted));
assert!(
demoted.message.is_none(),
"no message survives the demotion"
);
assert!(demoted.attachment_payload_text().is_none());
assert!(demoted.hook_additional_context_text().is_none());
assert!(demoted.csift_channel_text().is_none());
assert!(!demoted.opens_turn(), "and it opens no turn");
}
let boundary = spine_from_record(
&parse_line(BOUNDARY.as_bytes())
.expect("valid json")
.expect("a record"),
);
assert_eq!(boundary.subtype.as_deref(), Some("compact_boundary"));
assert_eq!(boundary.logical_parent_uuid.as_deref(), Some("a-9"));
assert!(
boundary.compact_metadata.is_some(),
"the cut fields survive"
);
let leaf = spine_from_record(
&parse_line(LAST_PROMPT.as_bytes())
.expect("valid json")
.expect("a record"),
);
assert_eq!(
leaf.leaf_uuid.as_deref(),
Some("a-9"),
"the leaf hint survives"
);
assert_eq!(leaf.explicit, Some(true));
assert_eq!(leaf.rewound, Some(false));
}
#[test]
fn a_duplicate_top_level_key_parts_the_two_entries_and_the_census_says_malformed() {
assert!(line_type_and_spine(DUPLICATE_KEY.as_bytes()).is_err());
let w = spine_record(DUPLICATE_KEY.as_bytes()).expect("the walk still builds a row");
assert_eq!(w.uuid.as_deref(), Some("u-2"), "the last occurrence wins");
assert_eq!(w.parent_uuid.as_deref(), Some("p-1"));
}
#[test]
fn a_key_truncated_mid_string_ends_the_walk_at_the_end_of_the_line() {
assert!(spine_record(br#"{"type":"user","parentUuid":"p-1","uuid"#).is_none());
}
#[test]
fn a_scalar_truncated_at_the_end_of_the_line_ends_the_walk_too() {
assert!(spine_record(br#"{"type":"user","uuid":"u-1","tokens":123"#).is_none());
}
#[test]
fn an_escape_inside_a_key_never_ends_that_key_early() {
for line in [
r#"{"a\"b":1,"type":"user","uuid":"u-1","parentUuid":"p-1"}"#,
r#"{"a\\":1,"type":"user","uuid":"u-1","parentUuid":"p-1"}"#,
] {
let rec = spine_record(line.as_bytes()).unwrap_or_else(|| panic!("a spine row: {line}"));
assert_eq!(rec.uuid.as_deref(), Some("u-1"), "{line}");
assert_eq!(rec.parent_uuid.as_deref(), Some("p-1"), "{line}");
}
}
#[test]
fn an_escaped_quote_inside_a_value_never_closes_that_value() {
let line = r#"{"type":"user","uuid":"u-1","parentUuid":"p-1","note":"a\"b"}"#;
let rec = spine_record(line.as_bytes()).expect("a spine row");
assert_eq!(rec.uuid.as_deref(), Some("u-1"));
assert_eq!(rec.parent_uuid.as_deref(), Some("p-1"));
}
#[test]
fn a_delimiter_inside_a_string_value_is_not_a_delimiter() {
let line = r#"{"type":"user","uuid":"a,b","parentUuid":"p-1"}"#;
let rec = spine_record(line.as_bytes()).expect("a spine row");
assert_eq!(rec.uuid.as_deref(), Some("a,b"));
assert_eq!(rec.parent_uuid.as_deref(), Some("p-1"));
}
#[test]
fn an_empty_string_value_is_a_present_field_not_an_absent_one() {
let line = r#"{"type":"user","uuid":"","parentUuid":"p-1"}"#;
let rec = spine_record(line.as_bytes()).expect("a spine row");
assert_eq!(
rec.uuid.as_deref(),
Some(""),
"an empty uuid is Some of empty"
);
assert_eq!(rec.parent_uuid.as_deref(), Some("p-1"));
}
#[test]
fn an_escaped_value_is_decoded_not_handed_back_raw() {
let line = r#"{"type":"user","uuid":"p\"1","parentUuid":"p-1"}"#;
let rec = spine_record(line.as_bytes()).expect("a spine row");
assert_eq!(rec.uuid.as_deref(), Some("p\"1"));
}