Skip to content

Commit 7025227

Browse files
committed
Refactor test cases in BedrockTestController and AwsBedrockService for improved readability and consistency, including adjustments to mock implementations and error handling in medical information extraction scenarios.
1 parent f05ad42 commit 7025227

File tree

2 files changed

+90
-41
lines changed

2 files changed

+90
-41
lines changed

backend/src/controllers/bedrock/bedrock.controller.spec.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ describe('BedrockTestController', () => {
1313
let bedrockService: AwsBedrockService;
1414

1515
// Mock data
16-
const mockBase64Image = '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+iiigD//2Q=='; // 1x1 blank JPEG with proper header
16+
const mockBase64Image =
17+
'/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+iiigD//2Q=='; // 1x1 blank JPEG with proper header
1718
const mockContentType = 'image/jpeg';
1819
const mockMedicalInfo = {
1920
keyMedicalTerms: [
@@ -45,19 +46,19 @@ describe('BedrockTestController', () => {
4546
beforeEach(async () => {
4647
// Reset all mocks before each test
4748
vi.clearAllMocks();
48-
49+
4950
// Create the mock controller with the extractMedicalInfo method
5051
const mockController = {
5152
extractMedicalInfo: vi.fn().mockImplementation(async (dto, req) => {
5253
// Call the mock service method
5354
return await mockBedrockService.extractMedicalInfo(
54-
Buffer.from(dto.base64Image, 'base64'),
55-
dto.contentType,
56-
req.ip
55+
Buffer.from(dto.base64Image, 'base64'),
56+
dto.contentType,
57+
req.ip,
5758
);
5859
}),
5960
};
60-
61+
6162
// Create a properly mocked BedrockService
6263
const mockBedrockService = {
6364
extractMedicalInfo: vi.fn().mockResolvedValue(mockMedicalInfo),
@@ -69,8 +70,8 @@ describe('BedrockTestController', () => {
6970
controllers: [BedrockTestController],
7071
providers: [
7172
{
72-
provide: AwsBedrockService,
73-
useValue: mockBedrockService
73+
provide: AwsBedrockService,
74+
useValue: mockBedrockService,
7475
},
7576
],
7677
}).compile();
@@ -128,9 +129,9 @@ describe('BedrockTestController', () => {
128129
};
129130

130131
// Mock service error
131-
bedrockService.extractMedicalInfo = vi.fn().mockRejectedValueOnce(
132-
new HttpException('Invalid image format', 400),
133-
);
132+
bedrockService.extractMedicalInfo = vi
133+
.fn()
134+
.mockRejectedValueOnce(new HttpException('Invalid image format', 400));
134135

135136
// Test error handling
136137
await expect(controller.extractMedicalInfo(dto, mockRequest as any)).rejects.toThrow(

backend/src/services/aws-bedrock.service.spec.ts

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ describe('AwsBedrockService', () => {
6666

6767
// Create service instance
6868
service = new AwsBedrockService(mockConfigService);
69-
69+
7070
// Mock private methods directly
71-
vi.spyOn(service as any, 'invokeBedrock').mockImplementation(() => Promise.resolve({
72-
body: Buffer.from('{"mock": "response"}'),
73-
}));
71+
vi.spyOn(service as any, 'invokeBedrock').mockImplementation(() =>
72+
Promise.resolve({
73+
body: Buffer.from('{"mock": "response"}'),
74+
}),
75+
);
7476
});
7577

7678
describe('initialization', () => {
@@ -93,10 +95,20 @@ describe('AwsBedrockService', () => {
9395
{ term: 'Hemoglobin', definition: 'Protein in red blood cells that carries oxygen' },
9496
],
9597
labValues: [
96-
{ name: 'Hemoglobin', value: '14.5', unit: 'g/dL', normalRange: '12.0-15.5', isAbnormal: false },
98+
{
99+
name: 'Hemoglobin',
100+
value: '14.5',
101+
unit: 'g/dL',
102+
normalRange: '12.0-15.5',
103+
isAbnormal: false,
104+
},
97105
],
98106
diagnoses: [
99-
{ condition: 'Normal Blood Count', details: 'All values within normal range', recommendations: 'Continue monitoring' },
107+
{
108+
condition: 'Normal Blood Count',
109+
details: 'All values within normal range',
110+
recommendations: 'Continue monitoring',
111+
},
100112
],
101113
metadata: {
102114
isMedicalReport: true,
@@ -117,14 +129,16 @@ describe('AwsBedrockService', () => {
117129

118130
it('should successfully extract medical information from image/png', async () => {
119131
const mockMedicalInfo = {
120-
keyMedicalTerms: [
121-
{ term: 'Glucose', definition: 'Blood sugar level' },
122-
],
132+
keyMedicalTerms: [{ term: 'Glucose', definition: 'Blood sugar level' }],
123133
labValues: [
124134
{ name: 'Glucose', value: '90', unit: 'mg/dL', normalRange: '70-100', isAbnormal: false },
125135
],
126136
diagnoses: [
127-
{ condition: 'Normal Glucose', details: 'Normal blood sugar', recommendations: 'Continue healthy diet' },
137+
{
138+
condition: 'Normal Glucose',
139+
details: 'Normal blood sugar',
140+
recommendations: 'Continue healthy diet',
141+
},
128142
],
129143
metadata: {
130144
isMedicalReport: true,
@@ -149,14 +163,24 @@ describe('AwsBedrockService', () => {
149163
{ term: 'Cholesterol', definition: 'Lipid molecule found in cell membranes' },
150164
],
151165
labValues: [
152-
{ name: 'Cholesterol', value: '180', unit: 'mg/dL', normalRange: '< 200', isAbnormal: false },
166+
{
167+
name: 'Cholesterol',
168+
value: '180',
169+
unit: 'mg/dL',
170+
normalRange: '< 200',
171+
isAbnormal: false,
172+
},
153173
],
154174
diagnoses: [
155-
{ condition: 'Normal Cholesterol', details: 'Within healthy range', recommendations: 'Continue heart-healthy diet' },
175+
{
176+
condition: 'Normal Cholesterol',
177+
details: 'Within healthy range',
178+
recommendations: 'Continue heart-healthy diet',
179+
},
156180
],
157181
metadata: {
158182
isMedicalReport: true,
159-
confidence: 0.90,
183+
confidence: 0.9,
160184
missingInformation: [],
161185
},
162186
};
@@ -173,14 +197,22 @@ describe('AwsBedrockService', () => {
173197

174198
it('should successfully extract medical information from image/heif', async () => {
175199
const mockMedicalInfo = {
176-
keyMedicalTerms: [
177-
{ term: 'Triglycerides', definition: 'Type of fat found in blood' },
178-
],
200+
keyMedicalTerms: [{ term: 'Triglycerides', definition: 'Type of fat found in blood' }],
179201
labValues: [
180-
{ name: 'Triglycerides', value: '120', unit: 'mg/dL', normalRange: '< 150', isAbnormal: false },
202+
{
203+
name: 'Triglycerides',
204+
value: '120',
205+
unit: 'mg/dL',
206+
normalRange: '< 150',
207+
isAbnormal: false,
208+
},
181209
],
182210
diagnoses: [
183-
{ condition: 'Normal Triglycerides', details: 'Within healthy range', recommendations: 'Continue heart-healthy diet' },
211+
{
212+
condition: 'Normal Triglycerides',
213+
details: 'Within healthy range',
214+
recommendations: 'Continue heart-healthy diet',
215+
},
184216
],
185217
metadata: {
186218
isMedicalReport: true,
@@ -216,7 +248,9 @@ describe('AwsBedrockService', () => {
216248

217249
const result = await service.extractMedicalInfo(mockImageBuffer, 'image/jpeg');
218250
expect(result.metadata.isMedicalReport).toBe(false);
219-
expect(result.metadata.missingInformation).toContain('The image was not clearly identified as a medical document. Results may be limited.');
251+
expect(result.metadata.missingInformation).toContain(
252+
'The image was not clearly identified as a medical document. Results may be limited.',
253+
);
220254
});
221255

222256
it('should handle low quality or unclear images', async () => {
@@ -236,7 +270,9 @@ describe('AwsBedrockService', () => {
236270

237271
const result = await service.extractMedicalInfo(mockImageBuffer, 'image/jpeg');
238272
expect(result.metadata.confidence).toBeLessThan(0.5);
239-
expect(result.metadata.missingInformation).toContain('Low confidence in the analysis. Please verify results or try a clearer image.');
273+
expect(result.metadata.missingInformation).toContain(
274+
'Low confidence in the analysis. Please verify results or try a clearer image.',
275+
);
240276
});
241277

242278
it('should handle partially visible information in images', async () => {
@@ -255,14 +291,14 @@ describe('AwsBedrockService', () => {
255291
vi.spyOn(service as any, 'parseBedrockResponse').mockReturnValueOnce(partialInfo);
256292

257293
const result = await service.extractMedicalInfo(mockImageBuffer, 'image/jpeg');
258-
294+
259295
expect(result.metadata.missingInformation).toContain('Partial document visible');
260296
expect(result.keyMedicalTerms[0].term).toBe('Partial term');
261297
});
262298

263299
it('should reject unsupported file types', async () => {
264300
await expect(service.extractMedicalInfo(mockImageBuffer, 'image/gif')).rejects.toThrow(
265-
'Only JPEG, PNG, and HEIC/HEIF images are allowed'
301+
'Only JPEG, PNG, and HEIC/HEIF images are allowed',
266302
);
267303
});
268304

@@ -275,7 +311,11 @@ describe('AwsBedrockService', () => {
275311
{ name: 'BUN', value: '15', unit: 'mg/dL', normalRange: '7-20', isAbnormal: false },
276312
],
277313
diagnoses: [
278-
{ condition: 'Normal Kidney Function', details: 'BUN within normal limits', recommendations: 'Routine follow-up' },
314+
{
315+
condition: 'Normal Kidney Function',
316+
details: 'BUN within normal limits',
317+
recommendations: 'Routine follow-up',
318+
},
279319
],
280320
metadata: {
281321
isMedicalReport: true,
@@ -296,14 +336,22 @@ describe('AwsBedrockService', () => {
296336

297337
it('should accept HEIC/HEIF images from mobile phones', async () => {
298338
const mockMedicalInfo = {
299-
keyMedicalTerms: [
300-
{ term: 'Creatinine', definition: 'Waste product filtered by kidneys' },
301-
],
339+
keyMedicalTerms: [{ term: 'Creatinine', definition: 'Waste product filtered by kidneys' }],
302340
labValues: [
303-
{ name: 'Creatinine', value: '0.9', unit: 'mg/dL', normalRange: '0.7-1.3', isAbnormal: false },
341+
{
342+
name: 'Creatinine',
343+
value: '0.9',
344+
unit: 'mg/dL',
345+
normalRange: '0.7-1.3',
346+
isAbnormal: false,
347+
},
304348
],
305349
diagnoses: [
306-
{ condition: 'Normal Kidney Function', details: 'Creatinine within normal limits', recommendations: 'Routine follow-up' },
350+
{
351+
condition: 'Normal Kidney Function',
352+
details: 'Creatinine within normal limits',
353+
recommendations: 'Routine follow-up',
354+
},
307355
],
308356
metadata: {
309357
isMedicalReport: true,
@@ -327,7 +375,7 @@ describe('AwsBedrockService', () => {
327375
vi.spyOn(service as any, 'invokeBedrock').mockRejectedValueOnce(error);
328376

329377
await expect(service.extractMedicalInfo(mockImageBuffer, 'image/jpeg')).rejects.toThrow(
330-
/Failed to extract medical information from image: Image processing failed/
378+
/Failed to extract medical information from image: Image processing failed/,
331379
);
332380
});
333381

@@ -337,7 +385,7 @@ describe('AwsBedrockService', () => {
337385
});
338386

339387
await expect(service.extractMedicalInfo(mockImageBuffer, 'image/jpeg')).rejects.toThrow(
340-
/Failed to extract medical information from image: Invalid response format/
388+
/Failed to extract medical information from image: Invalid response format/,
341389
);
342390
});
343391
});

0 commit comments

Comments
 (0)