|
| 1 | +/** |
| 2 | + * Copyright 2026 GitProxy Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, expect, test } from 'vitest'; |
| 18 | +import { createRequire } from 'node:module'; |
| 19 | + |
| 20 | +const require = createRequire(import.meta.url); |
| 21 | +const { normalizeEmail, isEmailFormatValid } = require('../lib/email.js'); |
| 22 | + |
| 23 | +describe('scripts/migrate/lib/email.js', () => { |
| 24 | + describe('normalizeEmail trims, lowercases, and coerces falsy to empty string', () => { |
| 25 | + test.each([ |
| 26 | + [null, ''], |
| 27 | + [undefined, ''], |
| 28 | + ['', ''], |
| 29 | + [' ', ''], |
| 30 | + ])('returns %j when input is %j', (input, expected) => { |
| 31 | + expect(normalizeEmail(input)).toBe(expected); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('isEmailFormatValid applies a basic format check after normalization', () => { |
| 36 | + describe('accepts addresses that match the migration regex', () => { |
| 37 | + test.each(['a@b.c', 'user+tag@example.com', 'alice@mail.example.com'])( |
| 38 | + 'treats %s as valid', |
| 39 | + (email) => { |
| 40 | + expect(isEmailFormatValid(email)).toBe(true); |
| 41 | + }, |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('rejects addresses that fail the migration regex', () => { |
| 46 | + test.each(['', 'a@b', '@x.com', 'a@', 'a @b.com', 'a@@b.com'])( |
| 47 | + 'treats %s as invalid', |
| 48 | + (email) => { |
| 49 | + expect(isEmailFormatValid(email)).toBe(false); |
| 50 | + }, |
| 51 | + ); |
| 52 | + }); |
| 53 | + }); |
| 54 | +}); |
0 commit comments