Intelligence, Advanced Charting & Trading Platform
test(db): assert consent, version CAS, atomic enqueue and no duplicate alerts in PostgreSQL
davoodmehraban89 committed
Oct 22, 2026 at 23:31 UTC
31e2b6b2a1e6b7ea6972ca8a52f6e8fa35c96979
1 file changed
+145
db/tests/alert_runtime_invariants.sql
new
+145
@@ -0,0 +1,145 @@
1
+-- Disposable CI PostgreSQL only: no production Neon writes.
2
+BEGIN;
3
+
4
+DO $$
5
+DECLARE
6
+ v_user UUID := 'f1111111-1111-4111-8111-111111111111';
7
+ v_other UUID := 'f2222222-2222-4222-8222-222222222222';
8
+ v_recipient UUID := 'f3333333-3333-4333-8333-333333333333';
9
+ t TIMESTAMPTZ := '2026-10-22 12:00:00+00';
10
+ before_state JSONB;
11
+ crossed_state JSONB;
12
+ result RECORD;
13
+ event_id TEXT := 'threshold1:2026-10-22T12:01:00.000Z:0';
14
+ n INTEGER;
15
+BEGIN
16
+ INSERT INTO public.app_users(id) VALUES (v_user),(v_other);
17
+
18
+ before_state := jsonb_build_object('alert',jsonb_build_object(
19
+ 'id','threshold1','phase','ARMED',
20
+ 'lastTimestamp','2026-10-22T12:00:00.000Z',
21
+ 'highWater',1000,
22
+ 'consecutive',0));
23
+ crossed_state := jsonb_build_object('alert',jsonb_build_object(
24
+ 'id','threshold1','phase','TRIGGERED',
25
+ 'lastTimestamp','2026-10-22T12:01:00.000Z',
26
+ 'triggeredAt','2026-10-22T12:01:00.000Z',
27
+ 'eventId',event_id,
28
+ 'highWater',1100,
29
+ 'consecutive',1));
30
+
31
+ -- Safe rule-state writes require no user notification at all.
32
+ SELECT * INTO result FROM public.alert_rule_commit(v_user,'threshold1',0,
33
+ before_state,NULL,NULL,NULL,t);
34
+ IF result.outcome <> 'STATE_SAVED' OR result.new_revision <> 1
35
+ OR result.outbox_id IS NOT NULL THEN
36
+ RAISE EXCEPTION 'initial safe state CAS did not commit as expected';
37
+ END IF;
38
+
39
+ -- Client that read old revision 0 cannot replace revision 1.
40
+ SELECT * INTO result FROM public.alert_rule_commit(v_user,'threshold1',0,
41
+ crossed_state,event_id,'BALE',v_recipient,t+interval '1 minute');
42
+ IF result.outcome <> 'STALE_REVISION' OR result.new_revision <> 1 THEN
43
+ RAISE EXCEPTION 'stale-version write overwrote durable state';
44
+ END IF;
45
+
46
+ -- Even an otherwise valid alert cannot enqueue without current consent.
47
+ BEGIN
48
+ PERFORM * FROM public.alert_rule_commit(v_user,'threshold1',1,
49
+ crossed_state,event_id,'BALE',v_recipient,t+interval '1 minute');
50
+ RAISE EXCEPTION 'unconsented notification was queued';
51
+ EXCEPTION WHEN insufficient_privilege THEN NULL;
52
+ END;
53
+
54
+ SELECT count(*) INTO n FROM public.notification_outbox
55
+ WHERE user_id=v_user;
56
+ IF n <> 0 THEN RAISE EXCEPTION 'failed consent check left message in outbox'; END IF;
57
+ SELECT revision INTO n FROM public.alert_rule_runtime
58
+ WHERE user_id=v_user AND rule_key='threshold1';
59
+ IF n <> 1 THEN RAISE EXCEPTION 'failed consent check changed runtime revision'; END IF;
60
+
61
+ INSERT INTO public.alert_notification_consents
62
+ (user_id,recipient_ref,channel,consented_at)
63
+ VALUES (v_user,v_recipient,'BALE',t-interval '10 minutes');
64
+
65
+ -- Two changes happen in the SAME SQL transaction, no write half-completion.
66
+ SELECT * INTO result FROM public.alert_rule_commit(v_user,'threshold1',1,
67
+ crossed_state,event_id,'BALE',v_recipient,t+interval '1 minute');
68
+ IF result.outcome <> 'TRIGGERED_ENQUEUED'
69
+ OR result.new_revision <> 2 OR result.outbox_id IS NULL THEN
70
+ RAISE EXCEPTION 'trigger/enqueue was not atomic';
71
+ END IF;
72
+
73
+ SELECT count(*) INTO n FROM public.notification_outbox AS o
74
+ WHERE o.user_id=v_user AND o.event_key=event_id
75
+ AND o.status='PENDING' AND o.recipient_ref=v_recipient;
76
+ IF n <> 1 THEN RAISE EXCEPTION 'trigger enqueue did not create one row'; END IF;
77
+
78
+ SELECT count(*) INTO n FROM public.alert_rule_runtime AS r
79
+ WHERE r.user_id=v_user AND r.rule_key='threshold1'
80
+ AND r.last_event_key=event_id AND r.revision=2
81
+ AND r.state #>> '{alert,phase}'='TRIGGERED';
82
+ IF n <> 1 THEN RAISE EXCEPTION 'trigger state not saved with event'; END IF;
83
+
84
+ -- No automatic re-arming or duplicate OCO event from another Worker.
85
+ SELECT * INTO result FROM public.alert_rule_commit(v_user,'threshold1',2,
86
+ crossed_state,event_id,'BALE',v_recipient,t+interval '1 minute');
87
+ IF result.outcome <> 'ALREADY_TERMINAL' OR result.new_revision <> 2 THEN
88
+ RAISE EXCEPTION 'duplicate event was not rejected';
89
+ END IF;
90
+ SELECT count(*) INTO n FROM public.notification_outbox WHERE event_key=event_id;
91
+ IF n <> 1 THEN RAISE EXCEPTION 'duplicate outbox message created'; END IF;
92
+
93
+ -- A revoked recipient never qualifies, even when the rule is valid.
94
+ INSERT INTO public.alert_notification_consents
95
+ (user_id,recipient_ref,channel,consented_at,revoked_at)
96
+ VALUES(v_other,v_recipient,'BALE',t-interval '10 minutes',t-interval '1 minute');
97
+
98
+ BEGIN
99
+ PERFORM * FROM public.alert_rule_commit(v_other,'threshold1',0,
100
+ crossed_state,event_id,'BALE',v_recipient,t+interval '1 minute');
101
+ RAISE EXCEPTION 'revoked consent accepted';
102
+ EXCEPTION WHEN insufficient_privilege THEN NULL;
103
+ END;
104
+
105
+ -- External event keys must match state, trigger and source timestamp.
106
+ BEGIN
107
+ PERFORM * FROM public.alert_rule_commit(v_other,'threshold1',0,
108
+ crossed_state,'wrong-event-id','BALE',v_recipient,t+interval '1 minute');
109
+ RAISE EXCEPTION 'mismatched event identity was accepted';
110
+ EXCEPTION WHEN invalid_parameter_value THEN NULL;
111
+ END;
112
+
113
+ -- Illegal direct jump into a NOTIFIED state is disallowed.
114
+ BEGIN
115
+ PERFORM * FROM public.alert_rule_commit(v_other,'threshold1',0,
116
+ jsonb_build_object('alert',jsonb_build_object('id','threshold1','phase','NOTIFIED')),
117
+ NULL,NULL,NULL,t+interval '1 minute');
118
+ RAISE EXCEPTION 'unverified NOTIFIED state accepted';
119
+ EXCEPTION WHEN invalid_parameter_value THEN NULL;
120
+ END;
121
+
122
+ -- Public ACLs on both new tables/functions must be absent.
123
+ IF EXISTS (
124
+ SELECT 1 FROM information_schema.role_table_grants
125
+ WHERE grantee='PUBLIC' AND table_name IN
126
+ ('alert_rule_runtime','alert_notification_consents')
127
+ ) THEN
128
+ RAISE EXCEPTION 'unsafe PUBLIC table grant';
129
+ END IF;
130
+
131
+ IF EXISTS (
132
+ SELECT 1
133
+ FROM pg_catalog.pg_proc AS p
134
+ JOIN pg_catalog.pg_namespace AS ns ON ns.oid = p.pronamespace
135
+ CROSS JOIN LATERAL pg_catalog.aclexplode(
136
+ COALESCE(p.proacl,pg_catalog.acldefault('f',p.proowner))
137
+ ) AS acl
138
+ WHERE ns.nspname='public' AND p.proname='alert_rule_commit'
139
+ AND acl.grantee=0 AND acl.privilege_type='EXECUTE'
140
+ ) THEN
141
+ RAISE EXCEPTION 'unsafe PUBLIC execute grant';
142
+ END IF;
143
+END $$;
144
+
145
+ROLLBACK;