2023-10-29 15:36:00 -04:00
let addImages = require ( './addImages' ) ;
describe ( 'addImages' , ( ) => {
let intermediateSteps ;
let responseMessage ;
let options ;
beforeEach ( ( ) => {
intermediateSteps = [ ] ;
responseMessage = { text : '' } ;
options = { debug : false } ;
this . options = options ;
addImages = addImages . bind ( this ) ;
} ) ;
it ( 'should handle null or undefined parameters' , ( ) => {
addImages ( null , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '' ) ;
addImages ( intermediateSteps , null ) ;
expect ( responseMessage . text ) . toBe ( '' ) ;
addImages ( null , null ) ;
expect ( responseMessage . text ) . toBe ( '' ) ;
} ) ;
it ( 'should append correct image markdown if not present in responseMessage' , ( ) => {
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n' ) ;
} ) ;
it ( 'should not append image markdown if already present in responseMessage' , ( ) => {
responseMessage . text = '' ;
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '' ) ;
} ) ;
it ( 'should correct and append image markdown with erroneous URL' , ( ) => {
responseMessage . text = '' ;
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '' ) ;
} ) ;
it ( 'should correct multiple erroneous URLs in responseMessage' , ( ) => {
responseMessage . text =
' ' ;
intermediateSteps . push ( { observation : '' } ) ;
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( ' ' ) ;
} ) ;
it ( 'should not append non-image markdown observations' , ( ) => {
intermediateSteps . push ( { observation : '[desc](/images/test.png)' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '' ) ;
} ) ;
it ( 'should handle multiple observations' , ( ) => {
intermediateSteps . push ( { observation : '' } ) ;
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n\n' ) ;
} ) ;
it ( 'should not append if observation does not contain image markdown' , ( ) => {
intermediateSteps . push ( { observation : 'This is a test observation without image markdown.' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '' ) ;
} ) ;
it ( 'should append correctly from a real scenario' , ( ) => {
responseMessage . text =
2025-11-21 11:25:14 -05:00
"Here is the generated image based on your request. It depicts a surreal landscape filled with floating musical notes. The style is impressionistic, with vibrant sunset hues dominating the scene. At the center, there's a silhouette of a grand piano, adding a dreamy emotion to the overall image. This could serve as a unique and creative music album cover. Would you like to make any changes or generate another image?" ;
2023-10-29 15:36:00 -04:00
const originalText = responseMessage . text ;
const imageMarkdown = '' ;
intermediateSteps . push ( { observation : imageMarkdown } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( ` ${ originalText } \n ${ imageMarkdown } ` ) ;
} ) ;
2024-08-04 20:53:11 -04:00
it ( 'should extract only image markdowns when there is text between them' , ( ) => {
const markdownWithTextBetweenImages = `
! [ image1 ] ( / i m a g e s / i m a g e 1 . p n g )
Some text between images that should not be included .
! [ image2 ] ( / i m a g e s / i m a g e 2 . p n g )
More text that should be ignored .
! [ image3 ] ( / i m a g e s / i m a g e 3 . p n g )
` ;
intermediateSteps . push ( { observation : markdownWithTextBetweenImages } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n' ) ;
} ) ;
it ( 'should only return the first image when multiple images are present' , ( ) => {
const markdownWithMultipleImages = `
! [ image1 ] ( / i m a g e s / i m a g e 1 . p n g )
! [ image2 ] ( / i m a g e s / i m a g e 2 . p n g )
! [ image3 ] ( / i m a g e s / i m a g e 3 . p n g )
` ;
intermediateSteps . push ( { observation : markdownWithMultipleImages } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n' ) ;
} ) ;
it ( 'should not include any text or metadata surrounding the image markdown' , ( ) => {
const markdownWithMetadata = `
Title : Test Document
Author : John Doe
! [ image1 ] ( / i m a g e s / i m a g e 1 . p n g )
Some content after the image .
Vector values : [ 0.1 , 0.2 , 0.3 ]
` ;
intermediateSteps . push ( { observation : markdownWithMetadata } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n' ) ;
} ) ;
it ( 'should handle complex markdown with multiple images and only return the first one' , ( ) => {
const complexMarkdown = `
# Document Title
# # Section 1
Here ' s some text with an embedded image :
! [ image1 ] ( / i m a g e s / i m a g e 1 . p n g )
# # Section 2
More text here ...
! [ image2 ] ( / i m a g e s / i m a g e 2 . p n g )
# # # Subsection
Even more content
! [ image3 ] ( / i m a g e s / i m a g e 3 . p n g )
` ;
intermediateSteps . push ( { observation : complexMarkdown } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n' ) ;
} ) ;
2025-11-21 11:25:14 -05:00
describe ( 'basePath functionality' , ( ) => {
let originalDomainClient ;
beforeEach ( ( ) => {
originalDomainClient = process . env . DOMAIN _CLIENT ;
} ) ;
afterEach ( ( ) => {
process . env . DOMAIN _CLIENT = originalDomainClient ;
} ) ;
it ( 'should prepend base path to image URLs when DOMAIN_CLIENT is set' , ( ) => {
process . env . DOMAIN _CLIENT = 'http://localhost:3080/librechat' ;
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n' ) ;
} ) ;
it ( 'should not prepend base path when image URL already has base path' , ( ) => {
process . env . DOMAIN _CLIENT = 'http://localhost:3080/librechat' ;
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n' ) ;
} ) ;
it ( 'should correct erroneous URLs with base path' , ( ) => {
process . env . DOMAIN _CLIENT = 'http://localhost:3080/librechat' ;
responseMessage . text = '' ;
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '' ) ;
} ) ;
it ( 'should handle empty base path (root deployment)' , ( ) => {
process . env . DOMAIN _CLIENT = 'http://localhost:3080/' ;
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n' ) ;
} ) ;
it ( 'should handle missing DOMAIN_CLIENT' , ( ) => {
delete process . env . DOMAIN _CLIENT ;
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n' ) ;
} ) ;
it ( 'should handle observation without image path match' , ( ) => {
process . env . DOMAIN _CLIENT = 'http://localhost:3080/librechat' ;
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n' ) ;
} ) ;
it ( 'should handle nested subdirectories in base path' , ( ) => {
process . env . DOMAIN _CLIENT = 'http://localhost:3080/apps/librechat' ;
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n' ) ;
} ) ;
it ( 'should handle multiple observations with mixed base path scenarios' , ( ) => {
process . env . DOMAIN _CLIENT = 'http://localhost:3080/librechat' ;
intermediateSteps . push ( { observation : '' } ) ;
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe (
'\n\n' ,
) ;
} ) ;
it ( 'should handle complex markdown with base path' , ( ) => {
process . env . DOMAIN _CLIENT = 'http://localhost:3080/librechat' ;
const complexMarkdown = `
# Document Title
! [ image1 ] ( / i m a g e s / i m a g e 1 . p n g )
Some text between images
! [ image2 ] ( / i m a g e s / i m a g e 2 . p n g )
` ;
intermediateSteps . push ( { observation : complexMarkdown } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n' ) ;
} ) ;
it ( 'should handle URLs that are already absolute' , ( ) => {
process . env . DOMAIN _CLIENT = 'http://localhost:3080/librechat' ;
intermediateSteps . push ( { observation : '' } ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe ( '\n' ) ;
} ) ;
it ( 'should handle data URLs' , ( ) => {
process . env . DOMAIN _CLIENT = 'http://localhost:3080/librechat' ;
intermediateSteps . push ( {
observation :
'' ,
} ) ;
addImages ( intermediateSteps , responseMessage ) ;
expect ( responseMessage . text ) . toBe (
'\n' ,
) ;
} ) ;
} ) ;
2023-10-29 15:36:00 -04:00
} ) ;