import { describe, it, expect } from '@jest/globals';
import {
asUuidOrNull,
normalizeEmail,
toPlainMultiline,
toPlainText
} from '../../sanitizeContactText.js';
describe('toPlainText', () => {
it('removes tags and script bodies', () => {
expect(toPlainText('Jane', 120)).toBe('Jane');
expect(toPlainText('Jane', 120)).toBe('Jane');
});
it('decodes entities so the value is not double-escaped downstream', () => {
expect(toPlainText('Tom & Jerry', 120)).toBe('Tom & Jerry');
});
it('does not leave a tag behind after entity decoding', () => {
// A tag smuggled as entities must not reassemble into markup.
expect(toPlainText('<script>x</script>', 200)).not.toMatch(
/Hi ', 5000)).toBe('Hi');
});
});
describe('normalizeEmail', () => {
it('lowercases and trims so the customer-account join is a plain match', () => {
expect(normalizeEmail(' Jane@Example.COM ')).toBe('jane@example.com');
});
it('removes all whitespace so CR/LF cannot reach the Reply-To header', () => {
const out = normalizeEmail('jane@example.com\r\nBcc: x@y.z');
expect(out).not.toMatch(/\s/);
expect(out).toBe('jane@example.combcc:x@y.z');
// And the collapsed result is no longer a valid address, so validation
// rejects it rather than letting it through as something sendable.
expect(out).not.toMatch(/^[^@\s]+@[^@\s]+\.[^@\s]+$/);
});
it('caps length', () => {
expect(normalizeEmail(`${'a'.repeat(300)}@x.com`)).toHaveLength(255);
});
});
describe('asUuidOrNull', () => {
// Without this guard a malformed value reaches a UUID column comparison and
// PostgreSQL 500s a public, unauthenticated endpoint.
it('rejects anything that is not a well-formed UUID', () => {
expect(asUuidOrNull('x')).toBeNull();
expect(asUuidOrNull("' OR 1=1--")).toBeNull();
expect(asUuidOrNull('')).toBeNull();
expect(asUuidOrNull(null)).toBeNull();
expect(asUuidOrNull(undefined)).toBeNull();
expect(asUuidOrNull(123)).toBeNull();
expect(asUuidOrNull('123e4567-e89b-12d3-a456')).toBeNull();
});
it('accepts a well-formed UUID in either case', () => {
expect(asUuidOrNull('123e4567-e89b-12d3-a456-426614174000')).toBe(
'123e4567-e89b-12d3-a456-426614174000'
);
expect(asUuidOrNull('123E4567-E89B-12D3-A456-426614174000')).toBe(
'123E4567-E89B-12D3-A456-426614174000'
);
});
});