import type { Prisma } from '../../generated/prisma'; import { releaseQueenIn } from './colony-lifecycle'; describe('releaseQueenIn', () => { let tx: { colony: { findUnique: jest.Mock; update: jest.Mock }; queenColonyHistory: { updateMany: jest.Mock }; }; const asTx = () => tx as unknown as Prisma.TransactionClient; const endDate = new Date('2027-03-12T00:00:00.000Z'); beforeEach(() => { tx = { colony: { findUnique: jest.fn(), update: jest.fn() }, queenColonyHistory: { updateMany: jest.fn() }, }; }); it('ends the open history with the end date and clears currentQueenId', async () => { tx.colony.findUnique.mockResolvedValue({ currentQueenId: 'queen-1' }); const released = await releaseQueenIn(asTx(), 'col-1', endDate); expect(released).toBe('queen-1'); expect(tx.queenColonyHistory.updateMany).toHaveBeenCalledWith({ where: { queenId: 'queen-1', colonyId: 'col-1', endDate: null }, data: { endDate }, }); expect(tx.colony.update).toHaveBeenCalledWith({ where: { id: 'col-1' }, data: { currentQueenId: null }, }); }); it('returns null for a colony without a queen and writes nothing', async () => { tx.colony.findUnique.mockResolvedValue({ currentQueenId: null }); await expect(releaseQueenIn(asTx(), 'col-1', endDate)).resolves.toBeNull(); expect(tx.queenColonyHistory.updateMany).not.toHaveBeenCalled(); expect(tx.colony.update).not.toHaveBeenCalled(); }); it('returns null for an unknown colony', async () => { tx.colony.findUnique.mockResolvedValue(null); await expect(releaseQueenIn(asTx(), 'gone', endDate)).resolves.toBeNull(); }); });