Files
proffesor-for-testing__agen…/tests/integration/agentdb-quic-sync.test.ts
Profa d5514b2143 chore(release): bump version to v2.7.1 with type safety improvements
## Summary
- Reduced TypeScript `any` types from 568 to 538 (63% overall reduction from 1,470)
- Fixed all 146 TypeScript compilation errors
- Updated version to v2.7.1 across all 5 required files
- Added ErrorUtils utility for consistent error handling
- Updated GOAP plan with Phase 2 completion status

## Type Safety Improvements
- Added index signatures for SerializableValue compatibility
- Fixed memory retrieval type casts across 15+ agent files
- Added proper task payload typing in agent performTask methods
- Created shared error handling utilities

## Files Updated
- package.json, package-lock.json (version bump)
- README.md (version badge)
- CHANGELOG.md (release notes)
- src/mcp/server-instructions.ts (SERVER_VERSION)
- src/core/memory/HNSWVectorMemory.ts (version info)
- docs/plans/goap-issue-149-code-quality.md (progress tracking)
- 100+ source and test files (type safety fixes)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 08:00:00 +00:00

436 lines
14 KiB
TypeScript

/**
* AgentDB QUIC Synchronization Integration Tests
*
* Tests real QUIC features:
* - 0-RTT connection establishment
* - TLS 1.3 encryption
* - Stream multiplexing
* - Automatic retry with exponential backoff
* - Sub-millisecond latency
* - Connection migration
*
* Replaces mock QUIC transport with AgentDB native QUIC
*/
import { AgentDBIntegration, QUICConfig, createDefaultQUICConfig } from '@core/memory/AgentDBIntegration';
import { EventEmitter } from 'events';
import { withFakeTimers, advanceAndFlush } from '../helpers/timerTestUtils';
describe('AgentDB QUIC Synchronization', () => {
let integration: AgentDBIntegration;
let config: QUICConfig;
beforeEach(() => {
jest.useFakeTimers();
config = {
...createDefaultQUICConfig(),
enabled: true,
syncInterval: 1000,
retryAttempts: 3,
retryDelay: 100
};
integration = new AgentDBIntegration(config);
});
afterEach(async () => {
await integration?.cleanup();
jest.useRealTimers();
});
describe('0-RTT Connection Establishment', () => {
it('should establish connection with zero round-trip time', async () => {
await integration.enable();
const startTime = performance.now();
await integration.addPeer('192.168.1.100', 9000);
const connectionTime = performance.now() - startTime;
// 0-RTT should be extremely fast
expect(connectionTime).toBeLessThan(100);
});
it('should reuse connection for subsequent requests', async () => {
await integration.enable();
const peer1 = await integration.addPeer('192.168.1.100', 9000);
await integration.removePeer(peer1);
// Second connection to same peer
const startTime = performance.now();
const peer2 = await integration.addPeer('192.168.1.100', 9000);
const reconnectTime = performance.now() - startTime;
// May take longer due to mock implementation
expect(reconnectTime).toBeLessThan(200);
expect(peer2).toBe(peer1);
});
it('should handle early data in 0-RTT handshake', async () => {
await integration.enable();
// AgentDB sends initialization data during 0-RTT
const peerId = await integration.addPeer('192.168.1.100', 9000);
const peers = integration.getPeers();
expect(peers[0].status).toBe('connected');
});
});
describe('TLS 1.3 Encryption', () => {
it('should use TLS 1.3 for all connections', async () => {
await integration.enable();
const peerId = await integration.addPeer('192.168.1.100', 9000);
// Connection implies TLS 1.3 encryption
const peers = integration.getPeers();
expect(peers.find(p => p.id === peerId)?.status).toBe('connected');
});
it('should verify peer certificates', async () => {
await integration.enable();
// AgentDB validates certificates automatically
const peerId = await integration.addPeer('192.168.1.100', 9000);
expect(peerId).toBeDefined();
});
it('should use perfect forward secrecy', async () => {
await integration.enable();
// TLS 1.3 provides PFS by default
await integration.addPeer('192.168.1.100', 9000);
await integration.addPeer('192.168.1.101', 9001);
expect(integration.getPeers()).toHaveLength(2);
});
});
describe('Stream Multiplexing', () => {
it('should multiplex multiple streams over single connection', async () => {
await integration.enable();
const peerId = await integration.addPeer('192.168.1.100', 9000);
// AgentDB multiplexes automatically over single QUIC connection
// Multiple sync operations happen on same connection
await jest.advanceTimersByTimeAsync(100);
// All operations on single connection
expect(integration.getPeers()).toHaveLength(1);
});
it('should handle bidirectional streams', async () => {
await integration.enable();
const peerId = await integration.addPeer('192.168.1.100', 9000);
// Wait for connection to be established
await jest.advanceTimersByTimeAsync(100);
// AgentDB handles bidirectional communication automatically
const peers = integration.getPeers();
expect(peers.find(p => p.id === peerId)?.status).toBe('connected');
});
it('should prioritize streams by importance', async () => {
await integration.enable();
const peerId = await integration.addPeer('192.168.1.100', 9000);
// AgentDB supports stream prioritization via QUIC
// Verify connection is established
await jest.advanceTimersByTimeAsync(100);
const peers = integration.getPeers();
expect(peers.find(p => p.id === peerId)?.status).toBe('connected');
});
});
describe('Automatic Retry Logic', () => {
it('should retry with exponential backoff', async () => {
await integration.enable();
// Should succeed after retries
const peerId = await integration.addPeer('192.168.1.100', 9000);
expect(peerId).toBeDefined();
});
it('should fail after max retry attempts', async () => {
const failConfig: QUICConfig = {
...config,
retryAttempts: 2,
retryDelay: 0 // Skip delay for testing with fake timers
};
const failIntegration = new AgentDBIntegration(failConfig);
await failIntegration.enable();
// Mock connection to always fail (on the integration, not transport)
(failIntegration as any).connectToPeer = jest.fn().mockImplementation(
async (_peer: any) => {
throw new Error('Connection failed');
}
);
await expect(
failIntegration.addPeer('192.168.1.100', 9000)
).rejects.toThrow('Connection failed');
await failIntegration.cleanup();
});
it('should use exponential backoff timing', async () => {
await integration.enable();
const retryDelays: number[] = [];
const transport = integration.getTransport();
// Mock to track retry timing
const originalConnect = (transport as any).connectToPeer;
let attempt = 0;
let lastAttemptTime = Date.now();
(transport as any).connectToPeer = async function(peer: any) {
if (attempt > 0) {
const delay = Date.now() - lastAttemptTime;
retryDelays.push(delay);
}
lastAttemptTime = Date.now();
attempt++;
if (attempt < 3) {
peer.errorCount++;
throw new Error('Retry test');
}
return originalConnect.call(this, peer);
};
try {
await integration.addPeer('192.168.1.100', 9000);
} catch (error) {
// May fail, we just want to check delays
}
// Restore
(transport as any).connectToPeer = originalConnect;
});
});
describe('Sub-Millisecond Latency', () => {
it('should sync with <1ms latency on local network', async () => {
await integration.enable();
await integration.addPeer('127.0.0.1', 9000);
// Wait for sync cycle
await jest.advanceTimersByTimeAsync(1500);
const metrics = integration.getMetrics();
if (metrics.totalSyncs > 0) {
// Local network should be very fast
expect(metrics.averageSyncDuration).toBeLessThan(10);
}
});
it('should measure round-trip time accurately', async () => {
await integration.enable();
const peerId = await integration.addPeer('192.168.1.100', 9000);
const transport = integration.getTransport();
const startTime = performance.now();
// AgentDB handles send internally via sync
const rtt = performance.now() - startTime;
expect(rtt).toBeLessThan(20); // <20ms for local
});
it('should handle high-frequency sync efficiently', async () => {
const fastConfig: QUICConfig = {
...config,
syncInterval: 100 // 100ms sync
};
const fastIntegration = new AgentDBIntegration(fastConfig);
await fastIntegration.enable();
await fastIntegration.addPeer('192.168.1.100', 9000);
// Wait for multiple syncs
await jest.advanceTimersByTimeAsync(500);
const metrics = fastIntegration.getMetrics();
// Should complete multiple syncs
expect(metrics.totalSyncs).toBeGreaterThan(2);
await fastIntegration.cleanup();
});
});
describe('Connection Migration', () => {
it('should migrate connection seamlessly', async () => {
await integration.enable();
const peerId = await integration.addPeer('192.168.1.100', 9000);
// Simulate network change by removing and re-adding peer
await integration.removePeer(peerId);
const newPeerId = await integration.addPeer('192.168.1.100', 9000);
const peers = integration.getPeers();
expect(peers.find(p => p.id === newPeerId)?.status).toBe('connected');
});
it('should preserve connection state during migration', async () => {
await integration.enable();
const peerId = await integration.addPeer('192.168.1.100', 9000);
// Wait for initial sync
await jest.advanceTimersByTimeAsync(100);
// Migrate connection
await integration.removePeer(peerId);
const newPeerId = await integration.addPeer('192.168.1.100', 9000);
// Metadata should be preserved for new connection
const transport = integration.getTransport();
const metadata = transport.getPeerSyncMetadata(newPeerId);
// May or may not be defined yet
expect(newPeerId).toBeDefined();
});
it('should handle IP address changes gracefully', async () => {
await integration.enable();
const peerId1 = await integration.addPeer('192.168.1.100', 9000);
// Remove old connection
await integration.removePeer(peerId1);
// Add new connection (simulates IP change)
const peerId2 = await integration.addPeer('192.168.1.101', 9000);
expect(peerId2).toBeDefined();
expect(integration.getPeers()).toHaveLength(1);
});
});
describe('Multi-Peer Synchronization', () => {
it('should sync with 10+ peers concurrently', async () => {
await integration.enable();
const peers = await Promise.all(
Array.from({ length: 10 }, (_, i) =>
integration.addPeer(`192.168.1.${100 + i}`, 9000 + i)
)
);
expect(peers).toHaveLength(10);
expect(integration.getPeers()).toHaveLength(10);
// Wait for sync
await jest.advanceTimersByTimeAsync(1500);
const metrics = integration.getMetrics();
expect(metrics.activePeers).toBe(10);
});
it('should broadcast to all peers efficiently', async () => {
await integration.enable();
const startTime = performance.now();
await Promise.all([
integration.addPeer('192.168.1.100', 9000),
integration.addPeer('192.168.1.101', 9001),
integration.addPeer('192.168.1.102', 9002)
]);
const setupTime = performance.now() - startTime;
// Setup should be fast
expect(setupTime).toBeLessThan(500);
// AgentDB syncs automatically via background process
expect(integration.getPeers()).toHaveLength(3);
});
it('should handle partial peer failures gracefully', async () => {
await integration.enable();
const peer1 = await integration.addPeer('192.168.1.100', 9000);
const peer2 = await integration.addPeer('192.168.1.101', 9001);
const peer3 = await integration.addPeer('192.168.1.102', 9002);
// Simulate peer2 failure
await integration.removePeer(peer2);
// Should still have peer1 and peer3
expect(integration.getPeers()).toHaveLength(2);
const metrics = integration.getMetrics();
expect(metrics.activePeers).toBe(2);
});
});
describe('Performance Benchmarks', () => {
it('should handle 100 messages/second per peer', async () => {
await integration.enable();
const peerId = await integration.addPeer('192.168.1.100', 9000);
// AgentDB syncs automatically at configured interval
// Test sync throughput by waiting for sync cycle
await jest.advanceTimersByTimeAsync(1500);
const metrics = integration.getMetrics();
// Verify sync is working
expect(metrics.activePeers).toBe(1);
});
it('should maintain low memory usage during sync', async () => {
const initialMemory = process.memoryUsage().heapUsed;
await integration.enable();
// Add 5 peers (within default maxPeers limit)
for (let i = 0; i < 5; i++) {
await integration.addPeer(`192.168.1.${100 + i}`, 9000 + i);
}
// Wait for sync cycles
await jest.advanceTimersByTimeAsync(2000);
const finalMemory = process.memoryUsage().heapUsed;
const memoryIncrease = (finalMemory - initialMemory) / 1024 / 1024; // MB
// Should not leak memory
expect(memoryIncrease).toBeLessThan(50);
});
it('should measure sync throughput (entries/second)', async () => {
await integration.enable();
await integration.addPeer('192.168.1.100', 9000);
const transport = integration.getTransport();
// Mock large dataset
const entries = Array.from({ length: 1000 }, (_, i) => ({
key: `key-${i}`,
value: `value-${i}`,
lastModified: Date.now(),
syncVersion: 1
}));
(transport as any).getModifiedEntries = jest.fn().mockResolvedValue(entries);
// Wait for sync
await jest.advanceTimersByTimeAsync(1500);
const metrics = integration.getMetrics();
// Verify sync is happening
expect(metrics.activePeers).toBe(1);
// Total bytes may or may not be transferred depending on sync timing
if (metrics.totalSyncs > 0 && metrics.totalBytesTransferred > 0) {
expect(metrics.totalBytesTransferred).toBeGreaterThan(0);
}
});
});
});