Files
Void-Homelab/tests/api/weather.test.js
2026-06-02 22:31:48 +10:00

28 lines
1.2 KiB
JavaScript

import { describe, it, expect, beforeAll, beforeEach, vi } from 'vitest';
import request from 'supertest';
import { setup } from './helpers.js';
import * as weather from '../../lib/weather.js';
let app, ownerHeaders;
beforeAll(async () => { ({ app, ownerHeaders } = await setup()); });
beforeEach(() => weather._resetCache());
const SAMPLE = { current: { temperature_2m: 14.2, apparent_temperature: 12.1, relative_humidity_2m: 71, wind_speed_10m: 9, weather_code: 3 } };
describe('weather api', () => {
it('401 without auth', async () => {
expect((await request(app).get('/api/weather')).status).toBe(401);
});
it('returns mapped weather and caches the upstream call', async () => {
const fetcher = vi.fn().mockResolvedValue(SAMPLE);
weather._setFetcher(fetcher);
const r1 = await request(app).get('/api/weather').set(ownerHeaders);
expect(r1.status).toBe(200);
expect(r1.body.temp).toBe(14.2);
expect(r1.body.humidity).toBe(71);
expect(typeof r1.body.label).toBe('string'); // weather_code → text
await request(app).get('/api/weather').set(ownerHeaders); // 2nd hit
expect(fetcher).toHaveBeenCalledTimes(1); // cached
});
});