@@ -14,13 +14,21 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17+ import { M_POLL_START } from "matrix-events-sdk" ;
18+
1719import { EventTimelineSet } from "../../src/models/event-timeline-set" ;
1820import { MatrixEvent , MatrixEventEvent } from "../../src/models/event" ;
1921import { Room } from "../../src/models/room" ;
20- import { Relations } from "../../src/models/relations" ;
22+ import { Relations , RelationsEvent } from "../../src/models/relations" ;
2123import { TestClient } from "../TestClient" ;
24+ import { RelationType } from "../../src" ;
25+ import { logger } from "../../src/logger" ;
2226
2327describe ( "Relations" , function ( ) {
28+ afterEach ( ( ) => {
29+ jest . spyOn ( logger , "error" ) . mockRestore ( ) ;
30+ } ) ;
31+
2432 it ( "should deduplicate annotations" , function ( ) {
2533 const room = new Room ( "room123" , null ! , null ! ) ;
2634 const relations = new Relations ( "m.annotation" , "m.reaction" , room ) ;
@@ -75,6 +83,92 @@ describe("Relations", function () {
7583 }
7684 } ) ;
7785
86+ describe ( "addEvent()" , ( ) => {
87+ const relationType = RelationType . Reference ;
88+ const eventType = M_POLL_START . stable ! ;
89+ const altEventTypes = [ M_POLL_START . unstable ! ] ;
90+ const room = new Room ( "room123" , null ! , null ! ) ;
91+
92+ it ( "should not add events without a relation" , async ( ) => {
93+ // dont pollute console
94+ const logSpy = jest . spyOn ( logger , "error" ) . mockImplementation ( ( ) => { } ) ;
95+ const relations = new Relations ( relationType , eventType , room ) ;
96+ const emitSpy = jest . spyOn ( relations , "emit" ) ;
97+ const event = new MatrixEvent ( { type : eventType } ) ;
98+
99+ await relations . addEvent ( event ) ;
100+ expect ( logSpy ) . toHaveBeenCalledWith ( "Event must have relation info" ) ;
101+ // event not added
102+ expect ( relations . getRelations ( ) . length ) . toBe ( 0 ) ;
103+ expect ( emitSpy ) . not . toHaveBeenCalled ( ) ;
104+ } ) ;
105+
106+ it ( "should not add events of incorrect event type" , async ( ) => {
107+ // dont pollute console
108+ const logSpy = jest . spyOn ( logger , "error" ) . mockImplementation ( ( ) => { } ) ;
109+ const relations = new Relations ( relationType , eventType , room ) ;
110+ const emitSpy = jest . spyOn ( relations , "emit" ) ;
111+ const event = new MatrixEvent ( {
112+ type : "different-event-type" ,
113+ content : {
114+ "m.relates_to" : {
115+ event_id : "$2s4yYpEkVQrPglSCSqB_m6E8vDhWsg0yFNyOJdVIb_o" ,
116+ rel_type : relationType ,
117+ } ,
118+ } ,
119+ } ) ;
120+
121+ await relations . addEvent ( event ) ;
122+
123+ expect ( logSpy ) . toHaveBeenCalledWith ( `Event relation info doesn't match this container` ) ;
124+ // event not added
125+ expect ( relations . getRelations ( ) . length ) . toBe ( 0 ) ;
126+ expect ( emitSpy ) . not . toHaveBeenCalled ( ) ;
127+ } ) ;
128+
129+ it ( "adds events that match alt event types" , async ( ) => {
130+ const relations = new Relations ( relationType , eventType , room , altEventTypes ) ;
131+ const emitSpy = jest . spyOn ( relations , "emit" ) ;
132+ const event = new MatrixEvent ( {
133+ type : M_POLL_START . unstable ! ,
134+ content : {
135+ "m.relates_to" : {
136+ event_id : "$2s4yYpEkVQrPglSCSqB_m6E8vDhWsg0yFNyOJdVIb_o" ,
137+ rel_type : relationType ,
138+ } ,
139+ } ,
140+ } ) ;
141+
142+ await relations . addEvent ( event ) ;
143+
144+ // event added
145+ expect ( relations . getRelations ( ) ) . toEqual ( [ event ] ) ;
146+ expect ( emitSpy ) . toHaveBeenCalledWith ( RelationsEvent . Add , event ) ;
147+ } ) ;
148+
149+ it ( "should not add events of incorrect relation type" , async ( ) => {
150+ const logSpy = jest . spyOn ( logger , "error" ) . mockImplementation ( ( ) => { } ) ;
151+ const relations = new Relations ( relationType , eventType , room ) ;
152+ const event = new MatrixEvent ( {
153+ type : eventType ,
154+ content : {
155+ "m.relates_to" : {
156+ event_id : "$2s4yYpEkVQrPglSCSqB_m6E8vDhWsg0yFNyOJdVIb_o" ,
157+ rel_type : "m.annotation" ,
158+ } ,
159+ } ,
160+ } ) ;
161+
162+ await relations . addEvent ( event ) ;
163+ const emitSpy = jest . spyOn ( relations , "emit" ) ;
164+
165+ expect ( logSpy ) . toHaveBeenCalledWith ( `Event relation info doesn't match this container` ) ;
166+ // event not added
167+ expect ( relations . getRelations ( ) . length ) . toBe ( 0 ) ;
168+ expect ( emitSpy ) . not . toHaveBeenCalled ( ) ;
169+ } ) ;
170+ } ) ;
171+
78172 it ( "should emit created regardless of ordering" , async function ( ) {
79173 const targetEvent = new MatrixEvent ( {
80174 sender : "@bob:example.com" ,
0 commit comments