Files
calendar/packages/features/watchlist/lib/utils/normalization.test.ts
T
Syed Ali ShahbazandGitHub a2bee76da6 feat: Add spam blocker DI structure (#24040)
* --init

* --

* replace old structure with new DI

* fix type

* --

* moving stuff around

* moving stuff around again

* minor clean up

* --

* resolve conflict

* clea up

* old schema clean up

* further clean up

* removing unwanted merged responsibilities

* --

* improve DI and SOLID

* some type fixes

* --1

* clean up --cont

* fix DI in facade for test

* more fix

* fix

* checking

* o.o

* fix import

* fix failing test --2

* fix failing test --3

* fix failing test --4

* normalization use

* introduce facade container injection

* uniform async telemetry spans

* further improvements

* replace prismock with repo mocks

* ensure we don't pass prisma outside of repo

* fix import

* remove try catch from repo calls

* async await fixes

* using deps pattern

* more clean up and fixes

* more clean up

* address feedback --1

* separation of concern

* clean up

* test clean up

* feedback --2

* remove extra fetch

* remove await

* migrate _post test from prismock

* fix type

* --

* fix tokens path

* rename AuditRepo

* test --1

* update _post to integration test

* fix test

* fix test

* test fix maybe?

* --

* feedback

* feedback

* fixes

* more feedback

* NIT

* use sentry and logger imports as planned

* assertion in test

* add missing test case

* add tests for controllers and services

* NITs

* fix domain normalisation
2025-10-14 10:16:18 +03:00

98 lines
4.0 KiB
TypeScript

import { describe, expect, test } from "vitest";
import { normalizeEmail, normalizeDomain, extractDomainFromEmail, normalizeUsername } from "./normalization";
describe("normalization", () => {
describe("normalizeEmail", () => {
test("should normalize basic email", () => {
expect(normalizeEmail("Test@Example.COM")).toBe("test@example.com");
expect(normalizeEmail(" user@domain.org ")).toBe("user@domain.org");
});
test("should throw on invalid emails", () => {
expect(() => normalizeEmail("invalid")).toThrow("Invalid email format");
expect(() => normalizeEmail("@domain.com")).toThrow("Invalid email format");
expect(() => normalizeEmail("user@")).toThrow("Invalid email format");
});
});
describe("normalizeDomain", () => {
test("should normalize basic domain", () => {
expect(normalizeDomain("Example.COM")).toBe("@example.com");
expect(normalizeDomain("@Domain.ORG")).toBe("@domain.org");
expect(normalizeDomain(" sub.domain.net ")).toBe("@sub.domain.net");
});
test("should preserve full domain including subdomains", () => {
expect(normalizeDomain("mail.google.com")).toBe("@mail.google.com");
expect(normalizeDomain("sub.domain.example.org")).toBe("@sub.domain.example.org");
});
test("should handle multi-level TLDs correctly", () => {
expect(normalizeDomain("example.co.uk")).toBe("@example.co.uk");
expect(normalizeDomain("mail.example.co.uk")).toBe("@mail.example.co.uk");
expect(normalizeDomain("company.com.au")).toBe("@company.com.au");
});
test("should throw on invalid domains", () => {
expect(() => normalizeDomain("invalid..domain")).toThrow("Invalid domain format");
expect(() => normalizeDomain(".domain.com")).toThrow("Invalid domain format");
expect(() => normalizeDomain("domain.")).toThrow("Invalid domain format");
});
});
describe("extractDomainFromEmail", () => {
test("should extract and normalize domain from email", () => {
expect(extractDomainFromEmail("user@Example.COM")).toBe("@example.com");
expect(extractDomainFromEmail("test@sub.domain.org")).toBe("@sub.domain.org");
expect(extractDomainFromEmail("user@mail.google.com")).toBe("@mail.google.com");
});
test("should handle multi-level TLDs", () => {
expect(extractDomainFromEmail("user@example.co.uk")).toBe("@example.co.uk");
expect(extractDomainFromEmail("admin@mail.company.com.au")).toBe("@mail.company.com.au");
});
test("should throw on invalid emails", () => {
expect(() => extractDomainFromEmail("invalid")).toThrow("Invalid email format");
});
});
describe("normalizeUsername", () => {
test("should normalize basic username", () => {
expect(normalizeUsername("TestUser")).toBe("testuser");
expect(normalizeUsername(" user_name ")).toBe("user_name");
expect(normalizeUsername("User.Name-123")).toBe("user.name-123");
});
test("should preserve special characters", () => {
expect(normalizeUsername("user@name!")).toBe("user@name!");
expect(normalizeUsername("test.user-123")).toBe("test.user-123");
});
test("should throw on invalid usernames", () => {
expect(() => normalizeUsername("")).toThrow("Invalid username: must be a non-empty string");
});
});
describe("Edge cases and consistency", () => {
test("should handle international domains", () => {
// Note: In a real implementation, you might want to handle punycode
expect(normalizeDomain("münchen.de")).toBe("@münchen.de");
});
test("should be consistent across multiple calls", () => {
const email = "Test.User+Tag@GMAIL.COM";
const result1 = normalizeEmail(email);
const result2 = normalizeEmail(email);
expect(result1).toBe(result2);
});
test("should handle empty strings gracefully", () => {
expect(() => normalizeEmail("")).toThrow();
expect(() => normalizeDomain("")).toThrow();
expect(() => normalizeUsername("")).toThrow();
});
});
});