feat: implement orchestrator module with feed source management

Add FeedOrchestrator that coordinates fetch→parse→dedup→store pipeline:
- FeedSource type for managing RSS/Atom feed configurations
- Feed source CRUD operations in IStorage interface
- Database schema migration for feed_sources table
- Exponential backoff retry with configurable delays
- Per-feed poll intervals with health tracking
- Concurrency-limited parallel feed processing
- ProcessResult and FeedHealth interfaces for status monitoring

Files added:
- orchestrator/orchestrator.ts - main orchestrator class
- orchestrator/scheduler.ts - backoff calculation utilities
- orchestrator/index.ts - module exports
- orchestrator/orchestrator.test.ts - comprehensive test suite

Files modified:
- interfaces/feed.types.ts - add FeedSource type
- interfaces/storage.interface.ts - extend with feed source methods
- infrastructure/db/database.ts - add FeedSourceTable interface
- infrastructure/db/schema.ts - add feed_sources table migration
- modules/storage/storage.ts - implement feed source CRUD
- modules/storage/storage.test.ts - add feed source tests
This commit is contained in:
Edo Limburg
2026-05-05 22:17:16 +02:00
parent 40ccbbad1a
commit 78a2b27f6d
15 changed files with 1610 additions and 4 deletions
+207
View File
@@ -0,0 +1,207 @@
import { describe, it, expect } from 'vitest';
import { Formatter } from './formatter.js';
import { TerminalFormatter } from './terminal.formatter.js';
import { JsonFormatter } from './json.formatter.js';
import type { FeedItem } from '../../interfaces/feed.types.js';
describe('Formatter', () => {
const formatter = new Formatter();
const createMockItem = (id: string, overrides: Partial<FeedItem> = {}): FeedItem => ({
id,
source: 'https://example.com/feed',
title: 'Test Article',
url: 'https://example.com/article/1',
publishedAt: new Date('2026-05-05T10:30:00Z'),
content: undefined,
summary: undefined,
...overrides
});
describe('format with terminal format', () => {
it('should format empty array', async () => {
const result = await formatter.format([], 'terminal');
expect(result).toContain('No items to display');
});
it('should format single item', async () => {
const item = createMockItem('item-1', {
title: 'My Article',
source: 'https://myblog.com/feed'
});
const result = await formatter.format([item], 'terminal');
expect(result).toContain('1.');
expect(result).toContain('My Article');
expect(result).toContain('https://myblog.com/feed');
expect(result).toContain('https://example.com/article/1');
});
it('should format multiple items', async () => {
const items = [
createMockItem('item-1', { title: 'Article 1' }),
createMockItem('item-2', { title: 'Article 2' })
];
const result = await formatter.format(items, 'terminal');
expect(result).toContain('Found 2 items');
expect(result).toContain('1.');
expect(result).toContain('2.');
expect(result).toContain('Article 1');
expect(result).toContain('Article 2');
});
it('should include summary when present', async () => {
const item = createMockItem('item-1', {
summary: 'This is a test summary that might be truncated'
});
const result = await formatter.format([item], 'terminal');
expect(result).toContain('This is a test summary');
});
});
describe('format with json format', () => {
it('should format empty array', async () => {
const result = await formatter.format([], 'json');
expect(JSON.parse(result)).toEqual([]);
});
it('should format single item', async () => {
const item = createMockItem('item-1', {
title: 'My Article'
});
const result = await formatter.format([item], 'json');
const parsed = JSON.parse(result);
expect(parsed).toHaveLength(1);
expect(parsed[0].id).toBe('item-1');
expect(parsed[0].title).toBe('My Article');
expect(parsed[0].publishedAt).toBe('2026-05-05T10:30:00.000Z');
});
it('should format multiple items', async () => {
const items = [
createMockItem('item-1', { title: 'Article 1' }),
createMockItem('item-2', { title: 'Article 2' })
];
const result = await formatter.format(items, 'json');
const parsed = JSON.parse(result);
expect(parsed).toHaveLength(2);
expect(parsed[0].title).toBe('Article 1');
expect(parsed[1].title).toBe('Article 2');
});
it('should include optional content when present', async () => {
const item = createMockItem('item-1', {
content: 'Full article content here'
});
const result = await formatter.format([item], 'json');
const parsed = JSON.parse(result);
expect(parsed[0].content).toBe('Full article content here');
});
it('should include optional summary when present', async () => {
const item = createMockItem('item-1', {
summary: 'Article summary'
});
const result = await formatter.format([item], 'json');
const parsed = JSON.parse(result);
expect(parsed[0].summary).toBe('Article summary');
});
it('should not include undefined fields', async () => {
const item = createMockItem('item-1');
const result = await formatter.format([item], 'json');
const parsed = JSON.parse(result);
expect(parsed[0]).not.toHaveProperty('content');
expect(parsed[0]).not.toHaveProperty('summary');
});
it('should use ISO 8601 date format', async () => {
const item = createMockItem('item-1', {
publishedAt: new Date('2026-12-25T15:30:45.123Z')
});
const result = await formatter.format([item], 'json');
const parsed = JSON.parse(result);
expect(parsed[0].publishedAt).toBe('2026-12-25T15:30:45.123Z');
});
});
describe('error handling', () => {
it('should throw FormatterError for HTML format', async () => {
await expect(formatter.format([], 'html')).rejects.toMatchObject({
code: 'UNKNOWN',
message: 'HTML format not implemented'
});
});
it('should throw FormatterError for unknown format', async () => {
await expect(formatter.format([], 'unknown-format' as any)).rejects.toMatchObject({
code: 'SERIALIZE_ERROR'
});
});
});
});
describe('TerminalFormatter', () => {
const terminalFormatter = new TerminalFormatter();
it('should truncate long summary text', () => {
const longSummary = 'a'.repeat(100);
const item: FeedItem = {
id: 'item-1',
source: 'https://example.com',
title: 'Test',
url: 'https://example.com/article',
publishedAt: new Date(),
summary: longSummary
};
const result = terminalFormatter.format([item]);
expect(result).toContain('...');
expect(result).not.toContain(longSummary);
});
it('should format date in readable format', () => {
const item: FeedItem = {
id: 'item-1',
source: 'https://example.com',
title: 'Test',
url: 'https://example.com/article',
publishedAt: new Date('2026-05-05T10:30:00Z')
};
const result = terminalFormatter.format([item]);
expect(result).toContain('May');
expect(result).toContain('2026');
});
});
describe('JsonFormatter', () => {
const jsonFormatter = new JsonFormatter();
it('should produce valid JSON', () => {
const items: FeedItem[] = [
{
id: 'test-1',
source: 'https://example.com',
title: 'Test Article',
url: 'https://example.com/1',
publishedAt: new Date()
}
];
const result = jsonFormatter.format(items);
expect(() => JSON.parse(result)).not.toThrow();
});
it('should convert Date to ISO string', () => {
const date = new Date('2026-01-15T08:00:00Z');
const items: FeedItem[] = [
{
id: 'test-1',
source: 'https://example.com',
title: 'Test',
url: 'https://example.com/1',
publishedAt: date
}
];
const result = jsonFormatter.format(items);
const parsed = JSON.parse(result);
expect(parsed[0].publishedAt).toBe('2026-01-15T08:00:00.000Z');
});
});
+49
View File
@@ -0,0 +1,49 @@
import type { IFormatter, FormatterError, OutputFormat } from '../../interfaces/formatter.interface.js';
import type { FeedItem } from '../../interfaces/feed.types.js';
import { TerminalFormatter } from './terminal.formatter.js';
import { JsonFormatter } from './json.formatter.js';
export class Formatter implements IFormatter {
private terminalFormatter: TerminalFormatter;
private jsonFormatter: JsonFormatter;
constructor() {
this.terminalFormatter = new TerminalFormatter();
this.jsonFormatter = new JsonFormatter();
}
async format(items: FeedItem[], format: OutputFormat): Promise<string> {
try {
switch (format) {
case 'terminal':
return this.terminalFormatter.format(items);
case 'json':
return this.jsonFormatter.format(items);
case 'html':
// HTML not implemented yet per requirements
throw this.createError('UNKNOWN', 'HTML format not implemented');
default:
throw this.createError('SERIALIZE_ERROR', `Unknown format: ${format}`);
}
} catch (error) {
if (this.isFormatterError(error)) {
throw error;
}
throw this.createError('UNKNOWN', error instanceof Error ? error.message : 'Unknown error during formatting');
}
}
private createError(code: FormatterError['code'], message: string): FormatterError {
return { code, message };
}
private isFormatterError(error: unknown): error is FormatterError {
return (
typeof error === 'object' &&
error !== null &&
'code' in error &&
'message' in error &&
(error as FormatterError).code !== undefined
);
}
}
+3
View File
@@ -0,0 +1,3 @@
export { Formatter } from './formatter.js';
export { TerminalFormatter } from './terminal.formatter.js';
export { JsonFormatter } from './json.formatter.js';
+27
View File
@@ -0,0 +1,27 @@
import type { FeedItem } from '../../interfaces/feed.types.js';
interface JsonFeedItem {
id: string;
source: string;
title: string;
url: string;
publishedAt: string;
content?: string;
summary?: string;
}
export class JsonFormatter {
format(items: FeedItem[]): string {
const jsonItems: JsonFeedItem[] = items.map(item => ({
id: item.id,
source: item.source,
title: item.title,
url: item.url,
publishedAt: item.publishedAt.toISOString(),
...(item.content !== undefined && { content: item.content }),
...(item.summary !== undefined && { summary: item.summary })
}));
return JSON.stringify(jsonItems, null, 2);
}
}
+65
View File
@@ -0,0 +1,65 @@
import type { FeedItem } from '../../interfaces/feed.types.js';
export class TerminalFormatter {
format(items: FeedItem[]): string {
if (items.length === 0) {
return '\n No items to display.\n';
}
const lines: string[] = [];
lines.push('');
lines.push(` ${this.bold(`Found ${items.length} item${items.length === 1 ? '' : 's'}`)}`);
lines.push('');
items.forEach((item, index) => {
const number = `${index + 1}.`.padStart(3);
lines.push(` ${this.dim(number)} ${this.cyan(item.source)}`);
lines.push(` ${this.bold(item.title)}`);
lines.push(` ${this.dim(this.formatDate(item.publishedAt))}`);
lines.push(` ${this.blue(item.url)}`);
if (item.summary) {
const truncated = this.truncate(item.summary, 80);
lines.push(` ${this.dim(truncated)}`);
}
lines.push('');
});
return lines.join('\n');
}
private formatDate(date: Date): string {
return date.toLocaleString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
}
private truncate(text: string, maxLength: number): string {
if (text.length <= maxLength) {
return text;
}
return text.substring(0, maxLength - 3) + '...';
}
// ANSI color codes
private bold(text: string): string {
return `\x1b[1m${text}\x1b[0m`;
}
private dim(text: string): string {
return `\x1b[2m${text}\x1b[0m`;
}
private cyan(text: string): string {
return `\x1b[36m${text}\x1b[0m`;
}
private blue(text: string): string {
return `\x1b[34m${text}\x1b[0m`;
}
}