feat(vacay): implement Nager.Holidays v4 integration and adapt holiday data structure

This commit is contained in:
Luffy
2026-11-11 17:39:14 +08:00
committed by Maurice
parent be305c3daf
commit a3622e91b6
8 changed files with 405 additions and 222 deletions
@@ -0,0 +1,82 @@
/** Vacay public holiday provider integration tests. */
import { buildApp } from '../../src/bootstrap';
import { db as testDb } from '../../src/db/database';
import { VacayService } from '../../src/nest/vacay/vacay.service';
import { authCookie } from '../helpers/auth';
import { createUser } from '../helpers/factories';
import { resetTestDb, resetRateLimits } from '../helpers/test-db';
import type { INestApplication } from '@nestjs/common';
import type { Application } from 'express';
import request from 'supertest';
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
vi.mock('../../src/db/database', async () => {
const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock');
return buildDbMock(createSnapshotTestDb());
});
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve([]),
}),
);
vi.spyOn(VacayService.prototype, 'getCountries').mockResolvedValue({ data: [] });
let nestApp: INestApplication;
let app: Application;
beforeAll(async () => {
nestApp = await buildApp();
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(async () => {
resetTestDb(testDb);
await resetRateLimits(nestApp);
});
afterAll(async () => {
await nestApp.close();
testDb.close();
vi.unstubAllGlobals();
});
describe('Vacay public holidays', () => {
it('VACAY-035 — returns 502 when the external API fetch fails', async () => {
const { user } = createUser(testDb);
vi.mocked(global.fetch as any).mockRejectedValueOnce(new Error('Network error'));
const res = await request(app).get('/api/addons/vacay/holidays/2099/ZZ').set('Cookie', authCookie(user.id));
expect(res.status).toBe(502);
});
it('VACAY-037 — adapts a Nager v4 record when the external API succeeds', async () => {
const { user } = createUser(testDb);
vi.mocked(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: () =>
Promise.resolve([
{
date: '2025-05-01',
name: 'Labour Day',
nationalHoliday: true,
subdivisionCodes: null,
},
]),
});
const res = await request(app).get('/api/addons/vacay/holidays/2025/AT').set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body).toEqual([
{
date: '2025-05-01',
name: 'Labour Day',
localName: 'Labour Day',
global: true,
counties: null,
},
]);
});
});
+10 -26
View File
@@ -28,7 +28,16 @@ vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve([{ date: '2025-01-01', name: "New Year's Day", countryCode: 'DE' }]),
json: () =>
Promise.resolve([
{
date: '2025-01-01',
name: "New Year's Day",
countryCode: 'DE',
nationalHoliday: true,
subdivisionCodes: null,
},
]),
}),
);
@@ -448,17 +457,6 @@ describe('Vacay stats restrictions', () => {
});
});
describe('Vacay holidays error path', () => {
it('VACAY-035 — GET /holidays/:year/:country returns 502 when external API fetch fails', async () => {
const { user } = createUser(testDb);
// Use an unusual country/year to avoid cache hits from other tests
vi.mocked(global.fetch as any).mockRejectedValueOnce(new Error('Network error'));
const res = await request(app).get('/api/addons/vacay/holidays/2099/ZZ').set('Cookie', authCookie(user.id));
expect(res.status).toBe(502);
});
});
describe('Vacay color restriction', () => {
it('VACAY-036 — PUT /color with target_user_id not in plan returns 403', async () => {
const { user: owner } = createUser(testDb);
@@ -473,20 +471,6 @@ describe('Vacay color restriction', () => {
});
});
describe('Vacay holidays success path', () => {
it('VACAY-037 — GET /holidays/:year/:country returns data when fetch succeeds', async () => {
const { user } = createUser(testDb);
// Use unique year/country to avoid cache from other tests
vi.mocked(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve([{ date: '2025-05-01', name: 'Labour Day', countryCode: 'AT' }]),
});
const res = await request(app).get('/api/addons/vacay/holidays/2025/AT').set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
});
});
describe('Vacay toggle entry for plan member', () => {
it('VACAY-038 — POST /entries/toggle with target_user_id in plan toggles their entry', async () => {
const { user: owner } = createUser(testDb);