File size: 1,862 Bytes
c3aa9b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node

/**
 * Test script to verify embedding cache functionality
 */

import { AIService } from '../services/ai-service.js';
import dotenv from 'dotenv';

dotenv.config();

console.log('🧪 Testing Embedding Cache\n');

const aiService = new AIService();

// Test data
const testTexts = [
  'This is a test document about Kubernetes.',
  'This is a test document about Kubernetes.', // Duplicate - should hit cache
  'SUSE Linux Enterprise Server documentation.',
  'Rancher multi-cluster management guide.',
  'This is a test document about Kubernetes.', // Another duplicate
];

console.log('Generating embeddings for test texts...\n');

for (let i = 0; i < testTexts.length; i++) {
  const text = testTexts[i];
  console.log(`${i + 1}. Text: "${text.substring(0, 50)}..."`);
  
  try {
    const embedding = await aiService.getOrCreateEmbedding(text);
    console.log(`   ✓ Generated embedding (${embedding.length} dimensions)`);
    
    const stats = aiService.getCacheStats();
    console.log(`   Cache: ${stats.hits} hits, ${stats.misses} misses (${stats.hitRate} hit rate)\n`);
  } catch (error) {
    console.error(`   ✗ Failed:`, error.message, '\n');
  }
}

// Save cache
console.log('Saving cache to disk...');
await aiService.saveEmbeddingCache();

// Test loading cache
console.log('\n🔄 Testing cache reload...\n');
const aiService2 = new AIService();
await aiService2.loadEmbeddingCache();

console.log('Testing with reloaded cache...');
try {
  const embedding = await aiService2.getOrCreateEmbedding('This is a test document about Kubernetes.');
  const stats = aiService2.getCacheStats();
  console.log(`✓ Cache hit on reload! Stats: ${stats.hits} hits, ${stats.misses} misses (${stats.hitRate} hit rate)`);
} catch (error) {
  console.error('✗ Failed:', error.message);
}

console.log('\n✅ Cache test complete!');