mirror of
https://github.com/marcusDenslow/terminalShop.git
synced 2026-10-25 18:08:45 +01:00
79 lines
2.2 KiB
Go
79 lines
2.2 KiB
Go
package config
|
|
|
|
import (
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// TestMain silences the package's slog warnings so the negative/garbage
|
|
// env-var cases plus the sub-10 valid-minimum case don't spam test output.
|
|
// The actual fall-back behavior is still asserted via the case tables.
|
|
func TestMain(m *testing.M) {
|
|
prev := slog.Default()
|
|
slog.SetDefault(slog.New(slog.NewTextHandler(io.Discard, nil)))
|
|
code := m.Run()
|
|
slog.SetDefault(prev)
|
|
os.Exit(code)
|
|
}
|
|
|
|
func TestLoadMaxOrderCents(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
env string // "" means unset
|
|
want int
|
|
}{
|
|
{"empty falls back to default", "", 50000},
|
|
{"explicit zero disables", "0", 0},
|
|
{"valid override", "30000", 30000},
|
|
{"negative falls back to default", "-1", 50000},
|
|
{"garbage falls back to default", "two hundred", 50000},
|
|
{"empty after trim falls back to default", "", 50000},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if tc.env == "" {
|
|
t.Setenv("MAX_ORDER_CENTS", "")
|
|
} else {
|
|
t.Setenv("MAX_ORDER_CENTS", tc.env)
|
|
}
|
|
got := loadMaxOrderCents()
|
|
if got != tc.want {
|
|
t.Errorf("loadMaxOrderCents() = %d, want %d", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestLoadAbandoned3DSThresholdMinutes covers the env-parsing surface for
|
|
// the sweep threshold. Zero and negative fall back to the default (unlike
|
|
// MaxOrderCents where zero is a deliberate off-switch) because a zero
|
|
// threshold would sweep every requires_action row on the first tick and
|
|
// race the customer's in-progress challenge.
|
|
func TestLoadAbandoned3DSThresholdMinutes(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
env string // "" means unset
|
|
want int
|
|
}{
|
|
{"empty falls back to default", "", 30},
|
|
{"valid override", "45", 45},
|
|
{"zero falls back to default", "0", 30},
|
|
{"negative falls back to default", "-5", 30},
|
|
{"garbage falls back to default", "half an hour", 30},
|
|
{"valid minimum (warns)", "1", 1},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Setenv("ABANDONED_3DS_THRESHOLD_MINUTES", tc.env)
|
|
got := loadAbandoned3DSThresholdMinutes()
|
|
if got != tc.want {
|
|
t.Errorf("loadAbandoned3DSThresholdMinutes() = %d, want %d", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|