33 BadRequestException ,
44 ConflictException ,
55 ForbiddenException ,
6+ GoneException ,
67 NotFoundException ,
78} from '@nestjs/common' ;
89import { ConfigService } from '@nestjs/config' ;
@@ -14,6 +15,7 @@ import { CallService } from './services/call.service';
1415import { CallUseCase } from './call.usecase' ;
1516import { mockRoomData , mockUserPayload } from './fixtures' ;
1617import { v4 } from 'uuid' ;
18+ import { Time } from '../../common/time' ;
1719
1820const autoGeneratedUUID = 'generated-uuid' ;
1921
@@ -124,6 +126,10 @@ describe('CallUseCase', () => {
124126 anonymous : true ,
125127 } ) ;
126128
129+ afterEach ( ( ) => {
130+ jest . useRealTimers ( ) ;
131+ } ) ;
132+
127133 it ( 'When joining call with registered user data, then it should join successfully with current user data' , async ( ) => {
128134 const userData = {
129135 userId,
@@ -280,6 +286,62 @@ describe('CallUseCase', () => {
280286 expect ( roomService . getRoomByRoomId ) . toHaveBeenCalledWith ( roomId ) ;
281287 } ) ;
282288
289+ it ( 'When room is expired, then it should remove the room and throw' , async ( ) => {
290+ const userData = { userId } ;
291+ const pastDate = Time . now ( '2024-01-01' ) ;
292+ const currentDate = Time . now ( '2024-01-02' ) ;
293+ const expiredRoomMock = {
294+ ...roomMock ,
295+ removeAt : pastDate ,
296+ } ;
297+ jest . useFakeTimers ( ) ;
298+ jest . setSystemTime ( currentDate ) ;
299+
300+ roomService . getRoomByRoomId . mockResolvedValueOnce ( expiredRoomMock ) ;
301+ roomService . removeRoom . mockResolvedValueOnce ( undefined ) ;
302+
303+ await expect ( callUseCase . joinCall ( roomId , userData ) ) . rejects . toThrow (
304+ GoneException ,
305+ ) ;
306+ expect ( roomService . getRoomByRoomId ) . toHaveBeenCalledWith ( roomId ) ;
307+ expect ( roomService . removeRoom ) . toHaveBeenCalledWith ( expiredRoomMock . id ) ;
308+ } ) ;
309+
310+ it ( 'When room has removeAt in the future, then it should allow joining' , async ( ) => {
311+ const userData = {
312+ userId,
313+ name : userName ,
314+ lastName : userLastName ,
315+ } ;
316+ const futureDate = new Date ( '2024-01-03' ) ;
317+ const currentDate = new Date ( '2024-01-02' ) ;
318+ const nonExpiredRoomMock = {
319+ ...roomMock ,
320+ removeAt : futureDate ,
321+ } ;
322+ jest . useFakeTimers ( ) ;
323+ jest . setSystemTime ( currentDate ) ;
324+ roomService . getRoomByRoomId . mockResolvedValueOnce ( nonExpiredRoomMock ) ;
325+ roomService . getUserInRoom . mockResolvedValueOnce ( null ) ;
326+ roomService . countUsersInRoom . mockResolvedValueOnce ( 0 ) ;
327+ roomService . handleUserJoined . mockResolvedValueOnce ( {
328+ roomUser : roomUserMock ,
329+ oldParticipantId : undefined ,
330+ } ) ;
331+ callService . generateJitsiJWT . mockReturnValueOnce ( 'test-jwt-token' ) ;
332+
333+ const result = await callUseCase . joinCall ( roomId , userData ) ;
334+
335+ expect ( roomService . getRoomByRoomId ) . toHaveBeenCalledWith ( roomId ) ;
336+ expect ( roomService . removeRoom ) . not . toHaveBeenCalled ( ) ;
337+ expect ( result ) . toEqual ( {
338+ token : 'test-jwt-token' ,
339+ room : roomId ,
340+ userId,
341+ appId : 'jitsi-app-id' ,
342+ } ) ;
343+ } ) ;
344+
283345 it ( 'When non-owner tries to join closed room, then it should throw' , async ( ) => {
284346 const userData = { userId } ;
285347 const closedRoomMock = {
0 commit comments