|
| 1 | +/* smoke-nurse-routes.ts — quick runtime checks for nurse route handlers |
| 2 | + - Mocks `requirePermission` and `prisma` methods used by the handlers |
| 3 | + - Executes each handler and asserts expected shape/status |
| 4 | +*/ |
| 5 | +import assert from 'assert'; |
| 6 | + |
| 7 | +// import modules under test |
| 8 | +import * as bedsRoute from '@/app/api/nurse/beds/route'; |
| 9 | +import * as bedsAvailableRoute from '@/app/api/nurse/beds/available/route'; |
| 10 | +import * as bedStatusRoute from '@/app/api/nurse/beds/[bedId]/status/route'; |
| 11 | +import * as assignmentsRoute from '@/app/api/nurse/bed-assignments/route'; |
| 12 | +import * as dischargeRoute from '@/app/api/nurse/bed-assignments/[id]/discharge/route'; |
| 13 | +import * as patientsRoute from '@/app/api/nurse/patients/[patientId]/route'; |
| 14 | +import * as patientBedRoute from '@/app/api/nurse/patients/[patientId]/bed/route'; |
| 15 | +import * as emrRoute from '@/app/api/nurse/patients/[patientId]/emr/route'; |
| 16 | +import * as labRoute from '@/app/api/nurse/patients/[patientId]/lab-tests/route'; |
| 17 | +import * as nursingRecordsRoute from '@/app/api/nurse/nursing-records/route'; |
| 18 | +import * as wardAssignmentsRoute from '@/app/api/nurse/ward/assignments/route'; |
| 19 | +import * as auditLogsRoute from '@/app/api/nurse/audit-logs/route'; |
| 20 | + |
| 21 | +import * as authLib from '@/lib/authorization'; |
| 22 | +import { prisma } from '@/lib/prisma'; |
| 23 | + |
| 24 | +// helpers |
| 25 | +const jsonRequest = (body?: any) => new Request('http://localhost', { method: body ? 'POST' : 'GET', body: body ? JSON.stringify(body) : undefined }); |
| 26 | + |
| 27 | +async function run() { |
| 28 | + // stub authorization to an authorized nurse by default |
| 29 | + (authLib as any).requirePermission = async (req: any, perm: string) => { |
| 30 | + if (perm === 'beds.update' || perm === 'nursing.create') { |
| 31 | + return { session: { user: { id: 'u-nurse' } } } as any; |
| 32 | + } |
| 33 | + return {} as any; |
| 34 | + }; |
| 35 | + |
| 36 | + // ---- beds list ---- |
| 37 | + (prisma.bed as any).findMany = async (opts: any) => [ { id: 'b1', bedNumber: '1', ward: 'A', bedType: 'standard', status: 'AVAILABLE' } ]; |
| 38 | + let res: any = await bedsRoute.GET(jsonRequest()); |
| 39 | + let j = await res.json(); |
| 40 | + assert(Array.isArray(j) && j.length > 0 && j[0].bedNumber === '1'); |
| 41 | + |
| 42 | + // ---- available beds ---- |
| 43 | + (prisma.bed as any).findMany = async (opts: any) => [ { id: 'b1', bedNumber: '1', ward: 'A', bedType: 'standard', status: 'AVAILABLE' } ]; |
| 44 | + res = await bedsAvailableRoute.GET(jsonRequest()); |
| 45 | + j = await res.json(); |
| 46 | + assert(j.every((x: any) => x.status === 'AVAILABLE')); |
| 47 | + |
| 48 | + // ---- update bed status (PATCH) ---- |
| 49 | + (prisma.bed as any).findUnique = async (q: any) => ({ id: 'b1', status: 'AVAILABLE' }); |
| 50 | + (prisma.bed as any).update = async (q: any) => ({ id: 'b1', status: q.data.status }); |
| 51 | + res = await bedStatusRoute.PATCH(new Request('http://localhost', { method: 'PATCH', body: JSON.stringify({ status: 'MAINTENANCE' }) }) as any, { params: { bedId: 'b1' } } as any); |
| 52 | + j = await res.json(); |
| 53 | + assert(j.status === 'MAINTENANCE'); |
| 54 | + |
| 55 | + // ---- create bed-assignment (POST) ---- |
| 56 | + (prisma.bed as any).findUnique = async (q: any) => ({ id: 'b1', status: 'AVAILABLE' }); |
| 57 | + (prisma.nurse as any).findUnique = async (q: any) => ({ id: 'n1' }); |
| 58 | + const fakeAssign = { id: 'a1', bedId: 'b1', patientId: 'p1', nurseId: 'n1' }; |
| 59 | + (prisma as any).$transaction = async (ops: any[]) => [ fakeAssign, { id: 'b1', status: 'OCCUPIED' } ]; |
| 60 | + |
| 61 | + res = await assignmentsRoute.POST(jsonRequest({ bedId: 'b1', patientId: 'p1' }) as any); |
| 62 | + j = await res.json(); |
| 63 | + assert(j.id === 'a1'); |
| 64 | + |
| 65 | + // ---- list active assignments ---- |
| 66 | + (prisma.bedAssignment as any).findMany = async (opts: any) => [ fakeAssign ]; |
| 67 | + res = await assignmentsRoute.GET(jsonRequest()); |
| 68 | + j = await res.json(); |
| 69 | + assert(Array.isArray(j)); |
| 70 | + |
| 71 | + // ---- discharge assignment ---- |
| 72 | + const existing = { id: 'a2', bedId: 'b2', dischargedAt: null, bed: { id: 'b2', status: 'OCCUPIED' } }; |
| 73 | + (prisma.bedAssignment as any).findUnique = async (q: any) => existing; |
| 74 | + (prisma as any).$transaction = async (ops: any[]) => [ { ...existing, dischargedAt: new Date() }, { id: 'b2', status: 'AVAILABLE' } ]; |
| 75 | + res = await dischargeRoute.PATCH(jsonRequest() as any, { params: { id: 'a2' } } as any); |
| 76 | + j = await res.json(); |
| 77 | + assert(j.assignment.dischargedAt); |
| 78 | + assert(j.bed.status === 'AVAILABLE'); |
| 79 | + |
| 80 | + // ---- patient basic info ---- |
| 81 | + (prisma.patient as any).findUnique = async (q: any) => ({ id: 'p1', firstName: 'A', lastName: 'B', dateOfBirth: new Date(), gender: 'F', phone: 'x' }); |
| 82 | + res = await patientsRoute.GET(jsonRequest() as any, { params: { patientId: 'p1' } } as any); |
| 83 | + j = await res.json(); |
| 84 | + assert(j.id === 'p1'); |
| 85 | + |
| 86 | + // ---- patient bed ---- |
| 87 | + (prisma.bedAssignment as any).findFirst = async (q: any) => ({ id: 'a1', bedId: 'b1', patientId: 'p1', bed: { id: 'b1', bedNumber: '1' } }); |
| 88 | + res = await patientBedRoute.GET(jsonRequest() as any, { params: { patientId: 'p1' } } as any); |
| 89 | + j = await res.json(); |
| 90 | + assert(j.bed && j.patientId === 'p1'); |
| 91 | + |
| 92 | + // ---- EMR and lab-tests (read-only) ---- |
| 93 | + (prisma.eMR as any).findMany = async (q: any) => [{ id: 'e1', diagnosis: 'X' }]; |
| 94 | + res = await emrRoute.GET(jsonRequest() as any, { params: { patientId: 'p1' } } as any); |
| 95 | + j = await res.json(); |
| 96 | + assert(Array.isArray(j)); |
| 97 | + |
| 98 | + (prisma.labTest as any).findMany = async (q: any) => [{ id: 'lt1', testType: 'CBC' }]; |
| 99 | + res = await labRoute.GET(jsonRequest() as any, { params: { patientId: 'p1' } } as any); |
| 100 | + j = await res.json(); |
| 101 | + assert(Array.isArray(j)); |
| 102 | + |
| 103 | + // ---- nursing records GET/POST ---- |
| 104 | + (prisma.nursingRecord as any).findMany = async (q: any) => [{ id: 'nr1', nurseId: 'n1', vitals: {} }]; |
| 105 | + res = await nursingRecordsRoute.GET(new Request('http://localhost?patientName=foo') as any); |
| 106 | + j = await res.json(); |
| 107 | + assert(Array.isArray(j)); |
| 108 | + |
| 109 | + (prisma.nursingRecord as any).create = async (q: any) => ({ id: 'nr2', ...q.data }); |
| 110 | + res = await nursingRecordsRoute.POST(jsonRequest({ nurseId: 'n1', patientName: 'p1', vitals: { bp: '120/80' } }) as any); |
| 111 | + j = await res.json(); |
| 112 | + assert(j.id === 'nr2'); |
| 113 | + |
| 114 | + // ---- ward assignments ---- |
| 115 | + (prisma.nurse as any).findUnique = async (q: any) => ({ id: 'n1', department: 'Ward A' }); |
| 116 | + (prisma.bedAssignment as any).findMany = async (q: any) => [ { id: 'a1', bed: { ward: 'Ward A' }, patient: { id: 'p1' } } ]; |
| 117 | + res = await wardAssignmentsRoute.GET(new Request('http://localhost') as any); |
| 118 | + j = await res.json(); |
| 119 | + assert(Array.isArray(j)); |
| 120 | + |
| 121 | + // ---- audit logs ---- |
| 122 | + (prisma.auditLog as any).findMany = async (q: any) => [ { id: 'al1', action: 'bed.assign' } ]; |
| 123 | + res = await auditLogsRoute.GET(new Request('http://localhost') as any); |
| 124 | + j = await res.json(); |
| 125 | + assert(Array.isArray(j)); |
| 126 | + |
| 127 | + console.log('SMOKE: all nurse route handlers returned expected responses ✅'); |
| 128 | +} |
| 129 | + |
| 130 | +run().catch(err => { |
| 131 | + console.error('SMOKE FAILED —', err); |
| 132 | + process.exitCode = 2; |
| 133 | +}); |
0 commit comments