//! The project-sections engine, with vectors that pin every string it emits —//! the section keys and the orders they come back in.usesuper::*;/// A caller's own member shape, with a field core never looks at, so the// "hands the caller's row back untouched" claim has something to check.#[derive(Debug, Clone, PartialEq)]structMember { kind: ProjectLinkKind, id: String, recency: Option<String>, standing_open: Option<bool>, pinned: Option<bool>, position: Option<f64>, title: Option<String>,}implSectionMemberforMember {fn kind(&self) -> ProjectLinkKind {self.kind }fn recency(&self) -> Option<&str> {self.recency.as_deref() }fn standing_open(&self) -> Option<bool> {self.standing_open }fn pinned(&self) -> Option<bool> {self.pinned }fn position(&self) -> Option<f64> {self.position }fn id(&self) -> &str { &self.id }}fn member(kind: ProjectLinkKind, id: &str, recency: Option<&str>) -> Member {Member { kind, id: id.to_string(), recency: recency.map(str::to_string), standing_open: None, pinned: None, position: None, title: None, }}fn standing(kind: ProjectLinkKind, id: &str, recency: Option<&str>, open: bool) -> Member {Member { standing_open: Some(open), ..member(kind, id, recency) }}fn arranged(id: &str, pinned: Option<bool>, position: Option<f64>) -> Member {Member { pinned, position, ..member(ProjectLinkKind::Memory, id, None) }}fn keys<M>(sections: &[ProjectSectionGroup<'_, M>]) -> Vec<&'static str> { sections.iter().map(|s| s.key.as_str()).collect()}fn ids<'a, M: SectionMember>(members: &[&'a M]) -> Vec<&'a str> { members.iter().copied().map(SectionMember::id).collect()}// ── the section registry is TOTAL over PROJECT_LINK_KINDS ────────────────────/// RULE: Every kind of record that can join a project lands in exactly one section of the project page./// In Rust the exhaustive `match` in [`section_of_kind`] IS this test — a kind/// with no section fails the build. It is ported anyway because the claim it/// makes is about the TABLE, not about the enum: the answer must be one of the/// declared section keys.#[test]fn claims_every_linkable_kind_for_exactly_one_section() {for kind inProjectLinkKind::ALL {let key = section_of_kind(kind); assert!(ProjectSectionKey::ALL.contains(&key),"kind: {kind} landed outside the declared sections" ); }}/// The other direction: a declared section no kind can ever fill is a heading/// that renders nowhere and a route that answers nothing. Cheap to state, and/// it is exactly what a half-finished rename leaves behind. The TypeScript/// throws at module load; Rust has no module-load hook, so it is a test.#[test]fn leaves_no_section_key_that_no_kind_can_ever_fill() {let claimed: Vec<ProjectSectionKey> = ProjectLinkKind::ALL .into_iter() .map(section_of_kind) .collect();for key inProjectSectionKey::ALL { assert!( claimed.contains(&key),"section: {key} is filled by no kind" ); }}#[test]fn keeps_notes_and_journal_apart() { assert_eq!( section_of_kind(ProjectLinkKind::Note),ProjectSectionKey::Notes ); assert_eq!( section_of_kind(ProjectLinkKind::Journal),ProjectSectionKey::Journal );}/// VECTOR — the whole map, read out of Node rather than retyped from the/// TypeScript source. Both columns are wire strings: the left one is a DB CHECK/// value and the right one is what the server's `get_project` and three/// surfaces key their labels by.#[test]fn the_section_of_every_kind_is_the_typescripts_own() {let vectors: &[(&str, &str)] = &[ ("thread", "chats"), ("email_thread", "email"), ("person", "people"), ("note", "notes"), ("journal", "journal"), ("todo", "todos"), ("event", "events"), ("document", "documents"), ("transaction", "money"), ("photo", "photos"), ("video", "videos"), ("memory", "memory"), ("call", "calls"), ("album", "albums"), ("secret", "passwords"), ("repository", "code"), ]; assert_eq!(vectors.len(), ProjectLinkKind::ALL.len());for (kind, expected) in vectors {let kind = ProjectLinkKind::parse(kind).expect("a declared kind"); assert_eq!(section_of_kind(kind).as_str(), *expected, "for {kind}"); }}/// VECTOR — `PROJECT_SECTION_KEYS` in its EDITORIAL order, as Node printed it./// The order is not derived from `PROJECT_LINK_KINDS` and must not be.#[test]fn the_section_keys_are_the_typescripts_own_in_its_own_order() { assert_eq!(ProjectSectionKey::ALL.map(|k| k.as_str()), ["people","chats","email","documents","todos","memory","notes","journal","events","money","code","photos","videos","albums","calls","passwords", ] );}// ── projectSectionsFor ───────────────────────────────────────────────────────/// VECTOR — one member of every kind, and the sections Node grouped them into,/// in order.#[test]fn returns_the_sections_in_the_registrys_fixed_order() {let all: Vec<Member> = ProjectLinkKind::ALL .into_iter() .enumerate() .map(|(i, kind)| member(kind, &format!("id-{i}"), Some("2026-08-20T09:00:00.000Z"))) .collect();let sections = project_sections_for(&all); assert_eq!( keys(§ions), ["people","chats","email","documents","todos","memory","notes","journal","events","money",// Code (v18), beside the containers a project was created FOR."code","photos","videos",// Albums (v42), after the pictures they group."albums",// Calls (v36), last: a project's calls are the thinnest rows on the// page and answer "when did we last actually speak", which is a// question you ask after you have looked at the work."calls",// Passwords (v6 of the Rust ladder), after the calls: what you// reach for to DO the work, not what you read to understand it."passwords", ] );// …and each holds the member whose index Node reported for it.let landed: Vec<(&str, Vec<&str>)> = sections .iter() .map(|s| (s.key.as_str(), ids(&s.members))) .collect(); assert_eq!( landed, [ ("people", vec!["id-2"]), ("chats", vec!["id-0"]), ("email", vec!["id-1"]), ("documents", vec!["id-8"]), ("todos", vec!["id-5"]), ("thoughts", vec!["id-14"]), ("memory", vec!["id-17"]), ("notes", vec!["id-3"]), ("journal", vec!["id-4"]), ("meetings", vec!["id-7"]), ("events", vec!["id-6"]), ("reader", vec!["id-9"]), ("locations", vec!["id-10"]), ("money", vec!["id-11"]), ("goals", vec!["id-12"]), ("code", vec!["id-21"]), ("decisions", vec!["id-22"]), ("feedback", vec!["id-13"]), ("photos", vec!["id-15"]), ("videos", vec!["id-16"]), ("albums", vec!["id-19"]), ("calls", vec!["id-18"]), ("passwords", vec!["id-20"]), ] );}#[test]fn hides_a_section_with_no_members() {let rows = [member(ProjectLinkKind::Todo,"t1",Some("2026-08-20T09:00:00.000Z"), )]; assert_eq!(keys(&project_sections_for(&rows)), ["todos"]);}/// The CAP is a number a surface may reach for; the grouping never applies it./// A desktop window draws them all.#[test]fn hands_back_every_member_of_a_section_uncapped() {let five: Vec<Member> = ["a", "b", "c", "d", "e"] .iter() .enumerate() .map(|(i, id)| { member(ProjectLinkKind::Todo, id,Some(&format!("2026-08-2{i}T09:00:00.000Z")), ) }) .collect();let sections = project_sections_for(&five); assert_eq!(sections[0].members.len(), 5); assert_eq!(SECTION_ROW_CAP, 3);}/// RULE: Inside a project section, records are listed newest first.#[test]fn orders_members_by_the_records_own_recency_newest_first() {let rows = [ member(ProjectLinkKind::Todo,"old",Some("2026-08-01T09:00:00.000Z"), ), member(ProjectLinkKind::Todo,"new",Some("2026-08-25T09:00:00.000Z"), ), ];let sections = project_sections_for(&rows); assert_eq!(ids(§ions[0].members), ["new", "old"]);}#[test]fn puts_a_member_with_no_recency_last_not_first() {let rows = [ member(ProjectLinkKind::Todo, "undated", None), member(ProjectLinkKind::Todo,"dated",Some("2026-08-01T09:00:00.000Z"), ), ];let sections = project_sections_for(&rows); assert_eq!(ids(§ions[0].members), ["dated", "undated"]);}/// `2026-08-21T04:00+05:30` is EARLIER than `2026-08-20T23:00Z`, and a string/// compare says the opposite.#[test]fn compares_instants_by_parsed_value_never_lexically() {let rows = [ member(ProjectLinkKind::Todo,"offset",Some("2026-08-21T04:00:00.000+05:30"), ), member(ProjectLinkKind::Todo,"utc",Some("2026-08-20T23:00:00.000Z"), ), ];let sections = project_sections_for(&rows); assert_eq!(ids(§ions[0].members), ["utc", "offset"]);}#[test]fn splits_journal_entries_out_of_notes_into_a_section_of_their_own() {let rows = [ member(ProjectLinkKind::Note,"n1",Some("2026-08-20T09:00:00.000Z"), ), member(ProjectLinkKind::Journal,"j1",Some("2026-08-21T09:00:00.000Z"), ), ];let sections = project_sections_for(&rows); assert_eq!(keys(§ions), ["notes", "journal"]); assert_eq!( sections.iter().map(|s| s.members.len()).collect::<Vec<_>>(), [1, 1] );}/// Core groups by `kind` and orders by `recency`, and every other field a/// surface put there arrives on the other side. In Rust the group holds/// BORROWS of the caller's own rows, so this is a promise the type makes rather/// than one a test has to check — checked anyway, because it is the claim.#[test]fn carries_the_callers_own_member_shape_through_untouched() {let rich = [Member { title: Some("Ship it".into()), ..member(ProjectLinkKind::Todo,"t1",Some("2026-08-20T09:00:00.000Z"), ) }];let sections = project_sections_for(&rich); assert_eq!(sections[0].members[0].title.as_deref(), Some("Ship it")); assert_eq!(sections[0].members[0], &rich[0]);}// ── an open section is a queue, not a history ────────────────────────────────/// RULE: A section whose members declare a standing reads as a working queue: the open ones sit on top, newest first within each half.#[test]fn puts_open_members_on_top_then_newest_first_inside_each_half() {useProjectLinkKind::TodoasOpen;let rows = [ standing(Open,"t-newest-settled",Some("2026-09-05T09:00:00.000Z"),false, ), standing(Open,"t-oldest-open",Some("2026-09-01T09:00:00.000Z"),true, ), standing(Open,"t-newest-open",Some("2026-09-03T09:00:00.000Z"),true, ), standing(Open,"t-older-settled",Some("2026-09-04T09:00:00.000Z"),false, ), ];let sections = project_sections_for(&rows); assert_eq!( ids(§ions[0].members), ["t-newest-open","t-oldest-open","t-newest-settled","t-older-settled", ] );}/// `standing_open` is `None` for a kind that never sets it, so its section is/// ordered by recency alone: `None` and `Some(false)` are the same answer.#[test]fn leaves_every_kind_that_declares_no_standing_exactly_as_it_was() {let rows = [ member(ProjectLinkKind::Document,"a",Some("2026-09-01T09:00:00.000Z"), ), member(ProjectLinkKind::Document,"b",Some("2026-09-05T09:00:00.000Z"), ), ];let sections = project_sections_for(&rows); assert_eq!(ids(§ions[0].members), ["b", "a"]);}// ── countsInSection ──────────────────────────────────────────────────────────//// What a FIGURE counts (2026-09-05). The owner, on a project page reading "120// to-dos" over a project whose todos were nearly all finished: *"We should only// show number of open todos here. Even though when we click through there will// be links to Done todos and dropped todos."*//// ONE declaration, because the project page's row, the tile on Home and the Mac// window all print this number, and three copies of the rule is how they come// to print three numbers./// RULE: A project's to-do figure counts only the ones still open, so the number reads as work remaining.#[test]fn counts_an_open_todo_and_skips_a_settled_one() { assert!(counts_in_section(&standing(ProjectLinkKind::Todo,"x",None,true ))); assert!(!counts_in_section(&standing(ProjectLinkKind::Todo,"x",None,false )));}/// An older cached member shape carries no standing. A figure has to choose,/// and a stale count that RISES on the next read is the safer of the two/// errors.#[test]fn skips_a_todo_whose_standing_was_never_read_rather_than_guessing_open() { assert!(!counts_in_section(&member(ProjectLinkKind::Todo,"x",None )));}/// VECTOR — the whole grid, all three standings over every kind. A todo is the/// one kind whose number reads as WORK REMAINING.#[test]fn counts_every_other_kind_whole() {for kind inProjectLinkKind::ALL {let todo = kind == ProjectLinkKind::Todo; assert_eq!( counts_in_section(&member(kind, "x", None)), !todo,"no standing, kind: {kind}" ); assert!( counts_in_section(&standing(kind, "x", None, true)),"open, kind: {kind}" ); assert_eq!( counts_in_section(&standing(kind, "x", None, false)), !todo,"settled, kind: {kind}" ); }}#[test]fn does_not_touch_membership_the_section_still_holds_every_one() {let rows = [ standing(ProjectLinkKind::Todo,"open",Some("2026-09-01T09:00:00.000Z"),true, ), standing(ProjectLinkKind::Todo,"done",Some("2026-09-02T09:00:00.000Z"),false, ), ];let sections = project_sections_for(&rows); assert_eq!(sections[0].members.len(), 2); assert_eq!( sections[0] .members .iter() .filter(|m| counts_in_section(**m)) .count(),1 );}// ── vectors: the orders, read out of Node ────────────────────────────────────/// VECTOR — four recency scenarios and the exact orders the TypeScript sorted/// them into. Two of them pin things reasoning would get wrong: an unparseable/// string sorts with the nulls rather than throwing, and `2026-08-21`/// (date-only, so UTC midnight) is NEWER than `2026-08-20T23:59:59.999Z`.#[test]fn the_recency_orders_are_the_typescripts_own() {// "mixed offsets and Z": `b` and `c` are the SAME instant (23:00Z), so the// stable sort keeps their input order, and `d` (null) ties with `e` (not a// date) at the bottom for the same reason.let mixed = [ member(ProjectLinkKind::Todo,"a",Some("2026-08-21T04:00:00.000+05:30"), ), member(ProjectLinkKind::Todo, "b", Some("2026-08-20T23:00:00.000Z")), member(ProjectLinkKind::Todo,"c",Some("2026-08-20T22:00:00.000-01:00"), ), member(ProjectLinkKind::Todo, "d", None), member(ProjectLinkKind::Todo, "e", Some("not a date")), ]; assert_eq!( ids(&project_sections_for(&mixed)[0].members), ["b", "c", "a", "d", "e"] );// "equal instants keep input order" — two spellings of one moment.let equal = [ member(ProjectLinkKind::Note, "z", Some("2026-08-20T09:00:00.000Z")), member(ProjectLinkKind::Note, "y", Some("2026-08-20T09:00:00Z")), member(ProjectLinkKind::Note, "x", Some("2026-08-20T09:00:00.000Z")), ]; assert_eq!( ids(&project_sections_for(&equal)[0].members), ["z", "y", "x"] );// "date-only and space-separated forms" — `2026-08-21` parses as UTC// midnight, which is a minute AFTER `2026-08-20T23:59:59.999Z`.let forms = [ member(ProjectLinkKind::Document, "dateonly", Some("2026-08-21")), member(ProjectLinkKind::Document,"zulu",Some("2026-08-20T23:59:59.999Z"), ), member(ProjectLinkKind::Document,"millis0",Some("1970-01-01T00:00:00.000Z"), ), ]; assert_eq!( ids(&project_sections_for(&forms)[0].members), ["dateonly", "zulu", "millis0"] );}/// VECTOR — the memory section's own order, three scenarios out of Node./// Pinned first, then position ascending, then the id; a member with no/// position sorts LAST.#[test]fn the_memory_arrangement_orders_are_the_typescripts_own() {let ordered = [ arranged("m-c", None, Some(2000.0)), arranged("m-a", Some(true), Some(1000.0)), arranged("m-b", None, Some(1000.0)), arranged("m-d", None, None), arranged("m-e", None, None), ];let sections = project_sections_for(&ordered); assert_eq!(keys(§ions), ["memory"]); assert_eq!( ids(§ions[0].members), ["m-a", "m-b", "m-c", "m-d", "m-e"] );// Ties on position break on the id, and the empty id sorts first.let tied = [ arranged("zz", None, Some(500.0)), arranged("aa", None, Some(500.0)), arranged("MM", None, Some(500.0)), arranged("", None, Some(500.0)), ]; assert_eq!( ids(&project_sections_for(&tied)[0].members), ["", "MM", "aa", "zz"] );// Two pinned entries sort among THEMSELVES by position, above an unpinned// one whose position is lower than either.let pinned = [ arranged("p2", Some(true), Some(2000.0)), arranged("p1", Some(true), Some(1000.0)), arranged("u1", Some(false), Some(1.0)), ]; assert_eq!( ids(&project_sections_for(&pinned)[0].members), ["p1", "p2", "u1"] );}/// RULE: Memory is the one project section that ignores dates entirely; it is in the order the owner arranged./// The memory section is the ONE that does not sort by recency, so a newer/// entry with a later position stays below an older one with an earlier/// position. "What order is my memory in" has exactly one answer — the/// owner's.#[test]fn the_memory_section_ignores_recency_entirely() {let rows = [Member { position: Some(2000.0), ..member(ProjectLinkKind::Memory,"newest",Some("2026-09-09T09:00:00.000Z"), ) },Member { position: Some(1000.0), ..member(ProjectLinkKind::Memory,"oldest",Some("2026-01-01T09:00:00.000Z"), ) }, ];let sections = project_sections_for(&rows); assert_eq!(ids(§ions[0].members), ["oldest", "newest"]);}/// The comparator the TypeScript wrote is NOT a total order: `undefined !==/// false`, so a member that declares `standingOpen: false` and one that/// declares nothing take the first branch, which then answers `0`. They never/// sort against each other by recency, and the newer one stays wherever the/// input put it.////// Pinned here rather than fixed, because it is the shipped behaviour of three/// surfaces; with two members every stable sort agrees, so this is the shape of/// the quirk with none of the sort algorithm's opinion in it.#[test]fn a_declared_settled_and_an_undeclared_standing_never_sort_by_date() {let settled_first = [ standing(ProjectLinkKind::Todo,"settled",Some("2026-09-05T09:00:00.000Z"),false, ), member(ProjectLinkKind::Todo,"undeclared-newer",Some("2026-09-06T09:00:00.000Z"), ), ]; assert_eq!( by_recency_desc(&settled_first[0], &settled_first[1]),Ordering::Equal ); assert_eq!( ids(&project_sections_for(&settled_first)[0].members), ["settled", "undeclared-newer"] );// The same two rows the other way round come back the other way round —// which is what "equal" means for a stable sort.let undeclared_first = [settled_first[1].clone(), settled_first[0].clone()]; assert_eq!( ids(&project_sections_for(&undeclared_first)[0].members), ["undeclared-newer", "settled"] );}/// Two members that AGREE about the field do fall through to recency, which is/// what makes the pair above a quirk rather than the rule.#[test]fn two_undeclared_standings_do_fall_through_to_recency() {let rows = [ member(ProjectLinkKind::Todo,"older",Some("2026-09-05T09:00:00.000Z"), ), member(ProjectLinkKind::Todo,"newer",Some("2026-09-06T09:00:00.000Z"), ), ]; assert_eq!( ids(&project_sections_for(&rows)[0].members), ["newer", "older"] );}/// The internal sort is a merge sort rather than `slice::sort_by`, because/// `by_recency_desc` is not a total order and the standard sort documents that/// it may PANIC on one. Sorting a hundred such rows must simply answer.#[test]fn a_section_full_of_the_ambiguous_pair_answers_rather_than_panicking() {let rows: Vec<Member> = (0..100) .map(|i| {let recency = format!("2026-09-{:02}T09:00:00.000Z", (i % 28) + 1);if i % 2 == 0 { standing(ProjectLinkKind::Todo, &format!("m-{i:03}"),Some(&recency),false, ) } else { member(ProjectLinkKind::Todo, &format!("m-{i:03}"), Some(&recency)) } }) .collect();let sections = project_sections_for(&rows); assert_eq!(sections[0].members.len(), 100);letmut seen: Vec<&str> = ids(§ions[0].members); seen.sort_unstable(); seen.dedup(); assert_eq!(seen.len(), 100, "every row comes back, exactly once");}