File size: 7,279 Bytes
34daf91 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | const request = require('supertest');
const jwt = require('jsonwebtoken');
// 1. Module-level mocks (hoisted)
jest.mock('../src/config/db', () => ({
query: jest.fn(),
getConnection: jest.fn()
}));
jest.mock('../src/services/notificationService', () => ({
notifyYourTurn: jest.fn(),
notifyTurnApproaching: jest.fn(),
notifyMissed: jest.fn()
}));
jest.mock('../src/services/durationPrediction', () => ({
predictConsultationDuration: jest.fn(),
recordConsultationDuration: jest.fn(),
calculateQueueWaitTime: jest.fn(),
recalculateQueueEstimates: jest.fn()
}));
jest.mock('../src/services/prescriptionService', () => ({
createPrescription: jest.fn()
}));
jest.mock('../src/services/vitalsService', () => ({
logVitals: jest.fn()
}));
jest.mock('../src/services/waitlistService', () => ({
handleSlotRelease: jest.fn()
}));
jest.mock('../src/services/sseManager', () => ({
broadcastQueueUpdate: jest.fn(),
broadcastToDoctor: jest.fn()
}));
jest.mock('../src/services/virtualCheckinService', () => ({
getWaitingRoomStatus: jest.fn()
}));
// 2. Imports
const app = require('../src/server');
const db = require('../src/config/db');
const notificationService = require('../src/services/notificationService');
const durationPrediction = require('../src/services/durationPrediction');
const prescriptionService = require('../src/services/prescriptionService');
const vitalsService = require('../src/services/vitalsService');
const waitlistService = require('../src/services/waitlistService');
const virtualCheckinService = require('../src/services/virtualCheckinService');
const { jwtSecret } = require('../src/middleware/authenticate');
describe('Queue State Machine — PATCH /api/appointments/queue/:queueId/status', () => {
let mockConn;
beforeEach(() => {
// Since resetMocks is true in jest.config.js, we MUST define implementation in beforeEach
notificationService.notifyYourTurn.mockResolvedValue();
notificationService.notifyTurnApproaching.mockResolvedValue();
notificationService.notifyMissed.mockResolvedValue();
durationPrediction.predictConsultationDuration.mockResolvedValue({ predictedDuration: 15, factors: {} });
durationPrediction.recordConsultationDuration.mockResolvedValue();
durationPrediction.calculateQueueWaitTime.mockResolvedValue({ estimatedWait: 0 });
durationPrediction.recalculateQueueEstimates.mockResolvedValue();
prescriptionService.createPrescription.mockResolvedValue();
vitalsService.logVitals.mockResolvedValue();
waitlistService.handleSlotRelease.mockResolvedValue();
virtualCheckinService.getWaitingRoomStatus.mockResolvedValue({ queuePosition: 1 });
db.query.mockResolvedValue([[], []]);
mockConn = {
query: jest.fn().mockResolvedValue([[{ affectedRows: 1 }], []]),
beginTransaction: jest.fn().mockResolvedValue(),
commit: jest.fn().mockResolvedValue(),
rollback: jest.fn().mockResolvedValue(),
release: jest.fn()
};
db.getConnection.mockResolvedValue(mockConn);
});
const doctorToken = jwt.sign({ id: 1, role: 'DOCTOR' }, jwtSecret);
it('should successfully transition to IN_PROGRESS', async () => {
mockConn.query.mockImplementation((sql, params) => {
if (sql.includes('SELECT lq.appointment_id')) {
return Promise.resolve([[{
appointment_id: 101,
doctor_id: 1,
patient_id: 2,
doc_first: 'Sarah',
doc_last: 'Jenkins',
location_room: 'Room 101',
queue_number: 5,
appointment_date: '2026-05-09'
}], []]);
}
if (sql.includes('SELECT a.patient_id, lq.queue_number')) {
return Promise.resolve([[], []]);
}
if (sql.includes('SELECT lq.appointment_id, a.patient_id')) {
return Promise.resolve([[], []]);
}
return Promise.resolve([[{ affectedRows: 1 }], []]);
});
const res = await request(app)
.patch('/api/appointments/queue/1/status')
.set('Authorization', `Bearer ${doctorToken}`)
.send({ status: 'IN_PROGRESS' });
if (res.statusCode !== 200) console.log('DEBUG RES BODY:', res.body);
expect(res.statusCode).toBe(200);
});
it('should handle MISSED status by repositioning patient', async () => {
mockConn.query.mockImplementation((sql, params) => {
if (sql.includes('SELECT lq.appointment_id')) {
return Promise.resolve([[{
appointment_id: 101,
doctor_id: 1,
patient_id: 2,
queue_number: 5,
appointment_date: '2026-05-09',
doc_first: 'Sarah',
doc_last: 'Jenkins'
}], []]);
}
if (sql.includes('MAX(lq.queue_number)')) {
return Promise.resolve([[{ maxQ: 10 }], []]);
}
if (sql.includes('SELECT lq.appointment_id, a.patient_id')) {
return Promise.resolve([[], []]);
}
return Promise.resolve([[{ affectedRows: 1 }], []]);
});
const res = await request(app)
.patch('/api/appointments/queue/1/status')
.set('Authorization', `Bearer ${doctorToken}`)
.send({ status: 'MISSED' });
if (res.statusCode !== 200) console.log('DEBUG RES BODY:', res.body);
expect(res.statusCode).toBe(200);
});
it('should successfully transition to COMPLETED with vitals', async () => {
mockConn.query.mockImplementation((sql, params) => {
if (sql.includes('SELECT lq.appointment_id')) {
return Promise.resolve([[{
appointment_id: 101,
doctor_id: 1,
patient_id: 2,
queue_number: 5,
appointment_date: '2026-05-09',
doc_first: 'Sarah',
doc_last: 'Jenkins'
}], []]);
}
if (sql.includes('SELECT consultation_start')) {
return Promise.resolve([[{ consultation_start: new Date(Date.now() - 15 * 60000) }], []]);
}
if (sql.includes('SELECT lq.appointment_id, a.patient_id')) {
return Promise.resolve([[]]);
}
return Promise.resolve([[{ affectedRows: 1 }], []]);
});
const res = await request(app)
.patch('/api/appointments/queue/1/status')
.set('Authorization', `Bearer ${doctorToken}`)
.send({
status: 'COMPLETED',
diagnosis: 'Common Cold',
vitals: {
weight_kg: 70,
heart_rate: 72
}
});
if (res.statusCode !== 200) console.log('DEBUG RES BODY:', res.body);
expect(res.statusCode).toBe(200);
});
});
|