Spaces:
Paused
Paused
File size: 13,016 Bytes
529090e | 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | /**
* ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* β KNOWLEDGE COMPILER - ENHANCED TEST SUITE β
* β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£
* β Tests for the KnowledgeCompiler auto-compilation and insight generation β
* ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
// Mock dependencies
vi.mock('../HyperLog.js', () => ({
hyperLog: {
exportForAnalysis: () => ({
summary: {
HEALING_ATTEMPT: 5,
HEALING_SUCCESS: 4,
HEALING_FAILED: 1,
}
}),
getRecentEvents: () => [
{ id: '1', eventType: 'HEALING_SUCCESS', timestamp: Date.now(), data: {} },
{ id: '2', eventType: 'ERROR_UNHANDLED', timestamp: Date.now(), data: {} },
],
},
HyperLog: class MockHyperLog { },
}));
vi.mock('../SelfHealingAdapter.js', () => ({
selfHealing: {
getSystemStatus: () => ({
overallHealth: 'HEALTHY',
services: [
{ name: 'neo4j', status: 'healthy' },
{ name: 'postgres', status: 'healthy' },
],
}),
getPredictiveAlerts: () => [],
},
SelfHealingAdapter: class MockSelfHealing { },
}));
vi.mock('../../adapters/Neo4jAdapter.js', () => ({
neo4jAdapter: {
executeQuery: async () => [{ nodes: 1000, relationships: 5000 }],
},
}));
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// AUTO-COMPILATION TESTS
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('KnowledgeCompiler - Auto-Compilation', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it('should start auto-compilation at specified interval', () => {
const mockCompile = vi.fn();
let interval: NodeJS.Timeout | null = null;
const startAutoCompilation = (intervalMs: number) => {
interval = setInterval(mockCompile, intervalMs);
};
startAutoCompilation(60000);
expect(interval).toBeTruthy();
// Advance time by 60 seconds
vi.advanceTimersByTime(60000);
expect(mockCompile).toHaveBeenCalledTimes(1);
// Advance another 60 seconds
vi.advanceTimersByTime(60000);
expect(mockCompile).toHaveBeenCalledTimes(2);
clearInterval(interval!);
});
it('should not start if already running', () => {
let isRunning = false;
let startCount = 0;
const startAutoCompilation = () => {
if (isRunning) return;
isRunning = true;
startCount++;
};
startAutoCompilation();
startAutoCompilation();
startAutoCompilation();
expect(startCount).toBe(1);
});
it('should stop auto-compilation cleanly', () => {
const mockCompile = vi.fn();
let interval: NodeJS.Timeout | null = null;
const start = () => {
interval = setInterval(mockCompile, 1000);
};
const stop = () => {
if (interval) {
clearInterval(interval);
interval = null;
}
};
start();
vi.advanceTimersByTime(3000);
expect(mockCompile).toHaveBeenCalledTimes(3);
stop();
vi.advanceTimersByTime(3000);
expect(mockCompile).toHaveBeenCalledTimes(3); // No additional calls
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// HEALTH STATUS TESTS
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('KnowledgeCompiler - Health Status', () => {
it('should calculate health score correctly', () => {
const calculateScore = (status: string, failures: number) => {
let score = 100;
if (status === 'DEGRADED') score = 70;
if (status === 'CRITICAL') score = 30;
score = Math.max(0, score - failures * 5);
return score;
};
expect(calculateScore('HEALTHY', 0)).toBe(100);
expect(calculateScore('DEGRADED', 0)).toBe(70);
expect(calculateScore('CRITICAL', 0)).toBe(30);
expect(calculateScore('HEALTHY', 5)).toBe(75);
expect(calculateScore('HEALTHY', 20)).toBe(0); // Clamped at 0
});
it('should track healing success rate', () => {
const healingStats = {
attempts: 10,
successes: 8,
failures: 2,
};
const successRate = healingStats.attempts > 0
? Math.round((healingStats.successes / healingStats.attempts) * 100)
: 100;
expect(successRate).toBe(80);
});
it('should identify unhealthy services', () => {
const services = [
{ name: 'neo4j', status: 'healthy' },
{ name: 'postgres', status: 'unhealthy' },
{ name: 'redis', status: 'healthy' },
];
const unhealthy = services.filter(s => s.status === 'unhealthy');
expect(unhealthy).toHaveLength(1);
expect(unhealthy[0].name).toBe('postgres');
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// INSIGHT GENERATION TESTS
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('KnowledgeCompiler - Insight Generation', () => {
it('should detect error patterns', () => {
const events = [
{ type: 'ERROR_DATABASE', count: 10 },
{ type: 'ERROR_NETWORK', count: 3 },
{ type: 'HEALING_SUCCESS', count: 5 },
];
const errorEvents = events.filter(e => e.type.includes('ERROR'));
const repeatedErrors = errorEvents.filter(e => e.count >= 5);
expect(errorEvents).toHaveLength(2);
expect(repeatedErrors).toHaveLength(1);
expect(repeatedErrors[0].type).toBe('ERROR_DATABASE');
});
it('should detect usage spikes', () => {
const events = [
{ type: 'API_CALL', count: 100 },
{ type: 'DB_QUERY', count: 50 },
{ type: 'CACHE_HIT', count: 10 },
];
const avgCount = events.reduce((sum, e) => sum + e.count, 0) / events.length;
const spikes = events.filter(e => e.count > avgCount * 1.5);
expect(avgCount).toBeCloseTo(53.33, 1);
expect(spikes).toHaveLength(1);
expect(spikes[0].type).toBe('API_CALL');
});
it('should generate recommendations based on health', () => {
const generateRecommendations = (health: string, errors: number) => {
const recs = [];
if (health === 'DEGRADED') {
recs.push({ priority: 'high', action: 'Investigate degraded services' });
}
if (errors > 20) {
recs.push({ priority: 'high', action: 'Review error logs' });
}
return recs;
};
expect(generateRecommendations('HEALTHY', 5)).toHaveLength(0);
expect(generateRecommendations('DEGRADED', 5)).toHaveLength(1);
expect(generateRecommendations('DEGRADED', 25)).toHaveLength(2);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// ACTIVITY SUMMARY TESTS
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('KnowledgeCompiler - Activity Summary', () => {
it('should count events in last 24h', () => {
const now = Date.now();
const oneDayAgo = now - 24 * 60 * 60 * 1000;
const events = [
{ timestamp: now - 1000 }, // Recent
{ timestamp: now - 60000 }, // Recent
{ timestamp: oneDayAgo - 1000 }, // Old
{ timestamp: oneDayAgo - 60000 }, // Old
];
const last24h = events.filter(e => e.timestamp > oneDayAgo);
expect(last24h).toHaveLength(2);
});
it('should aggregate top event types', () => {
const events = [
{ type: 'API_CALL' },
{ type: 'API_CALL' },
{ type: 'API_CALL' },
{ type: 'DB_QUERY' },
{ type: 'DB_QUERY' },
{ type: 'ERROR' },
];
const counts: Record<string, number> = {};
for (const e of events) {
counts[e.type] = (counts[e.type] || 0) + 1;
}
const sorted = Object.entries(counts)
.sort((a, b) => b[1] - a[1])
.map(([type, count]) => ({ type, count }));
expect(sorted[0]).toEqual({ type: 'API_CALL', count: 3 });
expect(sorted[1]).toEqual({ type: 'DB_QUERY', count: 2 });
expect(sorted[2]).toEqual({ type: 'ERROR', count: 1 });
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// GRAPH STATS TESTS
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('KnowledgeCompiler - Graph Statistics', () => {
it('should handle Neo4j Integer objects', () => {
// Neo4j returns special Integer objects
const mockNeo4jInt = { low: 1000, high: 0 };
const extractValue = (val: any) => {
if (typeof val === 'object' && val !== null && 'low' in val) {
return val.low;
}
return Number(val || 0);
};
expect(extractValue(mockNeo4jInt)).toBe(1000);
expect(extractValue(500)).toBe(500);
expect(extractValue(null)).toBe(0);
});
it('should aggregate nodes by label', () => {
const queryResult = [
{ label: 'Entity', count: 500 },
{ label: 'Document', count: 300 },
{ label: 'Concept', count: 200 },
];
const nodesByLabel: Record<string, number> = {};
for (const row of queryResult) {
nodesByLabel[row.label] = row.count;
}
expect(nodesByLabel['Entity']).toBe(500);
expect(nodesByLabel['Document']).toBe(300);
expect(Object.keys(nodesByLabel)).toHaveLength(3);
});
it('should handle graph query failures gracefully', async () => {
const compileGraphStats = async () => {
try {
throw new Error('Neo4j connection failed');
} catch (error) {
return {
totalNodes: 0,
totalRelationships: 0,
nodesByLabel: {},
recentChanges: { added: 0, modified: 0, deleted: 0 },
};
}
};
const stats = await compileGraphStats();
expect(stats.totalNodes).toBe(0);
expect(stats.nodesByLabel).toEqual({});
});
});
|