Skip to content

Commit

Permalink
[core] 🧪 tests: Report with ai use case
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnPetros committed Dec 3, 2024
1 parent 617dd75 commit bb7668b
Showing 1 changed file with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { CompanyFaker, UsersFaker } from '../../../../__tests__/fakers'
import {
InventoryMovementsRepositoryMock,
UsersRepositoryMock,
CompanyRepositoryMock,
} from '../../../../__tests__/mocks/repositories'
import { AiProviderMock } from '../../../../__tests__/mocks/providers'
import { NotAllowedError, NotFoundError } from '../../../errors'
import { ReportInventoryWithAiUseCase } from '../report-inventory-with-ai-use-case'
import { Role } from '../../../domain/structs'

let useCase: ReportInventoryWithAiUseCase
let inventoryMovementsRepositoryMock: InventoryMovementsRepositoryMock
let usersRepositoryMock: UsersRepositoryMock
let companiesRepositoryMock: CompanyRepositoryMock
let aiProviderMock: AiProviderMock

describe('Report with ai use case', () => {
beforeEach(() => {
inventoryMovementsRepositoryMock = new InventoryMovementsRepositoryMock()
usersRepositoryMock = new UsersRepositoryMock()
companiesRepositoryMock = new CompanyRepositoryMock()
aiProviderMock = new AiProviderMock()

useCase = new ReportInventoryWithAiUseCase(
inventoryMovementsRepositoryMock,
usersRepositoryMock,
companiesRepositoryMock,
aiProviderMock,
)
})

it('should not report if user that is requesting the report does exist', async () => {
expect(async () => {
await useCase.execute({ userId: 'fake user id', onGenerateChunck: vi.fn() })
}).rejects.toThrowError(NotFoundError)
})

it('should not report if user that is requesting the report does not have permission', async () => {
const fakeUser = UsersFaker.fake()
usersRepositoryMock.add(fakeUser)

expect(async () => {
await useCase.execute({ userId: fakeUser.id, onGenerateChunck: vi.fn() })
}).rejects.toThrowError(NotAllowedError)
})

it('should call a callback on generate chunk', async () => {
const fakeCompany = CompanyFaker.fake()
companiesRepositoryMock.add(fakeCompany)

const fakeUser = UsersFaker.fake({ companyId: fakeCompany.id })
companiesRepositoryMock.addRole(
Role.create(fakeUser.role, ['reports']),
fakeCompany.id,
)
usersRepositoryMock.add(fakeUser)

const onGenerateChunckMock = vi.fn()

await useCase.execute({ userId: fakeUser.id, onGenerateChunck: onGenerateChunckMock })

expect(onGenerateChunckMock).toHaveBeenCalledTimes(1)
})
})

0 comments on commit bb7668b

Please sign in to comment.