@@ -2205,7 +2205,7 @@ describe("MatrixClient", function () {
2205
2205
"creator" : "@daryl:alexandria.example.com" ,
2206
2206
"m.federate" : true ,
2207
2207
"predecessor" : {
2208
- event_id : "spec_is_not_clear_what_id_this_is " ,
2208
+ event_id : "id_of_last_event " ,
2209
2209
room_id : predecessorRoomId ,
2210
2210
} ,
2211
2211
"room_version" : "9" ,
@@ -2278,34 +2278,34 @@ describe("MatrixClient", function () {
2278
2278
} ) ;
2279
2279
2280
2280
describe ( "getRoomUpgradeHistory" , ( ) => {
2281
- function createRoomHistory ( ) : [ Room , Room , Room , Room ] {
2281
+ /**
2282
+ * Create a chain of room history with create events and tombstones.
2283
+ *
2284
+ * @param creates include create events (default=true)
2285
+ * @param tombstones include tomstone events (default=true)
2286
+ * @returns 4 rooms chained together with tombstones and create
2287
+ * events, in order from oldest to latest.
2288
+ */
2289
+ function createRoomHistory ( creates = true , tombstones = true ) : [ Room , Room , Room , Room ] {
2282
2290
const room1 = new Room ( "room1" , client , "@carol:alexandria.example.com" ) ;
2283
2291
const room2 = new Room ( "room2" , client , "@daryl:alexandria.example.com" ) ;
2284
2292
const room3 = new Room ( "room3" , client , "@rick:helicopter.example.com" ) ;
2285
2293
const room4 = new Room ( "room4" , client , "@michonne:hawthorne.example.com" ) ;
2286
2294
2287
- room1 . addLiveEvents ( [ tombstoneEvent ( room2 . roomId , room1 . roomId ) ] , { } ) ;
2288
- room2 . addLiveEvents ( [ roomCreateEvent ( room2 . roomId , room1 . roomId ) ] ) ;
2289
-
2290
- room2 . addLiveEvents ( [ tombstoneEvent ( room3 . roomId , room2 . roomId ) ] , { } ) ;
2291
- room3 . addLiveEvents ( [ roomCreateEvent ( room3 . roomId , room2 . roomId ) ] ) ;
2295
+ if ( creates ) {
2296
+ room2 . addLiveEvents ( [ roomCreateEvent ( room2 . roomId , room1 . roomId ) ] ) ;
2297
+ room3 . addLiveEvents ( [ roomCreateEvent ( room3 . roomId , room2 . roomId ) ] ) ;
2298
+ room4 . addLiveEvents ( [ roomCreateEvent ( room4 . roomId , room3 . roomId ) ] ) ;
2299
+ }
2292
2300
2293
- room3 . addLiveEvents ( [ tombstoneEvent ( room4 . roomId , room3 . roomId ) ] , { } ) ;
2294
- room4 . addLiveEvents ( [ roomCreateEvent ( room4 . roomId , room3 . roomId ) ] ) ;
2301
+ if ( tombstones ) {
2302
+ room1 . addLiveEvents ( [ tombstoneEvent ( room2 . roomId , room1 . roomId ) ] , { } ) ;
2303
+ room2 . addLiveEvents ( [ tombstoneEvent ( room3 . roomId , room2 . roomId ) ] , { } ) ;
2304
+ room3 . addLiveEvents ( [ tombstoneEvent ( room4 . roomId , room3 . roomId ) ] , { } ) ;
2305
+ }
2295
2306
2296
2307
mocked ( store . getRoom ) . mockImplementation ( ( roomId : string ) => {
2297
- switch ( roomId ) {
2298
- case "room1" :
2299
- return room1 ;
2300
- case "room2" :
2301
- return room2 ;
2302
- case "room3" :
2303
- return room3 ;
2304
- case "room4" :
2305
- return room4 ;
2306
- default :
2307
- return null ;
2308
- }
2308
+ return { room1, room2, room3, room4 } [ roomId ] || null ;
2309
2309
} ) ;
2310
2310
2311
2311
return [ room1 , room2 , room3 , room4 ] ;
@@ -2334,6 +2334,49 @@ describe("MatrixClient", function () {
2334
2334
] ) ;
2335
2335
} ) ;
2336
2336
2337
+ it ( "Returns the predecessors of this room (with verify links)" , ( ) => {
2338
+ const [ room1 , room2 , room3 , room4 ] = createRoomHistory ( ) ;
2339
+ const verifyLinks = true ;
2340
+ const history = client . getRoomUpgradeHistory ( room4 . roomId , verifyLinks ) ;
2341
+ expect ( history . map ( ( room ) => room . roomId ) ) . toEqual ( [
2342
+ room1 . roomId ,
2343
+ room2 . roomId ,
2344
+ room3 . roomId ,
2345
+ room4 . roomId ,
2346
+ ] ) ;
2347
+ } ) ;
2348
+
2349
+ it ( "With verify links, rejects predecessors that don't point forwards" , ( ) => {
2350
+ // Given successors point back with create events, but
2351
+ // predecessors do not point forwards with tombstones
2352
+ const [ , , , room4 ] = createRoomHistory ( true , false ) ;
2353
+
2354
+ // When I ask for history with verifyLinks on
2355
+ const verifyLinks = true ;
2356
+ const history = client . getRoomUpgradeHistory ( room4 . roomId , verifyLinks ) ;
2357
+
2358
+ // Then the predecessors are not included in the history
2359
+ expect ( history . map ( ( room ) => room . roomId ) ) . toEqual ( [ room4 . roomId ] ) ;
2360
+ } ) ;
2361
+
2362
+ it ( "Without verify links, includes predecessors that don't point forwards" , ( ) => {
2363
+ // Given successors point back with create events, but
2364
+ // predecessors do not point forwards with tombstones
2365
+ const [ room1 , room2 , room3 , room4 ] = createRoomHistory ( true , false ) ;
2366
+
2367
+ // When I ask for history with verifyLinks off
2368
+ const verifyLinks = false ;
2369
+ const history = client . getRoomUpgradeHistory ( room4 . roomId , verifyLinks ) ;
2370
+
2371
+ // Then the predecessors are included in the history
2372
+ expect ( history . map ( ( room ) => room . roomId ) ) . toEqual ( [
2373
+ room1 . roomId ,
2374
+ room2 . roomId ,
2375
+ room3 . roomId ,
2376
+ room4 . roomId ,
2377
+ ] ) ;
2378
+ } ) ;
2379
+
2337
2380
it ( "Returns the subsequent rooms" , ( ) => {
2338
2381
const [ room1 , room2 , room3 , room4 ] = createRoomHistory ( ) ;
2339
2382
const history = client . getRoomUpgradeHistory ( room1 . roomId ) ;
@@ -2345,6 +2388,49 @@ describe("MatrixClient", function () {
2345
2388
] ) ;
2346
2389
} ) ;
2347
2390
2391
+ it ( "Returns the subsequent rooms (with verify links)" , ( ) => {
2392
+ const [ room1 , room2 , room3 , room4 ] = createRoomHistory ( ) ;
2393
+ const verifyLinks = true ;
2394
+ const history = client . getRoomUpgradeHistory ( room1 . roomId , verifyLinks ) ;
2395
+ expect ( history . map ( ( room ) => room . roomId ) ) . toEqual ( [
2396
+ room1 . roomId ,
2397
+ room2 . roomId ,
2398
+ room3 . roomId ,
2399
+ room4 . roomId ,
2400
+ ] ) ;
2401
+ } ) ;
2402
+
2403
+ it ( "With verify links, rejects successors that don't point backwards" , ( ) => {
2404
+ // Given predecessors point forwards with tombstones, but
2405
+ // successors do not point back with create events.
2406
+ const [ room1 , , , ] = createRoomHistory ( false , true ) ;
2407
+
2408
+ // When I ask for history with verifyLinks on
2409
+ const verifyLinks = true ;
2410
+ const history = client . getRoomUpgradeHistory ( room1 . roomId , verifyLinks ) ;
2411
+
2412
+ // Then the successors are not included in the history
2413
+ expect ( history . map ( ( room ) => room . roomId ) ) . toEqual ( [ room1 . roomId ] ) ;
2414
+ } ) ;
2415
+
2416
+ it ( "Without verify links, includes predecessors that don't point forwards" , ( ) => {
2417
+ // Given predecessors point forwards with tombstones, but
2418
+ // successors do not point back with create events.
2419
+ const [ room1 , room2 , room3 , room4 ] = createRoomHistory ( false , true ) ;
2420
+
2421
+ // When I ask for history with verifyLinks off
2422
+ const verifyLinks = false ;
2423
+ const history = client . getRoomUpgradeHistory ( room1 . roomId , verifyLinks ) ;
2424
+
2425
+ // Then the successors are included in the history
2426
+ expect ( history . map ( ( room ) => room . roomId ) ) . toEqual ( [
2427
+ room1 . roomId ,
2428
+ room2 . roomId ,
2429
+ room3 . roomId ,
2430
+ room4 . roomId ,
2431
+ ] ) ;
2432
+ } ) ;
2433
+
2348
2434
it ( "Returns the predecessors and subsequent rooms" , ( ) => {
2349
2435
const [ room1 , room2 , room3 , room4 ] = createRoomHistory ( ) ;
2350
2436
const history = client . getRoomUpgradeHistory ( room3 . roomId ) ;
@@ -2355,6 +2441,18 @@ describe("MatrixClient", function () {
2355
2441
room4 . roomId ,
2356
2442
] ) ;
2357
2443
} ) ;
2444
+
2445
+ it ( "Returns the predecessors and subsequent rooms (with verify links)" , ( ) => {
2446
+ const [ room1 , room2 , room3 , room4 ] = createRoomHistory ( ) ;
2447
+ const verifyLinks = true ;
2448
+ const history = client . getRoomUpgradeHistory ( room3 . roomId , verifyLinks ) ;
2449
+ expect ( history . map ( ( room ) => room . roomId ) ) . toEqual ( [
2450
+ room1 . roomId ,
2451
+ room2 . roomId ,
2452
+ room3 . roomId ,
2453
+ room4 . roomId ,
2454
+ ] ) ;
2455
+ } ) ;
2358
2456
} ) ;
2359
2457
} ) ;
2360
2458
} ) ;
0 commit comments