@@ -24,8 +24,22 @@ import {
2424 getTimestamp ,
2525 getTimestampVariablesString ,
2626 splitJsonStrings ,
27+ transformDocumentsToSources ,
2728} from '../lightspeed-chatbox-utils' ;
2829
30+ const referenced_documents = [
31+ {
32+ doc_title : 'About Red Hat Developer Hub' ,
33+ doc_url :
34+ 'https://docs.redhat.com/en/documentation/red_hat_developer_hub/1.6/html-single/about_red_hat_developer_hub/index' ,
35+ } ,
36+ {
37+ doc_title : 'Adoption Insights in Red Hat Developer Hub' ,
38+ doc_url :
39+ 'https://docs.redhat.com/en/documentation/red_hat_developer_hub/1.6/html-single/adoption_insights_in_red_hat_developer_hub/index' ,
40+ } ,
41+ ] ;
42+
2943describe ( 'getTimestampVariablesString' , ( ) => {
3044 it ( 'should add a leading zero if the number is less than 10' , ( ) => {
3145 expect ( getTimestampVariablesString ( 5 ) ) . toBe ( '05' ) ;
@@ -203,6 +217,38 @@ describe('createBotMessage', () => {
203217 timestamp : '2024-10-30T14:00:00Z' ,
204218 } ) ;
205219 } ) ;
220+
221+ it ( 'should create a bot message with source links' , ( ) => {
222+ const message = createBotMessage ( {
223+ name : 'BotMaster' ,
224+ avatar : 'bot-avatar.png' ,
225+ content : 'Bot message content' ,
226+ timestamp : '2024-10-30T14:00:00Z' ,
227+ isLoading : true ,
228+ sources : transformDocumentsToSources ( referenced_documents ) ,
229+ } ) ;
230+
231+ expect ( message ?. sources ?. sources ) . toHaveLength ( 2 ) ;
232+ expect ( message ) . toEqual (
233+ expect . objectContaining ( {
234+ role : 'bot' ,
235+ name : 'BotMaster' ,
236+ avatar : 'bot-avatar.png' ,
237+ isLoading : true ,
238+ content : 'Bot message content' ,
239+ timestamp : '2024-10-30T14:00:00Z' ,
240+ sources : {
241+ sources : expect . arrayContaining ( [
242+ expect . objectContaining ( {
243+ isExternal : true ,
244+ link : expect . anything ( ) ,
245+ title : expect . anything ( ) ,
246+ } ) ,
247+ ] ) ,
248+ } ,
249+ } ) ,
250+ ) ;
251+ } ) ;
206252} ) ;
207253
208254describe ( 'getMessageData' , ( ) => {
@@ -223,6 +269,7 @@ describe('getMessageData', () => {
223269 content : 'This is the message content' ,
224270 model : 'granite3-dense:8b' ,
225271 timestamp : '15/04/2025, 05:39:08' ,
272+ referencedDocuments : [ ] ,
226273 } ) ;
227274 } ) ;
228275
@@ -232,10 +279,52 @@ describe('getMessageData', () => {
232279 expect ( result ) . toEqual ( {
233280 content : '' ,
234281 timestamp : '' ,
282+ model : undefined ,
283+ referencedDocuments : [ ] ,
284+ } ) ;
285+ } ) ;
286+
287+ it ( 'should return referenced_documents as sources from additional_kwargs' , ( ) => {
288+ const message = {
289+ additional_kwargs : {
290+ referenced_documents,
291+ } ,
292+ } ;
293+ const result = getMessageData ( message as any ) ;
294+ expect ( result ) . toEqual ( {
295+ content : '' ,
296+ timestamp : '' ,
297+ model : undefined ,
298+ referencedDocuments : referenced_documents ,
235299 } ) ;
236300 } ) ;
237301} ) ;
238302
303+ describe ( 'transformDocumentsToSources' , ( ) => {
304+ it ( 'should return undefined if invalid values are passed to referenced_documents' , ( ) => {
305+ expect (
306+ transformDocumentsToSources ( undefined as unknown as any ) ,
307+ ) . toBeUndefined ( ) ;
308+ expect ( transformDocumentsToSources ( null as unknown as any ) ) . toBeUndefined ( ) ;
309+ expect ( transformDocumentsToSources ( [ ] as unknown as any ) ) . toBeUndefined ( ) ;
310+ } ) ;
311+
312+ it ( 'should transform referenced documents into message sources' , ( ) => {
313+ const sources = transformDocumentsToSources ( referenced_documents ) ;
314+ expect ( sources ?. sources ) . toHaveLength ( 2 ) ;
315+ expect ( sources ) . toEqual (
316+ expect . objectContaining ( {
317+ sources : expect . arrayContaining ( [
318+ expect . objectContaining ( {
319+ isExternal : true ,
320+ link : expect . anything ( ) ,
321+ title : expect . anything ( ) ,
322+ } ) ,
323+ ] ) ,
324+ } ) ,
325+ ) ;
326+ } ) ;
327+ } ) ;
239328describe ( 'getCategorizeMessages' , ( ) => {
240329 const addProps = ( c : ConversationSummary ) => ( {
241330 customProp : `prop-${ c . conversation_id } ` ,
0 commit comments