-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsql-outbox-audit-columns.test.ts
More file actions
204 lines (168 loc) · 8.02 KB
/
Copy pathsql-outbox-audit-columns.test.ts
File metadata and controls
204 lines (168 loc) · 8.02 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* #4765 — the SQL outboxes must never put `updated_at` in an UPDATE payload.
*
* ObjectQL's builtin `sys_stamp_audit_update` hook owns that column on every
* update, and the column is `readonly`, so a caller-supplied value is stripped
* by `stripReadonlyFields` (#2948) — which WARNs once per call. `claim()` and
* `claimDigest()` open with an unconditional "reap stale in_flight" UPDATE that
* runs on every dispatcher tick whether or not a row is stale, so the write was
* both a no-op (stripped, then re-stamped) and, at 3 claim paths × 8 partitions
* × a 500 ms tick, 48 identical warnings a second on an idle `pnpm dev`.
*
* INSERT is a different path (no strip, and `created_at` IS caller-owned): it
* keeps writing both audit columns, as `Date`s — a native TIMESTAMP column
* rejects a bare epoch-ms number on Postgres (see `audit-timestamp.ts`).
*/
import { describe, it, expect } from 'vitest';
import type { IDataEngine } from '@objectstack/spec/contracts';
import { SqlNotificationOutbox } from './sql-outbox.js';
import { SqlHttpOutbox } from './sql-http-outbox.js';
interface RecordedUpdate {
object: string;
data: Record<string, unknown>;
}
/**
* Minimal recording `IDataEngine`. `find` replays a scripted queue of result
* sets so a claim can be driven all the way through candidate-select →
* atomic-claim → read-back; everything else is inert.
*/
function makeEngine(findResults: Array<Array<Record<string, unknown>>> = []) {
const updates: RecordedUpdate[] = [];
const inserts: Array<{ object: string; data: Record<string, unknown> }> = [];
let findCall = 0;
const engine = {
async insert(object: string, data: Record<string, unknown>) {
inserts.push({ object, data });
return data;
},
async update(object: string, data: Record<string, unknown>) {
updates.push({ object, data });
return { matched: 0, modified: 0 };
},
async find(_object: string) {
return findResults[findCall++] ?? [];
},
async findOne(_object: string, opts?: { fields?: string[] }) {
// `ack()` reads the current attempt count; `enqueue()` probes for a
// dedup winner and must miss so the insert path runs.
if (opts?.fields?.includes('attempts')) return { attempts: 2 };
return null;
},
async delete() { return { matched: 0, modified: 0 }; },
} as unknown as IDataEngine;
return { engine, updates, inserts };
}
/** Every audit column an UPDATE payload must leave to the platform. */
const PLATFORM_OWNED_ON_UPDATE = ['updated_at'];
function expectNoPlatformAuditColumns(updates: RecordedUpdate[]) {
expect(updates.length).toBeGreaterThan(0);
for (const u of updates) {
for (const column of PLATFORM_OWNED_ON_UPDATE) {
expect(
Object.prototype.hasOwnProperty.call(u.data, column),
`UPDATE on ${u.object} must not write '${column}' — keys: ${Object.keys(u.data).join(', ')}`,
).toBe(false);
}
}
}
describe('SqlNotificationOutbox — audit columns on UPDATE (#4765)', () => {
it('claim() writes no updated_at (reap + atomic claim)', async () => {
// find #1 → candidate ids; find #2 → read-back of the rows we own.
const { engine, updates } = makeEngine([[{ id: 'd1' }], []]);
const outbox = new SqlNotificationOutbox(engine, { partitionCount: 8 });
await outbox.claim({ nodeId: 'n1', limit: 10, claimTtlMs: 1000 });
// Both the unconditional reap and the atomic claim ran.
expect(updates).toHaveLength(2);
expectNoPlatformAuditColumns(updates);
// The claim still stamps its OWN columns — this is not a blanket strip.
expect(updates[1].data).toMatchObject({ status: 'in_flight', claimed_by: 'n1' });
expect(updates[1].data.claimed_at).toEqual(expect.any(Number));
});
it('claim() writes no updated_at even when nothing is claimable', async () => {
// The reap UPDATE fires on every tick regardless — that is the firehose.
const { engine, updates } = makeEngine([[]]);
const outbox = new SqlNotificationOutbox(engine, { partitionCount: 8 });
await outbox.claim({ nodeId: 'n1', limit: 10, claimTtlMs: 1000 });
expect(updates).toHaveLength(1);
expectNoPlatformAuditColumns(updates);
});
it('claimDigest() writes no updated_at', async () => {
const { engine, updates } = makeEngine([[{ id: 'd1' }], []]);
const outbox = new SqlNotificationOutbox(engine, { partitionCount: 8 });
await outbox.claimDigest({ nodeId: 'n1', limit: 10, claimTtlMs: 1000 });
expect(updates).toHaveLength(2);
expectNoPlatformAuditColumns(updates);
});
it('ack() writes no updated_at but still bumps its own columns', async () => {
const { engine, updates } = makeEngine();
const outbox = new SqlNotificationOutbox(engine, { partitionCount: 8 });
await outbox.ack('d1', { success: false, error: 'boom', nextAttemptAt: 123 });
expectNoPlatformAuditColumns(updates);
expect(updates[0].data).toMatchObject({
status: 'pending',
attempts: 3, // 2 (read back) + 1
error: 'boom',
next_attempt_at: 123,
});
});
it('enqueue() still writes both audit columns as Dates on INSERT', async () => {
const { engine, inserts } = makeEngine();
const outbox = new SqlNotificationOutbox(engine, { partitionCount: 8 });
await outbox.enqueue({ notificationId: 'n1', recipientId: 'u1', channel: 'email', payload: {} });
expect(inserts).toHaveLength(1);
expect(inserts[0].data.created_at).toBeInstanceOf(Date);
expect(inserts[0].data.updated_at).toBeInstanceOf(Date);
});
});
describe('SqlHttpOutbox — audit columns on UPDATE (#4765)', () => {
const enqueueInput = {
source: 'flow',
refId: 'r1',
dedupKey: 'd1',
url: 'https://example.test/hook',
payload: { hello: 'world' },
};
it('claim() writes no updated_at (reap + atomic claim)', async () => {
const { engine, updates } = makeEngine([[{ id: 'h1' }], []]);
const outbox = new SqlHttpOutbox(engine, { partitionCount: 8 });
await outbox.claim({ nodeId: 'n1', limit: 10, claimTtlMs: 1000 });
expect(updates).toHaveLength(2);
expectNoPlatformAuditColumns(updates);
expect(updates[1].data).toMatchObject({ status: 'in_flight', claimed_by: 'n1' });
});
it('claim() writes no updated_at even when nothing is claimable', async () => {
const { engine, updates } = makeEngine([[]]);
const outbox = new SqlHttpOutbox(engine, { partitionCount: 8 });
await outbox.claim({ nodeId: 'n1', limit: 10, claimTtlMs: 1000 });
expect(updates).toHaveLength(1);
expectNoPlatformAuditColumns(updates);
});
it('ack() writes no updated_at but still bumps its own columns', async () => {
const { engine, updates } = makeEngine();
const outbox = new SqlHttpOutbox(engine, { partitionCount: 8 });
await outbox.ack('h1', {
success: false,
error: 'boom',
httpStatus: 503,
nextRetryAt: 456,
durationMs: 12,
});
expectNoPlatformAuditColumns(updates);
expect(updates[0].data).toMatchObject({
status: 'pending',
attempts: 3,
error: 'boom',
response_code: 503,
next_retry_at: 456,
});
});
it('enqueue() still writes both audit columns as Dates on INSERT', async () => {
const { engine, inserts } = makeEngine();
const outbox = new SqlHttpOutbox(engine, { partitionCount: 8 });
await outbox.enqueue(enqueueInput);
expect(inserts).toHaveLength(1);
expect(inserts[0].data.created_at).toBeInstanceOf(Date);
expect(inserts[0].data.updated_at).toBeInstanceOf(Date);
});
});