@@ -19,7 +19,7 @@ import { Mocked } from "jest-mock";
1919import * as utils from "../test-utils/test-utils" ;
2020import { CRYPTO_ENABLED , IStoredClientOpts , MatrixClient } from "../../src/client" ;
2121import { MatrixEvent } from "../../src/models/event" ;
22- import { Filter , MemoryStore , Method , Room , SERVICE_TYPES } from "../../src/matrix" ;
22+ import { Filter , KnockRoomOpts , MemoryStore , Method , Room , SERVICE_TYPES } from "../../src/matrix" ;
2323import { TestClient } from "../TestClient" ;
2424import { THREAD_RELATION_TYPE } from "../../src/models/thread" ;
2525import { IFilterDefinition } from "../../src/filter" ;
@@ -205,6 +205,84 @@ describe("MatrixClient", function () {
205205 } ) ;
206206 } ) ;
207207
208+ describe ( "knockRoom" , function ( ) {
209+ const roomId = "!some-room-id:example.org" ;
210+ const reason = "some reason" ;
211+ const viaServers = "example.com" ;
212+
213+ type TestCase = [ string , KnockRoomOpts ] ;
214+ const testCases : TestCase [ ] = [
215+ [ "should knock a room" , { } ] ,
216+ [ "should knock a room for a reason" , { reason } ] ,
217+ [ "should knock a room via given servers" , { viaServers } ] ,
218+ [ "should knock a room for a reason via given servers" , { reason, viaServers } ] ,
219+ ] ;
220+
221+ it . each ( testCases ) ( "%s" , async ( _ , opts ) => {
222+ httpBackend
223+ . when ( "POST" , "/knock/" + encodeURIComponent ( roomId ) )
224+ . check ( ( request ) => {
225+ expect ( request . data ) . toEqual ( { reason : opts . reason } ) ;
226+ expect ( request . queryParams ) . toEqual ( { server_name : opts . viaServers } ) ;
227+ } )
228+ . respond ( 200 , { room_id : roomId } ) ;
229+
230+ const prom = client . knockRoom ( roomId , opts ) ;
231+ await httpBackend . flushAllExpected ( ) ;
232+ expect ( ( await prom ) . room_id ) . toBe ( roomId ) ;
233+ } ) ;
234+
235+ it ( "should no-op if you've already knocked a room" , function ( ) {
236+ const room = new Room ( roomId , client , userId ) ;
237+
238+ client . fetchRoomEvent = ( ) =>
239+ Promise . resolve ( {
240+ type : "test" ,
241+ content : { } ,
242+ } ) ;
243+
244+ room . addLiveEvents ( [
245+ utils . mkMembership ( {
246+ user : userId ,
247+ room : roomId ,
248+ mship : "knock" ,
249+ event : true ,
250+ } ) ,
251+ ] ) ;
252+
253+ httpBackend . verifyNoOutstandingRequests ( ) ;
254+ store . storeRoom ( room ) ;
255+ client . knockRoom ( roomId ) ;
256+ httpBackend . verifyNoOutstandingRequests ( ) ;
257+ } ) ;
258+
259+ describe ( "errors" , function ( ) {
260+ type TestCase = [ number , { errcode : string ; error ?: string } , string ] ;
261+ const testCases : TestCase [ ] = [
262+ [
263+ 403 ,
264+ { errcode : "M_FORBIDDEN" , error : "You don't have permission to knock" } ,
265+ "[M_FORBIDDEN: MatrixError: [403] You don't have permission to knock]" ,
266+ ] ,
267+ [
268+ 500 ,
269+ { errcode : "INTERNAL_SERVER_ERROR" } ,
270+ "[INTERNAL_SERVER_ERROR: MatrixError: [500] Unknown message]" ,
271+ ] ,
272+ ] ;
273+
274+ it . each ( testCases ) ( "should handle %s error" , async ( code , { errcode, error } , snapshot ) => {
275+ httpBackend . when ( "POST" , "/knock/" + encodeURIComponent ( roomId ) ) . respond ( code , { errcode, error } ) ;
276+
277+ const prom = client . knockRoom ( roomId ) ;
278+ await Promise . all ( [
279+ httpBackend . flushAllExpected ( ) ,
280+ expect ( prom ) . rejects . toMatchInlineSnapshot ( snapshot ) ,
281+ ] ) ;
282+ } ) ;
283+ } ) ;
284+ } ) ;
285+
208286 describe ( "getFilter" , function ( ) {
209287 const filterId = "f1lt3r1d" ;
210288
0 commit comments