import { beforeEach, describe, expect, it, vi } from 'vitest'; import type { IIssueRepository } from '../../../domain/repositories/issue.repository.interface.js'; import { Issue } from '../../../domain/entities/issue.entity.js'; import { IssueNotFoundException } from '../../../domain/exceptions/issue-not-found.exception.js'; import { GetIssueByCkeyUseCase } from './get-issue-by-ckey.use-case.js'; import { GetIssueByCkeyQuery } from './get-issue-by-ckey.query.js'; import type { IssueUserExpanderService } from '../../services/issue-user-expander.service.js'; import type { IssueWithUsersDto } from '../../dto/issue-with-users.dto.js'; const buildIssue = () => Issue.reconstitute({ id: 1, key: 'ISSUE-1', title: 'Fix login bug', content: null, status: 'OPEN', priority: 0, type: 'TASK', assignee: 'user-2', reporter: 'user-1', projectId: 'project-1', dueDate: null, createdAt: new Date(), updatedAt: new Date(), }); describe('GetIssueByCkeyUseCase', () => { let repository: IIssueRepository; let issueUserExpander: IssueUserExpanderService; let useCase: GetIssueByCkeyUseCase; beforeEach(() => { repository = { findById: vi.fn(), findByCKey: vi.fn(), findAll: vi.fn(), findByDueDateRange: vi.fn(), countByStatus: vi.fn(), create: vi.fn(), update: vi.fn(), delete: vi.fn(), }; issueUserExpander = { expand: vi.fn() } as unknown as IssueUserExpanderService; useCase = new GetIssueByCkeyUseCase(repository, issueUserExpander); }); it('returns the issue with assignee/reporter expanded by IssueUserExpanderService', async () => { const issue = buildIssue(); const expanded = { id: 1, key: 'ISSUE-1' } as IssueWithUsersDto; vi.mocked(repository.findByCKey).mockResolvedValue(issue); vi.mocked(issueUserExpander.expand).mockResolvedValue(expanded); const dto = await useCase.execute(new GetIssueByCkeyQuery('ISSUE-1', 'user-1')); expect(repository.findByCKey).toHaveBeenCalledWith('ISSUE-1', 'user-1'); expect(issueUserExpander.expand).toHaveBeenCalledWith(issue); expect(dto).toBe(expanded); }); it('throws IssueNotFoundException when the issue does not exist', async () => { vi.mocked(repository.findByCKey).mockResolvedValue(null); await expect(useCase.execute(new GetIssueByCkeyQuery('MISSING-999', 'user-1'))).rejects.toThrow( IssueNotFoundException, ); expect(issueUserExpander.expand).not.toHaveBeenCalled(); }); });