diff --git a/packages/headless/src/controllers/knowledge/generated-answer/headless-answerapi-generated-answer.test.ts b/packages/headless/src/controllers/knowledge/generated-answer/headless-answerapi-generated-answer.test.ts index fb7a584a334..298ef4ecbf5 100644 --- a/packages/headless/src/controllers/knowledge/generated-answer/headless-answerapi-generated-answer.test.ts +++ b/packages/headless/src/controllers/knowledge/generated-answer/headless-answerapi-generated-answer.test.ts @@ -31,16 +31,30 @@ vi.mock( '../../../features/generated-answer/generated-answer-analytics-actions' ); vi.mock('../../../features/search/search-actions'); + vi.mock('../../../api/knowledge/stream-answer-api', async () => { const originalStreamAnswerApi = await vi.importActual( '../../../api/knowledge/stream-answer-api' ); + const queryCounter = {count: 0}; + const queries = [ + {q: '', requestId: ''}, + {q: 'this est une question', requestId: '12'}, + {q: 'this est une another question', requestId: '12'}, + {q: '', requestId: '34'}, + {q: 'this est une yet another question', requestId: '56'}, + ]; return { ...originalStreamAnswerApi, fetchAnswer: vi.fn(), selectAnswer: () => ({ data: {answer: 'This est une answer', answerId: '12345_6'}, }), + selectAnswerTriggerParams: () => { + const query = {...queries[queryCounter.count]}; + queryCounter.count++; + return query; + }, }; }); vi.mock('../../../api/knowledge/post-answer-evaluation', () => ({ @@ -54,10 +68,6 @@ vi.mock('../../../api/knowledge/post-answer-evaluation', () => ({ })); describe('knowledge-generated-answer', () => { - it('should be tested', () => { - expect(true).toBe(true); - }); - let engine: MockedSearchEngine; const createGeneratedAnswer = (props: GeneratedAnswerProps = {}) => @@ -175,4 +185,32 @@ describe('knowledge-generated-answer', () => { expectedArgs ); }); + + describe('subscribeToSearchRequest', () => { + it('triggers a fetchAnswer only when there is a request id, a query, and the request is made with another request than the last one', () => { + createGeneratedAnswer(); + + const listener = engine.subscribe.mock.calls[0][0]; + + // no request id, no call + listener(); + expect(fetchAnswer).not.toHaveBeenCalled(); + + // first request id, call + listener(); + expect(fetchAnswer).toHaveBeenCalledTimes(1); + + // same request id, no call + listener(); + expect(fetchAnswer).toHaveBeenCalledTimes(1); + + // empty query, no call + listener(); + expect(fetchAnswer).toHaveBeenCalledTimes(1); + + // new request id, call + listener(); + expect(fetchAnswer).toHaveBeenCalledTimes(2); + }); + }); }); diff --git a/packages/headless/src/controllers/knowledge/generated-answer/headless-answerapi-generated-answer.ts b/packages/headless/src/controllers/knowledge/generated-answer/headless-answerapi-generated-answer.ts index 54bae540828..1fbbb17e8eb 100644 --- a/packages/headless/src/controllers/knowledge/generated-answer/headless-answerapi-generated-answer.ts +++ b/packages/headless/src/controllers/knowledge/generated-answer/headless-answerapi-generated-answer.ts @@ -96,16 +96,24 @@ const subscribeToSearchRequest = ( const strictListener = () => { const state = engine.state; const triggerParams = selectAnswerTriggerParams(state); - if (triggerParams.q.length === 0 || triggerParams.requestId.length === 0) { - return; + + if (!lastTriggerParams || triggerParams.q.length === 0) { + lastTriggerParams = triggerParams; } - if (triggerParams?.requestId === lastTriggerParams?.requestId) { + + if ( + triggerParams.q.length === 0 || + triggerParams.requestId.length === 0 || + triggerParams.requestId === lastTriggerParams.requestId + ) { return; } + lastTriggerParams = triggerParams; engine.dispatch(resetAnswer()); engine.dispatch(fetchAnswer(state)); }; + engine.subscribe(strictListener); };