|
| 1 | +import type { Connection } from 'mongoose'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, Mock, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { AppError } from '../../common/errors'; |
| 5 | +import * as fileUtils from '../../common/fileUtils'; |
| 6 | +import * as mongoPoolModule from '../../common/MongoPool'; |
| 7 | +import type { TargetUnit } from '../../common/types'; |
| 8 | +import type { Employee, Project } from '../../services/FinApp'; |
| 9 | +import type { IFinAppRepository } from '../../services/FinApp'; |
| 10 | +import * as finAppService from '../../services/FinApp'; |
| 11 | +import { fetchFinancialAppData } from './fetchFinancialAppData'; |
| 12 | + |
| 13 | +type MongoPoolMock = { |
| 14 | + connect: () => Promise<Connection>; |
| 15 | + disconnect: () => Promise<void>; |
| 16 | + getConnection: () => unknown; |
| 17 | + connection: null; |
| 18 | + uri: string; |
| 19 | +}; |
| 20 | + |
| 21 | +vi.mock('../../common/fileUtils', () => ({ |
| 22 | + readJsonFile: vi.fn(), |
| 23 | + writeJsonFile: vi.fn(), |
| 24 | +})); |
| 25 | +vi.mock('../../common/MongoPool', () => ({ |
| 26 | + MongoPool: { |
| 27 | + getInstance: vi.fn(), |
| 28 | + }, |
| 29 | +})); |
| 30 | +vi.mock('../../services/FinApp', () => ({ |
| 31 | + FinAppRepository: vi.fn(), |
| 32 | +})); |
| 33 | + |
| 34 | +const mockTargetUnits: TargetUnit[] = [ |
| 35 | + { |
| 36 | + group_id: 1, |
| 37 | + group_name: 'Group', |
| 38 | + project_id: 2, |
| 39 | + project_name: 'Project', |
| 40 | + user_id: 3, |
| 41 | + username: 'User', |
| 42 | + spent_on: '2024-06-01', |
| 43 | + total_hours: 8, |
| 44 | + }, |
| 45 | +]; |
| 46 | +const mockEmployees: Employee[] = [ |
| 47 | + { redmine_id: 3, history: { rate: { '2024-01-01': 100 } } }, |
| 48 | +]; |
| 49 | +const mockProjects: Project[] = [ |
| 50 | + { |
| 51 | + redmine_id: 2, |
| 52 | + quick_books_id: 10, |
| 53 | + history: { rate: { '2024-01-01': 200 } }, |
| 54 | + }, |
| 55 | +]; |
| 56 | + |
| 57 | +function createRepoInstance( |
| 58 | + overrides: Partial<IFinAppRepository> = {}, |
| 59 | +): IFinAppRepository { |
| 60 | + return { |
| 61 | + getEmployeesByRedmineIds: vi.fn().mockResolvedValue(mockEmployees), |
| 62 | + getProjectsByRedmineIds: vi.fn().mockResolvedValue(mockProjects), |
| 63 | + ...overrides, |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +function createMongoPoolInstance( |
| 68 | + connectImpl?: () => Promise<Connection>, |
| 69 | + disconnectImpl?: () => Promise<void>, |
| 70 | +): MongoPoolMock { |
| 71 | + return { |
| 72 | + connect: connectImpl || vi.fn().mockResolvedValue({} as Connection), |
| 73 | + disconnect: disconnectImpl || vi.fn().mockResolvedValue(undefined), |
| 74 | + getConnection: vi.fn(), |
| 75 | + connection: null, |
| 76 | + uri: '', |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +describe('getFinAppData', () => { |
| 81 | + let readJsonFile: Mock; |
| 82 | + let writeJsonFile: Mock; |
| 83 | + let connect: Mock; |
| 84 | + let disconnect: Mock; |
| 85 | + let FinAppRepository: Mock; |
| 86 | + let dateSpy: ReturnType<typeof vi.spyOn>; |
| 87 | + let repoInstance: IFinAppRepository; |
| 88 | + let mongoPoolInstance: MongoPoolMock; |
| 89 | + |
| 90 | + const fileLink = 'input.json'; |
| 91 | + const expectedFilename = |
| 92 | + 'data/weeklyFinancialReportsWorkflow/getFinAppData/data-123.json'; |
| 93 | + |
| 94 | + function setupSuccessMocks() { |
| 95 | + readJsonFile.mockResolvedValue(mockTargetUnits); |
| 96 | + writeJsonFile.mockResolvedValue(undefined); |
| 97 | + (repoInstance.getEmployeesByRedmineIds as Mock).mockResolvedValue( |
| 98 | + mockEmployees, |
| 99 | + ); |
| 100 | + (repoInstance.getProjectsByRedmineIds as Mock).mockResolvedValue( |
| 101 | + mockProjects, |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + async function expectAppError(promise: Promise<unknown>, msg: string) { |
| 106 | + await expect(promise).rejects.toThrow(AppError); |
| 107 | + await expect(promise).rejects.toThrow(msg); |
| 108 | + expect(() => mongoPoolInstance.disconnect()).not.toThrow(); |
| 109 | + } |
| 110 | + |
| 111 | + beforeEach(() => { |
| 112 | + dateSpy = vi.spyOn(Date, 'now').mockReturnValue(123); |
| 113 | + readJsonFile = vi.mocked(fileUtils.readJsonFile); |
| 114 | + writeJsonFile = vi.mocked(fileUtils.writeJsonFile); |
| 115 | + FinAppRepository = vi.mocked(finAppService.FinAppRepository); |
| 116 | + |
| 117 | + repoInstance = createRepoInstance(); |
| 118 | + FinAppRepository.mockImplementation(() => repoInstance); |
| 119 | + |
| 120 | + connect = vi.fn().mockResolvedValue(undefined); |
| 121 | + disconnect = vi.fn().mockResolvedValue(undefined); |
| 122 | + mongoPoolInstance = createMongoPoolInstance(connect, disconnect); |
| 123 | + (mongoPoolModule.MongoPool.getInstance as Mock).mockReturnValue( |
| 124 | + mongoPoolInstance as unknown as mongoPoolModule.MongoPool, |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + afterEach(() => { |
| 129 | + dateSpy.mockRestore(); |
| 130 | + vi.clearAllMocks(); |
| 131 | + }); |
| 132 | + |
| 133 | + describe('success cases', () => { |
| 134 | + it('returns fileLink when successful', async () => { |
| 135 | + setupSuccessMocks(); |
| 136 | + const result = await fetchFinancialAppData(fileLink); |
| 137 | + |
| 138 | + expect(result).toEqual({ fileLink: expectedFilename }); |
| 139 | + expect(() => mongoPoolInstance.connect()).not.toThrow(); |
| 140 | + expect(() => mongoPoolInstance.disconnect()).not.toThrow(); |
| 141 | + expect(readJsonFile).toHaveBeenCalledWith(fileLink); |
| 142 | + expect(writeJsonFile).toHaveBeenCalledWith(expectedFilename, { |
| 143 | + employees: mockEmployees, |
| 144 | + projects: mockProjects, |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it('always disconnects the mongo pool', async () => { |
| 149 | + setupSuccessMocks(); |
| 150 | + await fetchFinancialAppData(fileLink).catch(() => {}); |
| 151 | + expect(() => mongoPoolInstance.disconnect()).not.toThrow(); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + describe('error cases', () => { |
| 156 | + it('throws AppError when readJsonFile throws', async () => { |
| 157 | + readJsonFile.mockRejectedValue(new Error('fail-read')); |
| 158 | + await expectAppError( |
| 159 | + fetchFinancialAppData(fileLink), |
| 160 | + 'Failed to get Fin App Data', |
| 161 | + ); |
| 162 | + }); |
| 163 | + |
| 164 | + it('throws AppError when getEmployees throws', async () => { |
| 165 | + readJsonFile.mockResolvedValue(mockTargetUnits); |
| 166 | + (repoInstance.getEmployeesByRedmineIds as Mock).mockRejectedValue( |
| 167 | + new Error('fail-employees'), |
| 168 | + ); |
| 169 | + await expectAppError( |
| 170 | + fetchFinancialAppData(fileLink), |
| 171 | + 'Failed to get Fin App Data', |
| 172 | + ); |
| 173 | + }); |
| 174 | + |
| 175 | + it('throws AppError when getProjects throws', async () => { |
| 176 | + readJsonFile.mockResolvedValue(mockTargetUnits); |
| 177 | + (repoInstance.getProjectsByRedmineIds as Mock).mockRejectedValue( |
| 178 | + new Error('fail-projects'), |
| 179 | + ); |
| 180 | + await expectAppError( |
| 181 | + fetchFinancialAppData(fileLink), |
| 182 | + 'Failed to get Fin App Data', |
| 183 | + ); |
| 184 | + }); |
| 185 | + |
| 186 | + it('throws AppError when writeJsonFile throws', async () => { |
| 187 | + readJsonFile.mockResolvedValue(mockTargetUnits); |
| 188 | + writeJsonFile.mockRejectedValue(new Error('fail-write')); |
| 189 | + await expectAppError( |
| 190 | + fetchFinancialAppData(fileLink), |
| 191 | + 'Failed to get Fin App Data', |
| 192 | + ); |
| 193 | + }); |
| 194 | + }); |
| 195 | +}); |
0 commit comments