aboutsummaryrefslogtreecommitdiffstats
path: root/internal/notify/config_test.go
blob: 46032ee0e8a964460e7d28eb4c6da5aad179209e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package notify

import (
	"os"
	"path/filepath"
	"testing"
)

func writeConfig(t *testing.T, body string) string {
	t.Helper()
	p := filepath.Join(t.TempDir(), "config.json")
	if err := os.WriteFile(p, []byte(body), 0o600); err != nil {
		t.Fatal(err)
	}
	return p
}

func TestLoadConfigMissingFileIsDefaults(t *testing.T) {
	got := LoadConfig(filepath.Join(t.TempDir(), "absent.json"))
	if got != DefaultConfig() {
		t.Errorf("got %+v, want defaults", got)
	}
}

func TestLoadConfigMalformedIsDefaults(t *testing.T) {
	got := LoadConfig(writeConfig(t, `{"liveCap": 5,`))
	if got != DefaultConfig() {
		t.Errorf("got %+v, want defaults", got)
	}
}

func TestLoadConfigPartialMergesOverDefaults(t *testing.T) {
	got := LoadConfig(writeConfig(t, `{"timeouts": {"low": 3}, "historyCap": 50, "muted": ["x"]}`))
	want := DefaultConfig()
	want.Timeouts.Low = 3
	want.HistoryCap = 50
	if got != want {
		t.Errorf("got %+v, want %+v", got, want)
	}
}

func TestLoadConfigWrongTypeKeepsThatDefault(t *testing.T) {
	got := LoadConfig(writeConfig(t, `{"liveCap": "many", "historyCap": 7}`))
	if got.LiveCap != 20 || got.HistoryCap != 7 {
		t.Errorf("got live %d history %d, want 20 and 7", got.LiveCap, got.HistoryCap)
	}
}

func TestLoadConfigClamps(t *testing.T) {
	got := LoadConfig(writeConfig(t, `{"liveCap": 0, "historyCap": 1000, "timeouts": {"normal": -4}}`))
	if got.LiveCap != 1 || got.HistoryCap != 100 || got.Timeouts.Normal != 10 {
		t.Errorf("got %+v", got)
	}
}