clone

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

feat/app-decomposition
puzzletea / export / pdf / render_difficulty_test.go
render_difficulty_test.go 4.1K
1package pdfexport23import (4 "reflect"5 "testing"67 "codeberg.org/go-pdf/fpdf"8)910func newDifficultyTestPDF(t *testing.T) *fpdf.Fpdf {11 t.Helper()1213 pdf := fpdf.NewCustom(&fpdf.InitType{14 OrientationStr: "P",15 UnitStr: "mm",16 Size: fpdf.SizeType{17 Wd: halfLetterWidthMM,18 Ht: halfLetterHeightMM,19 },20 })21 if err := registerPDFFonts(pdf); err != nil {22 t.Fatalf("registerPDFFonts error: %v", err)23 }24 pdf.AddPage()25 return pdf26}2728func TestScoreToDifficultyStarStates(t *testing.T) {29 tests := []struct {30 name string31 score float6432 want []difficultyStarState33 }{34 {35 name: "zero score is empty",36 score: 0.0,37 want: []difficultyStarState{difficultyStarEmpty, difficultyStarEmpty, difficultyStarEmpty, difficultyStarEmpty, difficultyStarEmpty},38 },39 {40 name: "one tenth is half then empty",41 score: 0.1,42 want: []difficultyStarState{difficultyStarHalf, difficultyStarEmpty, difficultyStarEmpty, difficultyStarEmpty, difficultyStarEmpty},43 },44 {45 name: "three tenths is full half then empty",46 score: 0.3,47 want: []difficultyStarState{difficultyStarFull, difficultyStarHalf, difficultyStarEmpty, difficultyStarEmpty, difficultyStarEmpty},48 },49 {50 name: "five tenths is two and a half stars",51 score: 0.5,52 want: []difficultyStarState{difficultyStarFull, difficultyStarFull, difficultyStarHalf, difficultyStarEmpty, difficultyStarEmpty},53 },54 {55 name: "eight tenths is four full stars",56 score: 0.8,57 want: []difficultyStarState{difficultyStarFull, difficultyStarFull, difficultyStarFull, difficultyStarFull, difficultyStarEmpty},58 },59 {60 name: "one is five full stars",61 score: 1.0,62 want: []difficultyStarState{difficultyStarFull, difficultyStarFull, difficultyStarFull, difficultyStarFull, difficultyStarFull},63 },64 {65 name: "negative clamps low",66 score: -1.0,67 want: []difficultyStarState{difficultyStarEmpty, difficultyStarEmpty, difficultyStarEmpty, difficultyStarEmpty, difficultyStarEmpty},68 },69 {70 name: "above one clamps high",71 score: 2.0,72 want: []difficultyStarState{difficultyStarFull, difficultyStarFull, difficultyStarFull, difficultyStarFull, difficultyStarFull},73 },74 }7576 for _, tt := range tests {77 t.Run(tt.name, func(t *testing.T) {78 got := scoreToDifficultyStarStates(tt.score)79 if !reflect.DeepEqual(got, tt.want) {80 t.Fatalf("states = %v, want %v", got, tt.want)81 }82 })83 }84}8586func TestPuzzleDifficultySubtitleLayoutWidthNormalMode(t *testing.T) {87 pdf := newDifficultyTestPDF(t)88 layout := puzzleDifficultySubtitleLayout(pdf, Puzzle{89 ModeSelection: "Expert",90 DifficultyScore: 0.5,91 })92 if layout.labelText != "Mode: Expert | Difficulty:" {93 t.Fatalf("label = %q", layout.labelText)94 }95 if layout.totalWidth <= 0 {96 t.Fatalf("total width = %.3f, want positive", layout.totalWidth)97 }98 if layout.totalWidth > halfLetterWidthMM {99 t.Fatalf("total width = %.3f, want <= %.3f", layout.totalWidth, halfLetterWidthMM)100 }101}102103func TestPuzzleDifficultySubtitleLayoutWidthMixedMode(t *testing.T) {104 pdf := newDifficultyTestPDF(t)105 layout := puzzleDifficultySubtitleLayout(pdf, Puzzle{106 ModeSelection: "Mixed Modes",107 DifficultyScore: 0.3,108 })109 if layout.labelText != "Difficulty:" {110 t.Fatalf("label = %q", layout.labelText)111 }112 if layout.totalWidth <= 0 {113 t.Fatalf("total width = %.3f, want positive", layout.totalWidth)114 }115 if layout.totalWidth > halfLetterWidthMM {116 t.Fatalf("total width = %.3f, want <= %.3f", layout.totalWidth, halfLetterWidthMM)117 }118}119120func TestPuzzleDifficultySubtitleLayoutStatesSequence(t *testing.T) {121 pdf := newDifficultyTestPDF(t)122 layout := puzzleDifficultySubtitleLayout(pdf, Puzzle{123 ModeSelection: "Tricky",124 DifficultyScore: 0.3,125 })126 want := []difficultyStarState{127 difficultyStarFull,128 difficultyStarHalf,129 difficultyStarEmpty,130 difficultyStarEmpty,131 difficultyStarEmpty,132 }133 if !reflect.DeepEqual(layout.starStates, want) {134 t.Fatalf("states = %v, want %v", layout.starStates, want)135 }136}137138func TestDifficultyStarPointsReturnsTenVertices(t *testing.T) {139 points := difficultyStarPoints(10, 20, difficultyStarSizeMM)140 if len(points) != 10 {141 t.Fatalf("vertex count = %d, want 10", len(points))142 }143}