package config_test import ( "strings" "testing" "time" "git.wntrmute.dev/kyle/crossbar/internal/config" ) const v1Base = ` listen = "127.0.0.1:1" [hosts.a] base_url = "http://a:1" models = { "m" = { } } [routes.r] hosts = ["a"] ` func TestV1Defaults(t *testing.T) { c, err := config.Parse(strings.NewReader(v1Base)) if err != nil { t.Fatal(err) } if c.DB != "crossbar.db" { t.Errorf("DB default = %q", c.DB) } if c.LeaseIdle.Duration != 30*time.Minute { t.Errorf("LeaseIdle default = %v", c.LeaseIdle.Duration) } if c.Retention.Duration != 180*24*time.Hour { t.Errorf("Retention default = %v", c.Retention.Duration) } } func TestV1Values(t *testing.T) { c, err := config.Parse(strings.NewReader(` db = "/var/lib/crossbar/crossbar.db" lease_idle = "45m" retention = "30d" ` + v1Base)) if err != nil { t.Fatal(err) } if c.DB != "/var/lib/crossbar/crossbar.db" || c.LeaseIdle.Duration != 45*time.Minute || c.Retention.Duration != 30*24*time.Hour { t.Errorf("got db %q idle %v retention %v", c.DB, c.LeaseIdle.Duration, c.Retention.Duration) } } func TestDurationAcceptsDays(t *testing.T) { var d config.Duration for _, tc := range []struct { in string want time.Duration }{ {"1d", 24 * time.Hour}, {"7d", 7 * 24 * time.Hour}, {"90m", 90 * time.Minute}, {"2h30m", 150 * time.Minute}, } { if err := d.UnmarshalText([]byte(tc.in)); err != nil || d.Duration != tc.want { t.Errorf("UnmarshalText(%q) = %v %v, want %v", tc.in, d.Duration, err, tc.want) } } for _, bad := range []string{"1.5d", "d", "3 days", "1d2h"} { if err := d.UnmarshalText([]byte(bad)); err == nil { t.Errorf("UnmarshalText(%q) must fail", bad) } } } func TestV1Validation(t *testing.T) { for _, tc := range []struct{ name, text, field string }{ {"empty db", "db = \"\"\n" + v1Base, "db"}, {"lease_idle too short", "lease_idle = \"10s\"\n" + v1Base, "lease_idle"}, {"retention too short", "retention = \"12h\"\n" + v1Base, "retention"}, } { _, err := config.Parse(strings.NewReader(tc.text)) e, ok := config.IsError(err) if !ok || e.Field != tc.field { t.Errorf("%s: %v, want *Error on %s", tc.name, err, tc.field) } } }