import { Test, TestingModule } from '@nestjs/testing'; import { TasksService } from './tasks.service'; import { PrismaService } from '../prisma/prisma.service'; import { ActivityService } from '../activity/activity.service'; import { NotFoundException } from '@nestjs/common'; describe('TasksService', () => { let service: TasksService; const mockTask = { id: 1, taskId: 'TASK-001', title: 'Test', assignee: 'narang', status: 'pending', iteration: 0, sprintId: 1, createdAt: new Date(), updatedAt: new Date(), }; const mockSprint = { id: 1, projectId: 1, number: 1, name: 'Sprint 001', status: 'in_progress', startedAt: null, completedAt: null, createdAt: new Date(), updatedAt: new Date(), tasks: [mockTask], }; const mockPrisma = { project: { findUnique: jest.fn(), }, sprint: { create: jest.fn(), findUnique: jest.fn().mockResolvedValue(mockSprint), }, task: { findUnique: jest.fn(), update: jest.fn(), }, }; const mockActivity = { log: jest.fn().mockResolvedValue({}) }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ TasksService, { provide: PrismaService, useValue: mockPrisma }, { provide: ActivityService, useValue: mockActivity }, ], }).compile(); service = module.get(TasksService); jest.clearAllMocks(); }); it('getTaskLedger: project 없으면 NotFoundException', async () => { mockPrisma.project.findUnique.mockResolvedValue(null); await expect(service.getTaskLedger(99)).rejects.toThrow(NotFoundException); }); it('getTaskLedger: sprint + task + progress 반환', async () => { mockPrisma.project.findUnique.mockResolvedValue({ id: 1, sprints: [{ ...mockSprint, tasks: [{ ...mockTask, status: 'done' }, mockTask] }], }); const result = await service.getTaskLedger(1); expect(result).toHaveLength(1); expect(result[0].progress).toBe(50); }); it('updateTask: 상태 변경 시 ActivityLog 기록', async () => { mockPrisma.task.findUnique.mockResolvedValue({ ...mockTask, status: 'in_progress' }); mockPrisma.task.update.mockResolvedValue({ ...mockTask, status: 'done', sprintId: 1 }); await service.updateTask(1, { status: 'done' }); expect(mockActivity.log).toHaveBeenCalledWith( expect.objectContaining({ action: 'task_updated' }), ); }); });