|
| 1 | +package engine |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/devblac/watch-tower/internal/config" |
| 9 | + "github.com/devblac/watch-tower/internal/sink" |
| 10 | + "github.com/devblac/watch-tower/internal/storage" |
| 11 | +) |
| 12 | + |
| 13 | +type fakeSink struct { |
| 14 | + count int |
| 15 | +} |
| 16 | + |
| 17 | +func (f *fakeSink) Send(ctx context.Context, payload sink.EventPayload) error { |
| 18 | + f.count++ |
| 19 | + return nil |
| 20 | +} |
| 21 | + |
| 22 | +// Simple integration: ensure predicates + dedupe + dry-run behave. |
| 23 | +func TestRunnerPredicatesAndDryRun(t *testing.T) { |
| 24 | + store := newTestStore(t) |
| 25 | + rule := config.Rule{ |
| 26 | + ID: "r1", |
| 27 | + Match: config.MatchSpec{Where: []string{"value > 10"}}, |
| 28 | + Sinks: []string{"s1"}, |
| 29 | + Dedupe: &config.Dedupe{ |
| 30 | + Key: "txhash", |
| 31 | + TTL: "1h", |
| 32 | + }, |
| 33 | + } |
| 34 | + cfg := &config.Config{Rules: []config.Rule{rule}} |
| 35 | + s := &fakeSink{} |
| 36 | + runner, err := NewRunner(store, cfg, nil, nil, map[string]sink.Sender{"s1": s}, true, 0, 0) |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("runner: %v", err) |
| 39 | + } |
| 40 | + runner.nowFunc = func() time.Time { return time.Now() } |
| 41 | + |
| 42 | + evs := []Event{{ |
| 43 | + RuleID: "r1", |
| 44 | + TxHash: "0x1", |
| 45 | + Args: map[string]any{"value": 20}, |
| 46 | + }} |
| 47 | + if err := runner.handleEvents(context.Background(), evs); err != nil { |
| 48 | + t.Fatalf("handle: %v", err) |
| 49 | + } |
| 50 | + if s.count != 0 { // dry-run should skip sends |
| 51 | + t.Fatalf("expected no sends in dry-run, got %d", s.count) |
| 52 | + } |
| 53 | + |
| 54 | + // now run non-dry and ensure dedupe prevents duplicate |
| 55 | + runner.dryRun = false |
| 56 | + if err := runner.handleEvents(context.Background(), evs); err != nil { |
| 57 | + t.Fatalf("handle: %v", err) |
| 58 | + } |
| 59 | + if s.count != 1 { |
| 60 | + t.Fatalf("expected 1 send, got %d", s.count) |
| 61 | + } |
| 62 | + if err := runner.handleEvents(context.Background(), evs); err != nil { |
| 63 | + t.Fatalf("handle dup: %v", err) |
| 64 | + } |
| 65 | + if s.count != 1 { |
| 66 | + t.Fatalf("expected dedupe to skip duplicate send") |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func newTestStore(t *testing.T) *storage.Store { |
| 71 | + t.Helper() |
| 72 | + store, err := storage.Open(t.TempDir() + "/db.sqlite") |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("open store: %v", err) |
| 75 | + } |
| 76 | + t.Cleanup(func() { _ = store.Close() }) |
| 77 | + return store |
| 78 | +} |
0 commit comments