diff --git a/src/db/repositories/drizzle/phase.test.ts b/src/db/repositories/drizzle/phase.test.ts index 2e49f9a..9ab0b5a 100644 --- a/src/db/repositories/drizzle/phase.test.ts +++ b/src/db/repositories/drizzle/phase.test.ts @@ -168,4 +168,93 @@ describe('DrizzlePhaseRepository', () => { ); }); }); + + describe('findByNumber', () => { + it('should find phase by initiative and number', async () => { + await phaseRepo.create({ + initiativeId: testInitiativeId, + number: 1, + name: 'Phase 1', + }); + await phaseRepo.create({ + initiativeId: testInitiativeId, + number: 2, + name: 'Phase 2', + }); + + const found = await phaseRepo.findByNumber(testInitiativeId, 1); + expect(found).not.toBeNull(); + expect(found?.name).toBe('Phase 1'); + + const foundTwo = await phaseRepo.findByNumber(testInitiativeId, 2); + expect(foundTwo).not.toBeNull(); + expect(foundTwo?.name).toBe('Phase 2'); + }); + + it('should return null for non-existent number', async () => { + await phaseRepo.create({ + initiativeId: testInitiativeId, + number: 1, + name: 'Phase 1', + }); + + const found = await phaseRepo.findByNumber(testInitiativeId, 99); + expect(found).toBeNull(); + }); + + it('should return null for wrong initiative', async () => { + await phaseRepo.create({ + initiativeId: testInitiativeId, + number: 1, + name: 'Phase 1', + }); + + // Create another initiative + const otherInitiative = await initiativeRepo.create({ + name: 'Other Initiative', + }); + + const found = await phaseRepo.findByNumber(otherInitiative.id, 1); + expect(found).toBeNull(); + }); + }); + + describe('getNextNumber', () => { + it('should return 1 for empty initiative', async () => { + const next = await phaseRepo.getNextNumber(testInitiativeId); + expect(next).toBe(1); + }); + + it('should return max + 1', async () => { + await phaseRepo.create({ + initiativeId: testInitiativeId, + number: 1, + name: 'P1', + }); + await phaseRepo.create({ + initiativeId: testInitiativeId, + number: 2, + name: 'P2', + }); + + const next = await phaseRepo.getNextNumber(testInitiativeId); + expect(next).toBe(3); + }); + + it('should handle gaps in numbering', async () => { + await phaseRepo.create({ + initiativeId: testInitiativeId, + number: 1, + name: 'P1', + }); + await phaseRepo.create({ + initiativeId: testInitiativeId, + number: 5, + name: 'P5', + }); + + const next = await phaseRepo.getNextNumber(testInitiativeId); + expect(next).toBe(6); + }); + }); });