app/config_test.go

v1.5.1
skunky-art/app/config_test.go history · blame · raw

82 lines · 2267 bytes

 1package app
 2
 3import (
 4	"testing"
 5	"time"
 6)
 7
 8func TestParseLifetimeUnits(t *testing.T) {
 9	cases := map[string]time.Duration{
10		"5i":  5 * time.Minute,
11		"2h":  2 * time.Hour,
12		"3d":  72 * time.Hour,
13		"1w":  7 * 24 * time.Hour,
14		"1m":  30 * 24 * time.Hour,
15		"1y":  360 * 24 * time.Hour,
16		"12i": 12 * time.Minute,
17	}
18	for in, want := range cases {
19		got, err := parseLifetime(in)
20		if err != nil || got != want {
21			t.Errorf("parseLifetime(%q) = %v, %v; want %v", in, got, err, want)
22		}
23	}
24}
25
26func TestParseLifetimeRejectsBadInput(t *testing.T) {
27	for _, in := range []string{"", "5", "5x", "h"} {
28		if _, err := parseLifetime(in); err == nil {
29			t.Errorf("parseLifetime(%q) accepted, want an error", in)
30		}
31	}
32}
33
34func TestAPICacheDefaults(t *testing.T) {
35	if !CFG.APICache.Enabled || CFG.APICache.MaxSize != 64 || CFG.APICache.TTL != "5i" {
36		t.Errorf("defaults are %+v, want enabled, 64 MB, 5i", CFG.APICache)
37	}
38}
39
40func TestMediaCacheDefaults(t *testing.T) {
41	c := CFG.Cache
42	if !c.Enabled || c.Lifetime != "1w" || c.MaxSize != 200 || c.MemCache {
43		t.Errorf("defaults are %+v, want enabled, 1w, 200 MB, memcache off", c)
44	}
45}
46
47// withScratchConfig points CFG at a config path under a temporary directory,
48// with the media cache off so no rotation goroutine starts, and restores the
49// whole configuration afterwards.
50func withScratchConfig(t *testing.T, path string, explicit bool) {
51	t.Helper()
52	cfg, limiter, exp := CFG, daLimiter, cfgExplicit
53	CFG.cfg = path
54	CFG.Cache.Enabled = false
55	cfgExplicit = explicit
56	t.Cleanup(func() { CFG, daLimiter, cfgExplicit = cfg, limiter, exp })
57}
58
59func TestExecuteConfigRunsWithoutTheDefaultFile(t *testing.T) {
60	withScratchConfig(t, t.TempDir()+"/config.json", false)
61	msgs := captureExit(t)
62
63	ExecuteConfig()
64
65	if len(*msgs) != 0 {
66		t.Errorf("exit called with %v; want a start on the built-in defaults", *msgs)
67	}
68	if CFG.Listen != "127.0.0.1:3003" || CFG.Nsfw {
69		t.Errorf("defaults not in effect: listen %q nsfw %v", CFG.Listen, CFG.Nsfw)
70	}
71}
72
73func TestExecuteConfigExitsOnAMissingExplicitFile(t *testing.T) {
74	withScratchConfig(t, t.TempDir()+"/named.json", true)
75	msgs := captureExit(t)
76
77	ExecuteConfig()
78
79	if len(*msgs) == 0 {
80		t.Error("a missing file named with -c started the instance, want an exit")
81	}
82}