@ -3,6 +3,7 @@ import {
buildSidePanelProps ,
hasAskAi ,
hasKeywordSearch ,
mergeLangFilters ,
mergeLangFacetFilters ,
validateCredentials
} from 'client/theme-default/support/docsearch'
@ -41,13 +42,55 @@ describe('client/theme-default/support/docsearch', () => {
} )
} )
describe ( 'mergeLangFilters' , ( ) = > {
test ( 'adds a lang filter when none is provided' , ( ) = > {
expect ( mergeLangFilters ( undefined , 'en' ) ) . toBe ( 'lang:en' )
} )
test ( 'replaces a lang filter in an AND expression' , ( ) = > {
expect (
mergeLangFilters ( 'type:lvl AND lang:en-US AND content:*' , 'en' )
) . toBe ( '(type:lvl AND lang:en AND content:*) AND lang:en' )
} )
test ( 'replaces a lang filter in an OR group' , ( ) = > {
expect (
mergeLangFilters ( '(type:lvl OR lang:en-US) AND content:*' , 'en' )
) . toBe ( '((type:lvl OR lang:en) AND content:*) AND lang:en' )
} )
test ( 'preserves top-level OR precedence' , ( ) = > {
expect ( mergeLangFilters ( 'tag:a OR tag:b' , 'en' ) ) . toBe (
'(tag:a OR tag:b) AND lang:en'
)
} )
test ( 'preserves nested groups containing only lang filters' , ( ) = > {
expect ( mergeLangFilters ( 'type:a AND (lang:en OR lang:fr)' , 'de' ) ) . toBe (
'(type:a AND (lang:de OR lang:de)) AND lang:de'
)
} )
test ( 'preserves negated lang filters' , ( ) = > {
expect ( mergeLangFilters ( 'tag:a AND NOT lang:fr' , 'en' ) ) . toBe (
'(tag:a AND NOT lang:fr) AND lang:en'
)
} )
test ( 'normalizes quoted lang filter values' , ( ) = > {
expect ( mergeLangFilters ( 'lang:"en US" AND tag:a' , 'en' ) ) . toBe (
'(lang:en AND tag:a) AND lang:en'
)
} )
} )
describe ( 'hasKeywordSearch' , ( ) = > {
test ( 'returns true when all credentials are provided' , ( ) = > {
expect (
hasKeywordSearch ( {
appId : 'app' ,
apiKey : 'key' ,
indexName : 'index'
ind ices: [ 'index' ]
} )
) . toBe ( true )
} )
@ -57,41 +100,48 @@ describe('client/theme-default/support/docsearch', () => {
hasKeywordSearch ( {
appId : undefined ,
apiKey : 'key' ,
ind exName: 'index'
ind ices: [ 'index' ]
} )
) . toBe ( false )
expect (
hasKeywordSearch ( {
appId : 'app' ,
apiKey : undefined ,
indexName : 'index'
indices : [ 'index' ]
} )
) . toBe ( false )
expect (
hasKeywordSearch ( {
appId : 'app' ,
apiKey : 'key' ,
indices : undefined
} )
) . toBe ( false )
expect (
hasKeywordSearch ( {
appId : 'app' ,
apiKey : 'key' ,
indexName : undefined
ind ices: [ ]
} )
) . toBe ( false )
} )
} )
describe ( 'hasAskAi' , ( ) = > {
test ( 'returns true for valid string a ssista ntId', ( ) = > {
expect ( hasAskAi ( 'a ssista nt123') ) . toBe ( true )
test ( 'returns true for valid string a ge ntId', ( ) = > {
expect ( hasAskAi ( 'a ge nt123') ) . toBe ( true )
} )
test ( 'returns false for empty string a ssista ntId', ( ) = > {
test ( 'returns false for empty string a ge ntId', ( ) = > {
expect ( hasAskAi ( '' ) ) . toBe ( false )
} )
test ( 'returns true for object with a ssista ntId', ( ) = > {
expect ( hasAskAi ( { a ssistantId: 'assista nt123' } as any ) ) . toBe ( true )
test ( 'returns true for object with a ge ntId', ( ) = > {
expect ( hasAskAi ( { a gentId: 'age nt123' } as any ) ) . toBe ( true )
} )
test ( 'returns false for object without a ssista ntId', ( ) = > {
expect ( hasAskAi ( { a ssista ntId: null } as any ) ) . toBe ( false )
test ( 'returns false for object without a ge ntId', ( ) = > {
expect ( hasAskAi ( { a ge ntId: null } as any ) ) . toBe ( false )
expect ( hasAskAi ( { } as any ) ) . toBe ( false )
} )
@ -105,12 +155,12 @@ describe('client/theme-default/support/docsearch', () => {
const result = validateCredentials ( {
appId : 'app' ,
apiKey : 'key' ,
ind exName: 'index'
ind ices: [ 'index' ]
} )
expect ( result . valid ) . toBe ( true )
expect ( result . appId ) . toBe ( 'app' )
expect ( result . apiKey ) . toBe ( 'key' )
expect ( result . ind exName) . toBe ( 'index' )
expect ( result . ind ices) . toEqual ( [ 'index' ] )
} )
test ( 'invalidates incomplete credentials' , ( ) = > {
@ -118,87 +168,140 @@ describe('client/theme-default/support/docsearch', () => {
validateCredentials ( {
appId : undefined ,
apiKey : 'key' ,
ind exName: 'index'
ind ices: [ 'index' ]
} ) . valid
) . toBe ( false )
} )
} )
describe ( 'buildAskAiConfig' , ( ) = > {
test ( 'builds config from string a ssista ntId', ( ) = > {
test ( 'builds config from string a ge ntId', ( ) = > {
const result = buildAskAiConfig (
'a ssista nt123',
'a ge nt123',
{
appId : 'app' ,
apiKey : 'key' ,
ind exName: 'index'
ind ices: [ 'index' ]
} as any ,
'en'
)
expect ( result . a ssistantId) . toBe ( 'assista nt123')
expect ( result . a gentId) . toBe ( 'age nt123')
expect ( result . appId ) . toBe ( 'app' )
expect ( result . apiKey ) . toBe ( 'key' )
expect ( result . ind exName) . toBe ( 'index' )
expect ( result . ind ices) . toBeUndefined ( )
} )
test ( 'builds config from object with overrides' , ( ) = > {
const result = buildAskAiConfig (
{
a ssistantId: 'assista nt123',
a gentId: 'age nt123',
appId : 'custom-app' ,
apiKey : 'custom-key' ,
indexName : 'custom-index'
apiKey : 'custom-key'
} as any ,
{
appId : 'default-app' ,
apiKey : 'default-key' ,
ind exName: 'default-index'
ind ices: [ 'default-index' ]
} as any ,
'en'
)
expect ( result . a ssistantId) . toBe ( 'assista nt123')
expect ( result . a gentId) . toBe ( 'age nt123')
expect ( result . appId ) . toBe ( 'custom-app' )
expect ( result . apiKey ) . toBe ( 'custom-key' )
expect ( result . ind exName) . toBe ( 'custom-index' )
expect ( result . ind ices) . toBeUndefined ( )
} )
test ( 'merges f acet f ilters with lang', ( ) = > {
test ( 'merges f ilters with lang by index ', ( ) = > {
const result = buildAskAiConfig (
{
assistantId : 'assistant123' ,
agentId : 'agent123' ,
indices : [ 'ai_index' ] ,
searchParameters : {
facetFilters : [ 'tag:docs' ]
ai_index : {
filters : 'tag:docs'
}
} as any ,
}
} ,
{
appId : 'app' ,
apiKey : 'key' ,
ind exName: 'index'
} as any ,
ind ices: [ 'index' ]
} ,
'en'
)
expect ( result . searchParameters ? . facetFilters ) . toContain ( 'tag:docs' )
expect ( result . searchParameters ? . facetFilters ) . toContain ( 'lang:en' )
expect ( result . searchParameters ? . ai_index . filters ) . toBe (
'(tag:docs) AND lang:en'
)
} )
test ( 'a lways adds lang facet filter to searchP arameters', ( ) = > {
test ( 'a dds lang filters for indices without search p arameters', ( ) = > {
const result = buildAskAiConfig (
'assistant123' ,
{
agentId : 'agent123' ,
indices : [ 'ai_index' ]
} ,
{
appId : 'app' ,
apiKey : 'key' ,
indexName : 'index'
indices : [ 'index' ]
} ,
'en'
)
expect ( result . searchParameters ) . toEqual ( {
ai_index : {
filters : 'lang:en'
}
} )
} )
test ( 'merges configured indices with search parameter indices' , ( ) = > {
const result = buildAskAiConfig (
{
agentId : 'agent123' ,
indices : [ 'configured_index' ] ,
searchParameters : {
parameter_index : {
distinct : false
}
}
} ,
{
appId : 'app' ,
apiKey : 'key' ,
indices : [ 'index' ]
} ,
'en'
)
expect ( result . searchParameters ) . toEqual ( {
configured_index : {
filters : 'lang:en'
} ,
parameter_index : {
distinct : false ,
filters : 'lang:en'
}
} )
} )
test ( 'does not create index-specific search parameters for string config' , ( ) = > {
const result = buildAskAiConfig (
'agent123' ,
{
appId : 'app' ,
apiKey : 'key' ,
indices : [ 'index' ]
} as any ,
'en'
)
expect ( result . searchParameters ? . facetFilters ) . toEqual ( [ 'lang:en' ] )
expect ( result . searchParameters ) . toBeUndefined ( )
} )
test ( 'preserves Agent Studio search parameters by index' , ( ) = > {
test ( ' adds lang filters t o search parameters by index', ( ) = > {
const result = buildAskAiConfig (
{
assistantId : 'assistant123' ,
agentStudio : true ,
agentId : 'agent123' ,
searchParameters : {
index : {
distinct : false
@ -208,7 +311,7 @@ describe('client/theme-default/support/docsearch', () => {
{
appId : 'app' ,
apiKey : 'key' ,
ind exName: 'index' ,
ind ices: [ 'index' ] ,
searchParameters : {
facetFilters : [ 'tag:docs' ]
}
@ -218,7 +321,8 @@ describe('client/theme-default/support/docsearch', () => {
expect ( result . searchParameters ) . toEqual ( {
index : {
distinct : false
distinct : false ,
filters : 'lang:en'
}
} )
expect ( result . searchParameters ) . not . toHaveProperty ( 'facetFilters' )
@ -227,13 +331,12 @@ describe('client/theme-default/support/docsearch', () => {
test ( 'does not add legacy facet filters to Agent Studio config' , ( ) = > {
const result = buildAskAiConfig (
{
assistantId : 'assistant123' ,
agentStudio : true
agentId : 'agent123'
} as any ,
{
appId : 'app' ,
apiKey : 'key' ,
ind exName: 'index' ,
ind ices: [ 'index' ] ,
searchParameters : {
facetFilters : [ 'tag:docs' ]
}
@ -249,15 +352,13 @@ describe('client/theme-default/support/docsearch', () => {
test ( 'passes resolved Ask AI options to the side panel' , ( ) = > {
const result = buildSidePanelProps (
{
assistantId : 'assistant123' ,
agentStudio : true ,
agentId : 'agent123' ,
searchParameters : {
index : {
facetFilters : [ 'lang:en' ]
}
} ,
suggestedQuestions : true ,
useStagingEnv : true ,
sidePanel : {
button : {
variant : 'inline'
@ -271,7 +372,7 @@ describe('client/theme-default/support/docsearch', () => {
{
appId : 'app' ,
apiKey : 'key' ,
ind exName: 'index'
ind ices: [ 'index' ]
} as any
)
@ -279,16 +380,13 @@ describe('client/theme-default/support/docsearch', () => {
container : '#vp-docsearch-sidepanel' ,
appId : 'app' ,
apiKey : 'key' ,
indexName : 'index' ,
assistantId : 'assistant123' ,
agentStudio : true ,
agentId : 'agent123' ,
searchParameters : {
index : {
facetFilters : [ 'lang:en' ]
}
} ,
suggestedQuestions : true ,
useStagingEnv : true ,
button : {
variant : 'inline'
} ,