-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpollhook_test.go
More file actions
293 lines (260 loc) · 6.28 KB
/
pollhook_test.go
File metadata and controls
293 lines (260 loc) · 6.28 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
)
func TestExtractItems_RootArray(t *testing.T) {
data := `[{"id": 1}, {"id": 2}, {"id": 3}]`
items, err := ExtractItems([]byte(data), ".")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(items) != 3 {
t.Fatalf("expected 3 items, got %d", len(items))
}
}
func TestExtractItems_NestedPath(t *testing.T) {
data := `{"data": {"incidents": [{"id": "a"}, {"id": "b"}]}}`
items, err := ExtractItems([]byte(data), "data.incidents")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(items) != 2 {
t.Fatalf("expected 2 items, got %d", len(items))
}
}
func TestExtractItems_SingleKey(t *testing.T) {
data := `{"results": [{"id": 10}]}`
items, err := ExtractItems([]byte(data), "results")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(items) != 1 {
t.Fatalf("expected 1 item, got %d", len(items))
}
}
func TestExtractItems_NotArray(t *testing.T) {
data := `{"results": "not an array"}`
_, err := ExtractItems([]byte(data), "results")
if err == nil {
t.Fatal("expected error for non-array")
}
}
func TestExtractItems_MissingKey(t *testing.T) {
data := `{"other": [1]}`
_, err := ExtractItems([]byte(data), "results")
if err == nil {
t.Fatal("expected error for missing key")
}
}
func TestExtractID_String(t *testing.T) {
item := json.RawMessage(`{"id": "abc-123"}`)
id, err := ExtractID(item, "id")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if id != "abc-123" {
t.Fatalf("expected abc-123, got %s", id)
}
}
func TestExtractID_Number(t *testing.T) {
item := json.RawMessage(`{"id": 42}`)
id, err := ExtractID(item, "id")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if id != "42" {
t.Fatalf("expected 42, got %s", id)
}
}
func TestExtractID_Nested(t *testing.T) {
item := json.RawMessage(`{"meta": {"uid": "xyz"}}`)
id, err := ExtractID(item, "meta.uid")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if id != "xyz" {
t.Fatalf("expected xyz, got %s", id)
}
}
func TestExtractID_Missing(t *testing.T) {
item := json.RawMessage(`{"name": "test"}`)
_, err := ExtractID(item, "id")
if err == nil {
t.Fatal("expected error for missing id")
}
}
func TestState_HasAddID(t *testing.T) {
dir := t.TempDir()
s := NewState(dir)
if s.HasID("src", "1") {
t.Fatal("expected false for unseen ID")
}
s.AddID("src", "1")
if !s.HasID("src", "1") {
t.Fatal("expected true after AddID")
}
// Adding same ID again should be a no-op
s.AddID("src", "1")
s.mu.Lock()
count := len(s.sources["src"].IDs)
s.mu.Unlock()
if count != 1 {
t.Fatalf("expected 1 ID after duplicate add, got %d", count)
}
}
func TestState_SlidingWindowCap(t *testing.T) {
dir := t.TempDir()
s := NewState(dir)
// Add maxIDsPerSource + 100 IDs
for i := 0; i < maxIDsPerSource+100; i++ {
s.AddID("src", fmt.Sprintf("id-%d", i))
}
s.mu.Lock()
count := len(s.sources["src"].IDs)
s.mu.Unlock()
if count != maxIDsPerSource {
t.Fatalf("expected %d IDs after cap, got %d", maxIDsPerSource, count)
}
}
func TestState_PersistAndLoad(t *testing.T) {
dir := t.TempDir()
// Save state
s1 := NewState(dir)
s1.AddID("src1", "a")
s1.AddID("src1", "b")
s1.AddID("src2", "x")
if err := s1.Save(); err != nil {
t.Fatalf("save error: %v", err)
}
// Verify file exists
if _, err := os.Stat(filepath.Join(dir, "state.json")); err != nil {
t.Fatalf("state file not created: %v", err)
}
// Load into new state
s2 := NewState(dir)
if err := s2.Load(); err != nil {
t.Fatalf("load error: %v", err)
}
if !s2.HasID("src1", "a") || !s2.HasID("src1", "b") {
t.Fatal("expected persisted IDs for src1")
}
if !s2.HasID("src2", "x") {
t.Fatal("expected persisted IDs for src2")
}
if s2.HasID("src1", "c") {
t.Fatal("unexpected ID found")
}
}
func TestState_LoadMissing(t *testing.T) {
dir := t.TempDir()
s := NewState(dir)
if err := s.Load(); err != nil {
t.Fatalf("load of missing file should not error: %v", err)
}
}
func TestConfig_Valid(t *testing.T) {
yaml := `
sources:
- name: test
command: echo '[]'
interval: 5m
items: "."
id: "id"
webhook:
url: https://example.com/hook
secret: mysecret
`
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
os.WriteFile(path, []byte(yaml), 0o644)
cfg, err := LoadConfig(path)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(cfg.Sources) != 1 {
t.Fatalf("expected 1 source, got %d", len(cfg.Sources))
}
if cfg.Sources[0].Name != "test" {
t.Fatalf("expected name 'test', got %q", cfg.Sources[0].Name)
}
if cfg.Sources[0].Webhook.Secret != "mysecret" {
t.Fatalf("expected secret 'mysecret', got %q", cfg.Sources[0].Webhook.Secret)
}
}
func TestConfig_MissingName(t *testing.T) {
yaml := `
sources:
- command: echo '[]'
interval: 5m
items: "."
id: "id"
webhook:
url: https://example.com/hook
`
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
os.WriteFile(path, []byte(yaml), 0o644)
_, err := LoadConfig(path)
if err == nil {
t.Fatal("expected validation error for missing name")
}
}
func TestConfig_DuplicateName(t *testing.T) {
yaml := `
sources:
- name: dupe
command: echo '[]'
interval: 5m
items: "."
id: "id"
webhook:
url: https://example.com/hook
- name: dupe
command: echo '[]'
interval: 5m
items: "."
id: "id"
webhook:
url: https://example.com/hook
`
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
os.WriteFile(path, []byte(yaml), 0o644)
_, err := LoadConfig(path)
if err == nil {
t.Fatal("expected validation error for duplicate name")
}
}
func TestConfig_IntervalTooShort(t *testing.T) {
yaml := `
sources:
- name: test
command: echo '[]'
interval: 500ms
items: "."
id: "id"
webhook:
url: https://example.com/hook
`
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
os.WriteFile(path, []byte(yaml), 0o644)
_, err := LoadConfig(path)
if err == nil {
t.Fatal("expected validation error for short interval")
}
}
func TestConfig_NoSources(t *testing.T) {
yaml := `sources: []`
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
os.WriteFile(path, []byte(yaml), 0o644)
_, err := LoadConfig(path)
if err == nil {
t.Fatal("expected validation error for empty sources")
}
}