// @vitest-environment node import { test, expect } from 'vitest'; import { checkSignup, generateInviteCode, normalizeInviteCode, isSignupMode } from './signup.js'; test('open lets anyone in; the first account is always allowed', () => { expect(checkSignup({ mode: 'open', userCount: 5, inviteOk: false }).ok).toBe(true); expect(checkSignup({ mode: 'closed', userCount: 0, inviteOk: false }).ok).toBe(true); expect(checkSignup({ mode: 'invite', userCount: 0, inviteOk: false }).ok).toBe(true); }); test('invite-only needs a good code, closed lets nobody in', () => { expect(checkSignup({ mode: 'invite', userCount: 2, inviteOk: false })).toMatchObject({ ok: false, status: 403 }); expect(checkSignup({ mode: 'invite', userCount: 2, inviteOk: true }).ok).toBe(true); expect(checkSignup({ mode: 'closed', userCount: 2, inviteOk: true })).toMatchObject({ ok: false, status: 403 }); }); test('invite codes are readable and typed forgivingly', () => { const code = generateInviteCode(); expect(code).toMatch(/^[A-HJKMNP-Z2-9]{4}-[A-HJKMNP-Z2-9]{4}-[A-HJKMNP-Z2-9]{4}$/); expect(normalizeInviteCode(` ${code.toLowerCase().replace(/-/g, ' ')} `)).toBe(code); expect(normalizeInviteCode('abcd2345jkmn')).toBe('ABCD-2345-JKMN'); expect(isSignupMode('invite')).toBe(true); expect(isSignupMode('nope')).toBe(false); });