The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: add batch email org invites (#44832)
Samir Ketema8454ec241d0c
diff --git a/apps/studio/components/interfaces/Organization/TeamSettings/InviteMemberButton.tsx b/apps/studio/components/interfaces/Organization/TeamSettings/InviteMemberButton.tsxindex 0a523935d02..842910ae5ee 100644--- a/apps/studio/components/interfaces/Organization/TeamSettings/InviteMemberButton.tsx+++ b/apps/studio/components/interfaces/Organization/TeamSettings/InviteMemberButton.tsx@@ -31,6 +31,14 @@ import { Admonition } from 'ui-patterns/admonition'import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'import * as z from 'zod'+import {+ BatchInvitationResult,+ buildProjectPayload,+ buildSsoPayload,+ categorizeInviteEmails,+ emailSchema,+ parseEmails,+} from './InviteMemberButton.utils'import { useGetRolesManagementPermissions } from './TeamSettings.utils'import { DiscardChangesConfirmationDialog } from '@/components/ui-patterns/Dialogs/DiscardChangesConfirmationDialog'import { ButtonTooltip } from '@/components/ui/ButtonTooltip'@@ -50,13 +58,6 @@ import { useConfirmOnClose } from '@/hooks/ui/useConfirmOnClose'import { DOCS_URL } from '@/lib/constants'import { useProfile } from '@/lib/profile'-function parseEmails(value: string): string[] {- return value- .split(',')- .map((e) => e.trim())- .filter(Boolean)-}-export const InviteMemberButton = () => {const { slug } = useParams()const { profile } = useProfile()@@ -115,33 +116,23 @@ export const InviteMemberButton = () => {const { mutateAsync: inviteMemberAsync, isPending: isInviting } =useOrganizationCreateInvitationMutation()- const emailSchema = z- .string()- .min(1, 'At least one email address is required')- .refine(- (val) => {- const emails = parseEmails(val)- if (emails.length === 0) return false- return emails.every((e) => z.string().email().safeParse(e).success)- },- (val) => {- const emails = parseEmails(val)- const invalid = emails.find((e) => !z.string().email().safeParse(e).success)- return {- message: invalid- ? `Invalid email address: ${invalid}`- : 'At least one email address is required',- }+ const FormSchema = z+ .object({+ email: emailSchema,+ role: z.string().min(1, 'Role is required'),+ applyToOrg: z.boolean(),+ projectRef: z.string(),+ requireSso: z.enum(['auto', 'sso', 'non-sso']),+ })+ .superRefine((data, ctx) => {+ if (!data.applyToOrg && !data.projectRef) {+ ctx.addIssue({+ code: z.ZodIssueCode.custom,+ message: 'A project must be selected',+ path: ['projectRef'],+ })}- )-- const FormSchema = z.object({- email: emailSchema,- role: z.string().min(1, 'Role is required'),- applyToOrg: z.boolean(),- projectRef: z.string(),- requireSso: z.enum(['auto', 'sso', 'non-sso']),- })+ })const form = useForm<z.infer<typeof FormSchema>>({mode: 'onSubmit',@@ -159,22 +150,10 @@ export const InviteMemberButton = () => {if (profile?.id === undefined) return console.error('Profile ID required')const emails = parseEmails(values.email).map((e) => e.toLowerCase())- const alreadyInvited: string[] = []- const alreadyMembers: string[] = []- const toInvite: string[] = []-- for (const emailAddress of emails) {- const existingMember = (members ?? []).find((member) => member.primary_email === emailAddress)- if (existingMember !== undefined) {- if (existingMember.invited_id) {- alreadyInvited.push(emailAddress)- } else {- alreadyMembers.push(emailAddress)- }- } else {- toInvite.push(emailAddress)- }- }+ const { alreadyInvited, alreadyMembers, toInvite } = categorizeInviteEmails(+ emails,+ members ?? []+ )if (alreadyInvited.length > 0) {toast.error(@@ -194,46 +173,38 @@ export const InviteMemberButton = () => {if (toInvite.length === 0) return}- const projectPayload =- !values.applyToOrg && values.projectRef ? { projects: [values.projectRef] } : {}-- // Transform SSO preference to backend format- const ssoPayload =- values.requireSso === 'sso'- ? { requireSso: true }- : values.requireSso === 'non-sso'- ? { requireSso: false }- : {} // 'auto' - let backend use automatic behavior-- const results = await Promise.allSettled(- toInvite.map((emailAddress) =>- inviteMemberAsync({- slug,- email: emailAddress,- roleId: Number(values.role),- ...projectPayload,- ...ssoPayload,- })- )- )+ const projectPayload = buildProjectPayload(values.applyToOrg, values.projectRef)+ const ssoPayload = buildSsoPayload(values.requireSso)++ let result: BatchInvitationResult+ try {+ result = (await inviteMemberAsync({+ slug,+ emails: toInvite,+ roleId: Number(values.role),+ ...projectPayload,+ ...ssoPayload,+ })) as BatchInvitationResult+ } catch {+ return // onError callback already showed the toast+ }- const successCount = results.filter((r) => r.status === 'fulfilled').length- const failedEmails = toInvite.filter((_, i) => results[i].status === 'rejected')+ const { succeeded, failed } = result- if (successCount > 0) {+ if (succeeded.length > 0) {toast.success(- successCount === 1+ succeeded.length === 1? 'Successfully sent invitation to new member'- : `Successfully sent invitations to ${successCount} new members`+ : `Successfully sent invitations to ${succeeded.length} new members`)- closeInviteDialog()}- if (failedEmails.length > 0) {- toast.error(- failedEmails.length === 1- ? `Failed to send invitation to ${failedEmails[0]}`- : `Failed to send invitations to ${failedEmails.length} emails`- )++ for (const { email, error } of failed) {+ toast.error(`Failed to invite ${email}: ${error}`)+ }++ if (succeeded.length > 0) {+ closeInviteDialog()}}diff --git a/apps/studio/components/interfaces/Organization/TeamSettings/InviteMemberButton.utils.ts b/apps/studio/components/interfaces/Organization/TeamSettings/InviteMemberButton.utils.tsnew file mode 100644index 00000000000..a58dc5be922--- /dev/null+++ b/apps/studio/components/interfaces/Organization/TeamSettings/InviteMemberButton.utils.ts@@ -0,0 +1,109 @@+import * as z from 'zod'++import type { OrganizationMember } from '@/data/organizations/organization-members-query'++export const MAX_BATCH_INVITE_SIZE = 50++/** Max characters to show when an invalid token is long (e.g. comma-less paste of many addresses). */+const MAX_INVALID_EMAIL_SNIPPET_LENGTH = 120++function formatInvalidEmailSnippet(token: string): string {+ if (token.length <= MAX_INVALID_EMAIL_SNIPPET_LENGTH) return token+ return `${token.slice(0, MAX_INVALID_EMAIL_SNIPPET_LENGTH)}…`+}++export const emailSchema = z+ .string()+ .min(1, 'At least one email address is required')+ .refine(+ (val) => {+ const emails = parseEmails(val)+ if (emails.length === 0) return false+ return emails.every((e) => z.string().email().safeParse(e).success)+ },+ (val) => {+ const emails = parseEmails(val)+ const invalid = emails.find((e) => !z.string().email().safeParse(e).success)+ return {+ message: invalid+ ? `Invalid email address: "${formatInvalidEmailSnippet(invalid)}"`+ : 'At least one email address is required',+ }+ }+ )+ .refine(+ (val) => parseEmails(val).length <= MAX_BATCH_INVITE_SIZE,+ (val) => {+ const count = parseEmails(val).length+ return {+ message: `You can invite up to ${MAX_BATCH_INVITE_SIZE} members at a time. Remove ${count - MAX_BATCH_INVITE_SIZE} email ${count - MAX_BATCH_INVITE_SIZE === 1 ? 'address' : 'addresses'} to continue.`,+ }+ }+ )++export function parseEmails(value: string): string[] {+ const emails = value+ .split(/[\s,]+/)+ .map((e) => e.trim().toLowerCase())+ .filter(Boolean)+ return [...new Set(emails)]+}++export type CategorizedEmails = {+ alreadyInvited: string[]+ alreadyMembers: string[]+ toInvite: string[]+}++export type BatchInvitationFailure = {+ email: string+ error: string+}++export type BatchInvitationResult = {+ succeeded: string[]+ failed: BatchInvitationFailure[]+}++export function categorizeInviteEmails(+ emails: string[],+ members: OrganizationMember[]+): CategorizedEmails {+ const alreadyInvited: string[] = []+ const alreadyMembers: string[] = []+ const toInvite: string[] = []++ for (const email of emails) {+ const existingMember = members.find((m) => m.primary_email === email)+ if (existingMember !== undefined) {+ if (existingMember.invited_id) {+ alreadyInvited.push(email)+ } else {+ alreadyMembers.push(email)+ }+ } else {+ toInvite.push(email)+ }+ }++ return { alreadyInvited, alreadyMembers, toInvite }+}++export function buildProjectPayload(+ applyToOrg: boolean,+ projectRef: string+): { projects: string[] } | Record<string, never> {+ if (applyToOrg) return {}+ if (!projectRef) {+ throw new Error('projectRef is required when applyToOrg is false')+ }+ return { projects: [projectRef] }+}++export function buildSsoPayload(+ requireSso: 'auto' | 'sso' | 'non-sso'+): { requireSso: boolean } | Record<string, never> {+ if (requireSso === 'sso') return { requireSso: true }+ if (requireSso === 'non-sso') return { requireSso: false }+ return {}+}diff --git a/apps/studio/components/interfaces/Organization/TeamSettings/MemberActions.tsx b/apps/studio/components/interfaces/Organization/TeamSettings/MemberActions.tsxindex bffc2334788..6e32afd562a 100644--- a/apps/studio/components/interfaces/Organization/TeamSettings/MemberActions.tsx+++ b/apps/studio/components/interfaces/Organization/TeamSettings/MemberActions.tsx@@ -128,12 +128,12 @@ export const MemberActions = ({ member }: MemberActionsProps) => {const projects = projectScopedRole.projects.map(({ ref }) => ref)inviteMember({slug,- email: member.primary_email,+ emails: [member.primary_email],roleId: projectScopedRole.base_role_id,projects,})} else {- inviteMember({ slug, email: member.primary_email, roleId })+ inviteMember({ slug, emails: [member.primary_email], roleId })}},}diff --git a/apps/studio/data/organization-members/organization-invitation-create-mutation.ts b/apps/studio/data/organization-members/organization-invitation-create-mutation.tsindex f28af34d4a0..ea632dac7e3 100644--- a/apps/studio/data/organization-members/organization-invitation-create-mutation.ts+++ b/apps/studio/data/organization-members/organization-invitation-create-mutation.ts@@ -9,7 +9,7 @@ import type { ResponseError, UseCustomMutationOptions } from '@/types'export type OrganizationCreateInvitationVariables = {slug: string- email: string+ emails: string[]roleId: numberprojects?: string[]requireSso?: boolean@@ -17,12 +17,12 @@ export type OrganizationCreateInvitationVariables = {export async function createOrganizationInvitation({slug,- email,+ emails,roleId,projects,requireSso,}: OrganizationCreateInvitationVariables) {- const payload: components['schemas']['CreateInvitationBody'] = { email, role_id: roleId }+ const payload: components['schemas']['CreateInvitationBody'] = { emails, role_id: roleId }if (projects !== undefined) payload.role_scoped_projects = projectsif (requireSso !== undefined) payload.require_sso = requireSso@@ -69,7 +69,7 @@ export const useOrganizationCreateInvitationMutation = ({},async onError(data, variables, context) {if (onError === undefined) {- toast.error(`Failed to update member role: ${data.message}`)+ toast.error(`Failed to send invitation${data.message ? ': ' + data.message : ''}`)} else {onError(data, variables, context)}diff --git a/apps/studio/tests/components/Organization/TeamSettings/InviteMemberButton.test.tsx b/apps/studio/tests/components/Organization/TeamSettings/InviteMemberButton.test.tsxnew file mode 100644index 00000000000..2420b822bef--- /dev/null+++ b/apps/studio/tests/components/Organization/TeamSettings/InviteMemberButton.test.tsx@@ -0,0 +1,254 @@+import { fireEvent, screen, waitFor } from '@testing-library/react'+import userEvent from '@testing-library/user-event'+import { toast } from 'sonner'+import { beforeEach, describe, expect, it, vi } from 'vitest'++import { InviteMemberButton } from '@/components/interfaces/Organization/TeamSettings/InviteMemberButton'+import { customRender } from '@/tests/lib/custom-render'++vi.mock('sonner', () => ({+ toast: { success: vi.fn(), error: vi.fn() },+}))++vi.mock('common', async (importOriginal) => {+ const actual = (await importOriginal()) as typeof import('common')+ return { ...actual, useParams: () => ({ slug: 'test-org' }) }+})++vi.mock('@/lib/profile', () => ({+ useProfile: () => ({ profile: { id: 1, gotrue_id: 'user-1' } }),+}))++vi.mock('@/hooks/misc/useSelectedOrganization', () => ({+ useSelectedOrganizationQuery: () => ({+ data: { id: 1, slug: 'test-org', name: 'Test Org' },+ }),+}))++vi.mock('@/hooks/misc/useCheckPermissions', () => ({+ useGetPermissions: () => ({ permissions: [], organizationSlug: 'test-org' }),+ doPermissionsCheck: () => true,+ useAsyncCheckPermissions: () => ({ can: true, isSuccess: true }),+}))++vi.mock('@/hooks/misc/useIsFeatureEnabled', () => ({+ useIsFeatureEnabled: () => ({ organizationMembersCreate: true }),+}))++vi.mock('@/data/organizations/organization-members-query', () => ({+ useOrganizationMembersQuery: () => ({+ data: [+ {+ gotrue_id: 'user-1',+ primary_email: '[email protected]',+ role_ids: [1],+ },+ {+ gotrue_id: 'existing-user',+ primary_email: '[email protected]',+ role_ids: [1],+ },+ ],+ }),+}))++const mockRoles = {+ org_scoped_roles: [{ id: 1, name: 'Developer', description: null }],+}+vi.mock('@/data/organization-members/organization-roles-query', () => ({+ useOrganizationRolesV2Query: () => ({ data: mockRoles, isSuccess: true }),+}))++vi.mock('@/data/sso/sso-config-query', () => ({+ useOrgSSOConfigQuery: () => ({ data: null }),+}))++vi.mock('@/data/subscriptions/org-subscription-query', () => ({+ useHasAccessToProjectLevelPermissions: () => false,+}))++vi.mock('@/hooks/misc/useCheckEntitlements', () => ({+ useCheckEntitlements: () => ({ hasAccess: false }),+}))++vi.mock('@/components/interfaces/Organization/TeamSettings/TeamSettings.utils', () => ({+ useGetRolesManagementPermissions: () => ({ rolesAddable: [1], rolesRemovable: [1] }),+}))++const mockInvite = vi.fn().mockResolvedValue({ succeeded: [], failed: [] })+vi.mock('@/data/organization-members/organization-invitation-create-mutation', () => ({+ useOrganizationCreateInvitationMutation: () => ({+ mutateAsync: mockInvite,+ isPending: false,+ }),+}))++vi.mock('@/hooks/ui/useConfirmOnClose', () => ({+ useConfirmOnClose: ({ onClose }: { checkIsDirty: () => boolean; onClose: () => void }) => ({+ confirmOnClose: onClose,+ handleOpenChange: (open: boolean) => {+ if (!open) onClose()+ },+ modalProps: { visible: false, onClose, onCancel: vi.fn() },+ }),+}))++// Helpers+async function openDialog() {+ await userEvent.click(screen.getByRole('button', { name: /invite members/i }))+ return screen.findByRole('dialog')+}++async function submitForm(emailValue: string) {+ await openDialog()+ fireEvent.change(screen.getByPlaceholderText(/name@example\.com/i), {+ target: { value: emailValue },+ })+ fireEvent.click(screen.getByRole('button', { name: /send invitation/i }))+}++// Tests+describe('InviteMemberButton', () => {+ beforeEach(() => {+ vi.clearAllMocks()+ mockInvite.mockResolvedValue({ succeeded: [], failed: [] })+ })++ it('renders an enabled Invite members button', () => {+ customRender(<InviteMemberButton />)+ expect(screen.getByRole('button', { name: /invite members/i })).toBeEnabled()+ })++ it('opens the invite dialog when the button is clicked', async () => {+ customRender(<InviteMemberButton />)+ await openDialog()+ expect(screen.getByRole('dialog')).toBeInTheDocument()+ expect(screen.getByText('Invite team members')).toBeInTheDocument()+ })++ it('calls the mutation with a single email in an array', async () => {+ customRender(<InviteMemberButton />)+ await submitForm('[email protected]')++ await waitFor(() => {+ expect(mockInvite).toHaveBeenCalledWith(+ expect.objectContaining({ emails: ['[email protected]'] })+ )+ })+ })++ it('calls the mutation with multiple emails parsed from a comma-separated input', async () => {+ customRender(<InviteMemberButton />)++ await waitFor(() => {+ expect(mockInvite).toHaveBeenCalledWith(+ expect.objectContaining({+ })+ )+ })+ })++ it('lowercases emails before sending', async () => {+ customRender(<InviteMemberButton />)+ await submitForm('[email protected]')++ await waitFor(() => {+ expect(mockInvite).toHaveBeenCalledWith(+ expect.objectContaining({ emails: ['[email protected]'] })+ )+ })+ })++ it('shows a validation error for an invalid email', async () => {+ customRender(<InviteMemberButton />)+ await openDialog()+ fireEvent.change(screen.getByPlaceholderText(/name@example\.com/i), {+ target: { value: 'not-an-email' },+ })+ fireEvent.click(screen.getByRole('button', { name: /send invitation/i }))++ expect(await screen.findByText(/invalid email address: "not-an-email"/i)).toBeInTheDocument()+ expect(mockInvite).not.toHaveBeenCalled()+ })++ it('shows an error toast and skips the mutation for an already-existing member', async () => {+ customRender(<InviteMemberButton />)+ await submitForm('[email protected]')++ await waitFor(() => {+ expect(toast.error).toHaveBeenCalledWith(+ '[email protected] is already in this organization'+ )+ })+ expect(mockInvite).not.toHaveBeenCalled()+ })++ it('still invites new emails in a batch that also contains an existing member', async () => {+ customRender(<InviteMemberButton />)+ await submitForm('[email protected], [email protected]')++ await waitFor(() => {+ expect(toast.error).toHaveBeenCalled()+ expect(mockInvite).toHaveBeenCalledWith(+ expect.objectContaining({ emails: ['[email protected]'] })+ )+ })+ })++ it('shows a success toast for a single email in succeeded', async () => {+ mockInvite.mockResolvedValueOnce({ succeeded: ['[email protected]'], failed: [] })+ customRender(<InviteMemberButton />)+ await submitForm('[email protected]')++ await waitFor(() => {+ expect(toast.success).toHaveBeenCalledWith('Successfully sent invitation to new member')+ })+ })++ it('shows a plural success toast when multiple emails succeeded', async () => {+ mockInvite.mockResolvedValueOnce({+ succeeded: ['[email protected]', '[email protected]'],+ failed: [],+ })+ customRender(<InviteMemberButton />)+ await submitForm('[email protected], [email protected]')++ await waitFor(() => {+ expect(toast.success).toHaveBeenCalledWith('Successfully sent invitations to 2 new members')+ })+ })++ it('shows an error toast with the server error for each failed email', async () => {+ mockInvite.mockResolvedValueOnce({+ succeeded: [],+ failed: [{ email: '[email protected]', error: 'Domain not allowed' }],+ })+ customRender(<InviteMemberButton />)+ await submitForm('[email protected]')++ await waitFor(() => {+ expect(toast.error).toHaveBeenCalledWith(+ 'Failed to invite [email protected]: Domain not allowed'+ )+ })+ expect(toast.success).not.toHaveBeenCalled()+ })++ it('shows both success and error toasts for a partial batch result', async () => {+ mockInvite.mockResolvedValueOnce({+ succeeded: ['[email protected]'],+ failed: [{ email: '[email protected]', error: 'Domain not allowed' }],+ })+ customRender(<InviteMemberButton />)+ await submitForm('[email protected], [email protected]')++ await waitFor(() => {+ expect(toast.success).toHaveBeenCalledWith('Successfully sent invitation to new member')+ expect(toast.error).toHaveBeenCalledWith(+ 'Failed to invite [email protected]: Domain not allowed'+ )+ })+ })+})diff --git a/apps/studio/tests/components/Organization/TeamSettings/InviteMemberButton.utils.test.ts b/apps/studio/tests/components/Organization/TeamSettings/InviteMemberButton.utils.test.tsnew file mode 100644index 00000000000..3012b9a0209--- /dev/null+++ b/apps/studio/tests/components/Organization/TeamSettings/InviteMemberButton.utils.test.ts@@ -0,0 +1,261 @@+import { describe, expect, test } from 'vitest'++import {+ buildProjectPayload,+ buildSsoPayload,+ categorizeInviteEmails,+ emailSchema,+ MAX_BATCH_INVITE_SIZE,+ parseEmails,+} from '@/components/interfaces/Organization/TeamSettings/InviteMemberButton.utils'+import type { OrganizationMember } from '@/data/organizations/organization-members-query'++describe('parseEmails', () => {+ test('parses a single email', () => {+ expect(parseEmails('[email protected]')).toStrictEqual(['[email protected]'])+ })++ test('parses multiple comma-separated emails', () => {+ '[email protected]',+ '[email protected]',+ '[email protected]',+ ])+ })++ test('trims whitespace around each email', () => {+ expect(parseEmails(' [email protected] , [email protected] ')).toStrictEqual([+ '[email protected]',+ '[email protected]',+ ])+ })++ test('filters out empty entries from trailing, leading, or double commas', () => {+ expect(parseEmails(',[email protected],,[email protected],')).toStrictEqual([+ '[email protected]',+ '[email protected]',+ ])+ })++ test('removes duplicate email addresses', () => {+ })++ test('returns an empty array for an empty string', () => {+ expect(parseEmails('')).toStrictEqual([])+ })++ test('returns an empty array for a whitespace-only string', () => {+ expect(parseEmails(' ')).toStrictEqual([])+ })++ test('returns an empty array for commas only', () => {+ expect(parseEmails(',,,,')).toStrictEqual([])+ })++ test('parses space-separated emails', () => {+ expect(parseEmails('[email protected] [email protected]')).toStrictEqual([+ '[email protected]',+ '[email protected]',+ ])+ })++ test('parses line breaks and mixed comma or space separators', () => {+ '[email protected]',+ '[email protected]',+ '[email protected]',+ ])+ })+})++function makeMember(overrides: Partial<OrganizationMember> = {}): OrganizationMember {+ return {+ gotrue_id: 'gotrue-1',+ primary_email: '[email protected]',+ role_ids: [1],+ username: 'member',+ ...overrides,+ } as OrganizationMember+}++describe('categorizeInviteEmails', () => {+ test('places a new email in toInvite when no members exist', () => {+ expect(categorizeInviteEmails(['[email protected]'], [])).toStrictEqual({+ alreadyInvited: [],+ alreadyMembers: [],+ toInvite: ['[email protected]'],+ })+ })++ test('places an email in alreadyMembers when that member exists without an invited_id', () => {+ const members = [makeMember({ primary_email: '[email protected]' })]+ expect(categorizeInviteEmails(['[email protected]'], members)).toStrictEqual({+ alreadyInvited: [],+ alreadyMembers: ['[email protected]'],+ toInvite: [],+ })+ })++ test('places an email in alreadyInvited when that member has an invited_id', () => {+ const members = [makeMember({ primary_email: '[email protected]', invited_id: 42 })]+ expect(categorizeInviteEmails(['[email protected]'], members)).toStrictEqual({+ alreadyInvited: ['[email protected]'],+ alreadyMembers: [],+ toInvite: [],+ })+ })++ test('correctly categorizes a mixed batch', () => {+ const members = [+ makeMember({ primary_email: '[email protected]' }),+ makeMember({ primary_email: '[email protected]', invited_id: 7 }),+ ]+ expect(+ categorizeInviteEmails(+ members+ )+ ).toStrictEqual({+ alreadyInvited: ['[email protected]'],+ alreadyMembers: ['[email protected]'],+ toInvite: ['[email protected]'],+ })+ })++ test('places all emails in toInvite when none match existing members', () => {+ const members = [makeMember({ primary_email: '[email protected]' })]+ expect(+ ).toStrictEqual({+ alreadyInvited: [],+ alreadyMembers: [],+ })+ })++ test('returns all-empty for an empty email list', () => {+ expect(categorizeInviteEmails([], [makeMember()])).toStrictEqual({+ alreadyInvited: [],+ alreadyMembers: [],+ toInvite: [],+ })+ })++ test('uses strict equality — does not match different casing', () => {+ // The component lowercases emails before calling this function,+ // so '[email protected]' must NOT match '[email protected]'+ const members = [makeMember({ primary_email: '[email protected]' })]+ const result = categorizeInviteEmails(['[email protected]'], members)+ expect(result.toInvite).toStrictEqual(['[email protected]'])+ expect(result.alreadyMembers).toStrictEqual([])+ })+})++describe('buildProjectPayload', () => {+ test('returns empty object when applyToOrg is true', () => {+ expect(buildProjectPayload(true, 'ref_abc')).toStrictEqual({})+ })++ test('throws an error when applyToOrg is false but projectRef is empty', () => {+ expect(() => buildProjectPayload(false, '')).toThrowError(+ 'projectRef is required when applyToOrg is false'+ )+ })++ test('returns projects array when applyToOrg is false and projectRef is set', () => {+ expect(buildProjectPayload(false, 'ref_abc')).toStrictEqual({ projects: ['ref_abc'] })+ })++ test('wraps the projectRef in a single-element array', () => {+ expect(buildProjectPayload(false, 'my-project-ref')).toStrictEqual({+ projects: ['my-project-ref'],+ })+ })+})++describe('buildSsoPayload', () => {+ test('returns empty object for "auto"', () => {+ expect(buildSsoPayload('auto')).toStrictEqual({})+ })++ test('returns { requireSso: true } for "sso"', () => {+ expect(buildSsoPayload('sso')).toStrictEqual({ requireSso: true })+ })++ test('returns { requireSso: false } for "non-sso"', () => {+ expect(buildSsoPayload('non-sso')).toStrictEqual({ requireSso: false })+ })+})++function makeEmailList(count: number): string {+ return Array.from({ length: count }, (_, i) => `user${i + 1}@example.com`).join(', ')+}++function makeEmailListSpaceSeparated(count: number): string {+ return Array.from({ length: count }, (_, i) => `user${i + 1}@example.com`).join(' ')+}++describe('emailSchema', () => {+ test('accepts a single valid email', () => {+ expect(emailSchema.safeParse('[email protected]').success).toBe(true)+ })++ test('accepts exactly 50 emails', () => {+ expect(emailSchema.safeParse(makeEmailList(MAX_BATCH_INVITE_SIZE)).success).toBe(true)+ })++ test('rejects 51 emails — singular "address" when exactly 1 needs removing', () => {+ const result = emailSchema.safeParse(makeEmailList(51))+ expect(result.success).toBe(false)+ if (!result.success) {+ expect(result.error.issues[0].message).toBe(+ 'You can invite up to 50 members at a time. Remove 1 email address to continue.'+ )+ }+ })++ test('rejects 51 space-separated emails (same as comma-separated batch limit)', () => {+ const result = emailSchema.safeParse(makeEmailListSpaceSeparated(51))+ expect(result.success).toBe(false)+ if (!result.success) {+ expect(result.error.issues[0].message).toBe(+ 'You can invite up to 50 members at a time. Remove 1 email address to continue.'+ )+ }+ })++ test('rejects 99 emails — plural "addresses" when more than 1 needs removing', () => {+ const result = emailSchema.safeParse(makeEmailList(99))+ expect(result.success).toBe(false)+ if (!result.success) {+ expect(result.error.issues[0].message).toBe(+ 'You can invite up to 50 members at a time. Remove 49 email addresses to continue.'+ )+ }+ })++ test('rejects an empty string', () => {+ const result = emailSchema.safeParse('')+ expect(result.success).toBe(false)+ })++ test('rejects an invalid email address and names it', () => {+ const result = emailSchema.safeParse('notanemail')+ expect(result.success).toBe(false)+ if (!result.success) {+ expect(result.error.issues[0].message).toBe('Invalid email address: "notanemail"')+ }+ })++ test('truncates a very long invalid token in the error message', () => {+ const longToken = `${'x'.repeat(130)}@`+ const result = emailSchema.safeParse(longToken)+ expect(result.success).toBe(false)+ if (!result.success) {+ expect(result.error.issues[0].message.startsWith('Invalid email address: "')).toBe(true)+ expect(result.error.issues[0].message.endsWith('…"')).toBe(true)+ expect(result.error.issues[0].message.length).toBeLessThan(longToken.length + 50)+ }+ })+})