@@ -33,37 +33,34 @@ describe('writeIndexRecords', () => {
3333
3434 const result = await writeIndexRecords ( 'test-index' , records , '/tmp/out' )
3535
36- expect ( result ) . toBe ( '/tmp/out/test-index-records.json' )
36+ expect ( result . filePath ) . toBe ( '/tmp/out/test-index-records.json' )
37+ expect ( result . skippedRecords ) . toEqual ( [ ] )
3738 expect ( fs . writeFile ) . toHaveBeenCalledOnce ( )
3839 const writtenJson = vi . mocked ( fs . writeFile ) . mock . calls [ 0 ] [ 1 ] as string
3940 const parsed = JSON . parse ( writtenJson )
4041 expect ( Object . keys ( parsed ) ) . toEqual ( [ '/en/test-page' , '/en/other-page' ] )
4142 } )
4243
43- test ( 'filters out records with empty titles' , async ( ) => {
44- const warnSpy = vi . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } )
44+ test ( 'filters out records with empty titles and returns them as skipped' , async ( ) => {
4545 const records = [ makeRecord ( ) , makeRecord ( { objectID : '/en/bad-page' , title : '' } ) ]
4646
47- await writeIndexRecords ( 'test-index' , records , '/tmp/out' )
47+ const result = await writeIndexRecords ( 'test-index' , records , '/tmp/out' )
4848
4949 const writtenJson = vi . mocked ( fs . writeFile ) . mock . calls [ 0 ] [ 1 ] as string
5050 const parsed = JSON . parse ( writtenJson )
5151 expect ( Object . keys ( parsed ) ) . toEqual ( [ '/en/test-page' ] )
52- expect ( warnSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( 'empty title' ) )
53- warnSpy . mockRestore ( )
52+ expect ( result . skippedRecords ) . toEqual ( [ { objectID : '/en/bad-page' , reason : 'empty title' } ] )
5453 } )
5554
56- test ( 'filters out records with missing objectID' , async ( ) => {
57- const warnSpy = vi . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } )
55+ test ( 'filters out records with missing objectID and returns them as skipped' , async ( ) => {
5856 const records = [ makeRecord ( ) , makeRecord ( { objectID : '' , title : 'No ID' } ) ]
5957
60- await writeIndexRecords ( 'test-index' , records , '/tmp/out' )
58+ const result = await writeIndexRecords ( 'test-index' , records , '/tmp/out' )
6159
6260 const writtenJson = vi . mocked ( fs . writeFile ) . mock . calls [ 0 ] [ 1 ] as string
6361 const parsed = JSON . parse ( writtenJson )
6462 expect ( Object . keys ( parsed ) ) . toEqual ( [ '/en/test-page' ] )
65- expect ( warnSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( 'invalid objectID' ) )
66- warnSpy . mockRestore ( )
63+ expect ( result . skippedRecords ) . toEqual ( [ { objectID : '(unknown)' , reason : 'invalid objectID' } ] )
6764 } )
6865
6966 test ( 'deduplicates records with the same objectID' , async ( ) => {
@@ -82,34 +79,22 @@ describe('writeIndexRecords', () => {
8279 warnSpy . mockRestore ( )
8380 } )
8481
85- test ( 'does not log full record content for invalid objectID' , async ( ) => {
86- const warnSpy = vi . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } )
87- const records = [
88- makeRecord ( ) ,
89- makeRecord ( {
90- objectID : '' ,
91- title : 'No ID' ,
92- content : 'A very long content string that should not appear in logs' ,
93- } ) ,
94- ]
82+ test ( 'returns empty filePath and skipped record when name is empty' , async ( ) => {
83+ const result = await writeIndexRecords ( '' , [ makeRecord ( ) ] , '/tmp/out' )
9584
96- await writeIndexRecords ( 'test-index' , records , '/tmp/out' )
97-
98- const warnMessage = warnSpy . mock . calls [ 0 ] [ 0 ] as string
99- expect ( warnMessage ) . not . toContain ( 'very long content string' )
100- warnSpy . mockRestore ( )
85+ expect ( result . filePath ) . toBe ( '' )
86+ expect ( result . skippedRecords ) . toEqual ( [ { objectID : '(unknown)' , reason : 'name is required' } ] )
87+ expect ( fs . writeFile ) . not . toHaveBeenCalled ( )
10188 } )
10289
103- test ( 'throws when name is empty' , async ( ) => {
104- await expect ( writeIndexRecords ( '' , [ makeRecord ( ) ] , '/tmp/out' ) ) . rejects . toThrow (
105- '`name` is required' ,
106- )
107- } )
90+ test ( 'returns empty filePath and skipped record when records array is empty' , async ( ) => {
91+ const result = await writeIndexRecords ( 'test-index' , [ ] , '/tmp/out' )
10892
109- test ( 'throws when records array is empty' , async ( ) => {
110- await expect ( writeIndexRecords ( 'test-index' , [ ] , '/tmp/out' ) ) . rejects . toThrow (
111- '`records` must be a non-empty array' ,
112- )
93+ expect ( result . filePath ) . toBe ( '' )
94+ expect ( result . skippedRecords ) . toEqual ( [
95+ { objectID : '(unknown)' , reason : 'records array is empty' } ,
96+ ] )
97+ expect ( fs . writeFile ) . not . toHaveBeenCalled ( )
11398 } )
11499
115100 test ( 'creates output directory if it does not exist' , async ( ) => {
0 commit comments