|
| 1 | +jest.mock('@db', () => ({ |
| 2 | + db: { |
| 3 | + device: { |
| 4 | + findUnique: jest.fn(), |
| 5 | + findFirst: jest.fn(), |
| 6 | + update: jest.fn(), |
| 7 | + create: jest.fn(), |
| 8 | + }, |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +import { db } from '@db'; |
| 13 | +import { |
| 14 | + registerWithSerial, |
| 15 | + registerWithoutSerial, |
| 16 | +} from './device-registration.helpers'; |
| 17 | +import type { RegisterDeviceDto } from './dto/register-device.dto'; |
| 18 | + |
| 19 | +const mockDb = db as jest.Mocked<typeof db>; |
| 20 | + |
| 21 | +const orgId = 'org_test'; |
| 22 | +const member = { id: 'mem_test' }; |
| 23 | + |
| 24 | +function makeDto( |
| 25 | + overrides: Partial<RegisterDeviceDto> = {}, |
| 26 | +): RegisterDeviceDto { |
| 27 | + return { |
| 28 | + organizationId: orgId, |
| 29 | + hostname: 'my-laptop.local', |
| 30 | + name: 'My Laptop', |
| 31 | + platform: 'macos', |
| 32 | + osVersion: '15.0', |
| 33 | + serialNumber: 'ABC123', |
| 34 | + hardwareModel: 'MacBookPro18,1', |
| 35 | + agentVersion: '1.0.0', |
| 36 | + ...overrides, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +describe('registerWithSerial — orphan adoption', () => { |
| 41 | + beforeEach(() => { |
| 42 | + jest.clearAllMocks(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('adopts an existing serial-less row for the same hostname+member instead of creating a duplicate', async () => { |
| 46 | + // The bug scenario: agent first registered without a serial (e.g. cold- |
| 47 | + // boot `system_profiler` returned empty), creating a row with |
| 48 | + // serialNumber=null. A later registration succeeds in reading the |
| 49 | + // serial. Without adoption, registerWithSerial would create a brand-new |
| 50 | + // row and the old one would stay orphaned. |
| 51 | + (mockDb.device.findUnique as jest.Mock).mockResolvedValue(null); |
| 52 | + (mockDb.device.findFirst as jest.Mock).mockResolvedValue({ |
| 53 | + id: 'dev_orphan', |
| 54 | + }); |
| 55 | + (mockDb.device.update as jest.Mock).mockResolvedValue({ |
| 56 | + id: 'dev_orphan', |
| 57 | + }); |
| 58 | + |
| 59 | + const dto = makeDto(); |
| 60 | + await registerWithSerial({ member, dto }); |
| 61 | + |
| 62 | + expect(mockDb.device.findFirst).toHaveBeenCalledWith({ |
| 63 | + where: { |
| 64 | + hostname: dto.hostname, |
| 65 | + memberId: member.id, |
| 66 | + organizationId: orgId, |
| 67 | + serialNumber: null, |
| 68 | + }, |
| 69 | + select: { id: true }, |
| 70 | + }); |
| 71 | + expect(mockDb.device.update).toHaveBeenCalledWith({ |
| 72 | + where: { id: 'dev_orphan' }, |
| 73 | + data: expect.objectContaining({ |
| 74 | + serialNumber: dto.serialNumber, |
| 75 | + hostname: dto.hostname, |
| 76 | + }), |
| 77 | + }); |
| 78 | + expect(mockDb.device.create).not.toHaveBeenCalled(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('creates a fresh row when no orphan exists', async () => { |
| 82 | + (mockDb.device.findUnique as jest.Mock).mockResolvedValue(null); |
| 83 | + (mockDb.device.findFirst as jest.Mock).mockResolvedValue(null); |
| 84 | + (mockDb.device.create as jest.Mock).mockResolvedValue({ id: 'dev_new' }); |
| 85 | + |
| 86 | + const dto = makeDto(); |
| 87 | + await registerWithSerial({ member, dto }); |
| 88 | + |
| 89 | + expect(mockDb.device.update).not.toHaveBeenCalled(); |
| 90 | + expect(mockDb.device.create).toHaveBeenCalledWith({ |
| 91 | + data: expect.objectContaining({ |
| 92 | + serialNumber: dto.serialNumber, |
| 93 | + memberId: member.id, |
| 94 | + organizationId: orgId, |
| 95 | + }), |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('updates the existing serial-match row without looking for an orphan', async () => { |
| 100 | + // Plain re-registration of an already-known device — must not trigger |
| 101 | + // the orphan lookup or do anything other than an in-place update. |
| 102 | + (mockDb.device.findUnique as jest.Mock).mockResolvedValue({ |
| 103 | + id: 'dev_existing', |
| 104 | + memberId: member.id, |
| 105 | + }); |
| 106 | + (mockDb.device.update as jest.Mock).mockResolvedValue({ |
| 107 | + id: 'dev_existing', |
| 108 | + }); |
| 109 | + |
| 110 | + const dto = makeDto(); |
| 111 | + await registerWithSerial({ member, dto }); |
| 112 | + |
| 113 | + expect(mockDb.device.findFirst).not.toHaveBeenCalled(); |
| 114 | + expect(mockDb.device.create).not.toHaveBeenCalled(); |
| 115 | + expect(mockDb.device.update).toHaveBeenCalledWith({ |
| 116 | + where: { id: 'dev_existing' }, |
| 117 | + data: expect.objectContaining({ hostname: dto.hostname }), |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('only adopts an orphan that belongs to the same member', async () => { |
| 122 | + // Safety: the orphan lookup is scoped by memberId, so another member's |
| 123 | + // serial-less row for the same hostname must not be hijacked. |
| 124 | + (mockDb.device.findUnique as jest.Mock).mockResolvedValue(null); |
| 125 | + (mockDb.device.findFirst as jest.Mock).mockResolvedValue(null); |
| 126 | + (mockDb.device.create as jest.Mock).mockResolvedValue({ id: 'dev_new' }); |
| 127 | + |
| 128 | + const dto = makeDto(); |
| 129 | + await registerWithSerial({ member, dto }); |
| 130 | + |
| 131 | + const call = (mockDb.device.findFirst as jest.Mock).mock.calls[0]?.[0]; |
| 132 | + expect(call?.where.memberId).toBe(member.id); |
| 133 | + expect(call?.where.serialNumber).toBeNull(); |
| 134 | + }); |
| 135 | +}); |
| 136 | + |
| 137 | +describe('registerWithoutSerial — unchanged behavior', () => { |
| 138 | + beforeEach(() => { |
| 139 | + jest.clearAllMocks(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('updates the matching null-serial row when one exists', async () => { |
| 143 | + (mockDb.device.findFirst as jest.Mock).mockResolvedValue({ |
| 144 | + id: 'dev_null', |
| 145 | + }); |
| 146 | + (mockDb.device.update as jest.Mock).mockResolvedValue({ id: 'dev_null' }); |
| 147 | + |
| 148 | + const dto = makeDto({ serialNumber: undefined }); |
| 149 | + await registerWithoutSerial({ member, dto }); |
| 150 | + |
| 151 | + expect(mockDb.device.update).toHaveBeenCalledWith({ |
| 152 | + where: { id: 'dev_null' }, |
| 153 | + data: expect.any(Object), |
| 154 | + }); |
| 155 | + expect(mockDb.device.create).not.toHaveBeenCalled(); |
| 156 | + }); |
| 157 | +}); |
0 commit comments