forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackfill.controller.spec.ts
More file actions
304 lines (258 loc) · 8.47 KB
/
Copy pathbackfill.controller.spec.ts
File metadata and controls
304 lines (258 loc) · 8.47 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import { Test, TestingModule } from "@nestjs/testing";
import { INestApplication } from "@nestjs/common";
import request from "supertest";
import { BadRequestException } from "@nestjs/common";
import { BackfillController } from "./backfill.controller";
import { BackfillService, BackfillStats } from "./backfill.service";
import { BackfillRunStatus } from "@prisma/client";
import {
jwtAuthImports, jwtAuthProviders, signUserToken } from "../auth/guard/auth-testing.util";
describe("BackfillController", () => {
let controller: BackfillController;
let service: jest.Mocked<BackfillService>;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [BackfillController],
providers: [
{
provide: BackfillService,
useValue: {
reconcile: jest.fn(),
getHistory: jest.fn(),
getReport: jest.fn(),
getStats: jest.fn(),
cancelRun: jest.fn(),
getLatestCheckpoint: jest.fn(),
},
},
],
}).compile();
controller = module.get<BackfillController>(BackfillController);
service = module.get(BackfillService);
});
describe("startBackfill", () => {
const mockStats: BackfillStats = {
totalEvents: 100,
matched: 80,
skipped: 15,
failed: 5,
failedEvents: [
{ invoiceId: "invoice-1", eventId: "event-1", error: "Not found" },
],
};
it("should start a backfill with valid options", async () => {
const options = {
startLedger: 1000,
endLedger: 2000,
dryRun: false,
};
service.reconcile.mockResolvedValue({
runId: 1,
stats: mockStats,
});
const result = await controller.startBackfill(options);
expect(result).toEqual({
success: true,
runId: 1,
stats: mockStats,
message: "Backfill completed",
});
expect(service.reconcile).toHaveBeenCalledWith(options);
});
it("should throw error when startLedger, fromLast, and resumeFromRunId are all missing", async () => {
const options = {
startLedger: undefined,
fromLast: false,
};
await expect(controller.startBackfill(options)).rejects.toThrow(
BadRequestException,
);
expect(service.reconcile).not.toHaveBeenCalled();
});
it("should allow fromLast without startLedger", async () => {
const options = {
startLedger: undefined,
fromLast: true,
};
service.reconcile.mockResolvedValue({
runId: 2,
stats: mockStats,
});
await controller.startBackfill(options);
expect(service.reconcile).toHaveBeenCalledWith(options);
});
it("should allow resumeFromRunId without startLedger", async () => {
const options = { resumeFromRunId: 5 };
service.reconcile.mockResolvedValue({
runId: 6,
stats: mockStats,
});
await controller.startBackfill(options);
expect(service.reconcile).toHaveBeenCalledWith(options);
});
});
describe("cancelRun", () => {
it("should cancel a run", async () => {
service.cancelRun.mockResolvedValue({
id: 3,
status: BackfillRunStatus.cancelled,
});
const result = await controller.cancelRun("3", {
runId: 3,
operator: "alice",
note: "manual stop",
});
expect(result).toEqual({
success: true,
id: 3,
status: BackfillRunStatus.cancelled,
});
expect(service.cancelRun).toHaveBeenCalledWith({
runId: 3,
operator: "alice",
note: "manual stop",
});
});
});
describe("getLatestCheckpoint", () => {
it("should delegate to service", async () => {
const payload = {
runId: 1, status: BackfillRunStatus.failed, checkpoint: null };
service.getLatestCheckpoint.mockResolvedValue(payload);
expect(await controller.getLatestCheckpoint("1")).toBe(payload);
expect(service.getLatestCheckpoint).toHaveBeenCalledWith(1);
});
});
describe("getHistory", () => {
it("should return history with default limit", async () => {
const mockHistory = [
{ id: 1, status: BackfillRunStatus.completed },
{ id: 2, status: BackfillRunStatus.failed },
];
service.getHistory.mockResolvedValue(mockHistory);
const result = await controller.getHistory(undefined);
expect(result).toEqual(mockHistory);
expect(service.getHistory).toHaveBeenCalledWith(10);
});
it("should return history with custom limit", async () => {
const mockHistory = [{ id: 1, status: BackfillRunStatus.completed }];
service.getHistory.mockResolvedValue(mockHistory);
const result = await controller.getHistory("5");
expect(result).toEqual(mockHistory);
expect(service.getHistory).toHaveBeenCalledWith(5);
});
});
describe("getReport", () => {
it("should return a specific backfill report", async () => {
const mockReport = {
id: 1,
status: BackfillRunStatus.completed,
eventsProcessed: 100,
eventsMatched: 80,
eventsSkipped: 15,
eventsFailed: 5,
};
service.getReport.mockResolvedValue(mockReport);
const result = await controller.getReport("1");
expect(result).toEqual(mockReport);
expect(service.getReport).toHaveBeenCalledWith(1);
});
});
describe("getStats", () => {
it("should return stats without contractId filter", async () => {
const mockStats = {
total: 100,
success: 80,
failed: 10,
skipped: 10,
lastProcessedLedger: 12345,
};
service.getStats.mockResolvedValue(mockStats);
const result = await controller.getStats(undefined);
expect(result).toEqual(mockStats);
expect(service.getStats).toHaveBeenCalledWith(undefined);
});
it("should return stats with contractId filter", async () => {
const mockStats = {
total: 50,
success: 40,
failed: 5,
skipped: 5,
lastProcessedLedger: 10000,
};
const contractId = "C1234567890";
service.getStats.mockResolvedValue(mockStats);
const result = await controller.getStats(contractId);
expect(result).toEqual(mockStats);
expect(service.getStats).toHaveBeenCalledWith(contractId);
});
});
});
describe("BackfillController (auth enforcement)", () => {
let app: INestApplication;
let module: TestingModule;
beforeAll(async () => {
module = await Test.createTestingModule({
controllers: [BackfillController],
imports: [...jwtAuthImports],
providers: [
{
provide: BackfillService,
useValue: {
reconcile: jest.fn().mockResolvedValue({
runId: 1,
stats: {
totalEvents: 1,
matched: 1,
skipped: 0,
failed: 0,
failedEvents: [],
},
}),
getHistory: jest.fn().mockResolvedValue([]),
},
},
...jwtAuthProviders,
],
}).compile();
app = module.createNestApplication();
await app.init();
});
afterAll(async () => {
if (app) await app.close();
});
it("POST /backfill/reconcile should reject unauthenticated requests", async () => {
await request(app.getHttpServer())
.post("/backfill/reconcile")
.send({ startLedger: 1000 })
.expect(401);
});
it("POST /backfill/reconcile should allow authenticated users", async () => {
const token = signUserToken(module as any, {
id: "user-1",
merchantId: "merchant-1",
role: "owner" as any,
});
const res = await request(app.getHttpServer())
.post("/backfill/reconcile")
.set("Authorization", `Bearer ${token}`)
.send({ startLedger: 1000 })
.expect(202);
expect(res.body).toMatchObject({ success: true, runId: 1 });
});
it("GET /backfill/history should reject unauthenticated requests", async () => {
await request(app.getHttpServer()).get("/backfill/history").expect(401);
});
it("GET /backfill/history should allow authenticated users", async () => {
const token = signUserToken(module as any, {
id: "user-1",
merchantId: "merchant-1",
role: "owner" as any,
});
const res = await request(app.getHttpServer())
.get("/backfill/history")
.set("Authorization", `Bearer ${token}`)
.expect(200);
expect(res.body).toEqual([]);
});
});