@@ -114,6 +114,39 @@ describe('AwsBedrockService', () => {
114114 let mockConfigService : ConfigService ;
115115 const originalEnv = process . env . NODE_ENV ;
116116
117+ // Define sample text and mock analysis result
118+ const sampleText = `
119+ BLOOD TEST RESULTS
120+ Patient: John Doe
121+ Date: 2023-01-15
122+
123+ Red Blood Cells (RBC): 5.1 x10^6/µL (Normal: 4.5-5.9)
124+ White Blood Cells (WBC): 7.2 x10^3/µL (Normal: 4.5-11.0)
125+ Hemoglobin: 14.2 g/dL (Normal: 13.5-17.5)
126+ ` ;
127+
128+ const mockMedicalAnalysis : MedicalDocumentAnalysis = {
129+ keyMedicalTerms : [
130+ { term : 'RBC' , definition : 'Red Blood Cells' } ,
131+ { term : 'WBC' , definition : 'White Blood Cells' } ,
132+ ] ,
133+ labValues : [
134+ {
135+ name : 'Hemoglobin' ,
136+ value : '14.2' ,
137+ unit : 'g/dL' ,
138+ normalRange : '13.5-17.5' ,
139+ isAbnormal : false ,
140+ } ,
141+ ] ,
142+ diagnoses : [ ] ,
143+ metadata : {
144+ isMedicalReport : true ,
145+ confidence : 0.95 ,
146+ missingInformation : [ ] ,
147+ } ,
148+ } ;
149+
117150 beforeAll ( ( ) => {
118151 process . env . NODE_ENV = 'test' ;
119152 } ) ;
@@ -157,94 +190,97 @@ describe('AwsBedrockService', () => {
157190
158191 describe ( 'analyzeMedicalDocument' , ( ) => {
159192 it ( 'should successfully analyze a valid medical document' , async ( ) => {
160- // Create a sample medical document text
161- const sampleText = `
162- BLOOD TEST RESULTS
163- Patient: John Doe
164- Date: 2023-01-15
165-
166- Red Blood Cells (RBC): 5.1 x10^6/µL (Normal: 4.5-5.9)
167- White Blood Cells (WBC): 7.2 x10^3/µL (Normal: 4.5-11.0)
168- Hemoglobin: 14.2 g/dL (Normal: 13.5-17.5)
169- ` ;
170-
171- // Call the method
172- const result = await service . analyzeMedicalDocument ( sampleText ) ;
173-
174- // Assert the result
175- expect ( result ) . toBeDefined ( ) ;
176- expect ( result . keyMedicalTerms ) . toHaveLength ( 2 ) ;
177- expect ( result . keyMedicalTerms [ 0 ] . term ) . toBe ( 'RBC' ) ;
178- expect ( result . keyMedicalTerms [ 0 ] . definition ) . toBe ( 'Red Blood Cells' ) ;
179-
180- expect ( result . labValues ) . toHaveLength ( 1 ) ;
181- expect ( result . labValues [ 0 ] . name ) . toBe ( 'Hemoglobin' ) ;
182- expect ( result . labValues [ 0 ] . value ) . toBe ( '14.2' ) ;
183- expect ( result . labValues [ 0 ] . unit ) . toBe ( 'g/dL' ) ;
184- expect ( result . labValues [ 0 ] . isAbnormal ) . toBe ( false ) ;
185-
186- expect ( result . metadata . isMedicalReport ) . toBe ( true ) ;
187- expect ( result . metadata . confidence ) . toBeGreaterThan ( 0.9 ) ;
188- } ) ;
193+ // Create mock response
194+ const mockResponse = {
195+ body : Buffer . from (
196+ JSON . stringify ( {
197+ content : [
198+ {
199+ text : JSON . stringify ( mockMedicalAnalysis ) ,
200+ } ,
201+ ] ,
202+ } ) ,
203+ ) ,
204+ } ;
189205
190- it ( 'should correctly format the prompt for medical document analysis' , async ( ) => {
191- // Spy on the invokeBedrock method
192- const invokeBedrockSpy = vi . spyOn ( service as any , 'invokeBedrock' ) ;
206+ // Mock the invokeBedrock method instead of directly setting the client
207+ vi . spyOn ( service as any , 'invokeBedrock' ) . mockResolvedValue ( mockResponse ) ;
193208
194- // Sample document text
195- const sampleText = 'Sample medical document' ;
209+ // Call service with user ID
210+ const mockUserId = 'test-user-123' ;
211+ const result = await service . analyzeMedicalDocument ( sampleText , mockUserId ) ;
196212
197- try {
198- await service . analyzeMedicalDocument ( sampleText ) ;
199- } catch ( error ) {
200- // We don't care about the result, just the prompt format
201- }
213+ // Verify results
214+ expect ( result ) . toEqual ( mockMedicalAnalysis ) ;
202215
203- // Verify invokeBedrock was called
204- expect ( invokeBedrockSpy ) . toHaveBeenCalled ( ) ;
216+ // Verify the invokeBedrock was called with the correct prompt
217+ expect ( service [ 'invokeBedrock' ] ) . toHaveBeenCalled ( ) ;
218+ const prompt = ( service [ 'invokeBedrock' ] as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
219+ expect ( prompt ) . toContain ( 'Please analyze this medical document carefully' ) ;
220+ } ) ;
205221
206- // Verify the prompt format
207- const prompt = invokeBedrockSpy . mock . calls [ 0 ] [ 0 ] as string ;
222+ it ( 'should correctly format the request for Claude models' , async ( ) => {
223+ // Create mock response
224+ const mockResponse = {
225+ body : Buffer . from (
226+ JSON . stringify ( {
227+ content : [ { text : JSON . stringify ( mockMedicalAnalysis ) } ] ,
228+ } ) ,
229+ ) ,
230+ } ;
208231
209- // Check key elements of the prompt
232+ // Mock the invokeBedrock method
233+ vi . spyOn ( service as any , 'invokeBedrock' ) . mockResolvedValue ( mockResponse ) ;
234+
235+ // Call service with user ID
236+ const mockUserId = 'test-user-123' ;
237+ await service . analyzeMedicalDocument ( sampleText , mockUserId ) ;
238+
239+ // Verify the invokeBedrock was called with the correct prompt
240+ expect ( service [ 'invokeBedrock' ] ) . toHaveBeenCalled ( ) ;
241+ const prompt = ( service [ 'invokeBedrock' ] as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
210242 expect ( prompt ) . toContain ( 'Please analyze this medical document carefully' ) ;
211- expect ( prompt ) . toContain ( 'Format the response as a JSON object' ) ;
212- expect ( prompt ) . toContain ( 'keyMedicalTerms' ) ;
213- expect ( prompt ) . toContain ( 'labValues' ) ;
214- expect ( prompt ) . toContain ( 'diagnoses' ) ;
215- expect ( prompt ) . toContain ( 'metadata' ) ;
216- expect ( prompt ) . toContain ( 'Sample medical document' ) ; // Document text is appended
217243 } ) ;
218244
219- it ( 'should throw BadRequestException for invalid JSON response' , async ( ) => {
220- // Create a sample invalid document text
221- const invalidDocument = 'This is an invalid document that will cause an invalid response' ;
245+ it ( 'should throw an error for invalid input' , async ( ) => {
246+ const invalidDocument = '' ;
222247
223- // Expect the method to throw BadRequestException
224- await expect ( service . analyzeMedicalDocument ( invalidDocument ) ) . rejects . toThrow (
248+ // Call with user ID
249+ const mockUserId = 'test-user-123' ;
250+ await expect ( service . analyzeMedicalDocument ( invalidDocument , mockUserId ) ) . rejects . toThrow (
225251 BadRequestException ,
226252 ) ;
227253 } ) ;
228254
229- it ( 'should throw BadRequestException for empty response' , async ( ) => {
230- // Create a sample text that will trigger an empty response
255+ it ( 'should throw error for empty response' , async ( ) => {
256+ // Create mock response with empty content
257+ const mockResponse = {
258+ body : Buffer . from (
259+ JSON . stringify ( {
260+ content : [ ] ,
261+ } ) ,
262+ ) ,
263+ } ;
264+
265+ // Mock the invokeBedrock method
266+ vi . spyOn ( service as any , 'invokeBedrock' ) . mockResolvedValue ( mockResponse ) ;
267+
231268 const emptyResponseText = 'This will trigger an empty response' ;
232269
233- // Expect the method to throw BadRequestException
234- await expect ( service . analyzeMedicalDocument ( emptyResponseText ) ) . rejects . toThrow (
270+ // Call with user ID
271+ const mockUserId = 'test-user-123' ;
272+ await expect ( service . analyzeMedicalDocument ( emptyResponseText , mockUserId ) ) . rejects . toThrow (
235273 BadRequestException ,
236274 ) ;
237275 } ) ;
238276
239- it ( 'should handle rate limiting correctly ' , async ( ) => {
240- // Mock the rate limiter to reject requests
277+ it ( 'should handle rate limiting' , async ( ) => {
278+ // Mock rate limiter to reject the request
241279 service [ 'rateLimiter' ] . tryRequest = vi . fn ( ) . mockReturnValue ( false ) ;
242280
243- // Create a sample medical document text
244- const sampleText = 'Sample medical document text' ;
245-
246- // Expect the method to throw BadRequestException due to rate limiting
247- await expect ( service . analyzeMedicalDocument ( sampleText ) ) . rejects . toThrow (
281+ // Call with user ID
282+ const mockUserId = 'test-user-123' ;
283+ await expect ( service . analyzeMedicalDocument ( sampleText , mockUserId ) ) . rejects . toThrow (
248284 'Rate limit exceeded' ,
249285 ) ;
250286 } ) ;
0 commit comments