|
1 | | -import { describe, it, expect } from 'vitest'; |
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import * as loggerModule from '../logger.js'; |
2 | 3 | import { reassembleByDocument } from './reassemble-documents.js'; |
3 | 4 | import type { SearchResult } from '../types.js'; |
4 | 5 |
|
5 | 6 | describe('reassembleByDocument', () => { |
| 7 | + let warnSpy: ReturnType<typeof vi.spyOn>; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + warnSpy = vi.spyOn(loggerModule, 'warn').mockImplementation(() => {}); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + warnSpy.mockRestore(); |
| 15 | + }); |
| 16 | + |
6 | 17 | it('groups chunks by document_number', () => { |
7 | 18 | const results: SearchResult[] = [ |
8 | 19 | { |
@@ -85,4 +96,102 @@ describe('reassembleByDocument', () => { |
85 | 96 | expect(docs[0].chunk_count).toBe(3); |
86 | 97 | expect(docs[0].merged_content).toBe('Chunk 0\n\nChunk 1\n\nChunk 2'); |
87 | 98 | }); |
| 99 | + |
| 100 | + it('does not warn when all hits have a document key', () => { |
| 101 | + const results: SearchResult[] = [ |
| 102 | + { |
| 103 | + id: 'c1', |
| 104 | + content: 'Only doc.', |
| 105 | + score: 0.9, |
| 106 | + metadata: { document_number: 'P1' }, |
| 107 | + reranked: false, |
| 108 | + }, |
| 109 | + ]; |
| 110 | + reassembleByDocument(results); |
| 111 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('warns once with count when hits lack document key and empty vector id', () => { |
| 115 | + const results: SearchResult[] = [ |
| 116 | + { |
| 117 | + id: '', |
| 118 | + content: 'Orphan chunk.', |
| 119 | + score: 0.5, |
| 120 | + metadata: {}, |
| 121 | + reranked: false, |
| 122 | + }, |
| 123 | + ]; |
| 124 | + const docs = reassembleByDocument(results); |
| 125 | + expect(docs).toHaveLength(0); |
| 126 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 127 | + expect(warnSpy.mock.calls[0]?.[0]).toMatch(/skipped 1 hit/); |
| 128 | + expect(warnSpy.mock.calls[0]?.[0]).toMatch(/sample_ids=<empty>/); |
| 129 | + }); |
| 130 | + |
| 131 | + it('aggregates multiple skipped hits into one warn with total count', () => { |
| 132 | + const results: SearchResult[] = [ |
| 133 | + { |
| 134 | + id: '', |
| 135 | + content: 'A', |
| 136 | + score: 0.5, |
| 137 | + metadata: {}, |
| 138 | + reranked: false, |
| 139 | + }, |
| 140 | + { |
| 141 | + id: '', |
| 142 | + content: 'B', |
| 143 | + score: 0.4, |
| 144 | + metadata: {}, |
| 145 | + reranked: false, |
| 146 | + }, |
| 147 | + ]; |
| 148 | + const docs = reassembleByDocument(results); |
| 149 | + expect(docs).toHaveLength(0); |
| 150 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 151 | + expect(warnSpy.mock.calls[0]?.[0]).toMatch(/skipped 2 hit/); |
| 152 | + }); |
| 153 | + |
| 154 | + it('warns only for invalid hits when mixed with valid document keys', () => { |
| 155 | + const results: SearchResult[] = [ |
| 156 | + { |
| 157 | + id: '', |
| 158 | + content: 'Skipped.', |
| 159 | + score: 0.3, |
| 160 | + metadata: {}, |
| 161 | + reranked: false, |
| 162 | + }, |
| 163 | + { |
| 164 | + id: 'vec-1', |
| 165 | + content: 'Kept by id.', |
| 166 | + score: 0.9, |
| 167 | + metadata: {}, |
| 168 | + reranked: false, |
| 169 | + }, |
| 170 | + ]; |
| 171 | + const docs = reassembleByDocument(results); |
| 172 | + expect(docs).toHaveLength(1); |
| 173 | + expect(docs[0].document_id).toBe('vec-1'); |
| 174 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 175 | + expect(warnSpy.mock.calls[0]?.[0]).toMatch(/skipped 1 hit/); |
| 176 | + expect(warnSpy.mock.calls[0]?.[0]).toMatch(/sample_ids=<empty>/); |
| 177 | + }); |
| 178 | + |
| 179 | + it('includes at most three sample entries in skip warning when many hits are skipped', () => { |
| 180 | + const results: SearchResult[] = Array.from({ length: 4 }, (_, i) => ({ |
| 181 | + id: '', |
| 182 | + content: `chunk-${i}`, |
| 183 | + score: 0.1 * (i + 1), |
| 184 | + metadata: {}, |
| 185 | + reranked: false, |
| 186 | + })); |
| 187 | + reassembleByDocument(results); |
| 188 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 189 | + const msg = String(warnSpy.mock.calls[0]?.[0]); |
| 190 | + expect(msg).toMatch(/skipped 4 hit/); |
| 191 | + const samplePart = msg.match(/sample_ids=(.+)$/)?.[1]; |
| 192 | + expect(samplePart).toBeDefined(); |
| 193 | + const samples = samplePart!.split(','); |
| 194 | + expect(samples).toHaveLength(3); |
| 195 | + expect(samples.every((s) => s === '<empty>')).toBe(true); |
| 196 | + }); |
88 | 197 | }); |
0 commit comments