|
| 1 | +/** |
| 2 | + * @fileoverview Integration tests for the bundled platform knowledge corpus. |
| 3 | + * |
| 4 | + * These tests exercise the REAL `knowledge/platform-corpus.json` file (not |
| 5 | + * mocked) to verify structural integrity, category coverage, specific entry |
| 6 | + * existence, and keyword-based searchability. |
| 7 | + * |
| 8 | + * No LLM calls or embedding APIs are needed — all assertions are against the |
| 9 | + * static corpus file and the KeywordFallback engine. |
| 10 | + * |
| 11 | + * @module @framers/agentos/query-router/__tests__/platform-knowledge.integration |
| 12 | + */ |
| 13 | + |
| 14 | +import { existsSync, readFileSync } from 'node:fs'; |
| 15 | +import { dirname, join, resolve } from 'node:path'; |
| 16 | +import { fileURLToPath } from 'node:url'; |
| 17 | +import { describe, expect, it, beforeAll } from 'vitest'; |
| 18 | + |
| 19 | +import { KeywordFallback } from '../KeywordFallback.js'; |
| 20 | +import type { CorpusChunk } from '../types.js'; |
| 21 | + |
| 22 | +// --------------------------------------------------------------------------- |
| 23 | +// Locate the real platform corpus |
| 24 | +// --------------------------------------------------------------------------- |
| 25 | + |
| 26 | +const MODULE_DIR = dirname(fileURLToPath(import.meta.url)); |
| 27 | + |
| 28 | +/** Candidate paths where the corpus file may live relative to this test file. */ |
| 29 | +const CORPUS_CANDIDATES = [ |
| 30 | + // From src/query-router/__tests__/ -> knowledge/ |
| 31 | + resolve(MODULE_DIR, '../../../knowledge/platform-corpus.json'), |
| 32 | + // From dist/query-router/__tests__/ -> knowledge/ |
| 33 | + resolve(MODULE_DIR, '../../../../knowledge/platform-corpus.json'), |
| 34 | +]; |
| 35 | + |
| 36 | +/** Resolved path to the platform corpus, or null if not found. */ |
| 37 | +const corpusPath = CORPUS_CANDIDATES.find((p) => existsSync(p)) ?? null; |
| 38 | + |
| 39 | +// --------------------------------------------------------------------------- |
| 40 | +// Types for raw corpus entries |
| 41 | +// --------------------------------------------------------------------------- |
| 42 | + |
| 43 | +interface PlatformCorpusEntry { |
| 44 | + id: string; |
| 45 | + heading: string; |
| 46 | + content: string; |
| 47 | + category: string; |
| 48 | +} |
| 49 | + |
| 50 | +// --------------------------------------------------------------------------- |
| 51 | +// Test suite |
| 52 | +// --------------------------------------------------------------------------- |
| 53 | + |
| 54 | +describe('Platform Knowledge Corpus — integration', () => { |
| 55 | + let entries: PlatformCorpusEntry[]; |
| 56 | + let chunks: CorpusChunk[]; |
| 57 | + let fallback: KeywordFallback; |
| 58 | + |
| 59 | + beforeAll(() => { |
| 60 | + expect(corpusPath).not.toBeNull(); |
| 61 | + const raw = readFileSync(corpusPath!, 'utf-8'); |
| 62 | + entries = JSON.parse(raw) as PlatformCorpusEntry[]; |
| 63 | + |
| 64 | + // Convert to CorpusChunk format (same transform as QueryRouter.loadPlatformKnowledge) |
| 65 | + chunks = entries.map((entry) => ({ |
| 66 | + id: entry.id, |
| 67 | + heading: entry.heading, |
| 68 | + content: entry.content, |
| 69 | + sourcePath: `platform:${entry.category}/${entry.id}`, |
| 70 | + })); |
| 71 | + |
| 72 | + fallback = new KeywordFallback(chunks); |
| 73 | + }); |
| 74 | + |
| 75 | + // ========================================================================= |
| 76 | + // Structural integrity |
| 77 | + // ========================================================================= |
| 78 | + |
| 79 | + it('contains at least 200 entries', () => { |
| 80 | + expect(entries.length).toBeGreaterThanOrEqual(200); |
| 81 | + }); |
| 82 | + |
| 83 | + it('has all 5 expected categories', () => { |
| 84 | + const categories = new Set(entries.map((e) => e.category)); |
| 85 | + expect(categories).toContain('tools'); |
| 86 | + expect(categories).toContain('skills'); |
| 87 | + expect(categories).toContain('faq'); |
| 88 | + expect(categories).toContain('api'); |
| 89 | + expect(categories).toContain('troubleshooting'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('every entry has non-empty id, heading, content, and category', () => { |
| 93 | + for (const entry of entries) { |
| 94 | + expect(entry.id).toBeTruthy(); |
| 95 | + expect(entry.heading).toBeTruthy(); |
| 96 | + expect(entry.content).toBeTruthy(); |
| 97 | + expect(entry.category).toBeTruthy(); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + // ========================================================================= |
| 102 | + // Specific entry existence |
| 103 | + // ========================================================================= |
| 104 | + |
| 105 | + it('contains the generateText() API entry', () => { |
| 106 | + const match = entries.find((e) => e.id === 'api:generateText'); |
| 107 | + expect(match).toBeDefined(); |
| 108 | + expect(match!.heading).toContain('generateText'); |
| 109 | + expect(match!.category).toBe('api'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('contains the "How do I add voice?" FAQ entry', () => { |
| 113 | + const match = entries.find((e) => e.id === 'faq:add-voice'); |
| 114 | + expect(match).toBeDefined(); |
| 115 | + expect(match!.heading.toLowerCase()).toContain('voice'); |
| 116 | + expect(match!.category).toBe('faq'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('contains the document-export tool reference', () => { |
| 120 | + const match = entries.find((e) => e.id === 'tool-ref:com.framers.productivity.document-export'); |
| 121 | + expect(match).toBeDefined(); |
| 122 | + expect(match!.category).toBe('tools'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('contains the streamText() API entry', () => { |
| 126 | + const match = entries.find((e) => e.id === 'api:streamText'); |
| 127 | + expect(match).toBeDefined(); |
| 128 | + expect(match!.heading).toContain('streamText'); |
| 129 | + expect(match!.category).toBe('api'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('contains the "What models are supported?" FAQ entry', () => { |
| 133 | + const match = entries.find((e) => e.id === 'faq:supported-models'); |
| 134 | + expect(match).toBeDefined(); |
| 135 | + expect(match!.category).toBe('faq'); |
| 136 | + }); |
| 137 | + |
| 138 | + // ========================================================================= |
| 139 | + // Keyword fallback search |
| 140 | + // ========================================================================= |
| 141 | + |
| 142 | + it('finds document-export when searching "PDF generation"', () => { |
| 143 | + const results = fallback.search('PDF generation document export', 10); |
| 144 | + expect(results.length).toBeGreaterThan(0); |
| 145 | + const ids = results.map((r) => r.id); |
| 146 | + const hasDocExport = ids.some( |
| 147 | + (id) => id.includes('document-export') || id.includes('pdf') |
| 148 | + ); |
| 149 | + expect(hasDocExport).toBe(true); |
| 150 | + }); |
| 151 | + |
| 152 | + it('finds FAQ entry when searching "what models are supported"', () => { |
| 153 | + const results = fallback.search('what models are supported', 10); |
| 154 | + expect(results.length).toBeGreaterThan(0); |
| 155 | + const ids = results.map((r) => r.id); |
| 156 | + const hasFaq = ids.some((id) => id.includes('faq:')); |
| 157 | + expect(hasFaq).toBe(true); |
| 158 | + }); |
| 159 | + |
| 160 | + it('finds streamText API entry when searching "streaming"', () => { |
| 161 | + const results = fallback.search('streaming text generation', 10); |
| 162 | + expect(results.length).toBeGreaterThan(0); |
| 163 | + const ids = results.map((r) => r.id); |
| 164 | + const hasStream = ids.some( |
| 165 | + (id) => id.includes('streamText') || id.includes('streaming') |
| 166 | + ); |
| 167 | + expect(hasStream).toBe(true); |
| 168 | + }); |
| 169 | + |
| 170 | + it('finds voice-related entries when searching "voice pipeline"', () => { |
| 171 | + const results = fallback.search('voice pipeline speech recognition', 10); |
| 172 | + expect(results.length).toBeGreaterThan(0); |
| 173 | + const ids = results.map((r) => r.id); |
| 174 | + const hasVoice = ids.some( |
| 175 | + (id) => id.includes('voice') || id.includes('stt') || id.includes('tts') |
| 176 | + ); |
| 177 | + expect(hasVoice).toBe(true); |
| 178 | + }); |
| 179 | + |
| 180 | + it('returns results with valid relevance scores', () => { |
| 181 | + const results = fallback.search('authentication tokens', 5); |
| 182 | + for (const result of results) { |
| 183 | + expect(result.relevanceScore).toBeGreaterThanOrEqual(0); |
| 184 | + expect(result.relevanceScore).toBeLessThanOrEqual(1); |
| 185 | + } |
| 186 | + }); |
| 187 | +}); |
0 commit comments