//! Ported case-for-case from `packages/core/__tests__/engine/summaries.test.ts`, //! plus vectors read out of Node (`npx tsx` against the TypeScript) for every //! function whose answer is a string, a JSON patch or a decision a screen //! shows. //! //! The patch tests compare JSON TEXT rather than a set of pairs, because a //! summary write becomes an op's patch and an op's patch is stored as text: the //! key ORDER is part of the answer. use super::*; const NOW: &str = "2026-08-30T09:00:00.000Z"; const WRITTEN: &str = "2026-08-20T09:00:00.000Z"; /// The TypeScript's `SUMMARY_KINDS`, in its declared order. const KINDS: [SummaryKind; 4] = SummaryKind::ALL; fn arrivals(values: &[&str]) -> Vec { values.iter().map(|value| value.to_string()).collect() } fn stored(text: Option<&str>, by: Option<&str>, updated_at: Option<&str>) -> StoredSummary { StoredSummary { text: text.map(str::to_string), by: by.map(str::to_string), updated_at: updated_at.map(str::to_string), } } fn empty() -> StoredSummary { StoredSummary::default() } fn json(patch: &Map) -> String { serde_json::to_string(patch).expect("a patch serialises") } // ── SUMMARY_SOURCES ────────────────────────────────────────────────────────── /// The TypeScript's totality gate is a module-load loop over `SUMMARY_KINDS`; /// here the `match` in `summary_source` is that gate, one step earlier. This /// test is what remains of it: every kind resolves, and the four resolve to /// four different tables. #[test] fn is_total_over_summary_kinds() { let tables: Vec<&str> = KINDS.iter().map(|k| summary_source(*k).table).collect(); assert_eq!( tables, [ "cotton_people", "cotton_msg_threads", "cotton_email_threads", "cotton_memory_projects", ] ); } /// A synced column name is wire protocol exactly as its table name is. #[test] fn keeps_the_projects_prose_in_description() { assert_eq!(summary_source(SummaryKind::Project).column, "description"); assert_eq!(summary_source(SummaryKind::Person).column, "summary"); assert_eq!(summary_source(SummaryKind::Thread).column, "summary"); assert_eq!(summary_source(SummaryKind::EmailThread).column, "summary"); } #[test] fn counts_each_kinds_own_arrivals_and_names_them() { assert_eq!( summary_source(SummaryKind::Person).since, SummarySince { table: "cotton_person_notes", parent_column: "person_id", instant_column: "created_at", noun: SummaryStalenessNoun::Note, } ); assert_eq!( summary_source(SummaryKind::Thread).since.table, "cotton_msg_messages" ); assert_eq!( summary_source(SummaryKind::EmailThread).since.table, "cotton_emails" ); assert_eq!( summary_source(SummaryKind::Project).since.table, "cotton_project_links" ); } /// The one that has to be asserted rather than read: a message and an email /// both carry `sent_at`, and counting THAT means a 40,000-line WhatsApp archive /// imported into an already-summarised conversation reports nothing arrived, /// because every row was sent before the summary was written. #[test] fn counts_arrival_on_every_kind() { for kind in KINDS { assert_eq!( (kind, summary_source(kind).since.instant_column), (kind, "created_at") ); } } /// All four tables spell the provenance pair the same way today, and the pair /// is one literal rather than four copies that could drift. #[test] fn every_kind_points_at_the_one_provenance_pair() { for kind in KINDS { assert_eq!(summary_source(kind).provenance, SUMMARY_PROVENANCE); } assert_eq!(SUMMARY_PROVENANCE.by, "summary_by"); assert_eq!(SUMMARY_PROVENANCE.updated_at, "summary_updated_at"); } /// A refusal in the kit's words. fn refused(text: Option<&str>) -> Option { summary_refusal(text).map(|refusal| refusal.say(&[])) } // ── summary_refusal ────────────────────────────────────────────────────────── #[test] fn accepts_a_paragraph() { assert_eq!(refused(Some("She runs the Meridian build.")), None); } #[test] fn accepts_a_clear() { assert_eq!(refused(None), None); } #[test] fn refuses_text_over_the_cap_and_says_the_cap() { let long = "x".repeat(SUMMARY_MAX_CHARS + 1); let refusal = refused(Some(&long)).expect("over the cap is refused"); assert!( refusal.contains(&SUMMARY_MAX_CHARS.to_string()), "{refusal}" ); } #[test] fn refuses_whitespace_because_clearing_is_a_clear_not_a_blank_line() { let refusal = refused(Some(" ")).expect("blank is refused"); assert!(refusal.contains("null"), "{refusal}"); } /// Vectors read out of Node. Every sentence is byte-for-byte what the /// TypeScript produced, the whitespace edges included: JavaScript's `trim` eats /// U+FEFF (so a BOM-only string is blank) and leaves U+0085 alone (so a lone /// NEL is a one-character summary the rules accept). Rust's `str::trim` is the /// other way round on both, which is why this module carries its own `js_trim`. /// U+00A0 and U+2028 are in both sets and are pinned here so a future /// simplification back to `str::trim` shows up as a red test on the two that /// differ rather than passing on the ones that do not. #[test] fn refusal_sentences_are_the_typescripts_own() { const BLANK: &str = "a summary cannot be blank — pass null to clear it"; let long_emoji = "\u{1f600}".repeat(601); let ok_emoji = "\u{1f600}".repeat(600); let padded = format!(" padded but long {}", "y".repeat(SUMMARY_MAX_CHARS)); let cases: [(&str, Option<&str>); 12] = [ ("", Some(BLANK)), (" ", Some(BLANK)), ("\t\n \u{feff}", Some(BLANK)), ("\u{feff}", Some(BLANK)), ("\u{00a0}", Some(BLANK)), ("\u{2028}", Some(BLANK)), // NEL is NOT ECMAScript whitespace, so this IS a summary. ("\u{0085}", None), ("She runs the Meridian build.", None), // Two emoji are four code units, nowhere near the cap. ("\u{1f600}\u{1f600}", None), (&ok_emoji, None), ( &long_emoji, Some("a summary is at most 1200 characters (this one is 1202)"), ), ( &padded, Some("a summary is at most 1200 characters (this one is 1219)"), ), ]; for (input, expected) in cases { assert_eq!( refused(Some(input)).as_deref(), expected, "input {input:?}" ); } } /// The cap is UTF-16 code units, which is what `String.prototype.length` /// counts. 1,200 astral emoji would be 1,200 characters to a `chars()` count /// and 2,400 to this rule; 601 of them is the boundary case, and the TypeScript /// reports 1202. #[test] fn counts_the_cap_in_utf16_code_units_as_string_length_does() { let exactly = "x".repeat(SUMMARY_MAX_CHARS); assert_eq!(refused(Some(&exactly)), None); let over = "x".repeat(SUMMARY_MAX_CHARS + 1); assert_eq!( refused(Some(&over)).as_deref(), Some("a summary is at most 1200 characters (this one is 1201)") ); let accented = "é".repeat(SUMMARY_MAX_CHARS + 1); assert_eq!( refused(Some(&accented)).as_deref(), Some("a summary is at most 1200 characters (this one is 1201)") ); let emoji = "\u{1f600}".repeat(601); assert_eq!( refused(Some(&emoji)).as_deref(), Some("a summary is at most 1200 characters (this one is 1202)") ); } // ── summary_columns ────────────────────────────────────────────────────────── #[test] fn stamps_the_text_the_author_and_the_instant() { assert_eq!( json(&summary_columns( SummaryKind::Person, Some("She runs the build."), "user", NOW )), r#"{"summary":"She runs the build.","summary_by":"user","summary_updated_at":"2026-08-30T09:00:00.000Z"}"# ); } #[test] fn writes_a_projects_prose_to_description() { assert_eq!( json(&summary_columns( SummaryKind::Project, Some("The Meridian build."), "Claude Code", NOW )), r#"{"description":"The Meridian build.","summary_by":"Claude Code","summary_updated_at":"2026-08-30T09:00:00.000Z"}"# ); } /// Provenance for nothing is a claim about nothing. #[test] fn a_clear_takes_the_provenance_with_it() { assert_eq!( json(&summary_columns(SummaryKind::Thread, None, "user", NOW)), r#"{"summary":null,"summary_by":null,"summary_updated_at":null}"# ); } /// Vectors read out of Node, as JSON TEXT: the key order is observable, because /// this object becomes an op's patch and an op's patch is stored as text. Prose /// column first, then the byline, then the stamp — the TypeScript object's own /// order, on both branches. #[test] fn column_patches_are_the_typescripts_own_bytes() { let cases: [(SummaryKind, Option<&str>, &str, &str); 7] = [ ( SummaryKind::Person, Some("She runs the build."), "user", r#"{"summary":"She runs the build.","summary_by":"user","summary_updated_at":"2026-08-30T09:00:00.000Z"}"#, ), ( SummaryKind::Thread, Some("The site crew."), "Claude Code", r#"{"summary":"The site crew.","summary_by":"Claude Code","summary_updated_at":"2026-08-30T09:00:00.000Z"}"#, ), ( SummaryKind::EmailThread, Some("Invoices, mostly."), "job:summaries", r#"{"summary":"Invoices, mostly.","summary_by":"job:summaries","summary_updated_at":"2026-08-30T09:00:00.000Z"}"#, ), ( SummaryKind::Project, Some("The Meridian build."), "Claude Code", r#"{"description":"The Meridian build.","summary_by":"Claude Code","summary_updated_at":"2026-08-30T09:00:00.000Z"}"#, ), ( SummaryKind::Person, None, "user", r#"{"summary":null,"summary_by":null,"summary_updated_at":null}"#, ), ( SummaryKind::Project, None, "Claude Code", r#"{"description":null,"summary_by":null,"summary_updated_at":null}"#, ), ( // Prose is stored VERBATIM — the cap and the blank check trim, the // write does not. SummaryKind::Thread, Some(" leading and trailing kept "), "user", r#"{"summary":" leading and trailing kept ","summary_by":"user","summary_updated_at":"2026-08-30T09:00:00.000Z"}"#, ), ]; for (kind, text, by, expected) in cases { assert_eq!( json(&summary_columns(kind, text, by, NOW)), expected, "{kind} {text:?}" ); } } /// The key order is the answer, not an accident of the map — a set comparison /// would pass over a patch whose bytes had changed. #[test] fn the_patch_keys_are_in_the_typescripts_order() { for kind in KINDS { let patch = summary_columns(kind, Some("p"), "user", NOW); let keys: Vec<&str> = patch.keys().map(String::as_str).collect(); assert_eq!( keys, [ summary_source(kind).column, "summary_by", "summary_updated_at" ], "{kind}" ); } } // ── staleness_of ───────────────────────────────────────────────────────────── /// There is no summary to be stale. #[test] fn is_null_when_nothing_has_been_written() { assert_eq!( staleness_of(SummaryKind::Person, None, &arrivals(&[NOW, NOW])), None ); } /// Figure or silence, never a zero. #[test] fn is_null_when_nothing_arrived_after_it() { assert_eq!( staleness_of( SummaryKind::Person, Some(WRITTEN), &arrivals(&["2026-08-01T00:00:00.000Z"]) ), None ); } #[test] fn counts_only_what_arrived_after_the_summary_was_written() { assert_eq!( staleness_of( SummaryKind::Person, Some(WRITTEN), &arrivals(&[ "2026-08-19T23:59:59.000Z", "2026-08-21T00:00:00.000Z", "2026-08-29T00:00:00.000Z", ]) ), Some(SummaryStaleness { count: 2, noun: SummaryStalenessNoun::Note }) ); } /// `2026-08-20T14:00+05:30` is 08:30Z — BEFORE the summary, though it sorts /// after it as a string. #[test] fn compares_instants_parsed_never_lexically() { assert_eq!( staleness_of( SummaryKind::Thread, Some(WRITTEN), &arrivals(&["2026-08-20T14:00:00.000+05:30"]) ), None ); } #[test] fn names_each_kinds_own_noun() { let one = arrivals(&[NOW]); assert_eq!( staleness_of(SummaryKind::Thread, Some(WRITTEN), &one) .map(|s| s.noun) .map(SummaryStalenessNoun::as_str), Some("message") ); assert_eq!( staleness_of(SummaryKind::EmailThread, Some(WRITTEN), &one) .map(|s| s.noun) .map(SummaryStalenessNoun::as_str), Some("email") ); assert_eq!( staleness_of(SummaryKind::Project, Some(WRITTEN), &one) .map(|s| s.noun) .map(SummaryStalenessNoun::as_str), Some("record") ); } /// A malformed timestamp is not evidence that something happened. #[test] fn steps_over_an_unparseable_instant_rather_than_counting_it() { assert_eq!( staleness_of( SummaryKind::Person, Some(WRITTEN), &arrivals(&["not a date", NOW]) ), Some(SummaryStaleness { count: 1, noun: SummaryStalenessNoun::Note }) ); } /// The full mixed list, read out of Node: three of the seven arrivals are /// strictly after the stamp — the two later Zulu instants and the one that is a /// millisecond past it. The `+05:30` string and the equal instant are not, and /// the unparseable one is stepped over. Every kind reads the same count and its /// own noun. #[test] fn the_mixed_arrival_list_is_the_typescripts_own_count() { let list = arrivals(&[ "2026-08-19T23:59:59.000Z", "2026-08-21T00:00:00.000Z", "2026-08-29T00:00:00.000Z", "2026-08-20T14:00:00.000+05:30", "not a date", "2026-08-20T09:00:00.000Z", "2026-08-20T09:00:00.001Z", ]); let expected = ["note", "message", "email", "record"]; for (kind, noun) in KINDS.into_iter().zip(expected) { let staleness = staleness_of(kind, Some(WRITTEN), &list).expect("three arrived"); assert_eq!(staleness.count, 3, "{kind}"); assert_eq!(staleness.noun.as_str(), noun, "{kind}"); assert_eq!(staleness_of(kind, Some(WRITTEN), &[]), None, "{kind}"); assert_eq!(staleness_of(kind, None, &list), None, "{kind}"); assert_eq!( staleness_of(kind, Some("not a date"), &list), None, "{kind}" ); } } /// The TypeScript guards with `!summaryUpdatedAt`, which catches the empty /// string as well as the missing one; a legacy row really can hold one, and /// "measured from nothing" is not a measurement. #[test] fn an_empty_stamp_says_nothing_exactly_as_a_missing_one_does() { let list = arrivals(&[NOW]); for kind in KINDS { assert_eq!(staleness_of(kind, Some(""), &list), None, "{kind}"); } } // ── staleness_from ─────────────────────────────────────────────────────────── /// The tail of `staleness_of`, for a caller who counted in SQL: the SAME /// "figure or silence" rule and the SAME noun. #[test] fn a_count_made_elsewhere_reaches_the_same_rule_and_the_same_noun() { assert_eq!(staleness_from(SummaryKind::Person, 0), None); assert_eq!(staleness_from(SummaryKind::Person, -1), None); assert_eq!( staleness_from(SummaryKind::EmailThread, 40_000), Some(SummaryStaleness { count: 40_000, noun: SummaryStalenessNoun::Email }) ); for kind in KINDS { assert_eq!( staleness_from(kind, 7).map(|s| s.noun), staleness_of(kind, Some(WRITTEN), &arrivals(&[NOW])).map(|s| s.noun), "{kind}" ); } } // ── summary_patch_for ──────────────────────────────────────────────────────── const WROTE: &str = "Priya runs the Meridian build."; /// One row of the decision table: what it is called, the row as stored, the /// prose coming in, the refusal expected, and the patch expected as JSON text. type DecisionCase<'a> = ( &'a str, StoredSummary, Option<&'a str>, Option<&'a str>, Option<&'a str>, ); /// One table, because these eight are ONE decision seen from eight angles, and /// reading them apart is how two of them come to disagree. Each row names the /// failure it prevents rather than the branch it covers. #[test] fn the_summary_decision_from_every_angle() { let over_cap = "x".repeat(SUMMARY_MAX_CHARS + 1); let written = || stored(Some(WROTE), Some("Claude Code"), Some(WRITTEN)); let cases: [DecisionCase<'_>; 8] = [ ( // The save button that quietly re-dates a paragraph and // re-attributes it to whoever pressed it — every edit sheet sends // every field. "unchanged prose writes nothing", written(), Some(WROTE), None, None, ), ( "changed prose writes the text, the author and the instant", written(), Some("Priya runs Meridian, and now the Northwind pitch."), None, Some( r#"{"summary":"Priya runs Meridian, and now the Northwind pitch.","summary_by":"user","summary_updated_at":"2026-08-30T09:00:00.000Z"}"#, ), ), ( // Blank is REFUSED, never normalised to a clear: two spellings of // "make this empty" is two behaviours to keep in step forever. "blank prose is refused, and writes nothing", written(), Some(" "), Some("a summary cannot be blank — pass null to clear it"), None, ), ( "prose over the product cap is refused, and writes nothing", written(), Some(&over_cap), Some("a summary is at most 1200 characters (this one is 1201)"), None, ), ( "the FIRST write on an empty row is a change", empty(), Some(WROTE), None, Some( r#"{"summary":"Priya runs the Meridian build.","summary_by":"user","summary_updated_at":"2026-08-30T09:00:00.000Z"}"#, ), ), ( "a clear takes the provenance with it", written(), None, None, Some(r#"{"summary":null,"summary_by":null,"summary_updated_at":null}"#), ), ( // Clearing what is already clear is the only no-op a clear may be. "a clear over an already-empty row writes nothing", empty(), None, None, None, ), ( // The reason `stored.by` and `stored.updated_at` are fields at all: // a stamp standing over an empty string is exactly the state a // clear exists to end, so this write must go through. "a clear over a stamp stranded on empty prose still writes", stored(Some(""), Some("Claude Code"), Some(WRITTEN)), None, None, Some(r#"{"summary":null,"summary_by":null,"summary_updated_at":null}"#), ), ]; for (name, row, text, refusal, patch) in cases { let decision = summary_patch_for(&row, SummaryKind::Person, text, "user", NOW); assert_eq!(decision.text.as_deref(), text, "{name}: text"); assert_eq!(decision.refusal.as_ref().map(|refusal| refusal.say(&[])).as_deref(), refusal, "{name}: refusal"); assert_eq!( decision.patch.as_ref().map(json).as_deref(), patch, "{name}: patch" ); } } #[test] fn writes_a_projects_prose_into_description_never_summary() { let decision = summary_patch_for( &empty(), SummaryKind::Project, Some("The Meridian build, in one paragraph."), "user", NOW, ); assert_eq!( decision.patch.as_ref().map(json).as_deref(), Some( r#"{"description":"The Meridian build, in one paragraph.","summary_by":"user","summary_updated_at":"2026-08-30T09:00:00.000Z"}"# ) ); } /// R74: the label is the owner's own typing, and it is stored as given. #[test] fn keeps_the_author_it_is_given_verbatim() { let decision = summary_patch_for(&empty(), SummaryKind::Person, Some("x"), "Claude Code", NOW); assert_eq!( decision.patch.expect("a first write")["summary_by"], Value::String("Claude Code".into()) ); } /// The service and the server both write `decision.patch` verbatim, so if this /// ever diverged from the shared encoder the two surfaces would be writing /// different column sets for the same act. #[test] fn encodes_exactly_what_summary_columns_does() { let decision = summary_patch_for( &empty(), SummaryKind::Thread, Some("The site crew."), "user", NOW, ); assert_eq!( decision.patch, Some(summary_columns( SummaryKind::Thread, Some("The site crew."), "user", NOW )) ); } /// The contract callers depend on: `refusal` is checked FIRST, and a caller /// that looked only at `patch` must not be handed something to write. #[test] fn never_returns_a_patch_alongside_a_refusal() { let over_cap = "x".repeat(SUMMARY_MAX_CHARS + 1); for text in ["", " ", over_cap.as_str()] { let decision = summary_patch_for(&empty(), SummaryKind::Person, Some(text), "user", NOW); assert!(decision.refusal.is_some(), "{text:?}"); assert!(decision.patch.is_none(), "{text:?}"); } } /// The author and the instant are NOT part of the comparison — re-sending the /// same paragraph under a new name is still the same paragraph, and re-stamping /// it would reset the cue every time an edit sheet was opened and saved. #[test] fn the_same_prose_under_a_new_author_does_not_restamp() { let row = stored(Some("Same."), Some("user"), Some(WRITTEN)); let decision = summary_patch_for( &row, SummaryKind::EmailThread, Some("Same."), "Claude Code", NOW, ); assert_eq!(decision.refusal, None); assert_eq!(decision.patch, None); } /// The UTF-16 cap reaches the decision too: 601 emoji are 1,202 characters to /// the rule that refuses them, and the sentence says so. #[test] fn the_decisions_refusal_sentences_are_the_typescripts_own() { let emoji = "\u{1f600}".repeat(601); let decision = summary_patch_for(&empty(), SummaryKind::Thread, Some(&emoji), "user", NOW); assert_eq!( decision.refusal.as_ref().map(|refusal| refusal.say(&[])).as_deref(), Some("a summary is at most 1200 characters (this one is 1202)") ); assert!(decision.patch.is_none()); assert_eq!(decision.text.as_deref(), Some(emoji.as_str())); } // ── SUMMARY_REFRESH_THRESHOLDS ─────────────────────────────────────────────── #[test] fn declares_a_positive_threshold_for_every_kind() { for kind in KINDS { assert!(summary_refresh_threshold(kind) > 0, "{kind}"); } } /// The counts are not comparable quantities: twenty chat lines can be one /// exchange, three emails are three matters. A single number for both would /// either re-write every chat nightly or never re-write a mailbox. #[test] fn asks_for_a_chat_far_less_often_than_an_email_thread() { assert!( summary_refresh_threshold(SummaryKind::Thread) > summary_refresh_threshold(SummaryKind::EmailThread) ); } /// The four numbers, read out of Node. #[test] fn the_thresholds_are_the_typescripts_own() { assert_eq!( KINDS.map(summary_refresh_threshold), [10, 20, 3, 15], "person, thread, email_thread, project" ); } // ── summary_is_theirs ──────────────────────────────────────────────────────── #[test] fn is_true_only_for_the_persons_own_byline() { assert!(summary_is_theirs(Some(SUMMARY_AUTHOR_PERSON))); assert!(summary_is_theirs(Some("user"))); } #[test] fn is_false_for_every_assistant_label_and_for_no_byline_at_all() { assert!(!summary_is_theirs(Some("Claude Code"))); assert!(!summary_is_theirs(Some("job:summaries"))); assert!(!summary_is_theirs(None)); } /// Read out of Node: the comparison is exact — case, and both edges. A worker /// that matched loosely would one day overwrite somebody's own paragraph. #[test] fn the_byline_comparison_is_exact_on_case_and_on_whitespace() { for label in ["User", "USER", " user", "user ", ""] { assert!(!summary_is_theirs(Some(label)), "{label:?}"); } } // ── summary_refresh_due ────────────────────────────────────────────────────── fn blank() -> StoredSummary { StoredSummary::default() } fn written_row() -> StoredSummary { stored(Some("The site crew."), Some("user"), Some(WRITTEN)) } #[test] fn is_due_on_an_empty_slot_whatever_the_arrival_count_says() { assert_eq!( summary_refresh_due(SummaryKind::Thread, &blank(), 0), SummaryRefreshDecision { due: true, reason: Some(SummaryRefreshReason::Empty), arrivals: 0, threshold: 20, } ); } #[test] fn treats_a_whitespace_only_slot_as_empty() { let row = stored(Some(" "), Some("user"), Some(WRITTEN)); assert_eq!( summary_refresh_due(SummaryKind::Person, &row, 0).reason, Some(SummaryRefreshReason::Empty) ); } #[test] fn is_due_once_arrivals_reach_the_kinds_threshold_and_not_before() { assert!(!summary_refresh_due(SummaryKind::EmailThread, &written_row(), 2).due); assert_eq!( summary_refresh_due(SummaryKind::EmailThread, &written_row(), 3), SummaryRefreshDecision { due: true, reason: Some(SummaryRefreshReason::Stale), arrivals: 3, threshold: 3, } ); } /// `staleness_of` answers `None` for exactly this row, so a cue never appears /// for it — and a worker that rewrote it every night would be acting on a /// measurement no screen is allowed to make. #[test] fn says_nothing_about_a_summary_carrying_no_stamp() { let row = stored(Some("Ravi."), Some("user"), None); let decision = summary_refresh_due(SummaryKind::Person, &row, 99); assert!(!decision.due); assert_eq!(decision.reason, None); } /// The whole decision, as JSON, read out of Node — six rows per kind across the /// three states and the threshold boundary. Note what the counts say: in the /// EMPTY and NO-STAMP states the caller's own `arrivals` is REPLACED by 0, /// because it was never measured against anything. #[test] fn the_refresh_decisions_are_the_typescripts_own() { let whitespace = stored(Some(" \t "), Some("user"), Some(WRITTEN)); let no_stamp = stored(Some("P."), Some("user"), None); let with_stamp = stored(Some("P."), Some("user"), Some(WRITTEN)); let expected: [(SummaryKind, [&str; 6]); 4] = [ ( SummaryKind::Person, [ r#"{"due":true,"reason":"empty","arrivals":0,"threshold":10}"#, r#"{"due":true,"reason":"empty","arrivals":0,"threshold":10}"#, r#"{"due":false,"reason":null,"arrivals":0,"threshold":10}"#, r#"{"due":false,"reason":null,"arrivals":9,"threshold":10}"#, r#"{"due":true,"reason":"stale","arrivals":10,"threshold":10}"#, r#"{"due":true,"reason":"stale","arrivals":15,"threshold":10}"#, ], ), ( SummaryKind::Thread, [ r#"{"due":true,"reason":"empty","arrivals":0,"threshold":20}"#, r#"{"due":true,"reason":"empty","arrivals":0,"threshold":20}"#, r#"{"due":false,"reason":null,"arrivals":0,"threshold":20}"#, r#"{"due":false,"reason":null,"arrivals":19,"threshold":20}"#, r#"{"due":true,"reason":"stale","arrivals":20,"threshold":20}"#, r#"{"due":true,"reason":"stale","arrivals":25,"threshold":20}"#, ], ), ( SummaryKind::EmailThread, [ r#"{"due":true,"reason":"empty","arrivals":0,"threshold":3}"#, r#"{"due":true,"reason":"empty","arrivals":0,"threshold":3}"#, r#"{"due":false,"reason":null,"arrivals":0,"threshold":3}"#, r#"{"due":false,"reason":null,"arrivals":2,"threshold":3}"#, r#"{"due":true,"reason":"stale","arrivals":3,"threshold":3}"#, r#"{"due":true,"reason":"stale","arrivals":8,"threshold":3}"#, ], ), ( SummaryKind::Project, [ r#"{"due":true,"reason":"empty","arrivals":0,"threshold":15}"#, r#"{"due":true,"reason":"empty","arrivals":0,"threshold":15}"#, r#"{"due":false,"reason":null,"arrivals":0,"threshold":15}"#, r#"{"due":false,"reason":null,"arrivals":14,"threshold":15}"#, r#"{"due":true,"reason":"stale","arrivals":15,"threshold":15}"#, r#"{"due":true,"reason":"stale","arrivals":20,"threshold":15}"#, ], ), ]; for (kind, rows) in expected { let threshold = summary_refresh_threshold(kind); let actual = [ summary_refresh_due(kind, &blank(), 0), summary_refresh_due(kind, &whitespace, 99), summary_refresh_due(kind, &no_stamp, 99), summary_refresh_due(kind, &with_stamp, threshold - 1), summary_refresh_due(kind, &with_stamp, threshold), summary_refresh_due(kind, &with_stamp, threshold + 5), ]; for (decision, expected) in actual.into_iter().zip(rows) { assert_eq!( serde_json::to_string(&decision).expect("a decision serialises"), expected, "{kind}" ); } } } /// There is no "due for no reason", and no reason without a due. #[test] fn the_reason_is_present_exactly_when_it_is_due() { let rows = [ blank(), stored(Some(" "), None, None), stored(Some("P."), Some("user"), None), stored(Some("P."), Some("user"), Some(WRITTEN)), ]; for kind in KINDS { for row in &rows { for arrivals in [0, 1, 3, 10, 20, 100] { let decision = summary_refresh_due(kind, row, arrivals); assert_eq!(decision.due, decision.reason.is_some(), "{kind} {row:?}"); } } } } // ── the wire shapes ────────────────────────────────────────────────────────── /// `SummaryRead` is what a server hands a caller; its JSON is the TypeScript /// interface's, `camelCase` and all. #[test] fn a_summary_read_serialises_as_the_typescript_interface_does() { let read = SummaryRead { summary: ISummary { kind: SummaryKind::EmailThread, id: "e1".into(), text: Some("Invoices, mostly.".into()), by: Some("user".into()), updated_at: Some(WRITTEN.into()), }, staleness: Some(SummaryStaleness { count: 4, noun: SummaryStalenessNoun::Email, }), }; let json = serde_json::to_string(&read).expect("a read serialises"); assert_eq!( json, r#"{"summary":{"kind":"email_thread","id":"e1","text":"Invoices, mostly.","by":"user","updatedAt":"2026-08-20T09:00:00.000Z"},"staleness":{"count":4,"noun":"email"}}"# ); assert_eq!( serde_json::from_str::(&json).expect("and round-trips"), read ); } /// Nothing to say is `null`, never `{count: 0}`. #[test] fn a_read_with_no_cue_carries_a_null_staleness() { let read = SummaryRead { summary: ISummary { kind: SummaryKind::Person, id: "p1".into(), text: None, by: None, updated_at: None, }, staleness: None, }; assert_eq!( serde_json::to_string(&read).expect("a read serialises"), r#"{"summary":{"kind":"person","id":"p1","text":null,"by":null,"updatedAt":null},"staleness":null}"# ); } /// The four wire strings, and their declared order. #[test] fn the_summary_kinds_are_the_typescripts_own() { assert_eq!( KINDS.map(|k| k.as_str()), ["person", "thread", "email_thread", "project"] ); for kind in KINDS { assert_eq!(SummaryKind::parse(kind.as_str()), Some(kind)); } assert_eq!(SummaryKind::parse("document"), None); } /// RULE: a summary kind whose definition ships reads where its summary lives /// off the definitions — its table and prose column off its own, the arrivals' /// table and link back off theirs — never a hand list of table names that goes /// stale the day a tool's columns move (lane T11, 22 Oct 2026). A hand entry /// stands only for a kind no shipped definition describes, with its reason. #[test] fn a_summary_kind_with_a_definition_is_never_hand_listed() { for kind in SummaryKind::ALL { let defined = crate::definitions::builtin_table_definition(kind.as_str()).is_some(); let hand = matches!(summary_home(kind), SummaryHome::Hand(_)); // A person's arrivals are their notes, a retired table no definition describes. let expected = !defined || kind == SummaryKind::Person; assert_eq!(hand, expected, "{kind}: a kind with a built-in definition reads it"); } }