//! `packages/core/__tests__/messages/ids.test.ts`, case for case.//!//! The two derived chat ids, now that a person can be a HANDLE as well as a//! phone (pairing N-D4).//!//! The two vocabularies cannot collide, by GRAMMAR rather than by CHECK: a phone//! is 8–15 digits and nothing else, and a relay name always carries a non-digit.//! So one function per id keeps taking one argument, and an all-digit string is a//! phone — never a handle that happens to look like one.//!//! Both shapes stay frozen (chat spec §4.8): the v9 migrator spells the reaction//! form in SQL, because a migration cannot call TypeScript.//!//! Every handle below is invented and every number comes from the test pool's//! shape [nothing-real-ever-goes-in-cotton-s-public-tree].use std::collections::BTreeSet;usesuper::*;const THREAD: &str = "thread:one";const MESSAGE: &str = "msg:one";const PHONES: [&str; 3] = ["910123462100", "910123462101", "12345678"];const HANDLES: [&str; 4] = ["alice-t", "bright-oak", "quiet-fern", "a1"];// ── receiptRowId ─────────────────────────────────────────────────────────────#[test]fn mints_the_frozen_phone_form_unchanged() { assert_eq!( receipt_row_id(THREAD, "910123462100").as_deref(),Ok("receipt:thread:one:910123462100") );}#[test]fn mints_the_same_shape_for_a_handle() { assert_eq!( receipt_row_id(THREAD, "alice-t").as_deref(),Ok("receipt:thread:one:alice-t") );}/// `12345678` is not a relay name — a name always carries a non-digit — so there/// is no second reading of it to disagree with.#[test]fn reads_an_all_digit_value_as_a_phone_so_it_mints_one_id_and_not_two() { assert!(!is_relay_name("12345678")); assert_eq!( receipt_row_id(THREAD, "12345678").as_deref(),Ok("receipt:thread:one:12345678") );}#[test]fn gives_every_phone_and_every_handle_its_own_id() {let minted: Vec<String> = PHONES .iter() .chain(HANDLES.iter()) .map(|value| receipt_row_id(THREAD, value).expect("a phone or a handle")) .collect();let unique: BTreeSet<&String> = minted.iter().collect(); assert_eq!(unique.len(), minted.len());// And no handle can reach a phone's id: the phone ids are exactly the ones// minted from the phones.let from_handles: Vec<String> = HANDLES .iter() .map(|handle| receipt_row_id(THREAD, handle).expect("a handle")) .collect();let from_phones: Vec<String> = PHONES .iter() .map(|phone| receipt_row_id(THREAD, phone).expect("a phone")) .collect();for id in &from_handles { assert!(!from_phones.contains(id), "{id}"); }}#[test]fn refuses_anything_that_is_neither_rather_than_minting_an_id_nobody_can_split() {for bad in ["+910123462100","alice:t","alice|t","Alice","-alice","","7654321", ] { assert!(receipt_row_id(THREAD, bad).is_err(), "{bad:?}"); }}#[test]fn a_receipt_still_needs_a_thread() { assert_eq!(receipt_row_id("", "alice-t"), Err(ChatIdError::NoThreadId));}// ── reactionRowId ────────────────────────────────────────────────────────────/// RULE: A reaction's row is worked out from the message and who reacted, so the same reaction is always the same row.#[test]fn a_reaction_mints_the_frozen_phone_form_unchanged() { assert_eq!( reaction_row_id(MESSAGE, "910123462100").as_deref(),Ok("react:msg:one:910123462100") );}#[test]fn still_accepts_the_owners_own_reaction() { assert_eq!( reaction_row_id(MESSAGE, "self").as_deref(),Ok("react:msg:one:self") );}#[test]fn a_reaction_mints_the_same_shape_for_a_handle() { assert_eq!( reaction_row_id(MESSAGE, "bright-oak").as_deref(),Ok("react:msg:one:bright-oak") );}#[test]fn gives_every_actor_its_own_id() {let minted: Vec<String> = std::iter::once(&"self") .chain(PHONES.iter()) .chain(HANDLES.iter()) .map(|actor| reaction_row_id(MESSAGE, actor).expect("self, a phone or a handle")) .collect();let unique: BTreeSet<&String> = minted.iter().collect(); assert_eq!(unique.len(), minted.len());}/// RULE: A reaction is refused unless it names the owner, a phone number or a relay name, so no unreadable row is ever created.#[test]fn refuses_an_actor_that_is_neither_a_phone_a_handle_nor_self() {for bad in ["+910123462100","alice:t","alice|t","Alice","-alice","","SELF", ] { assert!(reaction_row_id(MESSAGE, bad).is_err(), "{bad:?}"); }}#[test]fn a_reaction_still_needs_a_message() { assert_eq!( reaction_row_id("", "alice-t"),Err(ChatIdError::NoMessageId) );}// ── the refusal sentences ────────────────────────────────────────────────────/// The `Display` of each refusal is the TypeScript's thrown sentence verbatim —/// the whole reason the error carries the offending value rather than a code./// Read out of Node, not composed here.#[test]fn the_refusals_read_exactly_as_the_typescript_throws_them() { assert_eq!( reaction_row_id(MESSAGE, "Alice").unwrap_err().to_string(),"cotton-chat: a reaction actor is \"self\", a phone as digits or a relay name, got \"Alice\"" ); assert_eq!( receipt_row_id(THREAD, "+910123462100") .unwrap_err() .to_string(),"cotton-chat: a receipt reader is a phone as digits or a relay name, got \"+910123462100\"" ); assert_eq!( reaction_row_id("", "alice-t").unwrap_err().to_string(),"cotton-chat: a reaction id needs a message id" ); assert_eq!( receipt_row_id("", "alice-t").unwrap_err().to_string(),"cotton-chat: a receipt id needs a thread id" );}// ── Node vectors ─────────────────────────────────────────────────────────────/// Printed by running the TypeScript (`vec_ids.ts`), pasted verbatim.////// The interesting one is the third: a message id may itself contain colons, and/// the id is deliberately not escaped — the shape is frozen and a reader splits/// from the ENDS, not on every separator.#[test]fn node_vectors() { assert_eq!( reaction_row_id("01J8ZQ4T7M9ABCDEF", "919000000001").as_deref(),Ok("react:01J8ZQ4T7M9ABCDEF:919000000001") ); assert_eq!( reaction_row_id("msg:a:b", "quiet-fern").as_deref(),Ok("react:msg:a:b:quiet-fern") ); assert_eq!( receipt_row_id("01J8ZQ4T7M9ABCDEF", "a1").as_deref(),Ok("receipt:01J8ZQ4T7M9ABCDEF:a1") ); assert_eq!( receipt_row_id("t", "999999999999999").as_deref(),Ok("receipt:t:999999999999999") );}