mcp-sampling-rerank.test.mjs
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠ breaking
10 days 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
4 commits
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠
10 days ago
sha256:d8c648b20a4d53b2673c5c082ee7edfa7b2fc9b11080832da1f38807b6bf940b
fix(7C-L1b): route hosted delegation proposals through cani…
Human
minor
⚠
30 days ago
sha256:2827ba9e7632a4b141c50caf1e8f7d77abbc3515be20e7465f2bccb0ac4edf91
fix: repair endpoint now sets has_active_subscription when …
Human
minor
⚠
49 days ago
sha256:6a102aafafdfe7e70a24f4e59740200f0ee713ce7915f1b53e9d4ba5ee8b4410
Initial Muse snapshot
Human
82 days ago