Skip to content

Commit 98d0164

Browse files
authored
test(rerank): cover result-mapping edge cases in rerankResults (cppalliance#226)
Add tests for empty and absent rerank data (non-empty input still returns empty results, degraded=false), a data item with no document (maps to empty id/content instead of throwing), and duplicate documents (one result per data item). Each asserts the mapped SearchResult fields. Also extract a makePc helper to drop the repeated pc cast across the file. Closes cppalliance#224.
1 parent bd94c14 commit 98d0164

1 file changed

Lines changed: 66 additions & 2 deletions

File tree

src/core/pinecone/rerank.test.ts

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const sampleMerged: MergedHit[] = [
66
{ _id: '1', _score: 0.5, chunk_text: 'hello', metadata: { k: 'v' } },
77
];
88

9+
function makePc(rerank: ReturnType<typeof vi.fn>) {
10+
return { inference: { rerank } } as Parameters<typeof rerankResults>[0];
11+
}
12+
913
describe('rerankResults', () => {
1014
it('returns empty outcome when there are no merged hits', async () => {
1115
const pc = {} as Parameters<typeof rerankResults>[0];
@@ -23,7 +27,7 @@ describe('rerankResults', () => {
2327
},
2428
],
2529
});
26-
const pc = { inference: { rerank } } as Parameters<typeof rerankResults>[0];
30+
const pc = makePc(rerank);
2731

2832
const out = await rerankResults(pc, 'm', 'q', sampleMerged, 5);
2933

@@ -37,7 +41,7 @@ describe('rerankResults', () => {
3741

3842
it('returns unreranked slice with degraded when rerank throws', async () => {
3943
const rerank = vi.fn().mockRejectedValue(new Error('rerank unavailable'));
40-
const pc = { inference: { rerank } } as Parameters<typeof rerankResults>[0];
44+
const pc = makePc(rerank);
4145

4246
const out = await rerankResults(pc, 'm', 'q', sampleMerged, 5);
4347

@@ -47,4 +51,64 @@ describe('rerankResults', () => {
4751
expect(out.results[0]?.reranked).toBe(false);
4852
expect(out.results[0]?.content).toBe('hello');
4953
});
54+
55+
it('returns empty reranked results with degraded=false when data is an empty array', async () => {
56+
const rerank = vi.fn().mockResolvedValue({ data: [] });
57+
const pc = makePc(rerank);
58+
59+
const out = await rerankResults(pc, 'm', 'q', sampleMerged, 5);
60+
61+
expect(out.results).toEqual([]);
62+
expect(out.degraded).toBe(false);
63+
expect(out.degradation_reason).toBeUndefined();
64+
});
65+
66+
it('returns empty reranked results with degraded=false when data is absent', async () => {
67+
const rerank = vi.fn().mockResolvedValue({});
68+
const pc = makePc(rerank);
69+
70+
const out = await rerankResults(pc, 'm', 'q', sampleMerged, 5);
71+
72+
expect(out.results).toEqual([]);
73+
expect(out.degraded).toBe(false);
74+
});
75+
76+
it('maps a rerank item with no document to an empty id/content result instead of throwing', async () => {
77+
const rerank = vi.fn().mockResolvedValue({
78+
data: [{ score: 0.42 }],
79+
});
80+
const pc = makePc(rerank);
81+
82+
const out = await rerankResults(pc, 'm', 'q', sampleMerged, 5);
83+
84+
expect(out.degraded).toBe(false);
85+
expect(out.results).toHaveLength(1);
86+
expect(out.results[0]?.id).toBe('');
87+
expect(out.results[0]?.content).toBe('');
88+
expect(out.results[0]?.metadata).toEqual({});
89+
expect(out.results[0]?.reranked).toBe(true);
90+
expect(out.results[0]?.score).toBeCloseTo(0.42);
91+
});
92+
93+
it('maps duplicate documents in the rerank output to one result per data item', async () => {
94+
const document = { _id: '1', chunk_text: 'hello', metadata: { k: 'v' } };
95+
const rerank = vi.fn().mockResolvedValue({
96+
data: [
97+
{ score: 0.9, document },
98+
{ score: 0.8, document },
99+
],
100+
});
101+
const pc = makePc(rerank);
102+
103+
const out = await rerankResults(pc, 'm', 'q', sampleMerged, 5);
104+
105+
expect(out.degraded).toBe(false);
106+
expect(out.results).toHaveLength(2);
107+
expect(out.results.map((r) => r.id)).toEqual(['1', '1']);
108+
expect(out.results.map((r) => r.content)).toEqual(['hello', 'hello']);
109+
expect(out.results[0]?.metadata).toEqual({ k: 'v' });
110+
expect(out.results[0]?.score).toBeCloseTo(0.9);
111+
expect(out.results[1]?.score).toBeCloseTo(0.8);
112+
expect(out.results.every((r) => r.reranked === true)).toBe(true);
113+
});
50114
});

0 commit comments

Comments
 (0)