Add Health/Recommendations/Request/Webhook controllers

This commit is contained in:
2026-08-12 21:21:26 +02:00
parent 3c5f723d22
commit e712470013
8 changed files with 313 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
import { describe, expect, it } from 'vitest';
import Fastify from 'fastify';
import { HealthController } from '../../../src/app/controllers/HealthController';
import type { AppConfig } from '../../../src/app/config/AppConfig';
describe('HealthController', () => {
it('reports status ok with llm/radarr flags from config', async () => {
const app = Fastify();
new HealthController({ LLM_ENABLED: true, RADARR_ENABLED: false } as AppConfig).register(app);
const res = await app.inject({ method: 'GET', url: '/health' });
expect(res.statusCode).toBe(200);
expect(res.json()).toEqual({ status: 'ok', llm: true, radarr: false });
});
});

View File

@@ -0,0 +1,66 @@
import { describe, expect, it, vi } from 'vitest';
import Fastify from 'fastify';
import { RecommendationsController } from '../../../src/app/controllers/RecommendationsController';
import type { AppConfig } from '../../../src/app/config/AppConfig';
import type { RecommendationPipeline } from '../../../src/app/pipelines/RecommendationPipeline';
function makeApp(config: Partial<AppConfig>, pipeline: Partial<RecommendationPipeline>) {
const app = Fastify();
new RecommendationsController(config as AppConfig, pipeline as RecommendationPipeline).register(app);
return app;
}
describe('RecommendationsController', () => {
it('GET /recommendations runs the pipeline with the configured count and LLM flag', async () => {
const runAsync = vi.fn().mockResolvedValue([{ title: 'Dune', year: 2021, tmdbId: 1, reason: 'r' }]);
const app = makeApp({ RECOMMEND_COUNT: 10, LLM_ENABLED: true }, { runAsync } as unknown as RecommendationPipeline);
const res = await app.inject({ method: 'GET', url: '/recommendations' });
expect(res.statusCode).toBe(200);
expect(runAsync).toHaveBeenCalledWith({ count: 10, useLlm: true });
});
it('GET /recommendations?count= and force_llm/force_tmdb override config', async () => {
const runAsync = vi.fn().mockResolvedValue([]);
const app = makeApp({ RECOMMEND_COUNT: 10, LLM_ENABLED: false }, { runAsync } as unknown as RecommendationPipeline);
await app.inject({ method: 'GET', url: '/recommendations?count=3&force_llm=true' });
expect(runAsync).toHaveBeenCalledWith({ count: 3, useLlm: true });
});
it('POST /recommendations/trigger auto-requests via Radarr when enabled', async () => {
const recs = [{ title: 'Dune', year: 2021, tmdbId: 1, reason: 'r' }];
const runAsync = vi.fn().mockResolvedValue(recs);
const autoRequest = vi.fn().mockResolvedValue(undefined);
const app = makeApp(
{ RECOMMEND_COUNT: 10, LLM_ENABLED: true, RADARR_ENABLED: true },
{ runAsync, autoRequest } as unknown as RecommendationPipeline
);
const res = await app.inject({ method: 'POST', url: '/recommendations/trigger' });
expect(res.statusCode).toBe(200);
expect(autoRequest).toHaveBeenCalledWith(recs);
});
it('POST /recommendations/search requires LLM to be enabled and a query body', async () => {
const app = makeApp({ LLM_ENABLED: false }, {} as RecommendationPipeline);
const res = await app.inject({ method: 'POST', url: '/recommendations/search', payload: { query: 'x' } });
expect(res.statusCode).toBe(400);
});
it('POST /recommendations/search runs pipeline.searchAsync and returns results', async () => {
const searchAsync = vi.fn().mockResolvedValue([{ title: 'Dune', year: 2021, tmdbId: 1, reason: 'r' }]);
const app = makeApp(
{ LLM_ENABLED: true, RECOMMEND_COUNT: 10 },
{ searchAsync } as unknown as RecommendationPipeline
);
const res = await app.inject({
method: 'POST',
url: '/recommendations/search',
payload: { query: 'sci-fi like Blade Runner' },
});
expect(res.statusCode).toBe(200);
expect(searchAsync).toHaveBeenCalledWith('sci-fi like Blade Runner', 10);
});
});

View File

@@ -0,0 +1,36 @@
import { describe, expect, it, vi } from 'vitest';
import Fastify from 'fastify';
import { RequestController } from '../../../src/app/controllers/RequestController';
import type { AppConfig } from '../../../src/app/config/AppConfig';
import type { RadarrClient } from '../../../src/app/clients/radarr/RadarrClient';
describe('RequestController', () => {
it('returns 400 when Radarr is not enabled', async () => {
const app = Fastify();
new RequestController({ RADARR_ENABLED: false } as AppConfig, undefined).register(app);
const res = await app.inject({ method: 'POST', url: '/recommendations/request', payload: { tmdb_id: 1 } });
expect(res.statusCode).toBe(400);
});
it('returns 400 when tmdb_id is missing', async () => {
const app = Fastify();
new RequestController({ RADARR_ENABLED: true } as AppConfig, {} as RadarrClient).register(app);
const res = await app.inject({ method: 'POST', url: '/recommendations/request', payload: {} });
expect(res.statusCode).toBe(400);
});
it('looks up and adds the movie, returning its title', async () => {
const radarr = {
lookup: vi.fn().mockResolvedValue({ title: 'Dune', tmdbId: 1 }),
addMovie: vi.fn().mockResolvedValue(undefined),
} as unknown as RadarrClient;
const app = Fastify();
new RequestController({ RADARR_ENABLED: true } as AppConfig, radarr).register(app);
const res = await app.inject({ method: 'POST', url: '/recommendations/request', payload: { tmdb_id: 1 } });
expect(res.statusCode).toBe(200);
expect(res.json()).toEqual({ requested: true, title: 'Dune' });
});
});

View File

@@ -0,0 +1,31 @@
import { describe, expect, it, vi } from 'vitest';
import Fastify from 'fastify';
import { WebhookController } from '../../../src/app/controllers/WebhookController';
import type { PromptService } from '../../../src/core/langfuse/PromptService';
describe('WebhookController', () => {
it('invalidates a single prompt when promptName is given', async () => {
const invalidate = vi.fn();
const app = Fastify();
new WebhookController({ invalidate } as unknown as PromptService).register(app);
const res = await app.inject({
method: 'POST',
url: '/webhooks/langfuse/invalidate',
payload: { promptName: 'movie-recommender' },
});
expect(res.statusCode).toBe(200);
expect(invalidate).toHaveBeenCalledWith('movie-recommender');
expect(res.json()).toEqual({ invalidated: true, promptName: 'movie-recommender' });
});
it('invalidates the whole cache when no promptName is given', async () => {
const invalidate = vi.fn();
const app = Fastify();
new WebhookController({ invalidate } as unknown as PromptService).register(app);
const res = await app.inject({ method: 'POST', url: '/webhooks/langfuse/invalidate', payload: {} });
expect(invalidate).toHaveBeenCalledWith(undefined);
expect(res.json()).toEqual({ invalidated: true, promptName: 'all' });
});
});