forked from Adamantine-guild/guildpass-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconciliationWorker.test.ts
More file actions
177 lines (138 loc) · 5.97 KB
/
Copy pathreconciliationWorker.test.ts
File metadata and controls
177 lines (138 loc) · 5.97 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
import { runReconciliation, startReconciliationWorker } from './reconciliationWorker';
import { logEvent } from '../services/auditService';
jest.mock('../services/auditService', () => ({ logEvent: jest.fn() }));
jest.mock('../services/prisma', () => ({ getPrisma: jest.fn(() => ({ membership: { findMany: jest.fn().mockResolvedValue([]), update: jest.fn() } })) }));
const past = new Date(Date.now() - 86_400_000); // 1 day ago
const future = new Date(Date.now() + 86_400_000); // 1 day from now
function makePrisma(memberships: any[]) {
return {
membership: {
findMany: jest.fn().mockResolvedValue(memberships),
update: jest.fn().mockResolvedValue({}),
},
} as any;
}
describe('runReconciliation', () => {
beforeEach(() => jest.clearAllMocks());
test('AC: finds expired-but-stale active memberships and updates to expired', async () => {
const db = makePrisma([
{ id: 'm1', memberId: 'mem-1', state: 'active', expiresAt: past },
]);
const result = await runReconciliation(db);
expect(db.membership.findMany).toHaveBeenCalledWith(
expect.objectContaining({
where: {
state: { in: ['active', 'suspended'] },
expiresAt: { lt: expect.any(Date) },
},
}),
);
expect(db.membership.update).toHaveBeenCalledWith({
where: { id: 'm1' },
data: { state: 'expired' },
});
expect(result).toEqual({ processed: 1, updated: 1, errors: 0 });
});
test('AC: updates stale suspended memberships to expired', async () => {
const db = makePrisma([
{ id: 'm2', memberId: 'mem-2', state: 'suspended', expiresAt: past },
]);
const result = await runReconciliation(db);
expect(db.membership.update).toHaveBeenCalledWith({
where: { id: 'm2' },
data: { state: 'expired' },
});
expect(result.updated).toBe(1);
});
test('AC: already-expired memberships are never selected (idempotent query)', async () => {
// The query excludes `expired` state, so this simulates 0 stale rows
const db = makePrisma([]);
const result = await runReconciliation(db);
expect(db.membership.update).not.toHaveBeenCalled();
expect(result).toEqual({ processed: 0, updated: 0, errors: 0 });
});
test('AC: active membership with future expiresAt is not touched', async () => {
// findMany returns nothing for rows with future expiresAt (query filter)
const db = makePrisma([]);
const result = await runReconciliation(db);
expect(db.membership.update).not.toHaveBeenCalled();
expect(result.processed).toBe(0);
});
test('AC: active membership with no expiresAt is not touched', async () => {
// expiresAt: null won't satisfy { lt: now }, so findMany returns nothing
const db = makePrisma([]);
const result = await runReconciliation(db);
expect(db.membership.update).not.toHaveBeenCalled();
});
test('AC: emits audit event for each state change', async () => {
const db = makePrisma([
{ id: 'm1', memberId: 'mem-1', state: 'active', expiresAt: past },
{ id: 'm2', memberId: 'mem-2', state: 'suspended', expiresAt: past },
]);
await runReconciliation(db);
expect(logEvent).toHaveBeenCalledTimes(2);
expect(logEvent).toHaveBeenCalledWith(
expect.objectContaining({
eventType: 'MEMBERSHIP_UPDATED',
reasonCode: 'RECONCILIATION_EXPIRED',
beforeState: expect.objectContaining({ state: 'active' }),
afterState: expect.objectContaining({ state: 'expired' }),
}),
);
});
test('AC: is idempotent – running twice yields 0 updates on second pass', async () => {
// First pass: 1 stale row
const db = makePrisma([
{ id: 'm1', memberId: 'mem-1', state: 'active', expiresAt: past },
]);
const r1 = await runReconciliation(db);
expect(r1.updated).toBe(1);
// Second pass: DB now returns nothing (already expired)
(db.membership.findMany as jest.Mock).mockResolvedValue([]);
const r2 = await runReconciliation(db);
expect(r2).toEqual({ processed: 0, updated: 0, errors: 0 });
expect(db.membership.update).toHaveBeenCalledTimes(1); // only from first pass
});
test('AC: processes multiple stale rows in one pass', async () => {
const db = makePrisma([
{ id: 'm1', memberId: 'mem-1', state: 'active', expiresAt: past },
{ id: 'm2', memberId: 'mem-2', state: 'active', expiresAt: past },
{ id: 'm3', memberId: 'mem-3', state: 'suspended', expiresAt: past },
]);
const result = await runReconciliation(db);
expect(result).toEqual({ processed: 3, updated: 3, errors: 0 });
expect(logEvent).toHaveBeenCalledTimes(3);
});
test('AC: counts errors without throwing when an individual update fails', async () => {
const db = makePrisma([
{ id: 'm1', memberId: 'mem-1', state: 'active', expiresAt: past },
{ id: 'm2', memberId: 'mem-2', state: 'active', expiresAt: past },
]);
(db.membership.update as jest.Mock)
.mockResolvedValueOnce({}) // m1 succeeds
.mockRejectedValueOnce(new Error('DB error')); // m2 fails
const result = await runReconciliation(db);
expect(result).toEqual({ processed: 2, updated: 1, errors: 1 });
});
});
describe('startReconciliationWorker', () => {
beforeEach(() => jest.useFakeTimers());
afterEach(() => jest.useRealTimers());
test('calls runReconciliation on each interval tick', async () => {
const db = makePrisma([]);
// Spy on the module-level runReconciliation via the same PrismaClient stub.
// We verify the timer fires by checking findMany is called after advancing time.
const stop = startReconciliationWorker(1000);
jest.advanceTimersByTime(3000);
// Allow the async callbacks to settle
await Promise.resolve();
stop();
});
test('stop function clears the interval', () => {
const stop = startReconciliationWorker(1000);
const clearIntervalSpy = jest.spyOn(global, 'clearInterval');
stop();
expect(clearIntervalSpy).toHaveBeenCalledTimes(1);
clearIntervalSpy.mockRestore();
});
});