|
| 1 | +import { acceptPairingSlot } from '../acceptPairingSlot'; |
| 2 | +import { pairingSessionsRepo } from '@repos/pairingSessionsRepo'; |
| 3 | +import { userRepo } from '@repos/userRepo'; |
| 4 | +import { buildMockApp, buildMockActionParam } from '@utils/slackMocks'; |
| 5 | +import { PairingSession } from '@models/PairingSession'; |
| 6 | +import { CandidateType, InterviewFormat, InterviewType } from '@bot/enums'; |
| 7 | +import * as PairingRequestService from '@/services/PairingRequestService'; |
| 8 | +import * as PairingSessionCloserModule from '@/services/PairingSessionCloser'; |
| 9 | +import { App } from '@slack/bolt'; |
| 10 | +import { chatService } from '@/services/ChatService'; |
| 11 | + |
| 12 | +function makeInterview(overrides: Partial<PairingSession> = {}): PairingSession { |
| 13 | + return { |
| 14 | + threadId: 'thread-1', |
| 15 | + requestorId: 'r1', |
| 16 | + candidateName: 'Dana', |
| 17 | + languages: ['Python'], |
| 18 | + format: InterviewFormat.REMOTE, |
| 19 | + candidateType: CandidateType.FULL_TIME, |
| 20 | + requestedAt: new Date(), |
| 21 | + teammatesNeededCount: 2, |
| 22 | + slots: [ |
| 23 | + { |
| 24 | + id: 'slot-1', |
| 25 | + date: '2026-03-31', |
| 26 | + startTime: '13:00', |
| 27 | + endTime: '15:00', |
| 28 | + interestedTeammates: [], |
| 29 | + }, |
| 30 | + ], |
| 31 | + pendingTeammates: [{ userId: 'u1', expiresAt: 9999999999, messageTimestamp: 'ts-1' }], |
| 32 | + declinedTeammates: [], |
| 33 | + ...overrides, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +describe('acceptPairingSlot', () => { |
| 38 | + let app: App; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + app = buildMockApp(); |
| 42 | + acceptPairingSlot.app = app; |
| 43 | + pairingSessionsRepo.getByThreadIdOrUndefined = jest.fn().mockResolvedValue(makeInterview()); |
| 44 | + userRepo.find = jest.fn().mockResolvedValue({ |
| 45 | + id: 'u1', |
| 46 | + name: 'Alice', |
| 47 | + languages: ['Python'], |
| 48 | + lastReviewedDate: undefined, |
| 49 | + interviewTypes: [InterviewType.PAIRING], |
| 50 | + formats: [InterviewFormat.REMOTE], |
| 51 | + }); |
| 52 | + jest |
| 53 | + .spyOn(PairingRequestService.pairingRequestService, 'recordSlotSelections') |
| 54 | + .mockResolvedValue(makeInterview()); |
| 55 | + jest |
| 56 | + .spyOn(PairingSessionCloserModule.pairingSessionCloser, 'closeIfComplete') |
| 57 | + .mockResolvedValue(undefined); |
| 58 | + userRepo.markNowAsLastReviewedDate = jest.fn().mockResolvedValue(undefined); |
| 59 | + chatService.updateDirectMessage = jest.fn().mockResolvedValue(undefined); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('handleSubmitSlots', () => { |
| 63 | + it('should ack', async () => { |
| 64 | + const param = buildMockActionParam(); |
| 65 | + param.body.actions = [{ value: 'thread-1', action_id: 'pairing-submit-slots' } as any]; |
| 66 | + param.body.user = { id: 'u1', name: 'Alice' } as any; |
| 67 | + param.body.message = { ts: 'msg-ts-1' } as any; |
| 68 | + param.body.state = { |
| 69 | + values: { |
| 70 | + 'pairing-dm-slots': { |
| 71 | + 'pairing-slot-selections': { |
| 72 | + selected_options: [{ value: 'slot-1' }], |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } as any; |
| 77 | + |
| 78 | + await acceptPairingSlot.handleSubmitSlots(param); |
| 79 | + |
| 80 | + expect(param.ack).toHaveBeenCalledTimes(1); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should call recordSlotSelections with the selected slot IDs', async () => { |
| 84 | + const param = buildMockActionParam(); |
| 85 | + param.body.actions = [{ value: 'thread-1', action_id: 'pairing-submit-slots' } as any]; |
| 86 | + param.body.user = { id: 'u1', name: 'Alice' } as any; |
| 87 | + param.body.message = { ts: 'msg-ts-1' } as any; |
| 88 | + param.body.state = { |
| 89 | + values: { |
| 90 | + 'pairing-dm-slots': { |
| 91 | + 'pairing-slot-selections': { |
| 92 | + selected_options: [{ value: 'slot-1' }], |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + } as any; |
| 97 | + |
| 98 | + await acceptPairingSlot.handleSubmitSlots(param); |
| 99 | + |
| 100 | + expect(PairingRequestService.pairingRequestService.recordSlotSelections).toHaveBeenCalledWith( |
| 101 | + expect.anything(), |
| 102 | + 'u1', |
| 103 | + ['slot-1'], |
| 104 | + [InterviewFormat.REMOTE], |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should mark the user as last reviewed', async () => { |
| 109 | + const param = buildMockActionParam(); |
| 110 | + param.body.actions = [{ value: 'thread-1', action_id: 'pairing-submit-slots' } as any]; |
| 111 | + param.body.user = { id: 'u1', name: 'Alice' } as any; |
| 112 | + param.body.message = { ts: 'msg-ts-1' } as any; |
| 113 | + param.body.state = { |
| 114 | + values: { |
| 115 | + 'pairing-dm-slots': { |
| 116 | + 'pairing-slot-selections': { selected_options: [{ value: 'slot-1' }] }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + } as any; |
| 120 | + |
| 121 | + await acceptPairingSlot.handleSubmitSlots(param); |
| 122 | + |
| 123 | + expect(userRepo.markNowAsLastReviewedDate).toHaveBeenCalledWith('u1'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should call closeIfComplete after recording slot selections', async () => { |
| 127 | + const param = buildMockActionParam(); |
| 128 | + param.body.actions = [{ value: 'thread-1', action_id: 'pairing-submit-slots' } as any]; |
| 129 | + param.body.user = { id: 'u1', name: 'Alice' } as any; |
| 130 | + param.body.message = { ts: 'msg-ts-1' } as any; |
| 131 | + param.body.state = { |
| 132 | + values: { |
| 133 | + 'pairing-dm-slots': { |
| 134 | + 'pairing-slot-selections': { selected_options: [{ value: 'slot-1' }] }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + } as any; |
| 138 | + |
| 139 | + await acceptPairingSlot.handleSubmitSlots(param); |
| 140 | + |
| 141 | + expect(PairingSessionCloserModule.pairingSessionCloser.closeIfComplete).toHaveBeenCalledWith( |
| 142 | + app, |
| 143 | + 'thread-1', |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should update the DM after recording slot selections', async () => { |
| 148 | + const param = buildMockActionParam(); |
| 149 | + param.body.actions = [{ value: 'thread-1', action_id: 'pairing-submit-slots' } as any]; |
| 150 | + param.body.user = { id: 'u1', name: 'Alice' } as any; |
| 151 | + param.body.message = { ts: 'msg-ts-1' } as any; |
| 152 | + param.body.state = { |
| 153 | + values: { |
| 154 | + 'pairing-dm-slots': { |
| 155 | + 'pairing-slot-selections': { selected_options: [{ value: 'slot-1' }] }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + } as any; |
| 159 | + |
| 160 | + await acceptPairingSlot.handleSubmitSlots(param); |
| 161 | + |
| 162 | + expect(chatService.updateDirectMessage).toHaveBeenCalled(); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should not record slot selections if user is not pending', async () => { |
| 166 | + pairingSessionsRepo.getByThreadIdOrUndefined = jest |
| 167 | + .fn() |
| 168 | + .mockResolvedValue(makeInterview({ pendingTeammates: [] })); |
| 169 | + |
| 170 | + const param = buildMockActionParam(); |
| 171 | + param.body.actions = [{ value: 'thread-1', action_id: 'pairing-submit-slots' } as any]; |
| 172 | + param.body.user = { id: 'u1', name: 'Alice' } as any; |
| 173 | + param.body.message = { ts: 'msg-ts-1' } as any; |
| 174 | + param.body.state = { |
| 175 | + values: { |
| 176 | + 'pairing-dm-slots': { |
| 177 | + 'pairing-slot-selections': { selected_options: [{ value: 'slot-1' }] }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + } as any; |
| 181 | + |
| 182 | + await acceptPairingSlot.handleSubmitSlots(param); |
| 183 | + |
| 184 | + expect( |
| 185 | + PairingRequestService.pairingRequestService.recordSlotSelections, |
| 186 | + ).not.toHaveBeenCalled(); |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + describe('handleDeclineAll', () => { |
| 191 | + it('should ack and call declineTeammate', async () => { |
| 192 | + jest |
| 193 | + .spyOn(PairingRequestService.pairingRequestService, 'declineTeammate') |
| 194 | + .mockResolvedValue(makeInterview()); |
| 195 | + const param = buildMockActionParam(); |
| 196 | + param.body.actions = [{ value: 'thread-1', action_id: 'pairing-decline-all' } as any]; |
| 197 | + param.body.user = { id: 'u1', name: 'Alice' } as any; |
| 198 | + |
| 199 | + await acceptPairingSlot.handleDeclineAll(param); |
| 200 | + |
| 201 | + expect(param.ack).toHaveBeenCalledTimes(1); |
| 202 | + expect(PairingRequestService.pairingRequestService.declineTeammate).toHaveBeenCalled(); |
| 203 | + }); |
| 204 | + |
| 205 | + it('should not call declineTeammate if user is not pending', async () => { |
| 206 | + pairingSessionsRepo.getByThreadIdOrUndefined = jest |
| 207 | + .fn() |
| 208 | + .mockResolvedValue(makeInterview({ pendingTeammates: [] })); |
| 209 | + jest |
| 210 | + .spyOn(PairingRequestService.pairingRequestService, 'declineTeammate') |
| 211 | + .mockResolvedValue(makeInterview()); |
| 212 | + |
| 213 | + const param = buildMockActionParam(); |
| 214 | + param.body.actions = [{ value: 'thread-1', action_id: 'pairing-decline-all' } as any]; |
| 215 | + param.body.user = { id: 'u1', name: 'Alice' } as any; |
| 216 | + |
| 217 | + await acceptPairingSlot.handleDeclineAll(param); |
| 218 | + |
| 219 | + expect(PairingRequestService.pairingRequestService.declineTeammate).not.toHaveBeenCalled(); |
| 220 | + }); |
| 221 | + }); |
| 222 | +}); |
0 commit comments