use super::*;
const L: usize = 1;
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(L, 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.kind_str(), "attachment");
assert_eq!(rec.timestamp.as_deref(), Some("2026-06-07T05:00:00.000Z"));
assert_eq!(rec.is_sidechain, Some(false));
assert_eq!(rec.line(), L, "a row carries the line it was lifted from");
}
#[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(L, line.as_bytes()).expect("a spine row");
assert_eq!(rec.kind_str(), "system");
assert_eq!(rec.subtype(), 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(L, 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(L, compact.as_bytes()).expect("compact");
let b = spine_record(L, 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.kind_str(), b.kind_str());
}
#[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(L, 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<SpineRow>, String, Option<SpineRow>) {
let walked = spine_record(L, line.as_bytes());
let (census, probed) = line_type_and_spine(L, line.as_bytes())
.expect("valid json")
.expect("non-blank");
(walked, census, probed)
}
fn same(a: Option<&SpineRow>, b: Option<&SpineRow>) {
match (a, b) {
(None, None) => {}
(Some(x), Some(y)) => {
assert_eq!(x.line(), y.line());
assert_eq!(x.kind_str(), y.kind_str());
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 a_row_carries_exactly_the_fields_the_shared_walk_lifts() {
fn text(raw: Option<&[u8]>) -> Option<String> {
let raw = raw?;
let s = std::str::from_utf8(raw).ok()?;
let inner = s.strip_prefix('"')?.strip_suffix('"')?;
Some(inner.to_string())
}
fn flag(raw: Option<&[u8]>) -> Option<bool> {
match raw? {
b"true" => Some(true),
b"false" => Some(false),
_ => None,
}
}
for line in [FULL, BOUNDARY, LAST_PROMPT, SPACED_BOOL] {
let f = spine_fields(line.as_bytes()).expect("an admitted line");
let row = spine_record(L, line.as_bytes()).expect("a row");
assert_eq!(Some(row.kind_str().to_string()), text(Some(f.r#type)));
assert_eq!(row.subtype().map(str::to_string), text(f.subtype));
assert_eq!(row.uuid.as_deref().map(str::to_string), text(f.uuid));
assert_eq!(
row.parent_uuid.as_deref().map(str::to_string),
text(f.parent_uuid)
);
assert_eq!(
row.logical_parent_uuid().map(str::to_string),
text(f.logical_parent_uuid)
);
assert_eq!(row.leaf_uuid().map(str::to_string), text(f.leaf_uuid));
assert_eq!(
row.timestamp.as_deref().map(str::to_string),
text(f.timestamp)
);
assert_eq!(row.is_sidechain, flag(f.is_sidechain));
assert_eq!(row.explicit(), flag(f.explicit));
assert_eq!(row.rewound(), flag(f.rewound));
assert_eq!(
row.compact_metadata().is_some(),
f.compact_metadata.is_some()
);
}
}
#[test]
fn the_walk_gate_and_the_row_tag_admit_the_same_types() {
for t in [
"user",
"assistant",
"attachment",
"system",
"last-prompt",
"file-history-snapshot",
"queue-operation",
"ai-title",
"",
] {
let raw = format!("\"{t}\"");
let wanted = spine_type_wanted(raw.as_bytes());
let tagged = SpineKind::from_raw(raw.as_bytes());
assert_eq!(wanted, tagged.is_some(), "type `{t}`");
if let Some(k) = tagged {
assert_eq!(k.as_str(), t, "the tag round-trips to its own value");
assert_eq!(
SpineKind::from_type(Some(t)),
Some(k),
"decoded entry agrees"
);
}
}
}
#[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));
let node = crate::model::ChainNode::Spine(&s);
assert!(node.message_id().is_none());
assert!(!node.has_tool_result());
assert!(!node.opens_turn());
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(), 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(L, line).is_err());
assert!(spine_record(L, line).is_none());
assert!(matches!(line_type_and_spine(L, 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(), 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(L, &parsed);
same(spine_record(L, line.as_bytes()).as_ref(), Some(&demoted));
let node = crate::model::ChainNode::Spine(&demoted);
assert!(node.message_id().is_none());
assert!(!node.has_tool_result());
assert!(!node.opens_turn(), "and it opens no turn");
}
let boundary = spine_from_record(
L,
&parse_line(BOUNDARY.as_bytes())
.expect("valid json")
.expect("a record"),
);
assert_eq!(boundary.subtype(), Some("compact_boundary"));
assert_eq!(boundary.logical_parent_uuid(), Some("a-9"));
assert!(
boundary.compact_metadata().is_some(),
"the cut fields survive"
);
let leaf = spine_from_record(
L,
&parse_line(LAST_PROMPT.as_bytes())
.expect("valid json")
.expect("a record"),
);
assert_eq!(leaf.leaf_uuid(), Some("a-9"), "the leaf hint survives");
assert_eq!(leaf.explicit(), Some(true));
assert_eq!(leaf.rewound(), Some(false));
}
#[test]
fn an_ordinary_row_carries_no_boxed_extra_and_a_rare_one_does() {
let plain = spine_record(L, FULL.as_bytes()).expect("an attachment lifts a row");
assert!(
plain.extra.is_none(),
"an ordinary attachment row carries none of the rare fields"
);
assert_eq!(
plain.subtype(),
None,
"and answers None for every one of them"
);
assert_eq!(plain.logical_parent_uuid(), None);
assert_eq!(plain.leaf_uuid(), None);
for line in [BOUNDARY, LAST_PROMPT] {
let rare = spine_record(L, line.as_bytes()).expect("a spine row");
assert!(
rare.extra.is_some(),
"a rare field has to be carried somewhere: {line}"
);
}
}
#[test]
fn a_spine_row_stays_far_narrower_than_a_record() {
let row = std::mem::size_of::<SpineRow>();
let rec = std::mem::size_of::<Record>();
assert!(
row <= 200,
"a spine row is {row} bytes - the budget is 200 (a record is {rec})"
);
assert!(
row * 4 < rec,
"a spine row ({row}) must stay a small fraction of a record ({rec})"
);
}
#[test]
fn a_duplicate_top_level_key_parts_the_two_entries_and_the_census_says_malformed() {
assert!(line_type_and_spine(L, DUPLICATE_KEY.as_bytes()).is_err());
let w = spine_record(L, 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(L, 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(L, 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(L, 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(L, 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(L, 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(L, 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(L, line.as_bytes()).expect("a spine row");
assert_eq!(rec.uuid.as_deref(), Some("p\"1"));
}