gunk-dev/the-valley

dashboard_reconcile_test.go

source672eff5ffaecmain's published tip history
2.7 KiB
1package cmd
2
3import (
4 "testing"
5 "time"
6
7 "the-valley/flow/internal/config"
8)
9
10// TestShouldScheduleReconcile verifies the heartbeat scheduling decision:
11// the slow reconcile sweep runs only in webhook-only mode (webhook on,
12// polling off) with a positive interval. It must NOT run when polling is
13// active (polling already re-fetches every 30s) or in plain poll mode.
14func TestShouldScheduleReconcile(t *testing.T) {
15 const iv = 5 * time.Minute
16 tests := []struct {
17 name string
18 useWebhook bool
19 pollEnabled bool
20 interval time.Duration
21 want bool
22 }{
23 {"webhook only", true, false, iv, true},
24 {"webhook plus polling (poll_fallback)", true, true, iv, false},
25 {"poll only (no webhook)", false, true, iv, false},
26 {"neither", false, false, iv, false},
27 {"webhook only but disabled interval", true, false, 0, false},
28 {"webhook only but negative interval", true, false, -1, false},
29 }
30 for _, tt := range tests {
31 t.Run(tt.name, func(t *testing.T) {
32 got := shouldScheduleReconcile(tt.useWebhook, tt.pollEnabled, tt.interval)
33 if got != tt.want {
34 t.Errorf("shouldScheduleReconcile(%v, %v, %v) = %v, want %v",
35 tt.useWebhook, tt.pollEnabled, tt.interval, got, tt.want)
36 }
37 })
38 }
39}
40
41// TestReconcileTickAfterCmd verifies the defensive guard: a non-positive
42// interval returns a nil command (heartbeat disabled), so the re-arm path can
43// never spin tea.Tick into an immediate-fire infinite loop. A positive interval
44// returns a real command.
45func TestReconcileTickAfterCmd(t *testing.T) {
46 if cmd := reconcileTickAfterCmd(0); cmd != nil {
47 t.Errorf("reconcileTickAfterCmd(0) = non-nil, want nil")
48 }
49 if cmd := reconcileTickAfterCmd(-1 * time.Second); cmd != nil {
50 t.Errorf("reconcileTickAfterCmd(-1s) = non-nil, want nil")
51 }
52 if cmd := reconcileTickAfterCmd(5 * time.Minute); cmd == nil {
53 t.Errorf("reconcileTickAfterCmd(5m) = nil, want non-nil")
54 }
55}
56
57// TestReconcileInterval verifies config resolution: zero/nil โ†’ default,
58// negative โ†’ disabled (0), positive โ†’ that many seconds.
59func TestReconcileInterval(t *testing.T) {
60 tests := []struct {
61 name string
62 cfg *config.WebhookConfig
63 want time.Duration
64 }{
65 {"nil config uses default", nil, defaultReconcileInterval},
66 {"zero uses default", &config.WebhookConfig{ReconcileIntervalSeconds: 0}, defaultReconcileInterval},
67 {"negative disables", &config.WebhookConfig{ReconcileIntervalSeconds: -1}, 0},
68 {"positive override", &config.WebhookConfig{ReconcileIntervalSeconds: 120}, 120 * time.Second},
69 }
70 for _, tt := range tests {
71 t.Run(tt.name, func(t *testing.T) {
72 if got := reconcileInterval(tt.cfg); got != tt.want {
73 t.Errorf("reconcileInterval(%+v) = %v, want %v", tt.cfg, got, tt.want)
74 }
75 })
76 }
77}