mcp-sampling-rerank.test.mjs
96 lines 3.1 KB
Raw
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd feat(calendar): enforce agent context tiers in retrieval AP… Human minor ⚠ breaking 1 day ago
1 import { describe, it } from 'node:test';
2 import assert from 'node:assert/strict';
3 import { parseRerankResponse, rerankWithSampling } from '../mcp/tools/sampling-rerank.mjs';
4
5 function makeMockServer({ sampling = true, response = null } = {}) {
6 return {
7 server: {
8 getClientCapabilities: () => (sampling ? { sampling: {} } : {}),
9 createMessage: async () => ({
10 content: { type: 'text', text: response },
11 model: 'mock',
12 role: 'assistant',
13 }),
14 },
15 };
16 }
17
18 describe('parseRerankResponse', () => {
19 it('parses JSON array of 1-based indices', () => {
20 const result = parseRerankResponse('[3, 1, 2]', 5);
21 assert.deepEqual(result, [2, 0, 1]);
22 });
23
24 it('strips markdown fences', () => {
25 const result = parseRerankResponse('```json\n[2, 4, 1]\n```', 5);
26 assert.deepEqual(result, [1, 3, 0]);
27 });
28
29 it('falls back to regex number extraction', () => {
30 const result = parseRerankResponse('The best order is 3, then 1, then 2.', 5);
31 assert.deepEqual(result, [2, 0, 1]);
32 });
33
34 it('filters out-of-range indices', () => {
35 const result = parseRerankResponse('[1, 99, 2, 0, -1]', 3);
36 assert.deepEqual(result, [0, 1]);
37 });
38
39 it('returns null for null input', () => {
40 assert.equal(parseRerankResponse(null, 5), null);
41 });
42
43 it('returns null for empty string', () => {
44 assert.equal(parseRerankResponse('', 5), null);
45 });
46 });
47
48 describe('rerankWithSampling', () => {
49 const results = [
50 { path: 'a.md', snippet: 'alpha' },
51 { path: 'b.md', snippet: 'beta' },
52 { path: 'c.md', snippet: 'gamma' },
53 ];
54
55 it('returns original when sampling unavailable', async () => {
56 const out = await rerankWithSampling(
57 makeMockServer({ sampling: false }),
58 'test query', results, 3
59 );
60 assert.deepEqual(out, results);
61 });
62
63 it('reorders results based on sampling response', async () => {
64 const server = makeMockServer({ sampling: true, response: '[3, 1, 2]' });
65 const out = await rerankWithSampling(server, 'test query', results, 3);
66 assert.equal(out[0].path, 'c.md');
67 assert.equal(out[1].path, 'a.md');
68 assert.equal(out[2].path, 'b.md');
69 });
70
71 it('preserves unreferenced results at the end', async () => {
72 const server = makeMockServer({ sampling: true, response: '[2]' });
73 const out = await rerankWithSampling(server, 'query', results, 10);
74 assert.equal(out[0].path, 'b.md');
75 assert.equal(out.length, 3);
76 });
77
78 it('returns original for single result', async () => {
79 const single = [{ path: 'x.md', snippet: 'only' }];
80 const out = await rerankWithSampling(makeMockServer(), 'q', single, 1);
81 assert.deepEqual(out, single);
82 });
83
84 it('returns original for empty results', async () => {
85 const out = await rerankWithSampling(makeMockServer(), 'q', [], 5);
86 assert.deepEqual(out, []);
87 });
88
89 it('respects limit parameter', async () => {
90 const server = makeMockServer({ sampling: true, response: '[3, 1, 2]' });
91 const out = await rerankWithSampling(server, 'q', results, 2);
92 assert.equal(out.length, 2);
93 assert.equal(out[0].path, 'c.md');
94 assert.equal(out[1].path, 'a.md');
95 });
96 });
File History 2 commits
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd feat(calendar): enforce agent context tiers in retrieval AP… Human minor 1 day ago
sha256:9103f98c89257ed2b01c237cea895dabb3e85ea337dccb1161c175e4422355b6 docs: accept Calendar Events v0 spec with Phase 0 security … Human 1 day ago