78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import { getCalendar } from '../../src/services/calendar.js';
|
|
|
|
function mockPrisma() {
|
|
return { planDay: { findMany: vi.fn() } };
|
|
}
|
|
|
|
const NOW = new Date(2026, 7, 24, 12, 0, 0);
|
|
|
|
beforeEach(() => {
|
|
vi.useFakeTimers();
|
|
vi.setSystemTime(NOW);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
describe('getCalendar', () => {
|
|
it('defaults to the current month', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.planDay.findMany.mockResolvedValue([]);
|
|
const result = await getCalendar(prisma, 'u1');
|
|
expect(prisma.planDay.findMany).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
where: expect.objectContaining({
|
|
week: { plan: { userId: 'u1', status: 'ACTIVE' } },
|
|
date: { gte: new Date(2026, 7, 1), lt: new Date(2026, 8, 1) },
|
|
}),
|
|
}),
|
|
);
|
|
expect(result.month).toBe('2026-08');
|
|
});
|
|
|
|
it('queries the requested month', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.planDay.findMany.mockResolvedValue([]);
|
|
const result = await getCalendar(prisma, 'u1', '2026-01');
|
|
expect(prisma.planDay.findMany).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
where: expect.objectContaining({
|
|
date: { gte: new Date(2026, 0, 1), lt: new Date(2026, 1, 1) },
|
|
}),
|
|
}),
|
|
);
|
|
expect(result.month).toBe('2026-01');
|
|
});
|
|
|
|
it('maps days to the public shape', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.planDay.findMany.mockResolvedValue([
|
|
{
|
|
id: 'd1',
|
|
date: new Date(2026, 7, 24),
|
|
status: 'PLANNED',
|
|
dayOfWeek: 1,
|
|
week: { plan: { id: 'p1', title: 'Plan', startDate: new Date(2026, 7, 17) } },
|
|
_count: { moveLog: 2 },
|
|
exercises: [{ exercise: { name: 'Squat', nameEn: 'Squat', primaryMuscle: 'legs' } }],
|
|
},
|
|
]);
|
|
const result = await getCalendar(prisma, 'u1', '2026-08');
|
|
expect(result.days).toEqual([
|
|
{
|
|
id: 'd1',
|
|
date: new Date(2026, 7, 24).toISOString(),
|
|
status: 'PLANNED',
|
|
dayOfWeek: 1,
|
|
planId: 'p1',
|
|
planTitle: 'Plan',
|
|
planStartDate: new Date(2026, 7, 17).toISOString(),
|
|
moves: 2,
|
|
exercises: [{ name: 'Squat', nameEn: 'Squat', muscle: 'legs' }],
|
|
},
|
|
]);
|
|
});
|
|
});
|