forked from InsurNiffy/niff-Stellar-shurance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.integration.spec.ts
More file actions
241 lines (196 loc) · 8.3 KB
/
Copy pathevents.integration.spec.ts
File metadata and controls
241 lines (196 loc) · 8.3 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
/**
* SSE events integration tests.
*
* Tests the full pipeline: ClaimEventsService.publish() -> Redis pub/sub ->
* SseConnectionRegistry.broadcast() -> Subject emission.
*
* Uses an in-memory Redis mock (no real Redis required for CI).
*/
import { Test, TestingModule } from "@nestjs/testing";
import { INestApplication, HttpStatus } from "@nestjs/common";
import * as request from "supertest";
import { ConfigModule } from "@nestjs/config";
import { EventsModule } from "../events.module";
import { ClaimEventsService, ClaimStatusChangedEvent } from "../claim-events.service";
import { SseConnectionRegistry } from "../sse-connection.registry";
// ── Helpers ───────────────────────────────────────────────────────────────────
function createMockRedis() {
const subscribers: Map<string, ((channel: string, msg: string) => void)[]> = new Map();
const publishedMessages: { channel: string; message: string }[] = [];
const instance = {
_isConnected: false,
connect: jest.fn(async () => {
instance._isConnected = true;
}),
quit: jest.fn(async () => {}),
subscribe: jest.fn(async (channel: string) => {
if (!subscribers.has(channel)) subscribers.set(channel, []);
}),
unsubscribe: jest.fn(async () => {}),
publish: jest.fn(async (channel: string, message: string) => {
publishedMessages.push({ channel, message });
// Simulate local delivery
const handlers = subscribers.get(channel) ?? [];
for (const h of handlers) h(channel, message);
return 1;
}),
on: jest.fn((event: string, handler: (...args: unknown[]) => void) => {
if (event === "message") {
const channel = "claim:status:changed";
if (!subscribers.has(channel)) subscribers.set(channel, []);
subscribers.get(channel)!.push(handler as (channel: string, msg: string) => void);
}
}),
_getPublished: () => publishedMessages,
};
return instance;
}
// ── Tests ─────────────────────────────────────────────────────────────────────
describe("EventsController SSE (integration)", () => {
let app: INestApplication;
let claimEventsService: ClaimEventsService;
let registry: SseConnectionRegistry;
beforeEach(async () => {
const moduleRef: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
isGlobal: true,
ignoreEnvFile: true,
load: [
() => ({
REDIS_URL: "redis://mock:6379",
SSE_MAX_CONNECTIONS: 10,
}),
],
}),
EventsModule,
],
}).compile();
app = moduleRef.createNestApplication();
app.setGlobalPrefix("api");
claimEventsService = moduleRef.get(ClaimEventsService);
registry = moduleRef.get(SseConnectionRegistry);
// Mock Redis connections — prevent real network calls in CI
const mockRedis = createMockRedis();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(claimEventsService as any).subscriber = mockRedis;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(claimEventsService as any).publisher = mockRedis;
await app.init();
});
afterEach(async () => {
await app.close();
});
// ── Connection validation ─────────────────────────────────────────────────
it("returns 400 when no claimId is provided", async () => {
const res = await request(app.getHttpServer()).get("/api/events/claims");
expect(res.status).toBe(HttpStatus.BAD_REQUEST);
});
// ── Registry unit tests ───────────────────────────────────────────────────
describe("SseConnectionRegistry", () => {
it("registers and unregisters connections", () => {
const conn = registry.register("conn-1", ["42", "43"]);
expect(registry.activeCount()).toBe(1);
expect(conn.claimIds.has("42")).toBe(true);
registry.unregister("conn-1");
expect(registry.activeCount()).toBe(0);
});
it("broadcasts only to connections watching the given claimId", () => {
const conn1 = registry.register("conn-a", ["1"]);
const conn2 = registry.register("conn-b", ["2"]);
const received1: object[] = [];
const received2: object[] = [];
conn1.subject.subscribe((e) => received1.push(e.data as object));
conn2.subject.subscribe((e) => received2.push(e.data as object));
registry.broadcast("1", { claimId: "1", status: "approved" });
expect(received1).toHaveLength(1);
expect(received2).toHaveLength(0); // conn2 watches claimId=2, not 1
registry.unregister("conn-a");
registry.unregister("conn-b");
});
it("rejects registration when connection limit is reached", () => {
// Limit is 10 in test config
for (let i = 0; i < 10; i++) {
registry.register(`conn-${i}`, ["1"]);
}
expect(() => registry.register("conn-overflow", ["1"])).toThrow();
// Cleanup
for (let i = 0; i < 10; i++) {
registry.unregister(`conn-${i}`);
}
});
it("drainAll completes all subjects", (done) => {
const conn = registry.register("drain-test", ["99"]);
let completed = false;
conn.subject.subscribe({
complete: () => {
completed = true;
},
});
registry.drainAll();
setImmediate(() => {
expect(completed).toBe(true);
done();
});
});
});
// ── ClaimEventsService unit tests ─────────────────────────────────────────
describe("ClaimEventsService", () => {
it("publish() sends event to Redis channel", async () => {
const mockRedis = createMockRedis();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(claimEventsService as any).publisher = mockRedis;
const event: ClaimStatusChangedEvent = {
claimId: "77",
status: "approved",
updatedAt: new Date().toISOString(),
ledger: 12345,
};
await claimEventsService.publish(event);
expect(mockRedis.publish).toHaveBeenCalledWith(
"claim:status:changed",
JSON.stringify(event),
);
});
it("publish() fails silently when Redis is unavailable", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(claimEventsService as any).publisher = {
publish: jest.fn().mockRejectedValue(new Error("Redis down")),
};
const event: ClaimStatusChangedEvent = {
claimId: "1",
status: "pending",
updatedAt: new Date().toISOString(),
};
await expect(claimEventsService.publish(event)).resolves.toBeUndefined();
});
it("handleMessage broadcasts to matching SSE connections", () => {
const conn = registry.register("msg-test", ["55"]);
const received: object[] = [];
conn.subject.subscribe((e) => received.push(e.data as object));
const event: ClaimStatusChangedEvent = {
claimId: "55",
status: "paid",
updatedAt: new Date().toISOString(),
};
// Trigger internal handler directly
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(claimEventsService as any).handleMessage(JSON.stringify(event));
expect(received).toHaveLength(1);
expect((received[0] as ClaimStatusChangedEvent).status).toBe("paid");
registry.unregister("msg-test");
});
it("handleMessage ignores malformed JSON without throwing", () => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(claimEventsService as any).handleMessage("not-json");
}).not.toThrow();
});
it("handleMessage ignores events missing required fields", () => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(claimEventsService as any).handleMessage(JSON.stringify({ claimId: "1" }));
}).not.toThrow();
});
});
});