@@ -46,6 +46,129 @@ describe("qa-bus state", () => {
4646 expect ( snapshot . messages . map ( ( message ) => message . id ) ) . toEqual ( [ inbound . id , outbound . id ] ) ;
4747 } ) ;
4848
49+ it ( "records provider-native inbound IDs before indexing and publishing messages" , ( ) => {
50+ const state = createQaBusState ( ) ;
51+ const providerMessageId = "$provider-event:matrix.test" ;
52+
53+ const inbound = state . addInboundMessage (
54+ {
55+ conversation : { id : "qa-room" , kind : "group" } ,
56+ senderId : "alice" ,
57+ text : "provider-native identity" ,
58+ } ,
59+ providerMessageId ,
60+ ) ;
61+ const snapshot = state . getSnapshot ( ) ;
62+
63+ expect ( inbound . id ) . toBe ( providerMessageId ) ;
64+ expect ( snapshot . messages [ 0 ] ?. id ) . toBe ( providerMessageId ) ;
65+ expect ( snapshot . events [ 0 ] ) . toMatchObject ( {
66+ kind : "inbound-message" ,
67+ message : { id : providerMessageId } ,
68+ } ) ;
69+ expect ( state . poll ( ) . events [ 0 ] ) . toMatchObject ( {
70+ kind : "inbound-message" ,
71+ message : { id : providerMessageId } ,
72+ } ) ;
73+ expect ( state . readMessage ( { messageId : providerMessageId } ) ) . toMatchObject ( {
74+ id : providerMessageId ,
75+ text : "provider-native identity" ,
76+ } ) ;
77+ } ) ;
78+
79+ it ( "keeps identical provider-native message IDs isolated by account" , ( ) => {
80+ const state = createQaBusState ( ) ;
81+ const messageId = "42" ;
82+
83+ for ( const accountId of [ "account-a" , "account-b" ] ) {
84+ state . addInboundMessage (
85+ {
86+ accountId,
87+ conversation : { id : `${ accountId } -room` , kind : "group" } ,
88+ senderId : accountId ,
89+ text : `${ accountId } original` ,
90+ } ,
91+ messageId ,
92+ ) ;
93+ }
94+
95+ const snapshot = state . getSnapshot ( ) ;
96+ expect ( snapshot . messages . map ( ( message ) => [ message . accountId , message . id ] ) ) . toEqual ( [
97+ [ "account-a" , messageId ] ,
98+ [ "account-b" , messageId ] ,
99+ ] ) ;
100+ expect ( snapshot . events . map ( ( event ) => event . accountId ) ) . toEqual ( [ "account-a" , "account-b" ] ) ;
101+
102+ for ( const accountId of [ "account-a" , "account-b" ] ) {
103+ expect ( state . readMessage ( { accountId, messageId } ) ) . toMatchObject ( {
104+ accountId,
105+ id : messageId ,
106+ text : `${ accountId } original` ,
107+ } ) ;
108+ expect ( state . searchMessages ( { accountId } ) ) . toEqual ( [
109+ expect . objectContaining ( { accountId, id : messageId } ) ,
110+ ] ) ;
111+ expect ( state . poll ( { accountId } ) . events ) . toEqual ( [
112+ expect . objectContaining ( { accountId, message : expect . objectContaining ( { id : messageId } ) } ) ,
113+ ] ) ;
114+ }
115+
116+ state . editMessage ( { accountId : "account-a" , messageId, text : "account-a edited" } ) ;
117+ state . reactToMessage ( { accountId : "account-b" , messageId, emoji : "eyes" } ) ;
118+ expect ( state . readMessage ( { accountId : "account-a" , messageId } ) ) . toMatchObject ( {
119+ text : "account-a edited" ,
120+ reactions : [ ] ,
121+ } ) ;
122+ expect ( state . readMessage ( { accountId : "account-b" , messageId } ) ) . toMatchObject ( {
123+ text : "account-b original" ,
124+ reactions : [ expect . objectContaining ( { emoji : "eyes" } ) ] ,
125+ } ) ;
126+ } ) ;
127+
128+ it ( "rejects ambiguous provider-native IDs instead of selecting the wrong conversation" , ( ) => {
129+ const state = createQaBusState ( ) ;
130+ const accountId = "account-a" ;
131+ const messageId = "42" ;
132+
133+ for ( const conversationId of [ "first-room" , "second-room" ] ) {
134+ state . addInboundMessage (
135+ {
136+ accountId,
137+ conversation : { id : conversationId , kind : "group" } ,
138+ senderId : "alice" ,
139+ text : `${ conversationId } original` ,
140+ } ,
141+ messageId ,
142+ ) ;
143+ }
144+
145+ const originalSnapshot = state . getSnapshot ( ) ;
146+ expect ( originalSnapshot . messages . map ( ( message ) => message . conversation . id ) ) . toEqual ( [
147+ "first-room" ,
148+ "second-room" ,
149+ ] ) ;
150+ expect ( originalSnapshot . events . map ( ( event ) => event . accountId ) ) . toEqual ( [ accountId , accountId ] ) ;
151+ for ( const conversationId of [ "first-room" , "second-room" ] ) {
152+ expect ( state . searchMessages ( { accountId, conversationId } ) ) . toEqual ( [
153+ expect . objectContaining ( {
154+ id : messageId ,
155+ conversation : expect . objectContaining ( { id : conversationId } ) ,
156+ } ) ,
157+ ] ) ;
158+ }
159+
160+ const ambiguousMessage = "qa-bus message id is ambiguous for selected account: 42" ;
161+ expect ( ( ) => state . readMessage ( { accountId, messageId } ) ) . toThrow ( ambiguousMessage ) ;
162+ expect ( ( ) => state . editMessage ( { accountId, messageId, text : "wrong conversation" } ) ) . toThrow (
163+ ambiguousMessage ,
164+ ) ;
165+ expect ( ( ) => state . reactToMessage ( { accountId, messageId, emoji : "eyes" } ) ) . toThrow (
166+ ambiguousMessage ,
167+ ) ;
168+ expect ( ( ) => state . deleteMessage ( { accountId, messageId } ) ) . toThrow ( ambiguousMessage ) ;
169+ expect ( state . getSnapshot ( ) ) . toEqual ( originalSnapshot ) ;
170+ } ) ;
171+
49172 it ( "normalizes the dm ingress alias before channel routing" , ( ) => {
50173 const state = createQaBusState ( ) ;
51174
0 commit comments