summaryrefslogtreecommitdiff
path: root/internal/notify_state_test.go
blob: 4f4947156161cc87ed4accece5af3339692c1e14 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package internal

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

func TestNewNotifyState_FirstRun(t *testing.T) {
	// First run with no existing state file should return empty state
	tmpDir := t.TempDir()
	ns, err := newNotifyState(tmpDir)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	if ns.LastNotifyEpoch != 0 {
		t.Errorf("expected LastNotifyEpoch=0, got %d", ns.LastNotifyEpoch)
	}
	if len(ns.CheckStates) != 0 {
		t.Errorf("expected empty CheckStates, got %d entries", len(ns.CheckStates))
	}
}

func TestNotifyState_Persistence(t *testing.T) {
	// Test round-trip: save and load notification state
	tmpDir := t.TempDir()
	ns, err := newNotifyState(tmpDir)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	// Create a mock state and record notification
	mockState := state{checks: map[string]checkState{
		"Check A": {Status: nagiosOk},
		"Check B": {Status: nagiosCritical},
	}}

	if err := ns.recordNotification(mockState); err != nil {
		t.Fatalf("failed to record notification: %v", err)
	}

	// Load the state back
	ns2, err := newNotifyState(tmpDir)
	if err != nil {
		t.Fatalf("failed to reload state: %v", err)
	}

	if ns2.LastNotifyEpoch == 0 {
		t.Error("expected non-zero LastNotifyEpoch after reload")
	}
	if len(ns2.CheckStates) != 2 {
		t.Errorf("expected 2 check states, got %d", len(ns2.CheckStates))
	}
	if ns2.CheckStates["Check A"] != int(nagiosOk) {
		t.Errorf("expected Check A status=%d, got %d", nagiosOk, ns2.CheckStates["Check A"])
	}
	if ns2.CheckStates["Check B"] != int(nagiosCritical) {
		t.Errorf("expected Check B status=%d, got %d", nagiosCritical, ns2.CheckStates["Check B"])
	}
}

func TestIntervalElapsed_FirstRun(t *testing.T) {
	// First run (LastNotifyEpoch=0) should always return true
	ns := notifyState{LastNotifyEpoch: 0}
	if !ns.intervalElapsed(3600) {
		t.Error("expected intervalElapsed=true on first run")
	}
}

func TestIntervalElapsed_NotYet(t *testing.T) {
	// Notification sent 30 seconds ago, interval is 60 seconds
	ns := notifyState{LastNotifyEpoch: time.Now().Unix() - 30}
	if ns.intervalElapsed(60) {
		t.Error("expected intervalElapsed=false when only 30s of 60s elapsed")
	}
}

func TestIntervalElapsed_Elapsed(t *testing.T) {
	// Notification sent 120 seconds ago, interval is 60 seconds
	ns := notifyState{LastNotifyEpoch: time.Now().Unix() - 120}
	if !ns.intervalElapsed(60) {
		t.Error("expected intervalElapsed=true when 120s of 60s elapsed")
	}
}

func TestIntervalElapsed_ZeroInterval(t *testing.T) {
	// Interval of 0 should always return true (immediate notification mode)
	ns := notifyState{LastNotifyEpoch: time.Now().Unix()}
	if !ns.intervalElapsed(0) {
		t.Error("expected intervalElapsed=true when interval is 0")
	}
}

func TestHasChanges_NoChanges(t *testing.T) {
	ns := notifyState{
		CheckStates: map[string]int{
			"Check A": int(nagiosOk),
			"Check B": int(nagiosCritical),
		},
	}

	currentState := state{checks: map[string]checkState{
		"Check A": {Status: nagiosOk},
		"Check B": {Status: nagiosCritical},
	}}

	if ns.hasChanges(currentState) {
		t.Error("expected hasChanges=false when states are identical")
	}
}

func TestHasChanges_StatusChanged(t *testing.T) {
	ns := notifyState{
		CheckStates: map[string]int{
			"Check A": int(nagiosOk),
			"Check B": int(nagiosOk),
		},
	}

	currentState := state{checks: map[string]checkState{
		"Check A": {Status: nagiosOk},
		"Check B": {Status: nagiosCritical}, // Changed from OK to CRITICAL
	}}

	if !ns.hasChanges(currentState) {
		t.Error("expected hasChanges=true when check status changed")
	}
}

func TestHasChanges_NewCheck(t *testing.T) {
	ns := notifyState{
		CheckStates: map[string]int{
			"Check A": int(nagiosOk),
		},
	}

	currentState := state{checks: map[string]checkState{
		"Check A": {Status: nagiosOk},
		"Check B": {Status: nagiosCritical}, // New check
	}}

	if !ns.hasChanges(currentState) {
		t.Error("expected hasChanges=true when new check added")
	}
}

func TestHasChanges_RemovedCheck(t *testing.T) {
	ns := notifyState{
		CheckStates: map[string]int{
			"Check A": int(nagiosOk),
			"Check B": int(nagiosCritical),
		},
	}

	currentState := state{checks: map[string]checkState{
		"Check A": {Status: nagiosOk},
		// Check B removed
	}}

	if !ns.hasChanges(currentState) {
		t.Error("expected hasChanges=true when check removed")
	}
}

func TestHasChanges_EmptyPrevious(t *testing.T) {
	// First notification - no previous state
	ns := notifyState{
		CheckStates: map[string]int{},
	}

	currentState := state{checks: map[string]checkState{
		"Check A": {Status: nagiosOk},
	}}

	if !ns.hasChanges(currentState) {
		t.Error("expected hasChanges=true on first run with checks")
	}
}

func TestHasChanges_BothEmpty(t *testing.T) {
	ns := notifyState{
		CheckStates: map[string]int{},
	}

	currentState := state{checks: map[string]checkState{}}

	if ns.hasChanges(currentState) {
		t.Error("expected hasChanges=false when both states are empty")
	}
}

func TestRecordNotification(t *testing.T) {
	tmpDir := t.TempDir()
	ns, _ := newNotifyState(tmpDir)

	mockState := state{checks: map[string]checkState{
		"Check A": {Status: nagiosOk},
		"Check B": {Status: nagiosWarning},
		"Check C": {Status: nagiosCritical},
	}}

	beforeRecord := time.Now().Unix()
	if err := ns.recordNotification(mockState); err != nil {
		t.Fatalf("failed to record notification: %v", err)
	}
	afterRecord := time.Now().Unix()

	// Verify timestamp is within expected range
	if ns.LastNotifyEpoch < beforeRecord || ns.LastNotifyEpoch > afterRecord {
		t.Errorf("LastNotifyEpoch=%d not in range [%d, %d]", ns.LastNotifyEpoch, beforeRecord, afterRecord)
	}

	// Verify all check states were captured
	if len(ns.CheckStates) != 3 {
		t.Errorf("expected 3 check states, got %d", len(ns.CheckStates))
	}

	// Verify state file was created
	stateFile := filepath.Join(tmpDir, "notify_state.json")
	if _, err := os.Stat(stateFile); os.IsNotExist(err) {
		t.Error("expected state file to be created")
	}
}