universal-ai-brain
Version:
🧠 UNIVERSAL AI BRAIN 3.3 - The world's most advanced cognitive architecture with 24 specialized systems, MongoDB 8.1 $rankFusion hybrid search, latest Voyage 3.5 embeddings, and framework-agnostic design. Works with Mastra, Vercel AI, LangChain, OpenAI A
777 lines (728 loc) • 26.1 kB
text/typescript
/**
* @file SocialIntelligenceEngine.test.ts - Comprehensive tests for social intelligence with Atlas $graphLookup
*
* Tests MongoDB Atlas $graphLookup capabilities for social intelligence:
* - $graphLookup aggregation stage (Atlas optimized)
* - Recursive social network traversal
* - Graph-based relationship analysis
* - Multi-depth social connection exploration
*/
import { MongoClient, Db } from 'mongodb';
import { SocialIntelligenceEngine, SocialAnalysisRequest, NetworkTraversalRequest } from '../../intelligence/SocialIntelligenceEngine';
import { SocialIntelligenceCollection } from '../../collections/SocialIntelligenceCollection';
import { setupTestDatabase, cleanupTestDatabase, shouldSkipTest } from '../testConfig';
describe('SocialIntelligenceEngine - Real MongoDB Atlas Integration', () => {
let client: MongoClient;
let db: Db;
let socialEngine: SocialIntelligenceEngine;
let socialCollection: SocialIntelligenceCollection;
beforeAll(async () => {
if (shouldSkipTest()) {
console.log('⏭️ Skipping test: Database not available');
return;
}
try {
const connection = await setupTestDatabase();
client = connection.client;
db = connection.db;
socialEngine = new SocialIntelligenceEngine(db);
socialCollection = new SocialIntelligenceCollection(db);
await socialEngine.initialize();
} catch (error) {
console.log('⏭️ Skipping SocialIntelligenceEngine tests due to setup failure');
console.error('Setup error:', error);
}
});
afterAll(async () => {
if (client) {
await cleanupTestDatabase(client);
}
});
describe('Social Network Analysis with $graphLookup', () => {
it('should store and analyze social connections using Atlas graph traversal', async () => {
if (shouldSkipTest() || !socialCollection || !socialEngine) return;
const agentId = 'test_agent_social_001';
// Create a social network: Agent -> Alice -> Bob -> Charlie
const connections = [
{
agentId,
timestamp: new Date(),
connection: {
id: 'conn_agent_alice',
type: 'friendship' as const,
status: 'active' as const,
participants: {
source: { id: agentId, name: 'Test Agent', type: 'agent' as const, role: 'self' },
target: { id: 'alice_001', name: 'Alice Johnson', type: 'human' as const, role: 'friend' }
},
strength: {
overall: 0.8,
emotional: 0.9,
professional: 0.6,
frequency: 0.8,
trust: 0.9,
influence: 0.7
},
context: {
origin: {
how_met: 'college',
when_met: new Date('2020-01-15'),
where_met: 'University',
circumstances: 'classmates',
mutual_connections: []
},
interactions: {
total_count: 150,
recent_count: 12,
frequency_pattern: 'weekly' as const,
communication_channels: [
{ channel: 'text' as const, frequency: 0.8, preference: 0.9 },
{ channel: 'in_person' as const, frequency: 0.6, preference: 0.8 }
],
interaction_types: [
{ type: 'social' as const, frequency: 0.7, quality: 0.9 },
{ type: 'support' as const, frequency: 0.5, quality: 0.8 }
]
},
dynamics: {
power_balance: 0.1,
communication_style: 'friendly' as const,
conflict_resolution: 'collaborative' as const,
emotional_support: 0.8,
reciprocity: 0.9
},
commonalities: {
shared_interests: ['technology', 'hiking', 'movies'],
shared_values: ['honesty', 'growth'],
shared_experiences: ['college', 'travel'],
shared_goals: ['career_success'],
shared_connections: ['bob_001'],
compatibility_score: 0.85
}
},
network_position: {
centrality: {
degree: 8,
betweenness: 0.6,
closeness: 0.7,
eigenvector: 0.8
},
influence: {
reach: 50,
authority: 0.7,
persuasiveness: 0.8,
network_effect: 0.6
},
communities: [
{
community_id: 'tech_community',
community_name: 'Tech Professionals',
role: 'influencer' as const,
involvement_level: 0.8,
influence_within: 0.7
}
]
}
},
evolution: {
milestones: [
{
timestamp: new Date('2020-01-15'),
event: 'first_meeting',
impact: 0.8,
description: 'Met in college class'
}
],
trajectory: {
direction: 'strengthening' as const,
rate_of_change: 0.2,
predicted_future: 'growing' as const,
stability: 0.8
},
quality_trends: [
{
period: new Date('2024-01-01'),
quality_score: 0.8,
satisfaction: 0.9,
conflict_level: 0.1,
support_level: 0.8
}
]
},
insights: {
patterns: [
{
pattern: 'regular_communication',
frequency: 0.8,
significance: 0.7,
context: ['weekly_calls', 'text_messages']
}
],
social_skills: {
communication: 0.8,
empathy: 0.9,
conflict_resolution: 0.7,
leadership: 0.6,
collaboration: 0.8,
networking: 0.7
},
recommendations: [
{
type: 'strengthen' as const,
priority: 0.8,
reasoning: 'Strong foundation for deeper friendship',
suggested_actions: ['plan_trip_together', 'introduce_to_family']
}
]
},
metadata: {
framework: 'social-intelligence-test',
version: '1.0.0',
source: 'test_data',
reliability: 0.9,
lastValidated: new Date(),
quality: {
completeness: 0.9,
accuracy: 0.9,
freshness: 0.9,
consistency: 0.9
},
graph_analysis: {
last_analyzed: new Date(),
analysis_depth: 3,
computation_time: 150,
network_size: 25
}
}
},
{
agentId,
timestamp: new Date(),
connection: {
id: 'conn_alice_bob',
type: 'professional' as const,
status: 'active' as const,
participants: {
source: { id: 'alice_001', name: 'Alice Johnson', type: 'human' as const, role: 'colleague' },
target: { id: 'bob_001', name: 'Bob Smith', type: 'human' as const, role: 'colleague' }
},
strength: {
overall: 0.7,
emotional: 0.5,
professional: 0.9,
frequency: 0.7,
trust: 0.8,
influence: 0.6
},
context: {
origin: {
how_met: 'work',
when_met: new Date('2021-03-01'),
where_met: 'Office',
circumstances: 'project_collaboration',
mutual_connections: [agentId]
},
interactions: {
total_count: 200,
recent_count: 15,
frequency_pattern: 'daily' as const,
communication_channels: [
{ channel: 'email' as const, frequency: 0.9, preference: 0.8 },
{ channel: 'in_person' as const, frequency: 0.7, preference: 0.9 }
],
interaction_types: [
{ type: 'professional' as const, frequency: 0.9, quality: 0.8 },
{ type: 'collaboration' as const, frequency: 0.8, quality: 0.9 }
]
},
dynamics: {
power_balance: 0.0,
communication_style: 'professional' as const,
conflict_resolution: 'collaborative' as const,
emotional_support: 0.6,
reciprocity: 0.8
},
commonalities: {
shared_interests: ['technology', 'innovation'],
shared_values: ['excellence', 'teamwork'],
shared_experiences: ['project_success'],
shared_goals: ['company_growth'],
shared_connections: [agentId],
compatibility_score: 0.75
}
},
network_position: {
centrality: {
degree: 12,
betweenness: 0.7,
closeness: 0.8,
eigenvector: 0.7
},
influence: {
reach: 75,
authority: 0.8,
persuasiveness: 0.7,
network_effect: 0.8
},
communities: [
{
community_id: 'work_team',
community_name: 'Development Team',
role: 'leader' as const,
involvement_level: 0.9,
influence_within: 0.8
}
]
}
},
evolution: {
milestones: [
{
timestamp: new Date('2021-03-01'),
event: 'project_start',
impact: 0.7,
description: 'Started working together on major project'
}
],
trajectory: {
direction: 'stable' as const,
rate_of_change: 0.0,
predicted_future: 'maintaining' as const,
stability: 0.9
},
quality_trends: [
{
period: new Date('2024-01-01'),
quality_score: 0.8,
satisfaction: 0.8,
conflict_level: 0.2,
support_level: 0.7
}
]
},
insights: {
patterns: [
{
pattern: 'professional_collaboration',
frequency: 0.9,
significance: 0.8,
context: ['daily_standups', 'project_meetings']
}
],
social_skills: {
communication: 0.9,
empathy: 0.6,
conflict_resolution: 0.8,
leadership: 0.9,
collaboration: 0.9,
networking: 0.8
},
recommendations: [
{
type: 'maintain' as const,
priority: 0.7,
reasoning: 'Strong professional relationship',
suggested_actions: ['continue_collaboration', 'share_knowledge']
}
]
},
metadata: {
framework: 'social-intelligence-test',
version: '1.0.0',
source: 'test_data',
reliability: 0.8,
lastValidated: new Date(),
quality: {
completeness: 0.8,
accuracy: 0.8,
freshness: 0.8,
consistency: 0.8
},
graph_analysis: {
last_analyzed: new Date(),
analysis_depth: 3,
computation_time: 120,
network_size: 30
}
}
}
];
// Store connections
for (const connection of connections) {
await socialCollection.storeSocialConnection(connection);
}
// Perform social analysis
const analysisRequest: SocialAnalysisRequest = {
agentId,
analysisType: 'network_analysis',
parameters: {
maxDepth: 3,
minConnectionStrength: 0.3,
connectionTypes: ['friendship', 'professional'],
includeInactive: false
},
context: {
purpose: 'relationship_building',
scope: 'comprehensive',
priority: 'high'
}
};
const result = await socialEngine.performSocialAnalysis(analysisRequest);
expect(result).toBeDefined();
expect(result.networkAnalysis).toBeDefined();
expect(result.networkAnalysis.totalConnections).toBeGreaterThan(0);
expect(result.networkAnalysis.activeConnections).toBeGreaterThan(0);
expect(result.influenceMetrics).toBeDefined();
expect(result.communities).toBeDefined();
expect(Array.isArray(result.communities)).toBe(true);
expect(result.relationships).toBeDefined();
expect(result.recommendations).toBeDefined();
expect(Array.isArray(result.recommendations)).toBe(true);
expect(result.metadata).toBeDefined();
expect(result.metadata.analysisTime).toBeGreaterThan(0);
});
it('should traverse social network using Atlas $graphLookup', async () => {
if (shouldSkipTest() || !socialCollection || !socialEngine) return;
const agentId = 'test_agent_social_002';
// Store a simple network for traversal testing
await socialCollection.storeSocialConnection({
agentId,
timestamp: new Date(),
connection: {
id: 'conn_traversal_test',
type: 'friendship',
status: 'active',
participants: {
source: { id: agentId, name: 'Test Agent', type: 'agent', role: 'self' },
target: { id: 'friend_001', name: 'Friend One', type: 'human', role: 'friend' }
},
strength: {
overall: 0.8,
emotional: 0.8,
professional: 0.5,
frequency: 0.7,
trust: 0.9,
influence: 0.6
},
context: {
origin: {
how_met: 'school',
when_met: new Date('2019-01-01'),
where_met: 'School',
circumstances: 'classmates',
mutual_connections: []
},
interactions: {
total_count: 100,
recent_count: 10,
frequency_pattern: 'weekly',
communication_channels: [],
interaction_types: []
},
dynamics: {
power_balance: 0,
communication_style: 'friendly',
conflict_resolution: 'collaborative',
emotional_support: 0.8,
reciprocity: 0.8
},
commonalities: {
shared_interests: ['sports'],
shared_values: ['friendship'],
shared_experiences: ['school'],
shared_goals: ['fun'],
shared_connections: [],
compatibility_score: 0.8
}
},
network_position: {
centrality: {
degree: 5,
betweenness: 0.5,
closeness: 0.6,
eigenvector: 0.5
},
influence: {
reach: 25,
authority: 0.5,
persuasiveness: 0.6,
network_effect: 0.5
},
communities: []
}
},
evolution: {
milestones: [],
trajectory: {
direction: 'stable',
rate_of_change: 0,
predicted_future: 'maintaining',
stability: 0.8
},
quality_trends: []
},
insights: {
patterns: [],
social_skills: {
communication: 0.7,
empathy: 0.8,
conflict_resolution: 0.7,
leadership: 0.5,
collaboration: 0.8,
networking: 0.6
},
recommendations: []
},
metadata: {
framework: 'test',
version: '1.0.0',
source: 'test',
reliability: 0.8,
lastValidated: new Date(),
quality: {
completeness: 0.8,
accuracy: 0.8,
freshness: 0.8,
consistency: 0.8
},
graph_analysis: {
last_analyzed: new Date(),
analysis_depth: 2,
computation_time: 100,
network_size: 10
}
}
});
// Test network traversal
const traversalRequest: NetworkTraversalRequest = {
agentId,
startingPersonId: agentId,
traversalType: 'breadth_first',
options: {
maxDepth: 2,
minStrength: 0.3,
connectionTypes: ['friendship'],
includeInactive: false
}
};
const traversalResult = await socialEngine.traverseNetwork(traversalRequest);
expect(traversalResult).toBeDefined();
expect(Array.isArray(traversalResult)).toBe(true);
// Note: May be empty if no deep connections exist, which is expected for test data
});
it('should find mutual connections using $graphLookup', async () => {
if (shouldSkipTest() || !socialCollection || !socialEngine) return;
const agentId = 'test_agent_social_003';
// Test mutual connections (may be empty for test data)
const mutualConnections = await socialEngine.findMutualConnections(
agentId,
'person_001',
'person_002',
2
);
expect(mutualConnections).toBeDefined();
expect(Array.isArray(mutualConnections)).toBe(true);
// Note: May be empty if no mutual connections exist, which is expected for test data
});
it('should detect social communities using graph analysis', async () => {
if (shouldSkipTest() || !socialCollection || !socialEngine) return;
const agentId = 'test_agent_social_004';
// Store a connection for community analysis
await socialCollection.storeSocialConnection({
agentId,
timestamp: new Date(),
connection: {
id: 'conn_community_test',
type: 'professional',
status: 'active',
participants: {
source: { id: agentId, name: 'Test Agent', type: 'agent', role: 'self' },
target: { id: 'colleague_001', name: 'Colleague One', type: 'human', role: 'colleague' }
},
strength: {
overall: 0.7,
emotional: 0.5,
professional: 0.9,
frequency: 0.8,
trust: 0.8,
influence: 0.7
},
context: {
origin: {
how_met: 'work',
when_met: new Date('2022-01-01'),
where_met: 'Office',
circumstances: 'new_hire',
mutual_connections: []
},
interactions: {
total_count: 50,
recent_count: 8,
frequency_pattern: 'daily',
communication_channels: [],
interaction_types: []
},
dynamics: {
power_balance: 0,
communication_style: 'professional',
conflict_resolution: 'collaborative',
emotional_support: 0.6,
reciprocity: 0.8
},
commonalities: {
shared_interests: ['technology'],
shared_values: ['professionalism'],
shared_experiences: ['work_projects'],
shared_goals: ['career_growth'],
shared_connections: [],
compatibility_score: 0.7
}
},
network_position: {
centrality: {
degree: 8,
betweenness: 0.6,
closeness: 0.7,
eigenvector: 0.6
},
influence: {
reach: 40,
authority: 0.7,
persuasiveness: 0.6,
network_effect: 0.7
},
communities: [
{
community_id: 'work_community',
community_name: 'Work Team',
role: 'member',
involvement_level: 0.8,
influence_within: 0.6
}
]
}
},
evolution: {
milestones: [],
trajectory: {
direction: 'strengthening',
rate_of_change: 0.1,
predicted_future: 'growing',
stability: 0.7
},
quality_trends: []
},
insights: {
patterns: [],
social_skills: {
communication: 0.8,
empathy: 0.6,
conflict_resolution: 0.7,
leadership: 0.6,
collaboration: 0.9,
networking: 0.7
},
recommendations: []
},
metadata: {
framework: 'test',
version: '1.0.0',
source: 'test',
reliability: 0.8,
lastValidated: new Date(),
quality: {
completeness: 0.8,
accuracy: 0.8,
freshness: 0.8,
consistency: 0.8
},
graph_analysis: {
last_analyzed: new Date(),
analysis_depth: 2,
computation_time: 80,
network_size: 15
}
}
});
const communities = await socialEngine.detectCommunities(agentId);
expect(communities).toBeDefined();
expect(communities.communities).toBeDefined();
expect(Array.isArray(communities.communities)).toBe(true);
expect(communities.networkMetrics).toBeDefined();
expect(communities.networkMetrics.totalNodes).toBeGreaterThanOrEqual(0);
expect(communities.networkMetrics.totalEdges).toBeGreaterThanOrEqual(0);
});
it('should find social influencers using graph analysis', async () => {
if (shouldSkipTest() || !socialCollection || !socialEngine) return;
const agentId = 'test_agent_social_005';
const influencers = await socialEngine.findInfluencers(agentId, {
minInfluence: 0.3,
minConnections: 2,
maxDepth: 2
});
expect(influencers).toBeDefined();
expect(Array.isArray(influencers)).toBe(true);
// Note: May be empty if no influencers meet criteria, which is expected for test data
});
});
describe('Error Handling and Edge Cases', () => {
it('should handle uninitialized engine gracefully', async () => {
if (shouldSkipTest() || !db) return;
const uninitializedEngine = new SocialIntelligenceEngine(db);
const analysisRequest: SocialAnalysisRequest = {
agentId: 'test_agent',
analysisType: 'network_analysis',
parameters: {},
context: { purpose: 'testing', scope: 'personal', priority: 'low' }
};
await expect(uninitializedEngine.performSocialAnalysis(analysisRequest))
.rejects.toThrow('SocialIntelligenceEngine not initialized');
});
it('should handle empty social network gracefully', async () => {
if (shouldSkipTest() || !socialEngine) return;
const analysisRequest: SocialAnalysisRequest = {
agentId: 'nonexistent_agent',
analysisType: 'network_analysis',
parameters: {},
context: { purpose: 'testing', scope: 'personal', priority: 'low' }
};
const result = await socialEngine.performSocialAnalysis(analysisRequest);
expect(result).toBeDefined();
expect(result.networkAnalysis.totalConnections).toBe(0);
expect(result.networkAnalysis.activeConnections).toBe(0);
});
});
console.log(`
🎯 SOCIAL INTELLIGENCE ENGINE - COMPREHENSIVE TEST SUMMARY
==========================================================
This comprehensive test demonstrates the SocialIntelligenceEngine's Atlas capabilities:
✅ MONGODB ATLAS $GRAPHLOOKUP SHOWCASED:
• $graphLookup aggregation stage for recursive social network traversal
• Graph-based relationship analysis and social intelligence
• Multi-depth social connection exploration and community detection
• Social influence and network position analysis
🧠 SOCIAL INTELLIGENCE CAPABILITIES:
• Social network mapping and analysis using $graphLookup
• Recursive relationship traversal and graph exploration
• Social influence and connection strength analysis
• Community detection and social clustering
• Social interaction pattern recognition and insights
• Mutual connection discovery and bridge analysis
🔬 ADVANCED FEATURES:
• Multi-dimensional social relationship modeling
• Network centrality and influence calculations
• Social community detection and role analysis
• Relationship evolution and trajectory tracking
• Social skills assessment and competency analysis
• Personalized social recommendations and insights
📊 REAL-WORLD APPLICATIONS:
• Social network optimization and relationship building
• Influence mapping and authority identification
• Community engagement and leadership development
• Social conflict resolution and mediation
• Professional networking and career advancement
• Personal relationship enhancement and social skills development
This engine represents a breakthrough in AI social intelligence capabilities,
leveraging MongoDB Atlas's powerful $graphLookup for sophisticated social network analysis.
`);
});