package bot import ( "fmt" "strings" "testing" ) func TestXpForLevel(t *testing.T) { cases := []struct { n int want int64 }{ {0, 0}, {1, 100}, {2, 300}, {5, 1500}, {10, 5500}, {27, 37800}, {50, 127500}, } for _, c := range cases { got := xpForLevel(c.n) if got != c.want { t.Errorf("xpForLevel(%d) = %d, want %d", c.n, got, c.want) } } } func TestLevelForXP(t *testing.T) { cases := []struct { xp int64 want int }{ {0, 0}, {50, 0}, {99, 0}, {100, 1}, {299, 1}, {300, 2}, {5500, 10}, {37800, 27}, {127500, 50}, } for _, c := range cases { got := levelForXP(c.xp) if got != c.want { t.Errorf("levelForXP(%d) = %d, want %d", c.xp, got, c.want) } } } func TestLevelForXP_Monotonic(t *testing.T) { for n := 0; n <= 100; n++ { xp := xpForLevel(n) l := levelForXP(xp) if l != n { t.Errorf("levelForXP(xpForLevel(%d)) = %d, want %d", n, l, n) } } } func TestXpForWorkSeconds(t *testing.T) { if got := xpForWorkSeconds(0); got != 0 { t.Errorf("xpForWorkSeconds(0) = %d, want 0", got) } if got := xpForWorkSeconds(3600); got != 3600 { t.Errorf("xpForWorkSeconds(3600) = %d, want 3600", got) } if got := xpForWorkSeconds(1800); got != 1800 { t.Errorf("xpForWorkSeconds(1800) = %d, want 1800", got) } } func TestStreakBonus(t *testing.T) { cases := []struct { streak int want int64 }{ {0, 0}, {1, 50}, {10, 500}, {5, 250}, {20, 500}, } for _, c := range cases { got := streakBonus(c.streak) if got != c.want { t.Errorf("streakBonus(%d) = %d, want %d", c.streak, got, c.want) } } } func TestCurrencySymbol(t *testing.T) { cases := []struct { currency string want string }{ {"", "$"}, {"$", "$"}, {"$ USD", "$"}, {"€ EUR", "€"}, {"£", "£"}, {"Toman", "Toman"}, {" ", "$"}, } for _, c := range cases { got := currencySymbol(c.currency) if got != c.want { t.Errorf("currencySymbol(%q) = %q, want %q", c.currency, got, c.want) } } } func TestComputeDailySalary(t *testing.T) { // 1 hour at $25/hr got := computeDailySalary(3600, 25.0) if got != 25.0 { t.Errorf("computeDailySalary(3600, 25) = %.2f, want 25.00", got) } // 2.5 hours at $20/hr = $50 got = computeDailySalary(9000, 20.0) if got != 50.0 { t.Errorf("computeDailySalary(9000, 20) = %.2f, want 50.00", got) } // 0 seconds = $0 got = computeDailySalary(0, 50.0) if got != 0 { t.Errorf("computeDailySalary(0, 50) = %.2f, want 0", got) } // rounding: 3599 sec (0.9997 hr) at $10/hr = $9.997 -> $10.00 got = computeDailySalary(3599, 10.0) if got != 10.0 { t.Errorf("computeDailySalary(3599, 10) = %.2f, want 10.00", got) } } func TestSanitizeNote(t *testing.T) { // Trims spaces if got := SanitizeNote(" hello "); got != "hello" { t.Errorf("SanitizeNote trim: got %q", got) } // Removes control chars except newline/tab if got := SanitizeNote("he\x00llo\nworld\t!"); got != "hello\nworld\t!" { t.Errorf("SanitizeNote control: got %q", got) } // Preserves newlines and tabs if got := SanitizeNote("line1\nline2\tindented"); got != "line1\nline2\tindented" { t.Errorf("SanitizeNote newline/tab: got %q", got) } // Truncates long notes long := string(make([]byte, 600)) for i := range long { long = long[:i] + "a" + long[i+1:] } got := SanitizeNote(long) if len(got) > 500 { t.Errorf("SanitizeNote length: got %d, want <= 500", len(got)) } } func TestSanitizeDisplayName(t *testing.T) { if got := SanitizeDisplayName(" Alice "); got != "Alice" { t.Errorf("SanitizeDisplayName trim: got %q", got) } if got := SanitizeDisplayName("Ali\x00ce"); got != "Alice" { t.Errorf("SanitizeDisplayName control: got %q", got) } if got := SanitizeDisplayName(""); got != "" { t.Errorf("SanitizeDisplayName empty: got %q", got) } } func TestParseCurrencyInput(t *testing.T) { cases := []struct { input string want string ok bool }{ {"$ USD", "$ USD", true}, {"T Toman", "T Toman", true}, {"IRR Rial", "IRR Rial", true}, {"€ EUR", "€ EUR", true}, {"", "", false}, {"$", "", false}, {" ", "", false}, {"$$$ TooLongCode", "", false}, } for _, c := range cases { got, errMsg := parseCurrencyInput(c.input) if c.ok && (got != c.want || errMsg != "") { t.Errorf("parseCurrencyInput(%q) = (%q, %q), want (%q, \"\")", c.input, got, errMsg, c.want) } if !c.ok && errMsg == "" { t.Errorf("parseCurrencyInput(%q) = (%q, \"\"), want error", c.input, got) } } } func TestComputeTrendFromAvgs(t *testing.T) { cases := []struct { recent, older float64 wantPts int wantDirSub string }{ {0, 0, 5, "stable"}, {100, 0, 15, "new"}, {0, 100, 0, "-100"}, {121, 100, 15, "+21"}, {100, 100, 5, "stable"}, {100, 130, 0, "-23"}, } for _, c := range cases { pts, dir := computeTrendFromAvgs(c.recent, c.older) if pts != c.wantPts { t.Errorf("computeTrendFromAvgs(%.0f, %.0f) pts = %d, want %d", c.recent, c.older, pts, c.wantPts) } if !strings.Contains(dir, c.wantDirSub) { t.Errorf("computeTrendFromAvgs(%.0f, %.0f) dir = %q, want containing %q", c.recent, c.older, dir, c.wantDirSub) } } } func TestComputeStreakFromDates_Empty(t *testing.T) { cur, longest := computeStreakFromDates(nil, "2026-06-25") if cur != 0 || longest != 0 { t.Errorf("empty dates: got (%d, %d), want (0, 0)", cur, longest) } } func TestComputeStreakFromDates_CurrentOnly(t *testing.T) { dates := []string{"2026-06-25", "2026-06-26"} cur, longest := computeStreakFromDates(dates, "2026-06-26") if cur != 2 { t.Errorf("current streak = %d, want 2", cur) } if longest != 2 { t.Errorf("longest streak = %d, want 2", longest) } } func TestComputeStreakFromDates_GapBreaksStreak(t *testing.T) { dates := []string{"2026-06-23", "2026-06-25", "2026-06-26"} cur, longest := computeStreakFromDates(dates, "2026-06-26") if cur != 2 { t.Errorf("current streak = %d, want 2 (today and yesterday)", cur) } if longest != 2 { t.Errorf("longest streak = %d, want 2", longest) } } func TestComputeStreakFromDates_NoEventsToday(t *testing.T) { dates := []string{"2026-06-24", "2026-06-25"} cur, longest := computeStreakFromDates(dates, "2026-06-26") if cur != 0 { t.Errorf("current streak = %d, want 0 (no events today)", cur) } if longest != 2 { t.Errorf("longest streak = %d, want 2", longest) } } func TestComputeStreakFromDates_LongStreak(t *testing.T) { dates := []string{} for d := 1; d <= 10; d++ { dates = append(dates, fmt.Sprintf("2026-06-%02d", d)) } cur, longest := computeStreakFromDates(dates, "2026-06-10") if cur != 10 { t.Errorf("current streak = %d, want 10", cur) } if longest != 10 { t.Errorf("longest streak = %d, want 10", longest) } } func TestComputeStreakFromDates_LongestNotCurrent(t *testing.T) { dates := []string{} for d := 1; d <= 5; d++ { dates = append(dates, fmt.Sprintf("2026-06-%02d", d)) } dates = append(dates, "2026-06-09", "2026-06-10") cur, longest := computeStreakFromDates(dates, "2026-06-10") if cur != 2 { t.Errorf("current streak = %d, want 2", cur) } if longest != 5 { t.Errorf("longest streak = %d, want 5", longest) } }