import { describe, expect, it } from 'vitest' import { appendInvitedMember, applyActivationStats, applyMemberActivation, applyMemberRoleUpdate, incrementInvitedStats, removeTeamMemberState, type TeamMember, type TeamStats, } from './useTeam' function member(overrides: Partial): TeamMember { return { id: 'member-1', firstName: 'Amina', lastName: 'Bennis', email: 'amina@example.com', phone: null, role: 'AGENT', isActive: true, createdAt: '2026-01-01T00:00:00.000Z', updatedAt: '2026-01-01T00:00:00.000Z', lastActiveAt: null, invitationStatus: 'accepted', ...overrides, } } const stats: TeamStats = { total: 3, active: 2, pending: 1, inactive: 0 } describe('useTeam state helpers', () => { it('appends invited employees and increments the pending counters immutably', () => { const existing = [member({ id: 'owner', role: 'OWNER' })] const invited = member({ id: 'invite-1', role: 'MANAGER', invitationStatus: 'pending', isActive: false }) expect(appendInvitedMember(existing, invited)).toEqual([...existing, invited]) expect(existing).toHaveLength(1) expect(incrementInvitedStats(stats)).toEqual({ total: 4, active: 2, pending: 2, inactive: 0 }) }) it('updates roles and activation flags only on the targeted member', () => { const members = [member({ id: 'a' }), member({ id: 'b', role: 'MANAGER', isActive: false })] expect(applyMemberRoleUpdate(members, 'a', 'MANAGER')).toEqual([ { ...members[0], role: 'MANAGER' }, members[1], ]) expect(applyMemberActivation(members, 'b', true)).toEqual([ members[0], { ...members[1], isActive: true }, ]) }) it('moves counters between active and inactive for deactivate/reactivate actions', () => { expect(applyActivationStats({ total: 5, active: 4, pending: 0, inactive: 1 }, 'deactivate')).toEqual({ total: 5, active: 3, pending: 0, inactive: 2, }) expect(applyActivationStats({ total: 5, active: 3, pending: 0, inactive: 2 }, 'reactivate')).toEqual({ total: 5, active: 4, pending: 0, inactive: 1, }) }) it('removes active, pending, and inactive accepted members with the correct counter impact', () => { const members = [ member({ id: 'active', isActive: true, invitationStatus: 'accepted' }), member({ id: 'pending', isActive: false, invitationStatus: 'pending' }), member({ id: 'inactive', isActive: false, invitationStatus: 'accepted' }), ] expect(removeTeamMemberState(members, { total: 3, active: 1, pending: 1, inactive: 1 }, 'active').stats).toEqual({ total: 2, active: 0, pending: 1, inactive: 1, }) expect(removeTeamMemberState(members, { total: 3, active: 1, pending: 1, inactive: 1 }, 'pending').stats).toEqual({ total: 2, active: 1, pending: 0, inactive: 1, }) expect(removeTeamMemberState(members, { total: 3, active: 1, pending: 1, inactive: 1 }, 'inactive').stats).toEqual({ total: 2, active: 1, pending: 1, inactive: 0, }) }) it('leaves counters untouched when asked to remove an unknown member', () => { const members = [member({ id: 'only' })] const result = removeTeamMemberState(members, { total: 1, active: 1, pending: 0, inactive: 0 }, 'missing') expect(result.members).toEqual(members) expect(result.stats).toEqual({ total: 1, active: 1, pending: 0, inactive: 0 }) }) })