feat(#71): implement graph data API
Implemented three new API endpoints for knowledge graph visualization: 1. GET /api/knowledge/graph - Full knowledge graph - Returns all entries and links with optional filtering - Supports filtering by tags, status, and node count limit - Includes orphan detection (entries with no links) 2. GET /api/knowledge/graph/stats - Graph statistics - Total entries and links counts - Orphan entries detection - Average links per entry - Top 10 most connected entries - Tag distribution across entries 3. GET /api/knowledge/graph/:slug - Entry-centered subgraph - Returns graph centered on specific entry - Supports depth parameter (1-5) for traversal distance - Includes all connected nodes up to specified depth New Files: - apps/api/src/knowledge/graph.controller.ts - apps/api/src/knowledge/graph.controller.spec.ts Modified Files: - apps/api/src/knowledge/dto/graph-query.dto.ts (added GraphFilterDto) - apps/api/src/knowledge/entities/graph.entity.ts (extended with new types) - apps/api/src/knowledge/services/graph.service.ts (added new methods) - apps/api/src/knowledge/services/graph.service.spec.ts (added tests) - apps/api/src/knowledge/knowledge.module.ts (registered controller) - apps/api/src/knowledge/dto/index.ts (exported new DTOs) - docs/scratchpads/71-graph-data-api.md (implementation notes) Test Coverage: 21 tests (all passing) - 14 service tests including orphan detection, filtering, statistics - 7 controller tests for all three endpoints Follows TDD principles with tests written before implementation. All code quality gates passed (lint, typecheck, tests). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
185
apps/orchestrator/src/queue/queue.service.spec.ts
Normal file
185
apps/orchestrator/src/queue/queue.service.spec.ts
Normal file
@@ -0,0 +1,185 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { QueueService } from './queue.service';
|
||||
|
||||
describe('QueueService', () => {
|
||||
describe('calculateBackoffDelay', () => {
|
||||
let service: QueueService;
|
||||
|
||||
beforeEach(() => {
|
||||
// Create a minimal instance for testing pure functions
|
||||
const mockValkeyService: any = {
|
||||
updateTaskStatus: vi.fn(),
|
||||
publishEvent: vi.fn(),
|
||||
};
|
||||
const mockConfigService: any = {
|
||||
get: vi.fn((key: string, defaultValue?: unknown) => defaultValue),
|
||||
};
|
||||
service = new QueueService(mockValkeyService, mockConfigService);
|
||||
});
|
||||
|
||||
it('should calculate exponential backoff delay', () => {
|
||||
const baseDelay = 1000;
|
||||
const maxDelay = 60000;
|
||||
|
||||
// Attempt 1: 2000ms (1000 * 2^1)
|
||||
const delay1 = service.calculateBackoffDelay(1, baseDelay, maxDelay);
|
||||
expect(delay1).toBe(2000);
|
||||
|
||||
// Attempt 2: 4000ms (1000 * 2^2)
|
||||
const delay2 = service.calculateBackoffDelay(2, baseDelay, maxDelay);
|
||||
expect(delay2).toBe(4000);
|
||||
|
||||
// Attempt 3: 8000ms (1000 * 2^3)
|
||||
const delay3 = service.calculateBackoffDelay(3, baseDelay, maxDelay);
|
||||
expect(delay3).toBe(8000);
|
||||
|
||||
// Attempt 4: 16000ms (1000 * 2^4)
|
||||
const delay4 = service.calculateBackoffDelay(4, baseDelay, maxDelay);
|
||||
expect(delay4).toBe(16000);
|
||||
});
|
||||
|
||||
it('should cap delay at maxDelay', () => {
|
||||
const baseDelay = 1000;
|
||||
const maxDelay = 60000;
|
||||
|
||||
// Attempt 10 would be 1024000ms, but should be capped at 60000ms
|
||||
const delay10 = service.calculateBackoffDelay(10, baseDelay, maxDelay);
|
||||
expect(delay10).toBe(maxDelay);
|
||||
|
||||
// Attempt 7 would be 128000ms, should be capped at 60000ms
|
||||
const delay7 = service.calculateBackoffDelay(7, baseDelay, maxDelay);
|
||||
expect(delay7).toBe(maxDelay);
|
||||
});
|
||||
|
||||
it('should handle zero baseDelay', () => {
|
||||
const delay = service.calculateBackoffDelay(3, 0, 60000);
|
||||
expect(delay).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle attempt 0', () => {
|
||||
const delay = service.calculateBackoffDelay(0, 1000, 60000);
|
||||
expect(delay).toBe(1000); // 1000 * 2^0 = 1000
|
||||
});
|
||||
|
||||
it('should handle large attempt numbers', () => {
|
||||
const baseDelay = 1000;
|
||||
const maxDelay = 100000;
|
||||
|
||||
const delay = service.calculateBackoffDelay(20, baseDelay, maxDelay);
|
||||
expect(delay).toBe(maxDelay);
|
||||
});
|
||||
|
||||
it('should work with different base delays', () => {
|
||||
const maxDelay = 100000;
|
||||
|
||||
// 500ms base
|
||||
const delay1 = service.calculateBackoffDelay(2, 500, maxDelay);
|
||||
expect(delay1).toBe(2000); // 500 * 2^2
|
||||
|
||||
// 2000ms base
|
||||
const delay2 = service.calculateBackoffDelay(2, 2000, maxDelay);
|
||||
expect(delay2).toBe(8000); // 2000 * 2^2
|
||||
});
|
||||
});
|
||||
|
||||
describe('validation logic', () => {
|
||||
let service: QueueService;
|
||||
let mockValkeyService: any;
|
||||
let mockConfigService: any;
|
||||
|
||||
beforeEach(() => {
|
||||
mockValkeyService = {
|
||||
updateTaskStatus: vi.fn().mockResolvedValue(undefined),
|
||||
publishEvent: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
mockConfigService = {
|
||||
get: vi.fn((key: string, defaultValue?: unknown) => {
|
||||
const config: Record<string, unknown> = {
|
||||
'orchestrator.valkey.host': 'localhost',
|
||||
'orchestrator.valkey.port': 6379,
|
||||
'orchestrator.queue.name': 'orchestrator-tasks',
|
||||
'orchestrator.queue.maxRetries': 3,
|
||||
'orchestrator.queue.baseDelay': 1000,
|
||||
'orchestrator.queue.maxDelay': 60000,
|
||||
'orchestrator.queue.concurrency': 5,
|
||||
};
|
||||
return config[key] ?? defaultValue;
|
||||
}),
|
||||
};
|
||||
service = new QueueService(mockValkeyService, mockConfigService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
expect(service.calculateBackoffDelay).toBeDefined();
|
||||
});
|
||||
|
||||
it('should load configuration from ConfigService', () => {
|
||||
expect(mockConfigService.get).toHaveBeenCalledWith(
|
||||
'orchestrator.queue.name',
|
||||
'orchestrator-tasks'
|
||||
);
|
||||
expect(mockConfigService.get).toHaveBeenCalledWith(
|
||||
'orchestrator.queue.maxRetries',
|
||||
3
|
||||
);
|
||||
expect(mockConfigService.get).toHaveBeenCalledWith(
|
||||
'orchestrator.queue.baseDelay',
|
||||
1000
|
||||
);
|
||||
expect(mockConfigService.get).toHaveBeenCalledWith(
|
||||
'orchestrator.queue.maxDelay',
|
||||
60000
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('retry configuration', () => {
|
||||
it('should use default retry configuration', () => {
|
||||
const mockValkeyService: any = {
|
||||
updateTaskStatus: vi.fn(),
|
||||
publishEvent: vi.fn(),
|
||||
};
|
||||
const mockConfigService: any = {
|
||||
get: vi.fn((key: string, defaultValue?: unknown) => defaultValue),
|
||||
};
|
||||
|
||||
const service = new QueueService(mockValkeyService, mockConfigService);
|
||||
|
||||
// Verify defaults were requested
|
||||
expect(mockConfigService.get).toHaveBeenCalledWith(
|
||||
'orchestrator.queue.maxRetries',
|
||||
3
|
||||
);
|
||||
expect(mockConfigService.get).toHaveBeenCalledWith(
|
||||
'orchestrator.queue.baseDelay',
|
||||
1000
|
||||
);
|
||||
expect(mockConfigService.get).toHaveBeenCalledWith(
|
||||
'orchestrator.queue.maxDelay',
|
||||
60000
|
||||
);
|
||||
});
|
||||
|
||||
it('should use custom retry configuration from env', () => {
|
||||
const mockValkeyService: any = {
|
||||
updateTaskStatus: vi.fn(),
|
||||
publishEvent: vi.fn(),
|
||||
};
|
||||
const mockConfigService: any = {
|
||||
get: vi.fn((key: string, defaultValue?: unknown) => {
|
||||
if (key === 'orchestrator.queue.maxRetries') return 5;
|
||||
if (key === 'orchestrator.queue.baseDelay') return 2000;
|
||||
if (key === 'orchestrator.queue.maxDelay') return 120000;
|
||||
return defaultValue;
|
||||
}),
|
||||
};
|
||||
|
||||
const service = new QueueService(mockValkeyService, mockConfigService);
|
||||
|
||||
// Verify custom values were used
|
||||
const delay1 = service.calculateBackoffDelay(1, 2000, 120000);
|
||||
expect(delay1).toBe(4000); // 2000 * 2^1
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user