template-saas/apps/backend/dist/modules/notifications/__tests__/devices.service.spec.js
rckrdmrd 50a821a415
Some checks failed
CI / Backend CI (push) Has been cancelled
CI / Frontend CI (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / CI Summary (push) Has been cancelled
[SIMCO-V38] feat: Actualizar a SIMCO v3.8.0
- HERENCIA-SIMCO.md actualizado con directivas v3.7 y v3.8
- Actualizaciones de configuracion

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 08:53:08 -06:00

176 lines
7.7 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const testing_1 = require("@nestjs/testing");
const typeorm_1 = require("@nestjs/typeorm");
const common_1 = require("@nestjs/common");
const devices_service_1 = require("../services/devices.service");
const entities_1 = require("../entities");
describe('DevicesService', () => {
let service;
let repository;
const mockDevice = {
id: 'device-1',
tenant_id: 'tenant-1',
user_id: 'user-1',
device_type: 'web',
device_token: JSON.stringify({
endpoint: 'https://fcm.googleapis.com/fcm/send/test',
keys: { p256dh: 'test-key', auth: 'test-auth' },
}),
device_name: 'Chrome on Windows',
browser: 'Chrome',
browser_version: '120',
os: 'Windows',
os_version: '11',
is_active: true,
last_used_at: new Date(),
created_at: new Date(),
};
const mockRepository = {
find: jest.fn(),
findOne: jest.fn(),
create: jest.fn(),
save: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
count: jest.fn(),
createQueryBuilder: jest.fn(() => ({
delete: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
execute: jest.fn().mockResolvedValue({ affected: 0 }),
})),
};
beforeEach(async () => {
const module = await testing_1.Test.createTestingModule({
providers: [
devices_service_1.DevicesService,
{
provide: (0, typeorm_1.getRepositoryToken)(entities_1.UserDevice),
useValue: mockRepository,
},
],
}).compile();
service = module.get(devices_service_1.DevicesService);
repository = module.get((0, typeorm_1.getRepositoryToken)(entities_1.UserDevice));
jest.clearAllMocks();
});
describe('findByUser', () => {
it('should return devices for a user', async () => {
mockRepository.find.mockResolvedValue([mockDevice]);
const result = await service.findByUser('user-1', 'tenant-1');
expect(result).toEqual([mockDevice]);
expect(mockRepository.find).toHaveBeenCalledWith({
where: { user_id: 'user-1', tenant_id: 'tenant-1' },
order: { last_used_at: 'DESC' },
});
});
it('should return empty array if no devices', async () => {
mockRepository.find.mockResolvedValue([]);
const result = await service.findByUser('user-1', 'tenant-1');
expect(result).toEqual([]);
});
});
describe('findActiveByUser', () => {
it('should return only active devices', async () => {
mockRepository.find.mockResolvedValue([mockDevice]);
const result = await service.findActiveByUser('user-1', 'tenant-1');
expect(result).toEqual([mockDevice]);
expect(mockRepository.find).toHaveBeenCalledWith({
where: { user_id: 'user-1', tenant_id: 'tenant-1', is_active: true },
order: { last_used_at: 'DESC' },
});
});
});
describe('findById', () => {
it('should return device by id', async () => {
mockRepository.findOne.mockResolvedValue(mockDevice);
const result = await service.findById('device-1', 'user-1', 'tenant-1');
expect(result).toEqual(mockDevice);
});
it('should throw NotFoundException if device not found', async () => {
mockRepository.findOne.mockResolvedValue(null);
await expect(service.findById('device-1', 'user-1', 'tenant-1')).rejects.toThrow(common_1.NotFoundException);
});
});
describe('register', () => {
const registerDto = {
deviceToken: mockDevice.device_token,
deviceType: 'web',
deviceName: 'Chrome on Windows',
browser: 'Chrome',
browserVersion: '120',
os: 'Windows',
osVersion: '11',
};
it('should create new device if not exists', async () => {
mockRepository.findOne.mockResolvedValue(null);
mockRepository.create.mockReturnValue(mockDevice);
mockRepository.save.mockResolvedValue(mockDevice);
const result = await service.register('user-1', 'tenant-1', registerDto);
expect(result).toEqual(mockDevice);
expect(mockRepository.create).toHaveBeenCalled();
expect(mockRepository.save).toHaveBeenCalled();
});
it('should reactivate existing device', async () => {
const inactiveDevice = { ...mockDevice, is_active: false };
mockRepository.findOne.mockResolvedValue(inactiveDevice);
mockRepository.save.mockResolvedValue({ ...inactiveDevice, is_active: true });
const result = await service.register('user-1', 'tenant-1', registerDto);
expect(result.is_active).toBe(true);
expect(mockRepository.save).toHaveBeenCalled();
});
});
describe('unregister', () => {
it('should mark device as inactive', async () => {
mockRepository.findOne.mockResolvedValue(mockDevice);
mockRepository.save.mockResolvedValue({ ...mockDevice, is_active: false });
await service.unregister('device-1', 'user-1', 'tenant-1');
expect(mockRepository.save).toHaveBeenCalledWith(expect.objectContaining({ is_active: false }));
});
it('should throw NotFoundException if device not found', async () => {
mockRepository.findOne.mockResolvedValue(null);
await expect(service.unregister('device-1', 'user-1', 'tenant-1')).rejects.toThrow(common_1.NotFoundException);
});
});
describe('delete', () => {
it('should delete device', async () => {
mockRepository.delete.mockResolvedValue({ affected: 1 });
await service.delete('device-1', 'user-1', 'tenant-1');
expect(mockRepository.delete).toHaveBeenCalledWith({
id: 'device-1',
user_id: 'user-1',
tenant_id: 'tenant-1',
});
});
it('should throw NotFoundException if device not found', async () => {
mockRepository.delete.mockResolvedValue({ affected: 0 });
await expect(service.delete('device-1', 'user-1', 'tenant-1')).rejects.toThrow(common_1.NotFoundException);
});
});
describe('countActiveDevices', () => {
it('should return count of active devices', async () => {
mockRepository.count.mockResolvedValue(3);
const result = await service.countActiveDevices('user-1', 'tenant-1');
expect(result).toBe(3);
expect(mockRepository.count).toHaveBeenCalledWith({
where: { user_id: 'user-1', tenant_id: 'tenant-1', is_active: true },
});
});
});
describe('markAsUsed', () => {
it('should update last_used_at', async () => {
mockRepository.update.mockResolvedValue({ affected: 1 });
await service.markAsUsed('device-1');
expect(mockRepository.update).toHaveBeenCalledWith('device-1', expect.objectContaining({ last_used_at: expect.any(Date) }));
});
});
describe('markAsInactive', () => {
it('should set is_active to false', async () => {
mockRepository.update.mockResolvedValue({ affected: 1 });
await service.markAsInactive('device-1');
expect(mockRepository.update).toHaveBeenCalledWith('device-1', { is_active: false });
});
});
});
//# sourceMappingURL=devices.service.spec.js.map