import { randomUUID } from 'node:crypto'; import { NotFoundException } from '@nestjs/common'; import { OwnerContext } from '../users/owner-context.service'; import { DevicesService } from './devices.service'; describe('DevicesService', () => { it('assigns newly registered devices to the bootstrap default owner', async () => { const ownerId = randomUUID(); const prismaService = { device: { create: jest.fn().mockImplementation(async ({ data }) => ({ id: randomUUID(), createdAt: new Date(), updatedAt: new Date(), ...data, })), }, } as any; const ownerContext = { resolve: jest.fn().mockResolvedValue({ userId: ownerId, }), } as any; const service = new DevicesService( prismaService, ownerContext as OwnerContext, ); await service.register({ platform: 'MACOS', deviceName: 'Velody Mac', appVersion: '0.1.0', }); expect(ownerContext.resolve).toHaveBeenCalledTimes(1); expect(prismaService.device.create).toHaveBeenCalledWith({ data: expect.objectContaining({ userId: ownerId, platform: 'MACOS', deviceName: 'Velody Mac', appVersion: '0.1.0', }), }); }); it('rejects heartbeat updates for a foreign-owner device', async () => { const ownerId = randomUUID(); const foreignOwnerId = randomUUID(); const deviceId = randomUUID(); const prismaService = { device: { findUnique: jest.fn().mockResolvedValue({ userId: foreignOwnerId, }), update: jest.fn(), }, } as any; const ownerContext = { resolve: jest.fn().mockResolvedValue({ userId: ownerId, }), } as any; const service = new DevicesService( prismaService, ownerContext as OwnerContext, ); await expect( service.heartbeat({ deviceId, appVersion: '0.1.1', }), ).rejects.toBeInstanceOf(NotFoundException); expect(prismaService.device.update).not.toHaveBeenCalled(); }); });