Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

5.1 KB
Raw Blame
package onboarding

import (
	"errors"
	"testing"
)

func TestNormalizeEmailAccepts(t *testing.T) {
	cases := []struct {
		name  string
		input string
		want  string
	}{
		{"plain", "[email protected]", "[email protected]"},
		{"uppercase domain", "[email protected]", "[email protected]"},
		{"uppercase local", "[email protected]", "[email protected]"},
		{"surrounding space", "  [email protected]  ", "[email protected]"},
		{"angle brackets from a mail client", "<[email protected]>", "[email protected]"},
		{"plus addressing", "[email protected]", "[email protected]"},
		{"subdomain", "[email protected]", "[email protected]"},
		{"hyphenated domain", "[email protected]", "[email protected]"},
		{"digits in domain", "[email protected]", "[email protected]"},
		{"long tld", "[email protected]", "[email protected]"},
		{"free provider is still valid", "[email protected]", "[email protected]"},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got, err := NormalizeEmail(tc.input)
			if err != nil {
				t.Fatalf("NormalizeEmail(%q) returned error: %v", tc.input, err)
			}
			if got != tc.want {
				t.Fatalf("NormalizeEmail(%q) = %q, want %q", tc.input, got, tc.want)
			}
		})
	}
}

func TestNormalizeEmailRejects(t *testing.T) {
	cases := []struct {
		name  string
		input string
		want  error
	}{
		{"empty", "", ErrEmailEmpty},
		{"no at", "shukan.asymptotelabs.ai", ErrEmailNoAt},
		{"two ats", "shukan@@asymptotelabs.ai", ErrEmailMultipleAt},
		{"no local part", "@asymptotelabs.ai", ErrEmailNoLocalPart},
		{"no domain", "shukan@", ErrEmailNoDomain},
		{"no dot in domain", "shukan@localhost", ErrEmailNoDot},
		{"trailing dot", "[email protected].", ErrEmailEmptyLabel},
		{"single character tld", "[email protected]", ErrEmailBadTLD},
		{"numeric tld", "[email protected]", ErrEmailBadTLD},
		{"space in local part", "shukan [email protected]", ErrEmailLocalBadChar},
		{"rfc2606 example domain", "[email protected]", ErrEmailPlaceholder},
		{"reserved test tld", "[email protected]", ErrEmailPlaceholder},
		{"documentation stand-in", "[email protected]", ErrEmailPlaceholder},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got, err := NormalizeEmail(tc.input)
			if err == nil {
				t.Fatalf("NormalizeEmail(%q) = %q, want error %v", tc.input, got, tc.want)
			}
			if !errors.Is(err, tc.want) {
				t.Fatalf("NormalizeEmail(%q) error = %v, want %v", tc.input, err, tc.want)
			}
		})
	}
}

func TestNormalizeEmailRejectsOverlongParts(t *testing.T) {
	long := make([]byte, maxLocalPart+1)
	for i := range long {
		long[i] = 'a'
	}
	if _, err := NormalizeEmail(string(long) + "@company.com"); !errors.Is(err, ErrEmailLocalTooLong) {
		t.Fatalf("overlong local part error = %v, want %v", err, ErrEmailLocalTooLong)
	}
}

// A registered domain that merely looks suspicious must still be accepted. A wrong
// guess hard-blocks a real user; a junk row only costs a downstream filter.
func TestNormalizeEmailAcceptsRealButOddDomains(t *testing.T) {
	for _, input := range []string{"[email protected]", "[email protected]", "[email protected]", "[email protected]"} {
		if _, err := NormalizeEmail(input); err != nil {
			t.Fatalf("NormalizeEmail(%q) returned error %v, want it accepted", input, err)
		}
	}
}

func TestEmailDomain(t *testing.T) {
	if got := EmailDomain("[email protected]"); got != "asymptotelabs.ai" {
		t.Fatalf("EmailDomain() = %q, want %q", got, "asymptotelabs.ai")
	}
	if got := EmailDomain("not-an-email"); got != "" {
		t.Fatalf("EmailDomain() = %q for a malformed address, want empty", got)
	}
}

func TestClassifyDomain(t *testing.T) {
	cases := []struct {
		domain string
		want   string
	}{
		{"asymptotelabs.ai", DomainCorporate},
		{"some-startup.io", DomainCorporate},
		{"gmail.com", DomainFree},
		{"GMAIL.COM", DomainFree},
		{"", DomainUnknown},
	}
	for _, tc := range cases {
		if got := ClassifyDomain(tc.domain); got != tc.want {
			t.Fatalf("ClassifyDomain(%q) = %q, want %q", tc.domain, got, tc.want)
		}
	}
}

func TestNormalizeUsage(t *testing.T) {
	cases := []struct {
		input string
		want  string
		ok    bool
	}{
		{"1", UsageWork, true},
		{"work", UsageWork, true},
		{"WORK", UsageWork, true},
		{" 2 ", UsagePersonal, true},
		{"3", UsageEvaluating, true},
		{"team", UsageEvaluating, true},
		{"", "", false},
		{"4", "", false},
		{"maybe", "", false},
	}
	for _, tc := range cases {
		got, ok := NormalizeUsage(tc.input)
		if ok != tc.ok || got != tc.want {
			t.Fatalf("NormalizeUsage(%q) = (%q, %t), want (%q, %t)", tc.input, got, ok, tc.want, tc.ok)
		}
	}
}

// The menu prints UsageLabels against ValidUsages by index, so they must stay aligned.
func TestUsageLabelsMatchValidUsages(t *testing.T) {
	if len(UsageLabels) != len(ValidUsages) {
		t.Fatalf("len(UsageLabels) = %d, len(ValidUsages) = %d; they are indexed together", len(UsageLabels), len(ValidUsages))
	}
	for i, usage := range ValidUsages {
		if _, ok := NormalizeUsage(usage); !ok {
			t.Fatalf("ValidUsages[%d] = %q is not accepted by NormalizeUsage", i, usage)
		}
	}
}