22import { access , readFile } from "node:fs/promises" ;
33import { basename , dirname } from "node:path" ;
44import { afterEach , describe , expect , it , vi } from "vitest" ;
5+ import { normalizeDirectChatIdentifier } from "./chat-context.js" ;
56
67const createIMessageRpcClientMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
78const runIMessageCliJsonCommandMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
@@ -19,8 +20,7 @@ vi.mock("./remote-file.js", () => ({
1920 withIMessageRemoteFile : withIMessageRemoteFileMock ,
2021} ) ) ;
2122
22- const { imessageActionsRuntime, findChatGuidForTest, normalizeDirectChatIdentifierForTest } =
23- await import ( "./actions.runtime.js" ) ;
23+ const { imessageActionsRuntime } = await import ( "./actions.runtime.js" ) ;
2424
2525afterEach ( ( ) => {
2626 vi . restoreAllMocks ( ) ;
@@ -672,111 +672,121 @@ describe("findChatGuid cross-format identifier resolution", () => {
672672 } ,
673673 ] ;
674674
675- it ( "matches a synthesized iMessage;-;<phone> target against the chats.list <phone> identifier" , ( ) => {
676- const result = findChatGuidForTest ( chatsList , {
677- kind : "chat_identifier" ,
678- chatIdentifier : "iMessage;-;+12069106512" ,
679- } ) ;
680- expect ( result ) . toBe ( "any;-;+12069106512" ) ;
681- } ) ;
682-
683- it ( "matches a synthesized SMS;-;<phone> target the same way" , ( ) => {
684- const result = findChatGuidForTest ( chatsList , {
685- kind : "chat_identifier" ,
686- chatIdentifier : "SMS;-;+12069106512" ,
687- } ) ;
688- expect ( result ) . toBe ( "any;-;+12069106512" ) ;
689- } ) ;
690-
691- it ( "matches a bare <phone> identifier exactly" , ( ) => {
692- const result = findChatGuidForTest ( chatsList , {
693- kind : "chat_identifier" ,
694- chatIdentifier : "+12069106512" ,
695- } ) ;
696- expect ( result ) . toBe ( "any;-;+12069106512" ) ;
697- } ) ;
675+ type ChatGuidCase = {
676+ name : string ;
677+ cliPath : string ;
678+ chats : Array < Record < string , unknown > > ;
679+ target :
680+ | { kind : "chat_id" ; chatId : number }
681+ | { kind : "chat_identifier" ; chatIdentifier : string } ;
682+ expected : string | null ;
683+ } ;
698684
699- it ( "matches an any;-;<phone> guid form against the chats.list guid column" , ( ) => {
700- const result = findChatGuidForTest ( chatsList , {
701- kind : "chat_identifier" ,
702- chatIdentifier : "any;-;+12069106512" ,
703- } ) ;
704- expect ( result ) . toBe ( "any;-;+12069106512" ) ;
705- } ) ;
706-
707- it ( "matches a group chat by exact guid" , ( ) => {
708- const result = findChatGuidForTest ( chatsList , {
709- kind : "chat_identifier" ,
710- chatIdentifier : "iMessage;+;chat0000" ,
711- } ) ;
712- expect ( result ) . toBe ( "iMessage;+;chat0000" ) ;
713- } ) ;
714-
715- it ( "matches a group chat by chat_id" , ( ) => {
716- const result = findChatGuidForTest ( chatsList , { kind : "chat_id" , chatId : 7 } ) ;
717- expect ( result ) . toBe ( "iMessage;+;chat0000" ) ;
718- } ) ;
685+ it . each ( [
686+ {
687+ name : "matches a synthesized iMessage;-;<phone> target against the chats.list <phone> identifier" ,
688+ cliPath : "imsg-cross-format-imessage" ,
689+ chats : chatsList ,
690+ target : { kind : "chat_identifier" , chatIdentifier : "iMessage;-;+12069106512" } ,
691+ expected : "any;-;+12069106512" ,
692+ } ,
693+ {
694+ name : "matches a synthesized SMS;-;<phone> target the same way" ,
695+ cliPath : "imsg-cross-format-sms" ,
696+ chats : chatsList ,
697+ target : { kind : "chat_identifier" , chatIdentifier : "SMS;-;+12069106512" } ,
698+ expected : "any;-;+12069106512" ,
699+ } ,
700+ {
701+ name : "matches a bare <phone> identifier exactly" ,
702+ cliPath : "imsg-cross-format-bare" ,
703+ chats : chatsList ,
704+ target : { kind : "chat_identifier" , chatIdentifier : "+12069106512" } ,
705+ expected : "any;-;+12069106512" ,
706+ } ,
707+ {
708+ name : "matches an any;-;<phone> guid form against the chats.list guid column" ,
709+ cliPath : "imsg-cross-format-any" ,
710+ chats : chatsList ,
711+ target : { kind : "chat_identifier" , chatIdentifier : "any;-;+12069106512" } ,
712+ expected : "any;-;+12069106512" ,
713+ } ,
714+ {
715+ name : "matches a group chat by exact guid" ,
716+ cliPath : "imsg-cross-format-group-guid" ,
717+ chats : chatsList ,
718+ target : { kind : "chat_identifier" , chatIdentifier : "iMessage;+;chat0000" } ,
719+ expected : "iMessage;+;chat0000" ,
720+ } ,
721+ {
722+ name : "matches a group chat by chat_id" ,
723+ cliPath : "imsg-cross-format-chat-id" ,
724+ chats : chatsList ,
725+ target : { kind : "chat_id" , chatId : 7 } ,
726+ expected : "iMessage;+;chat0000" ,
727+ } ,
728+ {
729+ name : "does not coerce non-decimal chat ids from chats.list" ,
730+ cliPath : "imsg-cross-format-nondecimal-id" ,
731+ chats : [ { id : "0x7" , identifier : "wrong" , guid : "iMessage;+;wrong" } ] ,
732+ target : { kind : "chat_id" , chatId : 7 } ,
733+ expected : null ,
734+ } ,
735+ {
736+ name : "returns null for a phone number that does not exist in chats.list" ,
737+ cliPath : "imsg-cross-format-missing-phone" ,
738+ chats : chatsList ,
739+ target : { kind : "chat_identifier" , chatIdentifier : "iMessage;-;+19999999999" } ,
740+ expected : null ,
741+ } ,
742+ {
743+ name : "does not cross-match different phone numbers via the prefix-stripping path" ,
744+ cliPath : "imsg-cross-format-different-phone" ,
745+ chats : chatsList ,
746+ target : { kind : "chat_identifier" , chatIdentifier : "iMessage;-;+18001234567" } ,
747+ expected : null ,
748+ } ,
749+ {
750+ name : "does not match a DM target against a group's chat_identifier" ,
751+ cliPath : "imsg-cross-format-group-mismatch" ,
752+ chats : chatsList ,
753+ target : { kind : "chat_identifier" , chatIdentifier : "iMessage;+;chat-not-here" } ,
754+ expected : null ,
755+ } ,
756+ ] satisfies ChatGuidCase [ ] ) ( "$name" , async ( { cliPath, chats, target, expected } ) => {
757+ const client = mockRpcChatList ( chats ) ;
719758
720- it ( "does not coerce non-decimal chat ids from chats.list" , ( ) => {
721- const result = findChatGuidForTest (
722- [
723- {
724- id : "0x7" ,
725- identifier : "wrong" ,
726- guid : "iMessage;+;wrong" ,
727- } ,
728- ] ,
729- { kind : "chat_id" , chatId : 7 } ,
759+ await expect (
760+ imessageActionsRuntime . resolveChatGuidForTarget ( {
761+ target,
762+ options : { cliPath } ,
763+ conversationReadOrigin : "delegated" ,
764+ } ) ,
765+ ) . resolves . toBe ( expected ) ;
766+ expect ( client . request ) . toHaveBeenCalledWith (
767+ "chats.list" ,
768+ { limit : 1000 } ,
769+ { timeoutMs : undefined } ,
730770 ) ;
731- expect ( result ) . toBeNull ( ) ;
732- } ) ;
733-
734- it ( "returns null for a phone number that does not exist in chats.list" , ( ) => {
735- const result = findChatGuidForTest ( chatsList , {
736- kind : "chat_identifier" ,
737- chatIdentifier : "iMessage;-;+19999999999" ,
738- } ) ;
739- expect ( result ) . toBeNull ( ) ;
740- } ) ;
741-
742- it ( "does not cross-match different phone numbers via the prefix-stripping path" , ( ) => {
743- const result = findChatGuidForTest ( chatsList , {
744- kind : "chat_identifier" ,
745- chatIdentifier : "iMessage;-;+18001234567" ,
746- } ) ;
747- expect ( result ) . toBeNull ( ) ;
748- } ) ;
749-
750- it ( "does not match a DM target against a group's chat_identifier" , ( ) => {
751- const result = findChatGuidForTest ( chatsList , {
752- kind : "chat_identifier" ,
753- chatIdentifier : "iMessage;+;chat-not-here" ,
754- } ) ;
755- expect ( result ) . toBeNull ( ) ;
771+ expect ( client . stop ) . toHaveBeenCalledOnce ( ) ;
756772 } ) ;
757773} ) ;
758774
759775describe ( "normalizeDirectChatIdentifier" , ( ) => {
760- it ( "strips the iMessage;-; prefix" , ( ) => {
761- expect ( normalizeDirectChatIdentifierForTest ( "iMessage;-;+12069106512" ) ) . toBe ( "+12069106512" ) ;
762- } ) ;
763- it ( "strips the SMS;-; prefix" , ( ) => {
764- expect ( normalizeDirectChatIdentifierForTest ( "SMS;-;+12069106512" ) ) . toBe ( "+12069106512" ) ;
765- } ) ;
766- it ( "strips the any;-; prefix" , ( ) => {
767- expect ( normalizeDirectChatIdentifierForTest ( "any;-;+12069106512" ) ) . toBe ( "+12069106512" ) ;
768- } ) ;
769- it ( "matches case-insensitively" , ( ) => {
770- expect ( normalizeDirectChatIdentifierForTest ( "IMESSAGE;-;+12069106512" ) ) . toBe ( "+12069106512" ) ;
771- } ) ;
772- it ( "leaves group identifiers (iMessage;+;chat...) unchanged" , ( ) => {
773- expect ( normalizeDirectChatIdentifierForTest ( "iMessage;+;chat0000" ) ) . toBe ( "iMessage;+;chat0000" ) ;
774- expect ( normalizeDirectChatIdentifierForTest ( "iMessage;+;[email protected] " ) ) . toBe ( 776+ it . each ( [
777+ [ "strips the iMessage;-; prefix" , "iMessage;-;+12069106512" , "+12069106512" ] ,
778+ [ "strips the SMS;-; prefix" , "SMS;-;+12069106512" , "+12069106512" ] ,
779+ [ "strips the any;-; prefix" , "any;-;+12069106512" , "+12069106512" ] ,
780+ [ "matches case-insensitively" , "IMESSAGE;-;+12069106512" , "+12069106512" ] ,
781+ [ "leaves group identifiers unchanged" , "iMessage;+;chat0000" , "iMessage;+;chat0000" ] ,
782+ [
783+ "leaves group email identifiers unchanged" ,
775784776- ) ;
777- } ) ;
778- it ( "leaves bare values unchanged" , ( ) => {
779- expect ( normalizeDirectChatIdentifierForTest ( "+12069106512" ) ) . toBe ( "+12069106512" ) ;
780- expect ( normalizeDirectChatIdentifierForTest ( "[email protected] " ) ) . toBe ( "[email protected] " ) ; 785+ 786+ ] ,
787+ [ "leaves bare phone values unchanged" , "+12069106512" , "+12069106512" ] ,
788+ [ "leaves bare email values unchanged" , "[email protected] " , "[email protected] " ] , 789+ ] ) ( "%s" , ( _name , input , expected ) => {
790+ expect ( normalizeDirectChatIdentifier ( input ) ) . toBe ( expected ) ;
781791 } ) ;
782792} ) ;
0 commit comments