Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, expect, test } from 'bun:test'; |
| 2 | import { createKnowledgeSearchHandler } from '@/app/api/knowledge-search/route'; |
| 3 | import type { PublicKbCandidateRecord } from '@/lib/knowledge/hybrid-search'; |
| 4 | import { |
| 5 | SemanticSearchBudget, |
| 6 | semanticSearchClientKey, |
| 7 | } from '@/lib/knowledge/semantic-protection'; |
| 8 | |
| 9 | function keywordCandidate(): PublicKbCandidateRecord { |
| 10 | return { |
| 11 | objectID: 'github-oauth', |
| 12 | pageID: '/kb/guide/github-oauth', |
| 13 | title: 'GitHub OAuth troubleshooting', |
| 14 | section: 'Reconnect expired access', |
| 15 | description: 'Reconnect GitHub after OAuth access expires.', |
| 16 | content: 'Reconnect the GitHub account and retry the action.', |
| 17 | canonicalUrl: '/kb/guide/github-oauth#reconnect-expired-access', |
| 18 | breadcrumbs: ['Knowledge Base', 'GitHub'], |
| 19 | productAreas: [], |
| 20 | toolkitSlugs: ['github'], |
| 21 | keywords: ['github oauth'], |
| 22 | slug: 'github-oauth', |
| 23 | toolNames: [], |
| 24 | toolSlugs: [], |
| 25 | pageRank: 1_900, |
| 26 | sectionRank: 96, |
| 27 | lastVerifiedAt: '2026-08-17', |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | describe('knowledge search semantic protection', () => { |
| 32 | test('returns keyword results when semantic search exceeds its latency budget', async () => { |
| 33 | const handler = createKnowledgeSearchHandler({ |
| 34 | hybridEnabled: () => true, |
| 35 | searchKeywordCandidates: async () => ({ candidates: [keywordCandidate()] }), |
| 36 | searchSemanticCandidates: async () => new Promise<never>(() => {}), |
| 37 | semanticTimeoutMs: () => 10, |
| 38 | }); |
| 39 | let guard: ReturnType<typeof setTimeout> | undefined; |
| 40 | const deadline = new Promise<'deadline'>(resolve => { |
| 41 | guard = setTimeout(() => resolve('deadline'), 250); |
| 42 | }); |
| 43 | |
| 44 | const result = await Promise.race([ |
| 45 | handler(new Request( |
| 46 | 'http://localhost/api/knowledge-search?q=github+oauth&filter=kb', |
| 47 | )), |
| 48 | deadline, |
| 49 | ]); |
| 50 | if (guard) clearTimeout(guard); |
| 51 | |
| 52 | expect(result).toBeInstanceOf(Response); |
| 53 | if (!(result instanceof Response)) return; |
| 54 | expect(result.status).toBe(200); |
| 55 | const responseBody = await result.json() as Record<string, unknown>; |
| 56 | expect(responseBody).toMatchObject({ |
| 57 | mode: 'keyword', |
| 58 | results: [{ objectID: 'github-oauth' }], |
| 59 | }); |
| 60 | expect(responseBody).not.toHaveProperty('degradedReason'); |
| 61 | }); |
| 62 | |
| 63 | test('returns keyword results without spending an embedding when the soft budget is exhausted', async () => { |
| 64 | let semanticCalls = 0; |
| 65 | const handler = createKnowledgeSearchHandler({ |
| 66 | hybridEnabled: () => true, |
| 67 | searchKeywordCandidates: async () => ({ candidates: [keywordCandidate()] }), |
| 68 | searchSemanticCandidates: async () => { |
| 69 | semanticCalls += 1; |
| 70 | return []; |
| 71 | }, |
| 72 | acquireSemanticSearch: () => ({ |
| 73 | allowed: false, |
| 74 | reason: 'semantic-rate-limited', |
| 75 | }), |
| 76 | }); |
| 77 | |
| 78 | const response = await handler(new Request( |
| 79 | 'http://localhost/api/knowledge-search?q=github+oauth&filter=kb', |
| 80 | )); |
| 81 | |
| 82 | expect(response.status).toBe(200); |
| 83 | expect(semanticCalls).toBe(0); |
| 84 | const responseBody = await response.json() as Record<string, unknown>; |
| 85 | expect(responseBody).toMatchObject({ |
| 86 | mode: 'keyword', |
| 87 | results: [{ objectID: 'github-oauth' }], |
| 88 | }); |
| 89 | expect(responseBody).not.toHaveProperty('degradedReason'); |
| 90 | }); |
| 91 | }); |
| 92 | |
| 93 | describe('semantic search budget', () => { |
| 94 | test('applies a global ceiling to anonymous and rotating clients', () => { |
| 95 | const budget = new SemanticSearchBudget({ |
| 96 | requestsPerWindow: 10, |
| 97 | globalRequestsPerWindow: 2, |
| 98 | windowMs: 60_000, |
| 99 | maxConcurrent: 2, |
| 100 | }); |
| 101 | const anonymous = budget.tryAcquire(); |
| 102 | anonymous.release?.(); |
| 103 | const firstClient = budget.tryAcquire('client-a'); |
| 104 | firstClient.release?.(); |
| 105 | |
| 106 | expect(budget.tryAcquire('client-b')).toEqual({ |
| 107 | allowed: false, |
| 108 | reason: 'semantic-rate-limited', |
| 109 | }); |
| 110 | }); |
| 111 | |
| 112 | test('derives a stable opaque client key from the trusted forwarding header', () => { |
| 113 | const first = semanticSearchClientKey(new Request('http://localhost', { |
| 114 | headers: { 'x-forwarded-for': '203.0.113.7, 10.0.0.1' }, |
| 115 | })); |
| 116 | const second = semanticSearchClientKey(new Request('http://localhost', { |
| 117 | headers: { 'x-forwarded-for': '203.0.113.7' }, |
| 118 | })); |
| 119 | |
| 120 | expect(first).toBe(second); |
| 121 | expect(first).not.toContain('203.0.113.7'); |
| 122 | expect(semanticSearchClientKey(new Request('http://localhost'))).toBeUndefined(); |
| 123 | }); |
| 124 | |
| 125 | test('replenishes a client budget after the configured window', () => { |
| 126 | let now = 0; |
| 127 | const budget = new SemanticSearchBudget({ |
| 128 | requestsPerWindow: 2, |
| 129 | windowMs: 1_000, |
| 130 | maxConcurrent: 2, |
| 131 | now: () => now, |
| 132 | }); |
| 133 | const first = budget.tryAcquire('client-a'); |
| 134 | first.release?.(); |
| 135 | const second = budget.tryAcquire('client-a'); |
| 136 | second.release?.(); |
| 137 | |
| 138 | expect(budget.tryAcquire('client-a')).toEqual({ |
| 139 | allowed: false, |
| 140 | reason: 'semantic-rate-limited', |
| 141 | }); |
| 142 | now = 1_001; |
| 143 | expect(budget.tryAcquire('client-a').allowed).toBe(true); |
| 144 | }); |
| 145 | |
| 146 | test('releases concurrency immediately without replenishing the request budget', () => { |
| 147 | const budget = new SemanticSearchBudget({ |
| 148 | requestsPerWindow: 10, |
| 149 | windowMs: 60_000, |
| 150 | maxConcurrent: 1, |
| 151 | }); |
| 152 | const first = budget.tryAcquire('client-a'); |
| 153 | expect(first.allowed).toBe(true); |
| 154 | expect(budget.tryAcquire('client-b')).toEqual({ |
| 155 | allowed: false, |
| 156 | reason: 'semantic-capacity-limited', |
| 157 | }); |
| 158 | |
| 159 | first.release?.(); |
| 160 | expect(budget.tryAcquire('client-b').allowed).toBe(true); |
| 161 | }); |
| 162 | }); |