//! Vectors for `derived_id.rs`, taken from //! `packages/store/__tests__/derivedId.test.ts` and extended with cases //! computed by running the TypeScript itself. //! //! Every named wrapper is pinned with at least three vectors, because the bug //! this whole module exists to prevent is one side hashing `(id, iso)` while //! the other hashes `(iso, id)` — an argument order swap produces a //! perfectly valid, perfectly different id, and only a vector catches it. use super::*; use crate::file_identity::{FileId, Namespace}; /// A namespace or a file id for a vector: the strings are the TypeScript's, /// and only the type is new. fn ns(namespace: &str) -> Namespace { Namespace::for_tests(namespace) } fn file(id: &str) -> FileId { FileId::for_tests(id) } const UUID_SHAPE: [usize; 5] = [8, 4, 4, 4, 12]; fn is_uuid_shaped(id: &str) -> bool { let groups: Vec<&str> = id.split('-').collect(); groups.len() == 5 && groups .iter() .zip(UUID_SHAPE) .all(|(group, width)| group.len() == width) && id .chars() .all(|c| c == '-' || c.is_ascii_digit() || ('a'..='f').contains(&c)) } #[cfg(test)] mod vectors { use super::*; #[test] fn derived_id_matches_the_typescript() { // Vectors computed by running `packages/store/src/protocol/derivedId.ts` // in node, so the two implementations are pinned to each other rather // than to anyone's reading of the algorithm. let cases: &[(&str, &[&str], &str)] = &[ ("t", &[""], "426c3d77-b13e-3495-a692-e288b674a24b"), ("t", &["a"], "13b44368-c3f2-949c-a93e-bdbd012303c4"), ("t", &["abc"], "097a0a7e-bc3f-e3ea-c503-fb7a6818aa14"), ("t", &["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"], "8dfcf1a9-22a2-9d58-558a-ac095e2baa7a"), ("t", &["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"], "51d6c711-0670-f245-3612-7339322c3276"), ("t", &["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"], "2f94af5b-5cf4-a278-3452-b7916ba84c95"), ("t", &["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"], "44289dab-9ec1-46f3-2004-e47c22297610"), ("t", &["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"], "67b85702-20db-d69a-6bb9-206b0fce8bc0"), ("t", &["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"], "f6baed9b-4d2a-4a25-cb20-a37d90a3771c"), ("t", &["caf\u{e9} \u{2014} na\u{ef}ve \u{2014} \u{65e5}\u{672c}\u{8a9e} \u{2014} \u{1f9f5}"], "81e53b09-6678-f32e-8545-28515baacec5"), ("t", &["ab", "c"], "b579fc12-dc02-43e9-5040-f70ba285441e"), ("t", &["a", "bc"], "31318bc7-5c8f-b987-7c50-266c6eefcbd5"), ("one", &["k"], "d9f81af6-157d-f64c-8bd2-9247fa2af651"), ("two", &["k"], "5131ae0a-2474-a9aa-7181-2f4d9afd496e"), ]; for (table, parts, expected) in cases { assert_eq!( &derived_id(table, parts), expected, "derivedId({table:?}, {parts:?})" ); } } #[test] fn recurrence_occurrence_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( recurrence_occurrence_id( "a3f1c2d4-0000-4000-8000-000000000001", "2026-11-02T04:00:00.000Z" ), "743a8fca-e45c-f782-6f85-10b10b8776e2" ); assert_eq!( recurrence_occurrence_id( "a3f1c2d4-0000-4000-8000-000000000001", "2026-12-02T04:00:00.000Z" ), "05472e02-868b-f367-b45b-69103fa9aaab" ); assert_eq!( recurrence_occurrence_id( "b4e2d3c5-0000-4000-8000-000000000002", "2026-11-02T04:00:00.000Z" ), "6a197900-58ae-a5bd-afbe-e705ef0ae5c5" ); } #[test] fn project_link_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( project_link_id("p1", "memory", "m1"), "bf2f8340-40ad-b418-e5b8-119c3439f498" ); assert_eq!( project_link_id("p1", "note", "n1"), "93c34661-a54a-5cf6-4749-9caa074bb66d" ); assert_eq!( project_link_id("p2", "memory", "m1"), "e746f0cf-994b-f7c0-492d-cd18e5767e0c" ); } #[test] fn email_thread_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( email_thread_id(&ns("ws-1"), "thread-a"), "8d3cccf0-e36b-c3b9-d934-de438b445745" ); assert_eq!( email_thread_id(&ns("ws-1"), "thread-b"), "462acfb1-f71f-ea34-0ca7-da862a375a26" ); assert_eq!( email_thread_id(&ns("ws-2"), "thread-a"), "12b608f6-cfba-9929-e0f7-7637eddd8ef2" ); } #[test] fn email_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( email_id(&ns("ws-1"), "mid:abc"), "1cd1b516-b689-c372-5170-abdb722a5310" ); assert_eq!( email_id(&ns("ws-1"), "eml2:def"), "4dfebcfa-eb4b-f422-cd04-140820465b74" ); assert_eq!( email_id(&ns("ws-2"), "mid:abc"), "42e4615d-9bd1-d9f2-8e29-93ae9dfd04e7" ); } #[test] fn email_participant_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( email_participant_id("th-1", "a@x.com"), "18425087-efa3-fed0-9e29-ff1a3db2dd2d" ); assert_eq!( email_participant_id("th-1", "b@x.com"), "7e9f01d7-5bf7-48c7-e3b8-54da3c133c40" ); assert_eq!( email_participant_id("th-2", "a@x.com"), "8d751c98-5bb3-9399-4074-2fcea65bc2dd" ); assert_eq!( email_participant_id("th-1", "A@X.com"), "8af3ebb6-aa8c-893e-32fa-8bdf88dc8936" ); } #[test] fn blocked_sender_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( blocked_sender_id(&ns("ws-1"), "spam@x.com"), "c01f180b-8138-4757-c77f-0ede455465b5" ); assert_eq!( blocked_sender_id(&ns("ws-1"), "other@x.com"), "dfc9dc8b-cc22-f421-5986-f9e214c0d7e4" ); assert_eq!( blocked_sender_id(&ns("ws-2"), "spam@x.com"), "e077ce68-1864-01a5-5b35-feb7696d3779" ); } #[test] fn record_link_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( record_link_id("note", "n1", "person", "p1"), "b76b2135-8839-78d6-5102-f7ddbbd2a47c" ); assert_eq!( record_link_id("person", "p1", "note", "n1"), "1c6be229-c9b5-33fe-16ba-57db03067f7a" ); assert_eq!( record_link_id("note", "n2", "person", "p1"), "d14eefd2-86ff-a2b3-3648-2f082da2b3ae" ); } #[test] fn photo_record_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!(photo_record_id("abc123"), "photo:abc123"); assert_eq!(photo_record_id("deadbeef"), "photo:deadbeef"); assert_eq!(photo_record_id(""), "photo:"); } #[test] fn video_record_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!(video_record_id("abc123"), "video:abc123"); assert_eq!(video_record_id("deadbeef"), "video:deadbeef"); assert_eq!(video_record_id(""), "video:"); } #[test] fn contact_handle_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( contact_handle_id(&ns("ws-1"), "phone", "919999900000"), "7823dc7c-736e-7a92-6cf3-5832189de9bb" ); assert_eq!( contact_handle_id(&ns("ws-1"), "email", "a@x.com"), "0565b149-dab8-da46-0f0a-b7559bd576a1" ); assert_eq!( contact_handle_id(&ns("ws-2"), "phone", "919999900000"), "7aa63a0c-a941-7034-26e9-dfcec596303c" ); } #[test] fn call_log_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( call_log_id(&ns("ws-1"), "ios", "Z-1"), "439c61e4-0e63-55e8-09bb-cc6df25b60da" ); assert_eq!( call_log_id(&ns("ws-1"), "ios", "Z-2"), "725beff3-3c22-f2e3-a945-d89e03290508" ); assert_eq!( call_log_id(&ns("ws-1"), "mac", "Z-1"), "fb0ab75c-911a-67ca-8f81-e9851f19f984" ); } #[test] fn media_sighting_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( media_sighting_id("photo", "photo:abc", "share", None), "0ce65914-e797-5c0b-f979-395b5af20732" ); assert_eq!( media_sighting_id("photo", "photo:abc", "share", Some("")), "0ce65914-e797-5c0b-f979-395b5af20732" ); assert_eq!( media_sighting_id("video", "video:abc", "photos", Some("asset-1")), "f6b2c52f-9f74-2bd9-8e23-52a22c350e63" ); assert_eq!( media_sighting_id("photo", "photo:abc", "photos", Some("asset-1")), "01498773-468c-f700-b661-2a96d59cb37b" ); } #[test] fn media_face_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( media_face_id("photo", "photo:abc", "photos", "face-1"), "99c1ec2f-36e9-8b84-a0ec-44d2c08cdd42" ); assert_eq!( media_face_id("photo", "photo:abc", "photos", "face-2"), "84ceb74a-d2f4-ae00-768b-05c159c65004" ); assert_eq!( media_face_id("video", "video:abc", "photos", "face-1"), "821e14ba-9b3f-36fa-7e8b-40c5a196edb0" ); } #[test] fn media_duplicate_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( media_duplicate_id("photo:keep", "photo:copy"), "a251ec86-9694-8b3b-1ff8-4275dfc79705" ); assert_eq!( media_duplicate_id("photo:copy", "photo:keep"), "7a3e0994-12f2-83f4-820d-1a06eebef655" ); assert_eq!( media_duplicate_id("photo:a", "photo:b"), "fe98664f-b586-c434-a607-038ee5b74283" ); } #[test] fn media_album_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( media_album_id("trip", "2026-03-14"), "album:trip:2026-03-14" ); assert_eq!( media_album_id("event", "2026-03-14T09"), "album:event:2026-03-14T09" ); assert_eq!( media_album_id("trip", "2026-03-15"), "album:trip:2026-03-15" ); } #[test] fn media_album_item_id_matches_the_typescript() { // Computed from the TypeScript — the ARGUMENT ORDER is what these pin. assert_eq!( media_album_item_id("album:trip:2026-03-14", "photo:abc"), "54390fc6-0d99-dcb9-6ba2-4a5b3c781f49" ); assert_eq!( media_album_item_id("album:trip:2026-03-14", "photo:def"), "7312a117-dab1-ca5c-1783-4257bf395645" ); assert_eq!( media_album_item_id("album:event:2026-03-14T09", "photo:abc"), "9dc056c2-2f1e-55ae-b963-e70fad611312" ); } #[test] fn is_uuid_shaped_like_every_other_id_in_the_schema() { assert!(is_uuid_shaped(&derived_id("tbl", &["a"]))); assert!(is_uuid_shaped(&project_link_id("p", "memory", "m"))); assert!(is_uuid_shaped(&record_link_id( "note", "n1", "person", "p1" ))); } #[test] fn separates_the_table_so_two_tables_cannot_collide_on_one_key() { assert_ne!(derived_id("one", &["k"]), derived_id("two", &["k"])); } #[test] fn cannot_be_confused_by_parts_that_would_join_ambiguously() { // The separator is the guard: without it ["ab","c"] and ["a","bc"] // hash the same string. assert_ne!(derived_id("t", &["ab", "c"]), derived_id("t", &["a", "bc"])); } #[test] fn a_record_link_is_direction_sensitive() { assert_ne!( record_link_id("note", "n1", "person", "p1"), record_link_id("person", "p1", "note", "n1") ); } /// `scripts/lib/link-id.mjs` writes links into the simulator's seed, so it /// and this module must derive one id for one edge. Both read these /// vectors (`scripts/__tests__/link-id.test.ts` is the other half): a port /// that drifts — an argument order, a `to_database` equal to the own file /// not read as none, a string hashed as anything but UTF-8 — fails here or /// there by the vector's name. #[test] fn link_id_for_matches_the_shared_vectors() { let vectors: Vec = serde_json::from_str(include_str!( "../../tests/fixtures/link-id-vectors.json" )) .expect("link-id-vectors.json is a list"); assert!(!vectors.is_empty()); for vector in &vectors { let field = |name: &str| vector[name].as_str().unwrap_or_else(|| panic!("{name} in {vector}")); let ends = LinkRow { from_kind: field("from_kind"), from_id: field("from_id"), to_kind: field("to_kind"), to_id: field("to_id"), to_database: vector["to_database"].as_str(), }; assert_eq!( link_id_for(&ends, &file(field("own_file"))), field("id"), "{}", field("what") ); } } #[test] fn a_record_link_does_not_collide_with_a_project_link_over_the_same_ids() { assert_ne!( record_link_id("note", "n1", "person", "p1"), project_link_id("n1", "person", "p1") ); } #[test] fn a_sighting_with_no_source_id_spells_null_as_the_empty_string() { // The table's unique index says `ifnull(source_id, '')`; the two agree // by construction, so a share-sheet sighting and an empty-string one // are ONE row. assert_eq!( media_sighting_id("photo", "photo:abc", "share", None), media_sighting_id("photo", "photo:abc", "share", Some("")) ); } #[test] fn a_photo_and_a_video_of_the_same_bytes_are_two_records() { assert_ne!(photo_record_id("abc"), video_record_id("abc")); assert_eq!(photo_record_id("abc"), "photo:abc"); assert_eq!(video_record_id("abc"), "video:abc"); } #[test] fn an_album_id_stays_readable_rather_than_hashed() { assert_eq!( media_album_id("trip", "2026-03-14"), "album:trip:2026-03-14" ); assert_eq!( media_album_id("event", "2026-03-14T09"), "album:event:2026-03-14T09" ); } // ── the merge's six, plus the two thread kinds ──────────────────────── // // The id two DEVICES agree on, which is what makes a workspace-scoped // UNIQUE index survive two files becoming one. Each is pinned for // determinism, for uuid shape, for sensitivity to EVERY part, and against // the neighbour it could be confused with. const WS: &str = "11111111-1111-4111-8111-111111111111"; #[test] fn a_project_id_is_the_workspace_and_the_slug_and_nothing_else() { let id = memory_project_id(&ns(WS), "cotton"); assert_eq!(id, memory_project_id(&ns(WS), "cotton")); assert!(is_uuid_shaped(&id)); assert_ne!(id, memory_project_id(&ns(WS), "cottondb")); assert_ne!(id, memory_project_id(&ns("other-workspace"), "cotton")); } #[test] fn a_memory_id_is_the_workspace_and_the_slug() { let id = memory_id(&ns(WS), "co-founder-mode"); assert_eq!(id, memory_id(&ns(WS), "co-founder-mode")); assert!(is_uuid_shaped(&id)); assert_ne!(id, memory_id(&ns(WS), "co-founder")); assert_ne!(id, memory_project_id(&ns(WS), "co-founder-mode")); } #[test] fn a_document_id_is_the_workspace_and_the_bytes() { let sha = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; let id = document_id(&ns(WS), sha); assert_eq!(id, document_id(&ns(WS), sha)); assert!(is_uuid_shaped(&id)); assert_ne!(id, document_id(&ns(WS), &sha.replace('e', "f"))); assert_ne!(id, document_id(&ns("other-workspace"), sha)); } #[test] fn a_contact_id_is_the_workspace_and_the_digits() { let id = msg_contact_id(&ns(WS), "919812345678"); assert_eq!(id, msg_contact_id(&ns(WS), "919812345678")); assert!(is_uuid_shaped(&id)); // The column holds DIGITS ONLY, and this function hashes what it is // given: a caller that keeps the `+` derives an id nothing finds. assert_ne!(id, msg_contact_id(&ns(WS), "+919812345678")); } #[test] fn a_post_id_is_the_workspace_the_source_the_account_and_the_external_id() { let id = post_id(&ns(WS), "instagram", "owner", "ig:shortcode:A1"); assert_eq!(id, post_id(&ns(WS), "instagram", "owner", "ig:shortcode:A1")); assert!(is_uuid_shaped(&id)); assert_ne!(id, post_id(&ns("other-workspace"), "instagram", "owner", "ig:shortcode:A1")); assert_ne!(id, post_id(&ns(WS), "facebook", "owner", "ig:shortcode:A1")); assert_ne!(id, post_id(&ns(WS), "instagram", "someone", "ig:shortcode:A1")); assert_ne!(id, post_id(&ns(WS), "instagram", "owner", "ig:shortcode:A2")); } #[test] fn a_keyed_dm_thread_is_its_own_family() { let id = keyed_dm_thread_id(&ns(WS), "instagram", "export:inbox/a_1"); assert_eq!(id, keyed_dm_thread_id(&ns(WS), "instagram", "export:inbox/a_1")); assert!(is_uuid_shaped(&id)); assert_ne!(id, keyed_dm_thread_id(&ns(WS), "whatsapp", "export:inbox/a_1")); assert_ne!(id, keyed_dm_thread_id(&ns(WS), "instagram", "export:inbox/b_2")); assert_ne!(id, group_thread_id(&ns(WS), "export:inbox/a_1")); assert_ne!(id, dm_thread_id(&ns(WS), "export:inbox/a_1")); } #[test] fn a_dm_thread_and_a_group_thread_never_share_an_id() { // Same column position, two different kinds of string. One hash over // both would let a contact id and a group key collide. assert_ne!(dm_thread_id(&ns(WS), "key"), group_thread_id(&ns(WS), "key")); assert_eq!(dm_thread_id(&ns(WS), "contact-1"), dm_thread_id(&ns(WS), "contact-1")); assert_eq!( group_thread_id(&ns(WS), "whatsapp:9199@g.us"), group_thread_id(&ns(WS), "whatsapp:9199@g.us") ); assert!(is_uuid_shaped(&dm_thread_id(&ns(WS), "contact-1"))); assert!(is_uuid_shaped(&group_thread_id(&ns(WS), "whatsapp:9199@g.us"))); assert_ne!(dm_thread_id(&ns(WS), "contact-1"), dm_thread_id(&ns(WS), "contact-2")); } #[test] fn a_repository_id_is_derived_so_two_macs_discovering_one_forge_converge() { // The whole of the bug this exists for: two `ReposWorker`s list the // same forge, see the same key, and used to mint a uuid apiece. const KEY: &str = "git.example.com/owner/name"; assert_eq!(repo_id(&file(WS), KEY), repo_id(&file(WS), KEY)); assert!(is_uuid_shaped(&repo_id(&file(WS), KEY))); // `(workspace_id, key)` is the table's UNIQUE, and both halves count. assert_ne!(repo_id(&file(WS), KEY), repo_id(&file("other"), KEY)); assert_ne!( repo_id(&file(WS), KEY), repo_id(&file(WS), "git.example.com/owner/other") ); // The host is part of the key, so one repository mirrored from two // forges is two rows — which is what the key says it is. assert_ne!( repo_id(&file(WS), KEY), repo_id(&file(WS), "git.other.com/owner/name") ); } #[test] fn a_session_id_is_derived_from_the_pair_the_table_is_unique_on() { // NOT workspace-scoped, because `UNIQUE (tool, external_id)` is not. const EXTERNAL: &str = "13a31e2d-4fc8-4918-a26f-97002dac0b49"; assert_eq!( session_id("claude-code", EXTERNAL), session_id("claude-code", EXTERNAL) ); assert!(is_uuid_shaped(&session_id("claude-code", EXTERNAL))); assert_ne!( session_id("claude-code", EXTERNAL), session_id("codex", EXTERNAL), "the same id from two tools is two sessions, exactly as the index says" ); assert_ne!( session_id("claude-code", EXTERNAL), session_id("claude-code", "other") ); } #[test] fn every_mirrored_child_is_derived_from_the_key_its_table_is_unique_on() { // The repository was only half of it: every child of a mirror carries // a workspace-scoped UNIQUE of its own, and the mirror MINTS. Two Macs // reading the same forge used to write two rows for one commit sha — // "UNIQUE constraint failed: cotton_repo_commits.workspace_id, // repo_id, sha", 96 of them on one file — so converging the parent // alone would only have moved the collision one level down. const REPO: &str = "repo-1"; const SHA: &str = "736aa24d07f1afadf2af96e720776b2c2a1c6b42"; for id in [ repo_pull_id(&file(WS), REPO, 929), repo_issue_id(&file(WS), REPO, 929), repo_comment_id(&file(WS), REPO, "review", 77), repo_commit_id(&file(WS), REPO, SHA), repo_release_id(&file(WS), REPO, "v0.11.1"), repo_bundle_id(&file(WS), REPO, &"a".repeat(64)), prompt_id("session-1", 131), ] { assert!(is_uuid_shaped(&id), "{id}"); } // Each is a FUNCTION of its key: two producers arrive at one id. assert_eq!(repo_commit_id(&file(WS), REPO, SHA), repo_commit_id(&file(WS), REPO, SHA)); // And the key's parts all count. assert_ne!(repo_pull_id(&file(WS), REPO, 929), repo_pull_id(&file(WS), REPO, 930)); assert_ne!(repo_pull_id(&file(WS), REPO, 929), repo_pull_id(&file(WS), "repo-2", 929)); assert_ne!(repo_pull_id(&file(WS), REPO, 929), repo_pull_id(&file("other"), REPO, 929)); assert_ne!( repo_comment_id(&file(WS), REPO, "review", 77), repo_comment_id(&file(WS), REPO, "issue", 77), "`kind` is in the UNIQUE, so two kinds with one remote id are two rows" ); assert_ne!(prompt_id("session-1", 131), prompt_id("session-2", 131)); // A pull and an issue numbered the same are two rows, which is what // separating the table name is for. assert_ne!(repo_pull_id(&file(WS), REPO, 929), repo_issue_id(&file(WS), REPO, 929)); } }