clone

fifteen puzzle games for the terminal, with daily puzzles, weekly ladders and xp, built on bubble tea

feat/app-decomposition
puzzletea / stats / stats_test.go
stats_test.go 2.3K
1package stats23import (4 "testing"5 "time"67 "github.com/FelineStateMachine/puzzletea/puzzle"8 "github.com/FelineStateMachine/puzzletea/store"9)1011func TestWeightsFromDefinitions(t *testing.T) {12 weights := WeightsFromDefinitions([]puzzle.Definition{{13 Name: "Sudoku",14 Modes: []puzzle.ModeDef{15 {Title: "Easy"},16 {Title: "Medium"},17 {Title: "Hard"},18 },19 }})20 if len(weights) != 3 {21 t.Fatalf("len(weights) = %d, want 3", len(weights))22 }23}2425func TestLevelFromXPAndXPForLevel(t *testing.T) {26 if got := LevelFromXP(0); got != 0 {27 t.Fatalf("LevelFromXP(0) = %d, want 0", got)28 }29 if got := XPForLevel(3); got != 29 {30 t.Fatalf("XPForLevel(3) = %d, want 29", got)31 }32}3334func TestComputeDailyStreak(t *testing.T) {35 now := time.Date(2026, time.February, 16, 15, 30, 0, 0, time.Local)36 dates := []time.Time{37 time.Date(2026, time.February, 16, 0, 0, 0, 0, time.Local),38 time.Date(2026, time.February, 15, 0, 0, 0, 0, time.Local),39 time.Date(2026, time.February, 14, 0, 0, 0, 0, time.Local),40 }41 if got := ComputeDailyStreak(dates, now); got != 3 {42 t.Fatalf("ComputeDailyStreak() = %d, want 3", got)43 }44}4546func TestComputeCategoryXP(t *testing.T) {47 weights := Weights{48 {"Sudoku", "Easy"}: 2,49 {"Sudoku", "Hard"}: 8,50 }51 modeStats := []store.ModeStat{52 {GameType: "Sudoku", Mode: "Easy", Victories: 5, DailyVictories: 2},53 {GameType: "Sudoku", Mode: "Hard", Victories: 2},54 }55 if got := ComputeCategoryXP(weights, "Sudoku", modeStats); got != 30 {56 t.Fatalf("ComputeCategoryXP() = %d, want 30", got)57 }58}5960func TestBuildCardsAndProfile(t *testing.T) {61 weights := Weights{62 {"Sudoku", "Easy"}: 2,63 {"Sudoku", "Hard"}: 8,64 }65 catStats := []store.CategoryStat{{66 GameType: "Sudoku",67 TotalAttempts: 10,68 TotalVictories: 7,69 TimesDaily: 3,70 PreferredMode: "Easy",71 }}72 modeStats := []store.ModeStat{73 {GameType: "Sudoku", Mode: "Easy", Victories: 5, DailyVictories: 2},74 {GameType: "Sudoku", Mode: "Hard", Victories: 2},75 }76 cards := BuildCards(weights, catStats, modeStats)77 if len(cards) != 1 || cards[0].CurrentXP != 30 {78 t.Fatalf("BuildCards() = %#v", cards)79 }80 profile := BuildProfileBanner(catStats, modeStats, weights, nil, false, 4, 17)81 if profile.WeekliesCompleted != 4 || profile.ThisWeekHighestIndex != 17 {82 t.Fatalf("BuildProfileBanner() = %#v", profile)83 }84}