/* * Copyright © 2025 Hexastack. All rights reserved. * * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). */ import { NotFoundException } from '@nestjs/common'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { MongooseModule } from '@nestjs/mongoose'; import { Test } from '@nestjs/testing'; import { AttachmentRepository } from '@/attachment/repositories/attachment.repository'; import { AttachmentModel } from '@/attachment/schemas/attachment.schema'; import { AttachmentService } from '@/attachment/services/attachment.service'; import { LoggerService } from '@/logger/logger.service'; import { RoleRepository } from '@/user/repositories/role.repository'; import { UserRepository } from '@/user/repositories/user.repository'; import { PermissionModel } from '@/user/schemas/permission.schema'; import { RoleModel } from '@/user/schemas/role.schema'; import { UserModel } from '@/user/schemas/user.schema'; import { RoleService } from '@/user/services/role.service'; import { UserService } from '@/user/services/user.service'; import { IGNORED_TEST_FIELDS } from '@/utils/test/constants'; import { getUpdateOneError } from '@/utils/test/errors/messages'; import { labelFixtures } from '@/utils/test/fixtures/label'; import { installSubscriberFixtures } from '@/utils/test/fixtures/subscriber'; import { getPageQuery } from '@/utils/test/pagination'; import { sortRowsBy } from '@/utils/test/sort'; import { closeInMongodConnection, rootMongooseTestModule, } from '@/utils/test/test'; import { LabelCreateDto, LabelUpdateDto } from '../dto/label.dto'; import { LabelRepository } from '../repositories/label.repository'; import { SubscriberRepository } from '../repositories/subscriber.repository'; import { Label, LabelModel } from '../schemas/label.schema'; import { SubscriberModel } from '../schemas/subscriber.schema'; import { LabelService } from '../services/label.service'; import { SubscriberService } from '../services/subscriber.service'; import { LabelController } from './label.controller'; describe('LabelController', () => { let labelController: LabelController; let labelService: LabelService; let label: Label; let labelToDelete: Label; let subscriberService: SubscriberService; beforeAll(async () => { const module = await Test.createTestingModule({ controllers: [LabelController], imports: [ rootMongooseTestModule(installSubscriberFixtures), MongooseModule.forFeature([ LabelModel, UserModel, RoleModel, PermissionModel, SubscriberModel, AttachmentModel, ]), ], providers: [ LoggerService, LabelController, LabelService, LabelRepository, UserService, UserRepository, RoleService, RoleRepository, SubscriberService, SubscriberRepository, EventEmitter2, AttachmentService, AttachmentRepository, ], }).compile(); labelService = module.get(LabelService); subscriberService = module.get(SubscriberService); labelController = module.get(LabelController); label = (await labelService.findOne({ name: 'TEST_TITLE_1' })) as Label; labelToDelete = (await labelService.findOne({ name: 'TEST_TITLE_2', })) as Label; }); afterEach(jest.clearAllMocks); afterAll(closeInMongodConnection); describe('count', () => { it('should count labels', async () => { jest.spyOn(labelService, 'count'); const result = await labelController.filterCount(); expect(labelService.count).toHaveBeenCalled(); expect(result).toEqual({ count: labelFixtures.length }); }); }); describe('findPage', () => { const pageQuery = getPageQuery