/** * The import engine's pure rules, ported case for case from their Rust tests: * `messages/ids_tests.rs` (the reaction id), `handles_tests.rs` (is_relay_name), * `import/group_tests.rs` (slug and key), `protocol/derived_id_tests.rs` (the * ids `ids.ts` exports), `protocol/fingerprint/fingerprint_tests.rs` (the * message part) and `rules/people/handles_tests.rs` (social handles). Where the * Rust fixes a literal, so does this. */ import { describe, expect, it } from "vitest"; import vectors from "../../../crates/cotton-rules/vectors/import.json"; import { CONTENT_EXTERNAL_ID_PREFIX, contentFingerprint, minuteBucket, normalizeBodyForId } from "../src/import/fingerprint.ts"; import { groupExternalKey, slugifyTitle } from "../src/import/group.ts"; import { normalizeHandle, socialHandle, type HandleKind } from "../src/import/handles.ts"; import { callLogId, contactHandleId, derivedId, dmThreadId, groupThreadId, mediaFaceId, mediaSightingId, msgContactId, photoRecordId, reactionRowId, videoRecordId } from "../src/import/ids.ts"; // ── chat_ids_tests.rs (the reaction id, through the rules module) ─────────── describe("reactionRowId", () => { const MESSAGE = "msg:one"; const PHONES = ["910123462100", "910123462101", "12345678"]; const HANDLES = ["alice-t", "bright-oak", "quiet-fern", "a1"]; it("mints the frozen phone form unchanged", () => { expect(reactionRowId(MESSAGE, "910123462100")).toBe("react:msg:one:910123462100"); }); it("still accepts the owner's own reaction", () => { expect(reactionRowId(MESSAGE, "self")).toBe("react:msg:one:self"); }); it("mints the same shape for a handle", () => { expect(reactionRowId(MESSAGE, "bright-oak")).toBe("react:msg:one:bright-oak"); }); it("gives every actor its own id", () => { const minted = ["self", ...PHONES, ...HANDLES].map((actor) => reactionRowId(MESSAGE, actor)); expect(minted.every((id) => id !== null)).toBe(true); expect(new Set(minted).size).toBe(minted.length); }); it("refuses an actor that is neither a phone, a handle nor self", () => { for (const bad of ["+910123462100", "alice:t", "alice|t", "Alice", "-alice", "", "SELF"]) expect(reactionRowId(MESSAGE, bad), bad).toBeNull(); }); it("still needs a message", () => { expect(reactionRowId("", "alice-t")).toBeNull(); }); it("node vectors", () => { expect(reactionRowId("01J8ZQ4T7M9ABCDEF", "919000000001")).toBe("react:01J8ZQ4T7M9ABCDEF:919000000001"); expect(reactionRowId("msg:a:b", "quiet-fern")).toBe("react:msg:a:b:quiet-fern"); }); }); // ── group_tests.rs (slug and key) ──────────────────────────────────────────── describe("slugifyTitle / groupExternalKey", () => { it("slugs like the spec freeze", () => { expect(slugifyTitle("AQ <> TinFactory")).toBe("aq-tinfactory"); expect(groupExternalKey("Fixture Crew")).toBe("wa:group:fixture-crew"); expect(slugifyTitle(" ")).toBe("untitled"); }); it("every slug matches the vector table", () => { expect(vectors.slug.length).toBeGreaterThanOrEqual(13); for (const c of vectors.slug) { expect(slugifyTitle(c.title), c.title).toBe(c.slug); expect(groupExternalKey(c.title), c.title).toBe(c.key); } }); it("does not normalise, and that is deliberate", () => { expect(slugifyTitle("e\u{0301}cole")).toBe("e-cole"); expect(slugifyTitle("你好世界")).toBe("untitled"); expect(slugifyTitle("\u{1f44d} Crew \u{1f44d}")).toBe("crew"); expect(slugifyTitle("a".repeat(60))).toBe("a".repeat(48)); }); }); // ── derived_id_tests.rs (the ids ids.ts exports) ───────────────────────────── const isUuidShaped = (id: string): boolean => /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(id); describe("derived ids", () => { it("derivedId matches the vectors", () => { const cases: [string, string[], string][] = [ ["t", [""], "426c3d77-b13e-3495-a692-e288b674a24b"], ["t", ["a"], "13b44368-c3f2-949c-a93e-bdbd012303c4"], ["t", ["abc"], "097a0a7e-bc3f-e3ea-c503-fb7a6818aa14"], ["t", ["x".repeat(55)], "8dfcf1a9-22a2-9d58-558a-ac095e2baa7a"], ["t", ["x".repeat(56)], "51d6c711-0670-f245-3612-7339322c3276"], ["t", ["x".repeat(63)], "2f94af5b-5cf4-a278-3452-b7916ba84c95"], ["t", ["x".repeat(64)], "44289dab-9ec1-46f3-2004-e47c22297610"], ["t", ["x".repeat(65)], "67b85702-20db-d69a-6bb9-206b0fce8bc0"], ["t", ["x".repeat(200)], "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 (const [table, parts, expected] of cases) expect(derivedId(table, parts), `${table} ${parts.join("|").slice(0, 20)}`).toBe(expected); }); it("photo and video record ids", () => { expect(photoRecordId("abc123")).toBe("photo:abc123"); expect(photoRecordId("deadbeef")).toBe("photo:deadbeef"); expect(photoRecordId("")).toBe("photo:"); expect(videoRecordId("abc123")).toBe("video:abc123"); expect(videoRecordId("deadbeef")).toBe("video:deadbeef"); expect(videoRecordId("")).toBe("video:"); expect(photoRecordId("abc")).not.toBe(videoRecordId("abc")); }); it("contactHandleId matches the vectors", () => { expect(contactHandleId("ws-1", "phone", "919999900000")).toBe("7823dc7c-736e-7a92-6cf3-5832189de9bb"); expect(contactHandleId("ws-1", "email", "a@x.com")).toBe("0565b149-dab8-da46-0f0a-b7559bd576a1"); expect(contactHandleId("ws-2", "phone", "919999900000")).toBe("7aa63a0c-a941-7034-26e9-dfcec596303c"); }); it("callLogId matches the vectors", () => { expect(callLogId("ws-1", "ios", "Z-1")).toBe("439c61e4-0e63-55e8-09bb-cc6df25b60da"); expect(callLogId("ws-1", "ios", "Z-2")).toBe("725beff3-3c22-f2e3-a945-d89e03290508"); expect(callLogId("ws-1", "mac", "Z-1")).toBe("fb0ab75c-911a-67ca-8f81-e9851f19f984"); }); it("mediaSightingId matches the vectors, and no source id is the empty string", () => { expect(mediaSightingId("photo", "photo:abc", "share", null)).toBe("0ce65914-e797-5c0b-f979-395b5af20732"); expect(mediaSightingId("photo", "photo:abc", "share", "")).toBe("0ce65914-e797-5c0b-f979-395b5af20732"); expect(mediaSightingId("video", "video:abc", "photos", "asset-1")).toBe("f6b2c52f-9f74-2bd9-8e23-52a22c350e63"); expect(mediaSightingId("photo", "photo:abc", "photos", "asset-1")).toBe("01498773-468c-f700-b661-2a96d59cb37b"); }); it("mediaFaceId matches the vectors", () => { expect(mediaFaceId("photo", "photo:abc", "photos", "face-1")).toBe("99c1ec2f-36e9-8b84-a0ec-44d2c08cdd42"); expect(mediaFaceId("photo", "photo:abc", "photos", "face-2")).toBe("84ceb74a-d2f4-ae00-768b-05c159c65004"); expect(mediaFaceId("video", "video:abc", "photos", "face-1")).toBe("821e14ba-9b3f-36fa-7e8b-40c5a196edb0"); }); it("is uuid-shaped, separates the table, and cannot be confused by an ambiguous join", () => { expect(isUuidShaped(derivedId("tbl", ["a"]))).toBe(true); expect(derivedId("one", ["k"])).not.toBe(derivedId("two", ["k"])); expect(derivedId("t", ["ab", "c"])).not.toBe(derivedId("t", ["a", "bc"])); }); const WS = "11111111-1111-4111-8111-111111111111"; it("a contact id is the workspace and the digits", () => { const id = msgContactId(WS, "919812345678"); expect(id).toBe(msgContactId(WS, "919812345678")); expect(isUuidShaped(id)).toBe(true); expect(id).not.toBe(msgContactId(WS, "+919812345678")); }); it("a dm thread and a group thread never share an id", () => { expect(dmThreadId(WS, "key")).not.toBe(groupThreadId(WS, "key")); expect(dmThreadId(WS, "contact-1")).toBe(dmThreadId(WS, "contact-1")); expect(groupThreadId(WS, "whatsapp:9199@g.us")).toBe(groupThreadId(WS, "whatsapp:9199@g.us")); expect(isUuidShaped(dmThreadId(WS, "contact-1"))).toBe(true); expect(isUuidShaped(groupThreadId(WS, "whatsapp:9199@g.us"))).toBe(true); expect(dmThreadId(WS, "contact-1")).not.toBe(dmThreadId(WS, "contact-2")); }); }); // ── fingerprint_tests.rs (the message part) ────────────────────────────────── describe("content fingerprint", () => { it("the txt2 algorithm is frozen", () => { expect(contentFingerprint("2025-07-26T04:42:45.000Z", "Hey, are we still on for tomorrow?", 0)).toBe("txt2:a8b76dfd3b44f33020ae85c5862bd139"); }); it("message fingerprints match the vectors", () => { expect(contentFingerprint("2025-07-26T04:42:45.000Z", "Hey, are we still on for tomorrow?", 0)).toBe("txt2:a8b76dfd3b44f33020ae85c5862bd139"); expect(contentFingerprint("2025-07-26T04:42:45.000Z", "ok", 0)).toBe("txt2:f178b349b21ccd96b2d8ca1953eb619a"); expect(contentFingerprint("2025-07-26T04:42:12.000Z", " ok ", 0)).toBe("txt2:f178b349b21ccd96b2d8ca1953eb619a"); expect(contentFingerprint("2025-07-26T04:42:45.000Z", "ok", 1)).toBe("txt2:715b108d5c155eef93e897822bb49e65"); expect(contentFingerprint("2026-01-01T00:00:00.000Z", "", 0)).toBe("txt2:76fdf9f98e7d3ca9bf6bf9ece847e9c4"); expect(contentFingerprint("2026-01-01T00:00:00.000Z", 'quote " and \\ backslash', 0)).toBe("txt2:6450437c870227e46750d4d6821a75dd"); expect(contentFingerprint("2026-01-01T00:00:00.000Z", "emoji \u{1f9f5} tail", 0)).toBe("txt2:340b88f0ac247383335a3826a30839a9"); expect(contentFingerprint("2026-01-01T00:00:00.000Z", "\u{65e5}\u{672c}\u{8a9e}\u{306e}\u{30c6}\u{30ad}\u{30b9}\u{30c8}", 3)).toBe("txt2:4e1d09dc7f88fe051f61d8a9c6f42f55"); }); it("minuteBucket matches the vectors", () => { for (const [iso, minute] of [ ["2025-07-26T04:42:45.000Z", "2025-07-26T04:42"], ["2025-07-26T04:42:12.000Z", "2025-07-26T04:42"], ["2025-07-26T04:42:45.123+05:30", "2025-07-25T23:12"], ["2026-08-29T10:15:30.000Z", "2026-08-29T10:15"], ["2026-08-29T10:15:59.000Z", "2026-08-29T10:15"], ["2026-08-29T10:16:00.000Z", "2026-08-29T10:16"], ["1970-01-01T00:00:00.000Z", "1970-01-01T00:00"], ["2026-02-29T12:00:00.000Z", "2026-03-01T12:00"], ["2024-02-29T12:00:00.000Z", "2024-02-29T12:00"], ["2026-12-31T23:59:59.999Z", "2026-12-31T23:59"], ["2026-01-01T00:00:00.000-05:00", "2026-01-01T05:00"], ]) { expect(minuteBucket(iso!), iso).toBe(minute); } // The Rust refuses an unparseable instant; this answers null. expect(minuteBucket("nope")).toBeNull(); expect(contentFingerprint("nope", "ok", 0)).toBeNull(); }); it("normalizeBodyForId matches the vectors", () => { expect(normalizeBodyForId("Hello\n world ")).toBe("Hello world"); expect(normalizeBodyForId(" ok ")).toBe("ok"); expect(normalizeBodyForId("ok")).toBe("ok"); expect(normalizeBodyForId("a\tb\r\nc")).toBe("a b c"); expect(normalizeBodyForId("\u{a0}nbsp\u{a0}here\u{a0}")).toBe("nbsp here"); expect(normalizeBodyForId("\u{feff}bom\u{feff}")).toBe("bom"); expect(normalizeBodyForId("emoji \u{1f9f5} and \u{65e5}\u{672c}\u{8a9e}")).toBe("emoji \u{1f9f5} and \u{65e5}\u{672c}\u{8a9e}"); expect(normalizeBodyForId(" ")).toBe(""); expect(normalizeBodyForId("")).toBe(""); expect(normalizeBodyForId("one\u{2028}two\u{2029}three")).toBe("one two three"); expect(normalizeBodyForId("trail\u{3000}")).toBe("trail"); }); it("a message fingerprint carries no identity", () => { const a = contentFingerprint("2025-07-26T04:42:45.000Z", "ok", 0)!; const b = contentFingerprint("2025-07-26T04:42:12.000Z", " ok ", 0); expect(a).toBe(b); expect(a.startsWith(CONTENT_EXTERNAL_ID_PREFIX)).toBe(true); expect(a.length).toBe(CONTENT_EXTERNAL_ID_PREFIX.length + 32); }); }); // ── rules/people/handles_tests.rs (social handles) ─────────────────────────── describe("social handles", () => { it("a social handle is one handle however it was pasted", () => { for (const raw of ["@Zara.Quill", "zara.quill", "https://www.instagram.com/zara.quill/", "instagram.com/Zara.Quill?igsh=abc", " @zara.quill "]) expect(socialHandle("instagram", raw), raw).toBe("zara.quill"); expect(socialHandle("linkedin", "https://www.linkedin.com/in/zara-quill-42/?trk=x")).toBe("zara-quill-42"); expect(socialHandle("facebook", "https://facebook.com/profile.php?id=100012345")).toBe("100012345"); expect(socialHandle("facebook", "fb.com/Zara.Quill")).toBe("zara.quill"); for (const kind of ["instagram", "facebook", "linkedin"] as HandleKind[]) { for (const raw of ["@Zara.Quill", "zara.quill", "Zara-Quill"]) { const once = socialHandle(kind, raw); if (once !== null) expect(socialHandle(kind, once), `${kind} ${raw}`).toBe(once); } } expect(normalizeHandle("instagram", "@Zara.Quill")).toEqual({ kind: "instagram", value: "zara.quill" }); }); it("a social handle that is not one is refused", () => { for (const raw of ["", " ", "@", "zara quill", "zara!quill", "https://instagram.com/", "a".repeat(31)]) expect(socialHandle("instagram", raw), JSON.stringify(raw)).toBeNull(); }); // Rust's `trim` and `is_alphanumeric`, not ECMAScript's. it("trims and reads letters as the Rust does", () => { expect(socialHandle("instagram", "\u{85}zara.quill")).toBe("zara.quill"); expect(socialHandle("instagram", "\u{feff}zara.quill")).toBeNull(); expect(socialHandle("linkedin", "राम")).toBe("राम"); }); });