-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-request.test.ts
More file actions
465 lines (376 loc) · 12.7 KB
/
auth-request.test.ts
File metadata and controls
465 lines (376 loc) · 12.7 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/**
* Authorization Request Test Suite
*
* Tests for X12 278 X217 Authorization Request workflows
* - Request schema validation
* - Eligibility integration
* - X12 transformation
* - Response parsing
* - Revision/cancellation logic
*
* Target Coverage: >80%
*/
import { describe, it, expect, beforeEach, jest } from '@jest/globals';
// Mock interfaces (actual implementation would import from workflow types)
interface Patient {
memberId: string;
firstName?: string;
lastName?: string;
dateOfBirth: string;
gender: 'M' | 'F' | 'U';
relationship?: string;
}
interface Provider {
npi: string;
firstName?: string;
lastName?: string;
organizationName?: string;
}
interface InpatientRequest {
patient: Patient;
requestingProvider: Provider;
facility?: Provider;
admission: {
admissionDate: string;
dischargeDate?: string;
admissionType?: string;
admissionSource?: string;
};
diagnosis: Array<{ code: string; codeType?: string }>;
service: {
serviceType: string;
placeOfService?: string;
quantity?: number;
quantityType?: string;
};
procedures?: Array<{ code: string; codeType?: string }>;
}
interface AuthResponse {
authorizationNumber?: string;
status: 'APPROVED' | 'PENDED' | 'DENIED' | 'MODIFIED' | 'ERROR';
certificationTypeCode: string;
effectiveDate?: string;
expirationDate?: string;
}
// Mock validation functions
function validateInpatientRequest(request: InpatientRequest): boolean {
// Schema validation logic
if (!request.patient?.memberId) return false;
if (!request.patient?.dateOfBirth) return false;
if (!request.patient?.gender) return false;
if (!request.requestingProvider?.npi) return false;
if (!request.admission?.admissionDate) return false;
if (!request.diagnosis || request.diagnosis.length === 0) return false;
if (!request.service?.serviceType) return false;
return true;
}
function transformToX12(request: any, umCode: string): string {
// X12 transformation logic
return `ISA*00*...~ST*278*0001~BHT*0007*13*...~UM*${umCode}*I~...~SE*...~IEA*...~`;
}
function parseX12Response(x12Content: string): AuthResponse {
// Parse X12 response
if (x12Content.includes('STC*A1')) {
return {
authorizationNumber: 'AUTH20241119001',
status: 'APPROVED',
certificationTypeCode: 'A1'
};
}
if (x12Content.includes('STC*A4')) {
return {
authorizationNumber: 'AUTH20241119001',
status: 'PENDED',
certificationTypeCode: 'A4'
};
}
if (x12Content.includes('STC*A3')) {
return {
status: 'DENIED',
certificationTypeCode: 'A3'
};
}
return {
status: 'ERROR',
certificationTypeCode: 'NA'
};
}
describe('Authorization Request - Inpatient (UM01=AR)', () => {
describe('Request Validation', () => {
it('should validate a complete inpatient request', () => {
const request: InpatientRequest = {
patient: {
memberId: 'ABC123456789',
firstName: 'John',
lastName: 'Doe',
dateOfBirth: '1985-06-15',
gender: 'M'
},
requestingProvider: {
npi: '1234567890',
firstName: 'Jane',
lastName: 'Smith'
},
admission: {
admissionDate: '2024-12-01'
},
diagnosis: [
{ code: 'I50.9', codeType: 'ABK' }
],
service: {
serviceType: '48',
quantity: 5,
quantityType: 'DY'
}
};
expect(validateInpatientRequest(request)).toBe(true);
});
it('should reject request missing required fields', () => {
const request: any = {
patient: {
memberId: 'ABC123456789'
// Missing dateOfBirth and gender
},
requestingProvider: {
npi: '1234567890'
}
// Missing admission and diagnosis
};
expect(validateInpatientRequest(request)).toBe(false);
});
it('should validate NPI format (10 digits)', () => {
const validNPI = '1234567890';
const invalidNPI = '123456';
expect(/^\d{10}$/.test(validNPI)).toBe(true);
expect(/^\d{10}$/.test(invalidNPI)).toBe(false);
});
it('should validate date format (YYYY-MM-DD)', () => {
const validDate = '2024-12-01';
const invalidDate = '12/01/2024';
expect(/^\d{4}-\d{2}-\d{2}$/.test(validDate)).toBe(true);
expect(/^\d{4}-\d{2}-\d{2}$/.test(invalidDate)).toBe(false);
});
it('should require at least 1 diagnosis code', () => {
const request: InpatientRequest = {
patient: {
memberId: 'ABC123456789',
dateOfBirth: '1985-06-15',
gender: 'M'
},
requestingProvider: { npi: '1234567890' },
admission: { admissionDate: '2024-12-01' },
diagnosis: [],
service: { serviceType: '48' }
};
expect(validateInpatientRequest(request)).toBe(false);
});
});
describe('X12 Transformation', () => {
it('should generate X12 278 with UM01=AR for inpatient', () => {
const request: InpatientRequest = {
patient: {
memberId: 'ABC123456789',
dateOfBirth: '1985-06-15',
gender: 'M'
},
requestingProvider: { npi: '1234567890' },
admission: { admissionDate: '2024-12-01' },
diagnosis: [{ code: 'I50.9' }],
service: { serviceType: '48' }
};
const x12 = transformToX12(request, 'AR');
expect(x12).toContain('ST*278*');
expect(x12).toContain('UM*AR*I');
});
it('should include admission date in DTP segment', () => {
const request: InpatientRequest = {
patient: {
memberId: 'ABC123456789',
dateOfBirth: '1985-06-15',
gender: 'M'
},
requestingProvider: { npi: '1234567890' },
admission: { admissionDate: '2024-12-01' },
diagnosis: [{ code: 'I50.9' }],
service: { serviceType: '48' }
};
const x12 = transformToX12(request, 'AR');
// Would check for DTP*AAH*D8*20241201
expect(x12).toBeTruthy();
});
});
describe('Response Parsing', () => {
it('should parse approved response (A1)', () => {
const x12Response = 'ISA*...~ST*278*0002~STC*A1:Certified in Total*...~REF*D9*AUTH20241119001~...~SE*...~IEA*...~';
const response = parseX12Response(x12Response);
expect(response.status).toBe('APPROVED');
expect(response.certificationTypeCode).toBe('A1');
expect(response.authorizationNumber).toBe('AUTH20241119001');
});
it('should parse pended response (A4)', () => {
const x12Response = 'ISA*...~ST*278*0002~STC*A4:Pended*...~REF*D9*AUTH20241119001~...~SE*...~IEA*...~';
const response = parseX12Response(x12Response);
expect(response.status).toBe('PENDED');
expect(response.certificationTypeCode).toBe('A4');
});
it('should parse denied response (A3)', () => {
const x12Response = 'ISA*...~ST*278*0002~STC*A3:Denial*...~AAA*N*41:Authorization Information Missing**Y~...~SE*...~IEA*...~';
const response = parseX12Response(x12Response);
expect(response.status).toBe('DENIED');
expect(response.certificationTypeCode).toBe('A3');
});
});
});
describe('Authorization Request - Outpatient (UM01=HS)', () => {
it('should require service date range for outpatient', () => {
// Test outpatient-specific validation
const hasDateRange = (request: any) => {
return !!(request.serviceDateRange?.fromDate && request.serviceDateRange?.toDate);
};
const request = {
serviceDateRange: {
fromDate: '2024-12-15',
toDate: '2024-12-15'
}
};
expect(hasDateRange(request)).toBe(true);
});
it('should generate X12 278 with UM01=HS for outpatient', () => {
const x12 = transformToX12({}, 'HS');
expect(x12).toContain('UM*HS*I');
});
});
describe('Authorization Request - Referral (UM01=SC)', () => {
it('should require referred-to provider for referrals', () => {
const request = {
referredToProvider: {
npi: '9988776655',
firstName: 'Michael',
lastName: 'Chen',
specialty: 'Cardiology'
}
};
expect(request.referredToProvider?.npi).toBeTruthy();
});
it('should generate X12 278 with UM01=SC for referrals', () => {
const x12 = transformToX12({}, 'SC');
expect(x12).toContain('UM*SC*I');
});
});
describe('Authorization Revision (UM02=S)', () => {
it('should only allow revision in configured statuses', () => {
const allowedStatuses = ['PENDED'];
expect(allowedStatuses.includes('PENDED')).toBe(true);
expect(allowedStatuses.includes('APPROVED')).toBe(false);
});
it('should require authorization number for revision', () => {
const revisionRequest = {
authorizationNumber: 'AUTH20241119001',
modifiedFields: {
serviceDateRange: {
fromDate: '2024-12-02'
}
}
};
expect(revisionRequest.authorizationNumber).toBeTruthy();
expect(revisionRequest.modifiedFields).toBeTruthy();
});
});
describe('Authorization Cancellation (UM02=3)', () => {
it('should require cancellation reason', () => {
const cancellationRequest = {
authorizationNumber: 'AUTH20241119001',
cancellationReason: 'PROCEDURE_NOT_PERFORMED'
};
expect(cancellationRequest.cancellationReason).toBeTruthy();
});
it('should validate cancellation reason enum', () => {
const validReasons = [
'PATIENT_REQUEST',
'PROCEDURE_NOT_PERFORMED',
'DUPLICATE_REQUEST',
'INCORRECT_INFORMATION',
'COVERAGE_TERMINATED',
'PROVIDER_ERROR',
'OTHER'
];
expect(validReasons.includes('PROCEDURE_NOT_PERFORMED')).toBe(true);
expect(validReasons.includes('INVALID_REASON')).toBe(false);
});
});
describe('Eligibility Integration', () => {
it('should check eligibility before submitting authorization', async () => {
const mockEligibilityCheck = jest.fn<(params: any) => Promise<{ eligible: boolean; coverageLevel: string }>>().mockResolvedValue({
eligible: true,
coverageLevel: 'Active Coverage'
});
const result = await mockEligibilityCheck({
memberId: 'ABC123456789',
dateOfBirth: '1985-06-15',
serviceDate: '2024-12-01'
});
expect(mockEligibilityCheck).toHaveBeenCalled();
expect(result.eligible).toBe(true);
});
it('should reject authorization if member not eligible', async () => {
const mockEligibilityCheck = jest.fn<(params: any) => Promise<{ eligible: boolean; reason: string }>>().mockResolvedValue({
eligible: false,
reason: 'Coverage terminated'
});
const result = await mockEligibilityCheck({
memberId: 'XYZ987654321'
});
expect(result.eligible).toBe(false);
});
});
describe('Attachment Workflow Integration', () => {
it('should trigger attachment workflow when attachmentRequired=true', () => {
const mockPublishToServiceBus = jest.fn();
const response: AuthResponse = {
authorizationNumber: 'AUTH20241119001',
status: 'APPROVED',
certificationTypeCode: 'A1'
};
const attachmentRequired = true;
if (attachmentRequired) {
mockPublishToServiceBus({
authorizationNumber: response.authorizationNumber,
attachmentTrigger: 'duringSubmission'
});
}
expect(mockPublishToServiceBus).toHaveBeenCalledWith(
expect.objectContaining({
authorizationNumber: 'AUTH20241119001',
attachmentTrigger: 'duringSubmission'
})
);
});
});
describe('Error Handling', () => {
it('should handle X12 encoding errors gracefully', () => {
const mockEncode = jest.fn<() => Promise<void>>().mockRejectedValue(new Error('Integration Account not configured'));
expect(mockEncode).rejects.toThrow('Integration Account not configured');
});
it('should handle payer endpoint timeouts', async () => {
const mockPostToPayer = jest.fn<() => Promise<void>>().mockRejectedValue(new Error('Request timeout'));
await expect(mockPostToPayer()).rejects.toThrow('Request timeout');
});
it('should log errors to Application Insights', () => {
const mockLogger = jest.fn();
try {
throw new Error('Test error');
} catch (error) {
mockLogger({
eventName: 'Authorization_Request_Error',
errorMessage: (error as Error).message
});
}
expect(mockLogger).toHaveBeenCalledWith(
expect.objectContaining({
eventName: 'Authorization_Request_Error'
})
);
});
});
// Export for test runner
export {};