11import { describe , it , expect , beforeEach , vi } from 'vitest' ;
22import { Retrieval } from '../retrieval.js' ;
33import type { HttpClient } from '../../lib/http-client.js' ;
4+ import type { RetrievalQueryResponse , RetrievalReferencedChunk } from '../../types/retrieval.js' ;
45
56describe ( 'Retrieval Resource' , ( ) => {
67 let retrieval : Retrieval ;
@@ -20,6 +21,8 @@ describe('Retrieval Resource', () => {
2021 namespace : 'support-center' ,
2122 query : 'refund policy' ,
2223 routerUsed : 'discovery+agent' ,
24+ answerText : null ,
25+ referencedChunks : [ ] ,
2326 results : [
2427 {
2528 content : 'Annual plans may be refunded within 30 days.' ,
@@ -95,6 +98,9 @@ describe('Retrieval Resource', () => {
9598 mockHttpClient . post . mockResolvedValue ( {
9699 namespace : 'default' ,
97100 query : 'refund policy' ,
101+ routerUsed : 'small_corpus_all' ,
102+ answerText : null ,
103+ referencedChunks : [ ] ,
98104 results : [ ] ,
99105 } ) ;
100106
@@ -111,7 +117,17 @@ describe('Retrieval Resource', () => {
111117 query : 'test' ,
112118 routerUsed : 'workflow_single_step' ,
113119 answerText : 'Generated answer' ,
114- referencedChunks : [ { chunkId : 'chunk-1' , assetUrl : 'https://example.com' } ] ,
120+ referencedChunks : [
121+ {
122+ chunkId : 'chunk-1' ,
123+ documentId : 'doc-1' ,
124+ chunkType : 'image' ,
125+ sectionPath : 'Images' ,
126+ filePath : 'images/chunk-1.png' ,
127+ jobId : 'job-1' ,
128+ assetUrl : 'https://example.com' ,
129+ } ,
130+ ] ,
115131 results : [ ] ,
116132 } ) ;
117133
@@ -129,30 +145,50 @@ describe('Retrieval Resource', () => {
129145 query : 'test' ,
130146 routerUsed : 'workflow_single_step' ,
131147 answerText : 'LLM-generated answer' ,
148+ evidenceText : 'Rendered retrieval evidence' ,
149+ stopReason : 'answer_done' ,
150+ failureReason : 'insufficient evidence' ,
132151 referencedChunks : [
133- { chunkId : 'chunk-1' , documentId : 'doc-1' , assetUrl : 'https://example.com/1' } ,
152+ {
153+ chunkId : 'chunk-1' ,
154+ documentId : 'doc-1' ,
155+ chunkType : 'text' ,
156+ sectionPath : 'Root' ,
157+ filePath : null ,
158+ jobId : 'job-1' ,
159+ assetUrl : 'https://example.com/1' ,
160+ } ,
134161 ] ,
135162 results : [ ] ,
136163 } ) ;
137164
138165 const response = await retrieval . query ( { query : 'test' , useAgentic : true } ) ;
166+ const typedResponse : RetrievalQueryResponse = response ;
167+ const referencedChunk : RetrievalReferencedChunk | undefined = response . referencedChunks [ 0 ] ;
139168
140- expect ( response . answerText ) . toBe ( 'LLM-generated answer' ) ;
169+ expect ( typedResponse . answerText ) . toBe ( 'LLM-generated answer' ) ;
170+ expect ( response . evidenceText ) . toBe ( 'Rendered retrieval evidence' ) ;
171+ expect ( response . stopReason ) . toBe ( 'answer_done' ) ;
172+ expect ( response . failureReason ) . toBe ( 'insufficient evidence' ) ;
141173 expect ( response . referencedChunks ) . toHaveLength ( 1 ) ;
142- expect ( response . referencedChunks ?. [ 0 ] ?. chunkId ) . toBe ( 'chunk-1' ) ;
174+ expect ( referencedChunk ?. chunkId ) . toBe ( 'chunk-1' ) ;
175+ expect ( referencedChunk ?. filePath ) . toBeNull ( ) ;
143176 } ) ;
144177
145178 it ( 'should handle legacy response without agentic fields' , async ( ) => {
146179 mockHttpClient . post . mockResolvedValue ( {
147180 namespace : 'default' ,
148181 query : 'refund policy' ,
182+ routerUsed : 'small_corpus_all' ,
183+ answerText : null ,
184+ referencedChunks : [ ] ,
149185 results : [ ] ,
150186 } ) ;
151187
152188 const response = await retrieval . query ( { query : 'refund policy' } ) ;
153189
154- expect ( response . answerText ) . toBeUndefined ( ) ;
155- expect ( response . referencedChunks ) . toBeUndefined ( ) ;
190+ expect ( response . answerText ) . toBeNull ( ) ;
191+ expect ( response . referencedChunks ) . toEqual ( [ ] ) ;
156192 expect ( response . results ) . toEqual ( [ ] ) ;
157193 } ) ;
158194
0 commit comments