Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions pkg/stats/spec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
//go:build !integration

package stats

import (
"math"
"testing"

"github.com/stretchr/testify/assert"
)

// TestSpec_PublicAPI_EmptyBehavior validates that an empty StatVar returns
// documented zero values as described in the package README.md.
//
// Specification:
// - Min: "Returns the minimum observed value (or 0 if empty)"
// - Max: "Returns the maximum observed value (or 0 if empty)"
// - Mean: "Returns the arithmetic mean (or 0 if empty)"
func TestSpec_PublicAPI_EmptyBehavior(t *testing.T) {
var sv StatVar

assert.Equal(t, 0, sv.Count(), "Count should return 0 for an empty StatVar")
assert.Equal(t, 0.0, sv.Sum(), "Sum should return 0 for an empty StatVar")
assert.Equal(t, 0.0, sv.Min(), "Min should return 0 when no observations have been added")
assert.Equal(t, 0.0, sv.Max(), "Max should return 0 when no observations have been added")
assert.Equal(t, 0.0, sv.Mean(), "Mean should return 0 when no observations have been added")
assert.Equal(t, 0.0, sv.Median(), "Median should return 0 when no observations have been added")
}

// TestSpec_PublicAPI_Add validates that each Add call records exactly one
// observation as described in the package README.md.
//
// Specification: "Add: Adds one observation"
func TestSpec_PublicAPI_Add(t *testing.T) {
var sv StatVar

sv.Add(10.0)
assert.Equal(t, 1, sv.Count(), "Count should be 1 after one Add call")

sv.Add(20.0)
assert.Equal(t, 2, sv.Count(), "Count should be 2 after two Add calls")

sv.Add(30.0)
assert.Equal(t, 3, sv.Count(), "Count should be 3 after three Add calls")
}

// TestSpec_PublicAPI_Sum validates that Sum returns the arithmetic sum of all
// observations as described in the package README.md.
//
// Specification: "Sum: Returns the arithmetic sum"
func TestSpec_PublicAPI_Sum(t *testing.T) {
var sv StatVar
sv.Add(1.0)
sv.Add(2.0)
sv.Add(3.0)

assert.Equal(t, 6.0, sv.Sum(),
"Sum should equal the arithmetic sum of all added observations")
}

// TestSpec_PublicAPI_MinMax validates that Min and Max track the extreme
// observed values as described in the package README.md.
//
// Specification:
// - Min: "Returns the minimum observed value"
// - Max: "Returns the maximum observed value"
func TestSpec_PublicAPI_MinMax(t *testing.T) {
var sv StatVar
sv.Add(5.0)
sv.Add(1.0)
sv.Add(9.0)
sv.Add(3.0)

assert.Equal(t, 1.0, sv.Min(),
"Min should return the smallest value ever passed to Add")
assert.Equal(t, 9.0, sv.Max(),
"Max should return the largest value ever passed to Add")
}

// TestSpec_PublicAPI_Mean validates that Mean returns the arithmetic mean
// as described in the package README.md.
//
// Specification: "Mean: Returns the arithmetic mean"
func TestSpec_PublicAPI_Mean(t *testing.T) {
var sv StatVar
sv.Add(2.0)
sv.Add(4.0)
sv.Add(6.0)

assert.InDelta(t, 4.0, sv.Mean(), 1e-9,
"Mean should equal (sum of observations) / count")
}

// TestSpec_PublicAPI_Variance_PopulationFormula validates that Variance uses
// the population formula with N as the denominator as described in the README.md.
//
// Specification: "Variance: Returns population variance (N denominator)"
//
// Example: values [2,4,4,4,5,5,7,9] (N=8, mean=5) → population variance = 32/8 = 4
func TestSpec_PublicAPI_Variance_PopulationFormula(t *testing.T) {
var sv StatVar
for _, v := range []float64{2, 4, 4, 4, 5, 5, 7, 9} {
sv.Add(v)
}

assert.InDelta(t, 4.0, sv.Variance(), 1e-9,
"Variance should use N (population) denominator: sum_sq_dev / N")
}

// TestSpec_PublicAPI_SampleVariance_SampleFormula validates that SampleVariance
// uses the sample formula with N-1 as the denominator as described in the README.md.
//
// Specification: "SampleVariance: Returns sample variance (N-1 denominator)"
//
// Example: values [2,4,4,4,5,5,7,9] (N=8, mean=5, sum_sq_dev=32) → sample variance = 32/7
func TestSpec_PublicAPI_SampleVariance_SampleFormula(t *testing.T) {
var sv StatVar
for _, v := range []float64{2, 4, 4, 4, 5, 5, 7, 9} {
sv.Add(v)
}

expected := 32.0 / 7.0
assert.InDelta(t, expected, sv.SampleVariance(), 1e-9,
"SampleVariance should use N-1 (sample) denominator: sum_sq_dev / (N-1)")
}

// TestSpec_PublicAPI_StdDev validates that StdDev and SampleStdDev equal the
// square root of their respective variances as described in the README.md.
//
// Specification:
// - StdDev: "Returns population standard deviation"
// - SampleStdDev: "Returns sample standard deviation"
func TestSpec_PublicAPI_StdDev(t *testing.T) {
var sv StatVar
for _, v := range []float64{2, 4, 4, 4, 5, 5, 7, 9} {
sv.Add(v)
}

assert.InDelta(t, math.Sqrt(sv.Variance()), sv.StdDev(), 1e-9,
"StdDev should equal sqrt(Variance)")
assert.InDelta(t, math.Sqrt(sv.SampleVariance()), sv.SampleStdDev(), 1e-9,
"SampleStdDev should equal sqrt(SampleVariance)")
}

// TestSpec_PublicAPI_Median_OddCount validates that Median returns the middle
// value for an odd-count set as described in the README.md.
//
// Specification: "Median: Returns the exact median (middle value...)"
func TestSpec_PublicAPI_Median_OddCount(t *testing.T) {
var sv StatVar
sv.Add(3.0)
sv.Add(1.0)
sv.Add(2.0)

assert.Equal(t, 2.0, sv.Median(),
"Median with odd count should return the middle value in sorted order")
}

// TestSpec_PublicAPI_Median_EvenCount validates that Median returns the midpoint
// of the two middle values for an even-count set as described in the README.md.
//
// Specification: "Median: Returns the exact median (...or midpoint of two middle values)"
func TestSpec_PublicAPI_Median_EvenCount(t *testing.T) {
var sv StatVar
sv.Add(4.0)
sv.Add(1.0)
sv.Add(3.0)
sv.Add(2.0)

assert.Equal(t, 2.5, sv.Median(),
"Median with even count should return the average of the two middle sorted values")
}
186 changes: 186 additions & 0 deletions pkg/styles/spec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
//go:build !integration && !js && !wasm

package styles

import (
"testing"

"github.com/stretchr/testify/assert"
)

// TestSpec_Constants_DocumentedSemanticColorHexValues validates that the four
// primary semantic color variables have the exact hex values stated in the
// package README.md specification table.
//
// Specification (README.md adaptive color table):
//
// ColorError: Light=#D73737, Dark=#FF5555
// ColorWarning: Light=#E67E22, Dark=#FFB86C
// ColorSuccess: Light=#27AE60, Dark=#50FA7B
// ColorInfo: Light=#2980B9, Dark=#8BE9FD
func TestSpec_Constants_DocumentedSemanticColorHexValues(t *testing.T) {
tests := []struct {
colorName string
lightHex string
darkHex string
specLight string
specDark string
}{
{"ColorError", hexColorErrorLight, hexColorErrorDark, "#D73737", "#FF5555"},
{"ColorWarning", hexColorWarningLight, hexColorWarningDark, "#E67E22", "#FFB86C"},
{"ColorSuccess", hexColorSuccessLight, hexColorSuccessDark, "#27AE60", "#50FA7B"},
{"ColorInfo", hexColorInfoLight, hexColorInfoDark, "#2980B9", "#8BE9FD"},
}

for _, tt := range tests {
t.Run(tt.colorName, func(t *testing.T) {
assert.Equal(t, tt.specLight, tt.lightHex,
"%s Light hex should match README.md specification", tt.colorName)
assert.Equal(t, tt.specDark, tt.darkHex,
"%s Dark hex should match README.md specification", tt.colorName)
})
}
}

// TestSpec_Constants_AllElevenAdaptiveColors validates that all 11 documented
// adaptive color variables are defined with both Light and Dark hex values as
// listed in the package README.md adaptive color table.
//
// Specification: "These variables provide compat.AdaptiveColor values that
// auto-select the correct shade at render time."
func TestSpec_Constants_AllElevenAdaptiveColors(t *testing.T) {
type colorDef struct {
name string
light string
dark string
}

colors := []colorDef{
{"ColorError", hexColorErrorLight, hexColorErrorDark},
{"ColorWarning", hexColorWarningLight, hexColorWarningDark},
{"ColorSuccess", hexColorSuccessLight, hexColorSuccessDark},
{"ColorInfo", hexColorInfoLight, hexColorInfoDark},
{"ColorPurple", hexColorPurpleLight, hexColorPurpleDark},
{"ColorYellow", hexColorYellowLight, hexColorYellowDark},
{"ColorComment", hexColorCommentLight, hexColorCommentDark},
{"ColorForeground", hexColorForegroundLight, hexColorForegroundDark},
{"ColorBackground", hexColorBackgroundLight, hexColorBackgroundDark},
{"ColorBorder", hexColorBorderLight, hexColorBorderDark},
{"ColorTableAltRow", hexColorTableAltRowLight, hexColorTableAltRowDark},
}

assert.Len(t, colors, 11,
"README.md documents exactly 11 adaptive color variables")

for _, c := range colors {
t.Run(c.name, func(t *testing.T) {
assert.NotEmpty(t, c.light,
"%s must have a non-empty Light hex value", c.name)
assert.NotEmpty(t, c.dark,
"%s must have a non-empty Dark hex value", c.name)
assert.NotEqual(t, c.light, c.dark,
"%s Light and Dark values must differ — adaptive color requires distinct shades", c.name)
})
}
}

// TestSpec_Constants_RoundedBorderCornerCharacters validates that RoundedBorder
// uses the documented "╭╮╰╯" rounded corner characters as described in the README.md.
//
// Specification: "RoundedBorder: ╭╮╰╯ rounded corners — Tables, boxes, panels (primary)"
func TestSpec_Constants_RoundedBorderCornerCharacters(t *testing.T) {
assert.Equal(t, "╭", RoundedBorder.TopLeft,
"RoundedBorder.TopLeft should be the rounded corner character ╭")
assert.Equal(t, "╮", RoundedBorder.TopRight,
"RoundedBorder.TopRight should be the rounded corner character ╮")
assert.Equal(t, "╰", RoundedBorder.BottomLeft,
"RoundedBorder.BottomLeft should be the rounded corner character ╰")
assert.Equal(t, "╯", RoundedBorder.BottomRight,
"RoundedBorder.BottomRight should be the rounded corner character ╯")
}

// TestSpec_Constants_ThreeBordersAreDistinct validates that RoundedBorder,
// NormalBorder, and ThickBorder produce visually distinct output as documented
// in the README.md border definitions table.
//
// Specification:
//
// RoundedBorder: "╭╮╰╯ rounded corners"
// NormalBorder: "Straight lines"
// ThickBorder: "Thick lines"
func TestSpec_Constants_ThreeBordersAreDistinct(t *testing.T) {
t.Run("RoundedBorder differs from NormalBorder", func(t *testing.T) {
assert.NotEqual(t, RoundedBorder.TopLeft, NormalBorder.TopLeft,
"RoundedBorder and NormalBorder TopLeft characters must differ")
})

t.Run("RoundedBorder differs from ThickBorder", func(t *testing.T) {
assert.NotEqual(t, RoundedBorder.Top, ThickBorder.Top,
"RoundedBorder and ThickBorder Top characters must differ")
})

t.Run("NormalBorder differs from ThickBorder", func(t *testing.T) {
assert.NotEqual(t, NormalBorder.Top, ThickBorder.Top,
"NormalBorder and ThickBorder Top characters must differ")
})
}

// TestSpec_Types_StylesAreRenderableValues validates that all documented
// pre-configured lipgloss.Style variables are ready-to-use directly (not
// functions) as stated in the package README.md.
//
// Specification: "These lipgloss.Style values are ready to use directly"
// Design note: "All * styles export pre-configured lipgloss.Style values
// (not functions), so they can be used with method chaining"
func TestSpec_Types_StylesAreRenderableValues(t *testing.T) {
styleTests := []struct {
name string
render func() string
}{
{"Error", func() string { return Error.Render("x") }},
{"Warning", func() string { return Warning.Render("x") }},
{"Success", func() string { return Success.Render("x") }},
{"Info", func() string { return Info.Render("x") }},
{"FilePath", func() string { return FilePath.Render("x") }},
{"LineNumber", func() string { return LineNumber.Render("x") }},
{"ContextLine", func() string { return ContextLine.Render("x") }},
{"Highlight", func() string { return Highlight.Render("x") }},
{"Location", func() string { return Location.Render("x") }},
{"Command", func() string { return Command.Render("x") }},
{"Progress", func() string { return Progress.Render("x") }},
{"Prompt", func() string { return Prompt.Render("x") }},
{"Count", func() string { return Count.Render("x") }},
{"Verbose", func() string { return Verbose.Render("x") }},
{"ListHeader", func() string { return ListHeader.Render("x") }},
{"ListItem", func() string { return ListItem.Render("x") }},
{"TableHeader", func() string { return TableHeader.Render("x") }},
{"TableCell", func() string { return TableCell.Render("x") }},
{"TableTotal", func() string { return TableTotal.Render("x") }},
{"TableTitle", func() string { return TableTitle.Render("x") }},
{"TableBorder", func() string { return TableBorder.Render("x") }},
{"ServerName", func() string { return ServerName.Render("x") }},
{"ServerType", func() string { return ServerType.Render("x") }},
{"ErrorBox", func() string { return ErrorBox.Render("x") }},
{"Header", func() string { return Header.Render("x") }},
{"TreeEnumerator", func() string { return TreeEnumerator.Render("x") }},
{"TreeNode", func() string { return TreeNode.Render("x") }},
}

for _, s := range styleTests {
t.Run(s.name, func(t *testing.T) {
rendered := s.render()
assert.NotEmpty(t, rendered,
"%s.Render(\"x\") should return non-empty string — styles are values, not nil", s.name)
})
}
}

// TestSpec_Types_HuhTheme validates that HuhTheme is exported and non-nil
// as described in the package README.md.
//
// Specification: "The package also exports HuhTheme — a huh.ThemeFunc that
// applies the same Dracula-inspired color palette to interactive forms."
func TestSpec_Types_HuhTheme(t *testing.T) {
assert.NotNil(t, HuhTheme,
"HuhTheme should be exported and non-nil as documented in the README.md")
}
Loading