Skip to content

Commit

Permalink
Merge pull request #12143 from daily-co/pre-1299
Browse files Browse the repository at this point in the history
  • Loading branch information
Regaddi authored Nov 13, 2024
2 parents a98b38f + fbf3f87 commit e4f2661
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 19 deletions.
31 changes: 13 additions & 18 deletions src/hooks/useRoomExp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,27 @@ export const useRoomExp = ({ onCountdown }: Props = {}) => {
const [ejectDate, setEjectDate] = useState<Date | null>(null);

useEffect(() => {
const expCandidates: number[] = [];

const ejectAfterElapsed =
room?.tokenConfig?.eject_after_elapsed ??
room?.config?.eject_after_elapsed ??
0;
const expUTCTimeStamp = room?.tokenConfig?.exp ?? room?.config?.exp ?? 0;
const ejectAtExp =
room?.tokenConfig?.eject_at_token_exp ??
room?.config?.eject_at_room_exp ??
false;

let newEjectDate: Date = new Date(0);

if (ejectAfterElapsed && localJoinDate) {
newEjectDate = new Date(
localJoinDate.getTime() + 1000 * ejectAfterElapsed
);
expCandidates.push(localJoinDate.getTime() + 1000 * ejectAfterElapsed);
}

if (ejectAtExp && expUTCTimeStamp) {
const expDate = new Date(expUTCTimeStamp * 1000);
if (
!newEjectDate.getTime() ||
(newEjectDate.getTime() > 0 && expDate < newEjectDate)
)
newEjectDate = expDate;
if (room?.tokenConfig?.exp && room?.tokenConfig?.eject_at_token_exp) {
expCandidates.push(room.tokenConfig.exp * 1000);
}
if (room?.config?.exp && room?.config?.eject_at_room_exp) {
expCandidates.push(room.config.exp * 1000);
}

const newEjectDate =
expCandidates.length > 0
? new Date(Math.min(...expCandidates))
: new Date(0);

if (newEjectDate.getTime() === 0) return;

Expand Down
135 changes: 134 additions & 1 deletion test/hooks/useRoomExp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ describe('useRoomExp', () => {
config: {
eject_after_elapsed: null,
eject_at_room_exp: true,
exp: 4100760732, // 2099-12-12T12:12:12.000Z
exp: 4100760732, // 2099-12-12T12:12:12.000Z UTC
},
}));
const daily = Daily.createCallObject();
Expand Down Expand Up @@ -255,4 +255,137 @@ describe('useRoomExp', () => {
});
});
});

describe('eject_at_token_exp', () => {
it('should return correct ejectDate', async () => {
(useRoom as jest.Mock).mockImplementation(() => ({
id: faker.string.uuid(),
name: faker.person.fullName(),
domainConfig: {},
tokenConfig: {
eject_at_token_exp: true,
exp: 4100760732, // 2099-12-12T12:12:12.000Z UTC
},
config: {
eject_after_elapsed: null,
},
}));
const daily = Daily.createCallObject();
const { result } = renderHook(() => useRoomExp(), {
wrapper: createWrapper(daily),
});

await waitFor(() => {
expect(result.current.ejectDate).toBeInstanceOf(Date);
expect(result.current.ejectDate).toEqual(
new Date('2099-12-12T12:12:12.000Z')
);
});
});
it('should call onCountdown correctly during the countdown', () => {
const now = new Date();
const nowUnix = Math.ceil(now.getTime() / 1000);
const exp = nowUnix + 60;

(useRoom as jest.Mock).mockImplementation(() => ({
id: faker.string.uuid(),
name: faker.person.fullName(),
domainConfig: {},
tokenConfig: {
eject_at_token_exp: true,
exp,
},
config: {
eject_after_elapsed: null,
},
}));

const daily = Daily.createCallObject();
const onCountdown = jest.fn();
renderHook(() => useRoomExp({ onCountdown }), {
wrapper: createWrapper(daily),
});

expect(onCountdown).toHaveBeenCalledTimes(0);

act(() => {
jest.advanceTimersByTime(1000);
});

expect(onCountdown).toHaveBeenCalledTimes(1);
expect(onCountdown).toHaveBeenCalledWith({
hours: 0,
minutes: 0,
seconds: 59,
});

act(() => {
jest.advanceTimersByTime(2000);
});

expect(onCountdown).toHaveBeenCalledTimes(3);
expect(onCountdown).toHaveBeenLastCalledWith({
hours: 0,
minutes: 0,
seconds: 57,
});
});
});

describe('token & room eject', () => {
it('when token expires before room, eject at token expiry', async () => {
(useRoom as jest.Mock).mockImplementation(() => ({
id: faker.string.uuid(),
name: faker.person.fullName(),
domainConfig: {},
tokenConfig: {
eject_at_token_exp: true,
exp: 3816763932, // 2090-12-12T12:12:12.000Z UTC
},
config: {
eject_after_elapsed: null,
eject_at_room_exp: true,
exp: 4100760732, // 2099-12-12T12:12:12.000Z UTC
},
}));
const daily = Daily.createCallObject();
const { result } = renderHook(() => useRoomExp(), {
wrapper: createWrapper(daily),
});

await waitFor(() => {
expect(result.current.ejectDate).toBeInstanceOf(Date);
expect(result.current.ejectDate).toEqual(
new Date('2090-12-12T12:12:12.000Z')
);
});
});
it('when room expires before token, eject at room expiry', async () => {
(useRoom as jest.Mock).mockImplementation(() => ({
id: faker.string.uuid(),
name: faker.person.fullName(),
domainConfig: {},
tokenConfig: {
eject_at_token_exp: true,
exp: 4100760732, // 2099-12-12T12:12:12.000Z
},
config: {
eject_after_elapsed: null,
eject_at_room_exp: true,
exp: 3816763932, // 2090-12-12T12:12:12.000Z
},
}));
const daily = Daily.createCallObject();
const { result } = renderHook(() => useRoomExp(), {
wrapper: createWrapper(daily),
});

await waitFor(() => {
expect(result.current.ejectDate).toBeInstanceOf(Date);
expect(result.current.ejectDate).toEqual(
new Date('2090-12-12T12:12:12.000Z')
);
});
});
});
});

0 comments on commit e4f2661

Please sign in to comment.