feat: Wetterintegration + Trachtprognose pro Bienenstand
Neues WeatherModule: liest Bienenstand-Koordinaten und ruft die freie Open-Meteo-API ab (kein API-Key nötig), liefert 14 Tage Wetterhistorie + 7-Tage-Vorhersage sowie eine heuristische Trachtprognose (Saison + Wetter der letzten 7 Tage + historischer Ernteschnitt für denselben Kalendermonat am selben Standort - explizit kein ML-Modell, siehe TASKS.md Abschnitt 26). - GET /api/v1/apiaries/:id/weather, GET .../trachtprognose, beide ueber die bestehende apiaries:read-Berechtigung abgesichert (kein neuer Permission-Key nötig, gleiches Muster wie die bestehenden Colony-Sub-Controller). - Bienenstände ohne hinterlegte Koordinaten liefern 400 statt eines stillen Fallbacks. - test/weather.e2e-spec.ts ruft bewusst die echte Open-Meteo-API auf (kostenlos, kein Auth, stabil genug für einen einzelnen Testlauf) - validiert damit den echten Response-Vertrag statt nur ein Mock. - TASKS.md: beide Punkte abgehakt, stale Duplikat-Zeilen im Später-Abschnitt bereinigt. Verifiziert: tsc, lint, build, unit (228/228), volle e2e-Suite 163/163 gruen. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
1 parent
335c572dfc
commit
ce9cea1b94
7 files changed
+568
-7
No files matched your search
@@ -0,0 +1,202 @@
|
||||
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||
import { WeatherService } from './weather.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
function openMeteoResponse(
|
||||
days: {
|
||||
date: string;
|
||||
tempMax: number | null;
|
||||
tempMin: number | null;
|
||||
precipitation: number | null;
|
||||
}[],
|
||||
): unknown {
|
||||
return {
|
||||
daily: {
|
||||
time: days.map((day) => day.date),
|
||||
temperature_2m_max: days.map((day) => day.tempMax),
|
||||
temperature_2m_min: days.map((day) => day.tempMin),
|
||||
precipitation_sum: days.map((day) => day.precipitation),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('WeatherService', () => {
|
||||
let weatherService: WeatherService;
|
||||
let prisma: {
|
||||
apiary: { findUnique: jest.Mock };
|
||||
harvest: { findMany: jest.Mock };
|
||||
};
|
||||
let fetchMock: jest.SpiedFunction<typeof fetch>;
|
||||
|
||||
beforeEach(() => {
|
||||
prisma = {
|
||||
apiary: { findUnique: jest.fn() },
|
||||
harvest: { findMany: jest.fn().mockResolvedValue([]) },
|
||||
};
|
||||
weatherService = new WeatherService(prisma as unknown as PrismaService);
|
||||
fetchMock = jest.spyOn(global, 'fetch');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
fetchMock.mockRestore();
|
||||
});
|
||||
|
||||
describe('getWeather', () => {
|
||||
it('throws NotFoundException when the apiary does not exist', async () => {
|
||||
prisma.apiary.findUnique.mockResolvedValue(null);
|
||||
await expect(weatherService.getWeather('missing')).rejects.toBeInstanceOf(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
it('throws BadRequestException when the apiary has no coordinates', async () => {
|
||||
prisma.apiary.findUnique.mockResolvedValue({
|
||||
latitude: null,
|
||||
longitude: null,
|
||||
});
|
||||
await expect(
|
||||
weatherService.getWeather('apiary-1'),
|
||||
).rejects.toBeInstanceOf(BadRequestException);
|
||||
});
|
||||
|
||||
it('returns daily weather mapped from the Open-Meteo response', async () => {
|
||||
prisma.apiary.findUnique.mockResolvedValue({
|
||||
latitude: 52.5,
|
||||
longitude: 13.4,
|
||||
});
|
||||
fetchMock.mockResolvedValue(
|
||||
new Response(
|
||||
JSON.stringify(
|
||||
openMeteoResponse([
|
||||
{
|
||||
date: '2026-06-01',
|
||||
tempMax: 22,
|
||||
tempMin: 12,
|
||||
precipitation: 0,
|
||||
},
|
||||
]),
|
||||
),
|
||||
{ status: 200 },
|
||||
),
|
||||
);
|
||||
|
||||
const result = await weatherService.getWeather('apiary-1');
|
||||
|
||||
expect(result.daily).toEqual([
|
||||
{ date: '2026-06-01', tempMaxC: 22, tempMinC: 12, precipitationMm: 0 },
|
||||
]);
|
||||
const [url] = fetchMock.mock.calls[0] as [URL];
|
||||
expect(url.toString()).toContain('latitude=52.5');
|
||||
expect(url.toString()).toContain('longitude=13.4');
|
||||
});
|
||||
|
||||
it('throws BadRequestException when Open-Meteo responds with an error status', async () => {
|
||||
prisma.apiary.findUnique.mockResolvedValue({
|
||||
latitude: 52.5,
|
||||
longitude: 13.4,
|
||||
});
|
||||
fetchMock.mockResolvedValue(new Response('', { status: 500 }));
|
||||
|
||||
await expect(
|
||||
weatherService.getWeather('apiary-1'),
|
||||
).rejects.toBeInstanceOf(BadRequestException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTrachtprognose', () => {
|
||||
const realDate = Date;
|
||||
|
||||
function mockToday(isoDate: string) {
|
||||
const fixed = new realDate(isoDate);
|
||||
jest
|
||||
.spyOn(global, 'Date')
|
||||
.mockImplementation((...args: unknown[]) =>
|
||||
args.length ? new realDate(...(args as [string])) : fixed,
|
||||
);
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('returns KEINE_TRACHT outside the bloom season', async () => {
|
||||
mockToday('2026-01-15');
|
||||
prisma.apiary.findUnique.mockResolvedValue({
|
||||
latitude: 52.5,
|
||||
longitude: 13.4,
|
||||
});
|
||||
fetchMock.mockResolvedValue(
|
||||
new Response(JSON.stringify(openMeteoResponse([])), { status: 200 }),
|
||||
);
|
||||
|
||||
const result = await weatherService.getTrachtprognose('apiary-1');
|
||||
|
||||
expect(result.rating).toBe('KEINE_TRACHT');
|
||||
expect(result.season).toBe('Winterruhe');
|
||||
});
|
||||
|
||||
it('returns GUT for warm, dry weather during bloom season', async () => {
|
||||
mockToday('2026-06-10');
|
||||
prisma.apiary.findUnique.mockResolvedValue({
|
||||
latitude: 52.5,
|
||||
longitude: 13.4,
|
||||
});
|
||||
const days = Array.from({ length: 21 }, (_, index) => ({
|
||||
date: `2026-06-${String(index + 1).padStart(2, '0')}`,
|
||||
tempMax: 24,
|
||||
tempMin: 14,
|
||||
precipitation: 0,
|
||||
}));
|
||||
fetchMock.mockResolvedValue(
|
||||
new Response(JSON.stringify(openMeteoResponse(days)), { status: 200 }),
|
||||
);
|
||||
|
||||
const result = await weatherService.getTrachtprognose('apiary-1');
|
||||
|
||||
expect(result.rating).toBe('GUT');
|
||||
expect(result.season).toBe('Sommertracht');
|
||||
});
|
||||
|
||||
it('returns SCHWACH for cold, wet weather during bloom season', async () => {
|
||||
mockToday('2026-04-10');
|
||||
prisma.apiary.findUnique.mockResolvedValue({
|
||||
latitude: 52.5,
|
||||
longitude: 13.4,
|
||||
});
|
||||
const days = Array.from({ length: 21 }, (_, index) => ({
|
||||
date: `2026-04-${String(index + 1).padStart(2, '0')}`,
|
||||
tempMax: 8,
|
||||
tempMin: 2,
|
||||
precipitation: 10,
|
||||
}));
|
||||
fetchMock.mockResolvedValue(
|
||||
new Response(JSON.stringify(openMeteoResponse(days)), { status: 200 }),
|
||||
);
|
||||
|
||||
const result = await weatherService.getTrachtprognose('apiary-1');
|
||||
|
||||
expect(result.rating).toBe('SCHWACH');
|
||||
expect(result.season).toBe('Frühtracht');
|
||||
});
|
||||
|
||||
it('includes the historical average yield when past harvests exist for the same month', async () => {
|
||||
mockToday('2026-06-10');
|
||||
prisma.apiary.findUnique.mockResolvedValue({
|
||||
latitude: 52.5,
|
||||
longitude: 13.4,
|
||||
});
|
||||
prisma.harvest.findMany.mockResolvedValue([
|
||||
{ date: new realDate('2025-06-15'), netWeight: 20 },
|
||||
{ date: new realDate('2024-06-20'), netWeight: 30 },
|
||||
{ date: new realDate('2025-09-01'), netWeight: 100 },
|
||||
]);
|
||||
fetchMock.mockResolvedValue(
|
||||
new Response(JSON.stringify(openMeteoResponse([])), { status: 200 }),
|
||||
);
|
||||
|
||||
const result = await weatherService.getTrachtprognose('apiary-1');
|
||||
|
||||
expect(result.historicalAverageYieldKg).toBe(25);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user