Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | import { describe, expect, test } from 'bun:test'; |
| 2 | import { |
| 3 | fusePublicKbCandidates, |
| 4 | type PublicKbCandidateRecord, |
| 5 | } from '@/lib/knowledge/hybrid-search'; |
| 6 | |
| 7 | function candidate( |
| 8 | objectID: string, |
| 9 | title: string, |
| 10 | overrides: Partial<PublicKbCandidateRecord> = {}, |
| 11 | ): PublicKbCandidateRecord { |
| 12 | return { |
| 13 | objectID, |
| 14 | pageID: `/kb/guide/${objectID}`, |
| 15 | title, |
| 16 | section: `${title} section`, |
| 17 | description: `${title} description`, |
| 18 | content: `${title} body`, |
| 19 | canonicalUrl: `/kb/guide/${objectID}#answer`, |
| 20 | breadcrumbs: ['Knowledge Base'], |
| 21 | productAreas: [], |
| 22 | toolkitSlugs: [], |
| 23 | keywords: [], |
| 24 | slug: objectID, |
| 25 | toolNames: [], |
| 26 | toolSlugs: [], |
| 27 | pageRank: 1_900, |
| 28 | sectionRank: 96, |
| 29 | lastVerifiedAt: '2026-08-12', |
| 30 | ...overrides, |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | describe('public KB hybrid ranking', () => { |
| 35 | test('pins exact titles and identifiers ahead of non-exact RRF winners', () => { |
| 36 | const generic = candidate('generic', 'Troubleshoot calendar actions'); |
| 37 | const exactIdentifier = candidate('exact', 'Create a Calendly invitee', { |
| 38 | toolSlugs: ['CALENDLY_POST_INVITEE'], |
| 39 | }); |
| 40 | const semanticWinner = candidate('semantic', 'Invite people to events'); |
| 41 | |
| 42 | const result = fusePublicKbCandidates({ |
| 43 | query: 'CALENDLY_POST_INVITEE', |
| 44 | keyword: [generic, exactIdentifier], |
| 45 | semantic: [semanticWinner, generic, exactIdentifier], |
| 46 | limit: 20, |
| 47 | }); |
| 48 | |
| 49 | expect(result.map(item => item.record.objectID)).toEqual(['exact', 'generic', 'semantic']); |
| 50 | expect(result[0]?.exactTier).toBe(2); |
| 51 | }); |
| 52 | |
| 53 | test('lets a paraphrase retrieved by both lists beat one-list keyword matches', () => { |
| 54 | const paraphrase = candidate('paraphrase', 'Reconnect an expired OAuth account'); |
| 55 | const keywordOnly = candidate('keyword', 'Connection status reference'); |
| 56 | const semanticOnly = candidate('semantic', 'Refresh provider access'); |
| 57 | |
| 58 | const result = fusePublicKbCandidates({ |
| 59 | query: 'my integration stopped working after access was revoked', |
| 60 | keyword: [keywordOnly, paraphrase], |
| 61 | semantic: [paraphrase, semanticOnly], |
| 62 | limit: 20, |
| 63 | }); |
| 64 | |
| 65 | expect(result[0]?.record.objectID).toBe('paraphrase'); |
| 66 | expect(result[0]?.rrfScore).toBeCloseTo(1 / 62 + 1 / 61); |
| 67 | }); |
| 68 | |
| 69 | test('keeps only the strongest section for each canonical page', () => { |
| 70 | const weakerSection = candidate('github-overview', 'GitHub overview', { |
| 71 | canonicalUrl: '/kb/guide/github#overview', |
| 72 | pageID: '/kb/guide/github', |
| 73 | sectionRank: 80, |
| 74 | }); |
| 75 | const strongerSection = candidate('github-tokens', 'GitHub tokens', { |
| 76 | canonicalUrl: '/kb/guide/github#tokens', |
| 77 | pageID: '/kb/guide/github', |
| 78 | sectionRank: 100, |
| 79 | }); |
| 80 | |
| 81 | const result = fusePublicKbCandidates({ |
| 82 | query: 'github', |
| 83 | keyword: [strongerSection, weakerSection], |
| 84 | semantic: [weakerSection, strongerSection], |
| 85 | limit: 20, |
| 86 | }); |
| 87 | |
| 88 | expect(result).toHaveLength(1); |
| 89 | expect(result[0]?.record.objectID).toBe('github-tokens'); |
| 90 | }); |
| 91 | |
| 92 | test('is deterministic with either retriever absent and caps displayed pages at twenty', () => { |
| 93 | const records = Array.from({ length: 30 }, (_, index) => candidate( |
| 94 | `record-${String(index).padStart(2, '0')}`, |
| 95 | `Answer ${index}`, |
| 96 | { pageRank: 1_900 - index }, |
| 97 | )); |
| 98 | const expected = fusePublicKbCandidates({ |
| 99 | query: 'unmatched phrase', |
| 100 | keyword: [], |
| 101 | semantic: records, |
| 102 | limit: 50, |
| 103 | }).map(item => item.record.objectID); |
| 104 | |
| 105 | expect(expected).toHaveLength(20); |
| 106 | for (let run = 0; run < 20; run += 1) { |
| 107 | expect(fusePublicKbCandidates({ |
| 108 | query: 'unmatched phrase', |
| 109 | keyword: [], |
| 110 | semantic: records, |
| 111 | limit: 50, |
| 112 | }).map(item => item.record.objectID)).toEqual(expected); |
| 113 | } |
| 114 | }); |
| 115 | }); |