@nathapp/nestjs-health 3.3.0 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +13 -15
- package/.eslintrc.js +0 -8
- package/.nax/features/nestjs-health/.nax-acceptance.test.ts +0 -756
- package/.turbo/turbo-build.log +0 -4
- package/CHANGELOG.md +0 -15
- package/lib/constant/health.constants.ts +0 -23
- package/lib/constant/index.ts +0 -1
- package/lib/controller/health.controller.ts +0 -37
- package/lib/controller/index.ts +0 -1
- package/lib/index.ts +0 -6
- package/lib/indicator/cache-health.check.ts +0 -59
- package/lib/indicator/generic-health.check.ts +0 -43
- package/lib/indicator/index.ts +0 -4
- package/lib/indicator/prisma-health.check.ts +0 -59
- package/lib/indicator/queue-health.check.ts +0 -72
- package/lib/interface/health-indicator.interface.ts +0 -18
- package/lib/interface/health-module-options.interface.ts +0 -85
- package/lib/interface/index.ts +0 -2
- package/lib/module/health.module.ts +0 -62
- package/lib/module/index.ts +0 -1
- package/lib/service/health-executor.service.ts +0 -69
- package/lib/service/index.ts +0 -1
- package/test/constant/health.constants.spec.ts +0 -34
- package/test/controller/health.controller.spec.ts +0 -117
- package/test/indicator/cache-health.check.spec.ts +0 -88
- package/test/indicator/generic-health.check.spec.ts +0 -61
- package/test/indicator/prisma-health.check.spec.ts +0 -69
- package/test/indicator/queue-health.check.spec.ts +0 -88
- package/test/interface/health-module-options.spec.ts +0 -59
- package/test/jest-nax.json +0 -27
- package/test/jest-test.json +0 -28
- package/test/module/health.module.spec.ts +0 -311
- package/test/service/health-executor.service.spec.ts +0 -186
- package/tsconfig.json +0 -9
|
@@ -1,756 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeAll, afterAll } from '@jest/globals';
|
|
2
|
-
import { Test } from '@nestjs/testing';
|
|
3
|
-
import { Injectable, INestApplication } from '@nestjs/common';
|
|
4
|
-
import { TerminusModule, HealthIndicatorService } from '@nestjs/terminus';
|
|
5
|
-
import { firstValueFrom, catchError, of } from 'rxjs';
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
GenericHealthCheck,
|
|
9
|
-
CACHE_HEALTH_SOURCE,
|
|
10
|
-
PRISMA_HEALTH_SOURCE,
|
|
11
|
-
QUEUE_HEALTH_SOURCE,
|
|
12
|
-
resolveHealthControllerOptions,
|
|
13
|
-
CacheHealthCheck,
|
|
14
|
-
PrismaHealthCheck,
|
|
15
|
-
QueueHealthCheck,
|
|
16
|
-
HealthExecutorService,
|
|
17
|
-
createHealthController,
|
|
18
|
-
HealthModule,
|
|
19
|
-
HEALTH_MODULE_OPTIONS,
|
|
20
|
-
} from '../../../lib';
|
|
21
|
-
|
|
22
|
-
// ---------------------------------------------------------------------------
|
|
23
|
-
// AC-1: GenericHealthCheck is injectable and resolvable from a test module
|
|
24
|
-
// ---------------------------------------------------------------------------
|
|
25
|
-
describe('AC-1: GenericHealthCheck injectable and resolvable from test module', () => {
|
|
26
|
-
it('compiles test module with GenericHealthCheck and gets the instance without throwing', async () => {
|
|
27
|
-
const moduleRef = await Test.createTestingModule({
|
|
28
|
-
imports: [TerminusModule],
|
|
29
|
-
providers: [GenericHealthCheck],
|
|
30
|
-
}).compile();
|
|
31
|
-
|
|
32
|
-
const instance = moduleRef.get(GenericHealthCheck);
|
|
33
|
-
expect(instance).toBeDefined();
|
|
34
|
-
expect(instance).toBeInstanceOf(GenericHealthCheck);
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
// ---------------------------------------------------------------------------
|
|
39
|
-
// AC-2: GenericHealthCheck returns "up" when normalize maps source result to true
|
|
40
|
-
// ---------------------------------------------------------------------------
|
|
41
|
-
describe('AC-2: GenericHealthCheck.isHealthy returns up when normalize returns true', () => {
|
|
42
|
-
it('resolved value svc property has status up', async () => {
|
|
43
|
-
const hi = new HealthIndicatorService();
|
|
44
|
-
const source = { isHealthy: () => Promise.resolve({ status: 'ok' }) };
|
|
45
|
-
const normalize = (r: any) => r.status === 'ok';
|
|
46
|
-
const check = new (GenericHealthCheck as any)(hi, source, normalize);
|
|
47
|
-
const result: any = await check.isHealthy('svc');
|
|
48
|
-
expect(result?.svc?.status).toBe('up');
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// ---------------------------------------------------------------------------
|
|
53
|
-
// AC-3: GenericHealthCheck returns "down" when normalize maps source result to false
|
|
54
|
-
// ---------------------------------------------------------------------------
|
|
55
|
-
describe('AC-3: GenericHealthCheck.isHealthy returns down when normalize returns false', () => {
|
|
56
|
-
it('resolved value svc property has status down', async () => {
|
|
57
|
-
const hi = new HealthIndicatorService();
|
|
58
|
-
const source = { isHealthy: () => Promise.resolve({ status: 'degraded' }) };
|
|
59
|
-
const normalize = (r: any) => r.status !== 'degraded';
|
|
60
|
-
const check = new (GenericHealthCheck as any)(hi, source, normalize);
|
|
61
|
-
const result: any = await check.isHealthy('svc');
|
|
62
|
-
expect(result?.svc?.status).toBe('down');
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
// ---------------------------------------------------------------------------
|
|
67
|
-
// AC-4: GenericHealthCheck resolves with "down" when source rejects — never propagates
|
|
68
|
-
// ---------------------------------------------------------------------------
|
|
69
|
-
describe('AC-4: GenericHealthCheck.isHealthy resolves (does not reject) with svc status down when source rejects', () => {
|
|
70
|
-
it('resolves with svc entry having status down', async () => {
|
|
71
|
-
const hi = new HealthIndicatorService();
|
|
72
|
-
const source = { isHealthy: () => Promise.reject(new Error('fail')) };
|
|
73
|
-
const normalize = (_: any) => true;
|
|
74
|
-
const check = new (GenericHealthCheck as any)(hi, source, normalize);
|
|
75
|
-
|
|
76
|
-
await expect(check.isHealthy('svc')).resolves.toBeDefined();
|
|
77
|
-
const result: any = await check.isHealthy('svc');
|
|
78
|
-
expect(result?.svc?.status).toBe('down');
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
// ---------------------------------------------------------------------------
|
|
83
|
-
// AC-5: Source injection tokens are importable, truthy, and pairwise distinct
|
|
84
|
-
// ---------------------------------------------------------------------------
|
|
85
|
-
describe('AC-5: CACHE_HEALTH_SOURCE, PRISMA_HEALTH_SOURCE, QUEUE_HEALTH_SOURCE are distinct tokens', () => {
|
|
86
|
-
it('all three tokens are truthy and no two are equal', () => {
|
|
87
|
-
expect(CACHE_HEALTH_SOURCE).toBeTruthy();
|
|
88
|
-
expect(PRISMA_HEALTH_SOURCE).toBeTruthy();
|
|
89
|
-
expect(QUEUE_HEALTH_SOURCE).toBeTruthy();
|
|
90
|
-
expect(CACHE_HEALTH_SOURCE).not.toBe(PRISMA_HEALTH_SOURCE);
|
|
91
|
-
expect(PRISMA_HEALTH_SOURCE).not.toBe(QUEUE_HEALTH_SOURCE);
|
|
92
|
-
expect(CACHE_HEALTH_SOURCE).not.toBe(QUEUE_HEALTH_SOURCE);
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
// ---------------------------------------------------------------------------
|
|
97
|
-
// AC-6: resolveHealthControllerOptions returns default liveness/readiness paths
|
|
98
|
-
// ---------------------------------------------------------------------------
|
|
99
|
-
describe('AC-6: resolveHealthControllerOptions() with no arguments returns defaults', () => {
|
|
100
|
-
it('returns livenessPath "health/live" and readinessPath "health/ready"', () => {
|
|
101
|
-
const opts = resolveHealthControllerOptions();
|
|
102
|
-
expect(opts).not.toBe(false);
|
|
103
|
-
if (opts === false) return;
|
|
104
|
-
expect(opts.livenessPath).toBe('health/live');
|
|
105
|
-
expect(opts.readinessPath).toBe('health/ready');
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
// ---------------------------------------------------------------------------
|
|
110
|
-
// AC-7: Named adapters are importable, have isHealthy, and are @Injectable()
|
|
111
|
-
// ---------------------------------------------------------------------------
|
|
112
|
-
describe('AC-7: CacheHealthCheck, PrismaHealthCheck, QueueHealthCheck are injectable classes with isHealthy', () => {
|
|
113
|
-
it('all three are functions with isHealthy on prototype and carry @Injectable metadata', () => {
|
|
114
|
-
expect(typeof CacheHealthCheck).toBe('function');
|
|
115
|
-
expect(typeof PrismaHealthCheck).toBe('function');
|
|
116
|
-
expect(typeof QueueHealthCheck).toBe('function');
|
|
117
|
-
|
|
118
|
-
expect(typeof CacheHealthCheck.prototype.isHealthy).toBe('function');
|
|
119
|
-
expect(typeof PrismaHealthCheck.prototype.isHealthy).toBe('function');
|
|
120
|
-
expect(typeof QueueHealthCheck.prototype.isHealthy).toBe('function');
|
|
121
|
-
|
|
122
|
-
// @Injectable() sets '__injectable__' metadata to true
|
|
123
|
-
expect(Reflect.getMetadata('__injectable__', CacheHealthCheck)).toBe(true);
|
|
124
|
-
expect(Reflect.getMetadata('__injectable__', PrismaHealthCheck)).toBe(true);
|
|
125
|
-
expect(Reflect.getMetadata('__injectable__', QueueHealthCheck)).toBe(true);
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
// ---------------------------------------------------------------------------
|
|
130
|
-
// AC-8: CacheHealthCheck returns "up" when source is healthy
|
|
131
|
-
// ---------------------------------------------------------------------------
|
|
132
|
-
describe('AC-8: CacheHealthCheck.isHealthy returns up for a healthy source', () => {
|
|
133
|
-
it('cache entry has status up', async () => {
|
|
134
|
-
const mockSource = {
|
|
135
|
-
isHealthy: async () => ({ healthy: true, connections: ['redis://:6379'] }),
|
|
136
|
-
};
|
|
137
|
-
const moduleRef = await Test.createTestingModule({
|
|
138
|
-
imports: [TerminusModule],
|
|
139
|
-
providers: [
|
|
140
|
-
CacheHealthCheck,
|
|
141
|
-
{ provide: CACHE_HEALTH_SOURCE, useValue: mockSource },
|
|
142
|
-
],
|
|
143
|
-
}).compile();
|
|
144
|
-
|
|
145
|
-
const check = moduleRef.get(CacheHealthCheck);
|
|
146
|
-
const result: any = await check.isHealthy('cache');
|
|
147
|
-
expect(result?.cache?.status).toBe('up');
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
// ---------------------------------------------------------------------------
|
|
152
|
-
// AC-9: CacheHealthCheck returns "down" when source is unhealthy
|
|
153
|
-
// ---------------------------------------------------------------------------
|
|
154
|
-
describe('AC-9: CacheHealthCheck.isHealthy returns down for an unhealthy source', () => {
|
|
155
|
-
it('cache entry has status down', async () => {
|
|
156
|
-
const mockSource = {
|
|
157
|
-
isHealthy: async () => ({ healthy: false, connections: [] }),
|
|
158
|
-
};
|
|
159
|
-
const moduleRef = await Test.createTestingModule({
|
|
160
|
-
imports: [TerminusModule],
|
|
161
|
-
providers: [
|
|
162
|
-
CacheHealthCheck,
|
|
163
|
-
{ provide: CACHE_HEALTH_SOURCE, useValue: mockSource },
|
|
164
|
-
],
|
|
165
|
-
}).compile();
|
|
166
|
-
|
|
167
|
-
const check = moduleRef.get(CacheHealthCheck);
|
|
168
|
-
const result: any = await check.isHealthy('cache');
|
|
169
|
-
expect(result?.cache?.status).toBe('down');
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
// ---------------------------------------------------------------------------
|
|
174
|
-
// AC-10: CacheHealthCheck returns "down" when source token is not bound
|
|
175
|
-
// ---------------------------------------------------------------------------
|
|
176
|
-
describe('AC-10: CacheHealthCheck.isHealthy returns down and does not throw when source is absent', () => {
|
|
177
|
-
it('cache entry has status down with no CACHE_HEALTH_SOURCE provider', async () => {
|
|
178
|
-
const moduleRef = await Test.createTestingModule({
|
|
179
|
-
imports: [TerminusModule],
|
|
180
|
-
providers: [CacheHealthCheck],
|
|
181
|
-
}).compile();
|
|
182
|
-
|
|
183
|
-
const check = moduleRef.get(CacheHealthCheck);
|
|
184
|
-
const result: any = await expect(check.isHealthy('cache')).resolves.toBeDefined();
|
|
185
|
-
const resolved: any = await check.isHealthy('cache');
|
|
186
|
-
expect(resolved?.cache?.status).toBe('down');
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
// ---------------------------------------------------------------------------
|
|
191
|
-
// AC-11: PrismaHealthCheck reflects source status — up and down
|
|
192
|
-
// ---------------------------------------------------------------------------
|
|
193
|
-
describe('AC-11: PrismaHealthCheck.isHealthy reflects source status up and down', () => {
|
|
194
|
-
it('returns prisma up for { status: "up" } and down for { status: "down" }', async () => {
|
|
195
|
-
const upSource = { isHealthy: async () => ({ status: 'up' }) };
|
|
196
|
-
const upModuleRef = await Test.createTestingModule({
|
|
197
|
-
imports: [TerminusModule],
|
|
198
|
-
providers: [
|
|
199
|
-
PrismaHealthCheck,
|
|
200
|
-
{ provide: PRISMA_HEALTH_SOURCE, useValue: upSource },
|
|
201
|
-
],
|
|
202
|
-
}).compile();
|
|
203
|
-
const upResult: any = await upModuleRef.get(PrismaHealthCheck).isHealthy('prisma');
|
|
204
|
-
expect(upResult?.prisma?.status).toBe('up');
|
|
205
|
-
|
|
206
|
-
const downSource = { isHealthy: async () => ({ status: 'down', message: 'Connection refused' }) };
|
|
207
|
-
const downModuleRef = await Test.createTestingModule({
|
|
208
|
-
imports: [TerminusModule],
|
|
209
|
-
providers: [
|
|
210
|
-
PrismaHealthCheck,
|
|
211
|
-
{ provide: PRISMA_HEALTH_SOURCE, useValue: downSource },
|
|
212
|
-
],
|
|
213
|
-
}).compile();
|
|
214
|
-
const downResult: any = await downModuleRef.get(PrismaHealthCheck).isHealthy('prisma');
|
|
215
|
-
expect(downResult?.prisma?.status).toBe('down');
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
// ---------------------------------------------------------------------------
|
|
220
|
-
// AC-12: PrismaHealthCheck.isHealthy resolves with "down" when source throws
|
|
221
|
-
// ---------------------------------------------------------------------------
|
|
222
|
-
describe('AC-12: PrismaHealthCheck.isHealthy resolves with down when source throws', () => {
|
|
223
|
-
it('does not propagate the error and returns prisma entry with status down', async () => {
|
|
224
|
-
const throwingSource = {
|
|
225
|
-
isHealthy: async () => { throw new Error('db gone'); },
|
|
226
|
-
};
|
|
227
|
-
const moduleRef = await Test.createTestingModule({
|
|
228
|
-
imports: [TerminusModule],
|
|
229
|
-
providers: [
|
|
230
|
-
PrismaHealthCheck,
|
|
231
|
-
{ provide: PRISMA_HEALTH_SOURCE, useValue: throwingSource },
|
|
232
|
-
],
|
|
233
|
-
}).compile();
|
|
234
|
-
|
|
235
|
-
const check = moduleRef.get(PrismaHealthCheck);
|
|
236
|
-
await expect(check.isHealthy('prisma')).resolves.toBeDefined();
|
|
237
|
-
const result: any = await check.isHealthy('prisma');
|
|
238
|
-
expect(result?.prisma?.status).toBe('down');
|
|
239
|
-
});
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
// ---------------------------------------------------------------------------
|
|
243
|
-
// AC-13: QueueHealthCheck reflects source healthy/unhealthy
|
|
244
|
-
// ---------------------------------------------------------------------------
|
|
245
|
-
describe('AC-13: QueueHealthCheck.isHealthy reflects source healthy flag', () => {
|
|
246
|
-
it('returns queue up for { healthy: true } and down for { healthy: false }', async () => {
|
|
247
|
-
const upSource = {
|
|
248
|
-
isHealthy: async () => ({ healthy: true, provider: 'bull', connections: 2 }),
|
|
249
|
-
};
|
|
250
|
-
const upModuleRef = await Test.createTestingModule({
|
|
251
|
-
imports: [TerminusModule],
|
|
252
|
-
providers: [
|
|
253
|
-
QueueHealthCheck,
|
|
254
|
-
{ provide: QUEUE_HEALTH_SOURCE, useValue: upSource },
|
|
255
|
-
],
|
|
256
|
-
}).compile();
|
|
257
|
-
const upResult: any = await upModuleRef.get(QueueHealthCheck).isHealthy('queue');
|
|
258
|
-
expect(upResult?.queue?.status).toBe('up');
|
|
259
|
-
|
|
260
|
-
const downSource = { isHealthy: async () => ({ healthy: false }) };
|
|
261
|
-
const downModuleRef = await Test.createTestingModule({
|
|
262
|
-
imports: [TerminusModule],
|
|
263
|
-
providers: [
|
|
264
|
-
QueueHealthCheck,
|
|
265
|
-
{ provide: QUEUE_HEALTH_SOURCE, useValue: downSource },
|
|
266
|
-
],
|
|
267
|
-
}).compile();
|
|
268
|
-
const downResult: any = await downModuleRef.get(QueueHealthCheck).isHealthy('queue');
|
|
269
|
-
expect(downResult?.queue?.status).toBe('down');
|
|
270
|
-
});
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
// ---------------------------------------------------------------------------
|
|
274
|
-
// AC-14: HealthExecutorService has runLiveness and runReadiness returning Observable<HealthCheckResult>
|
|
275
|
-
// ---------------------------------------------------------------------------
|
|
276
|
-
describe('AC-14: HealthExecutorService exposes runLiveness and runReadiness returning Observable with status', () => {
|
|
277
|
-
it('resolves to HealthCheckResult with status "ok" | "error"', async () => {
|
|
278
|
-
const moduleRef = await Test.createTestingModule({
|
|
279
|
-
imports: [TerminusModule],
|
|
280
|
-
providers: [
|
|
281
|
-
HealthExecutorService,
|
|
282
|
-
{ provide: HEALTH_MODULE_OPTIONS, useValue: { liveness: [], readiness: [] } },
|
|
283
|
-
],
|
|
284
|
-
}).compile();
|
|
285
|
-
|
|
286
|
-
const executor = moduleRef.get(HealthExecutorService);
|
|
287
|
-
expect(typeof executor.runLiveness).toBe('function');
|
|
288
|
-
expect(typeof executor.runReadiness).toBe('function');
|
|
289
|
-
|
|
290
|
-
const liveness$ = executor.runLiveness();
|
|
291
|
-
expect(typeof (liveness$ as any).subscribe).toBe('function');
|
|
292
|
-
|
|
293
|
-
const livenessResult: any = await firstValueFrom(
|
|
294
|
-
liveness$.pipe(catchError((err: any) => of(err?.response ?? err?.getResponse?.() ?? err))),
|
|
295
|
-
);
|
|
296
|
-
expect(['ok', 'error']).toContain(livenessResult?.status);
|
|
297
|
-
|
|
298
|
-
const readinessResult: any = await firstValueFrom(
|
|
299
|
-
executor.runReadiness().pipe(catchError((err: any) => of(err?.response ?? err?.getResponse?.() ?? err))),
|
|
300
|
-
);
|
|
301
|
-
expect(['ok', 'error']).toContain(readinessResult?.status);
|
|
302
|
-
});
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
// ---------------------------------------------------------------------------
|
|
306
|
-
// AC-15: runReadiness() with a single up adapter returns status "ok" and up entry
|
|
307
|
-
// ---------------------------------------------------------------------------
|
|
308
|
-
describe('AC-15: runReadiness with one up adapter yields status ok and entry key up', () => {
|
|
309
|
-
it('result.status is ok and details/info contains key with status up', async () => {
|
|
310
|
-
@Injectable()
|
|
311
|
-
class MockAdapterSingleUp {
|
|
312
|
-
async isHealthy() { return { key: { status: 'up' as const } }; }
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
const moduleRef = await Test.createTestingModule({
|
|
316
|
-
imports: [TerminusModule],
|
|
317
|
-
providers: [
|
|
318
|
-
HealthExecutorService,
|
|
319
|
-
MockAdapterSingleUp,
|
|
320
|
-
{ provide: HEALTH_MODULE_OPTIONS, useValue: { readiness: [MockAdapterSingleUp] } },
|
|
321
|
-
],
|
|
322
|
-
}).compile();
|
|
323
|
-
|
|
324
|
-
const executor = moduleRef.get(HealthExecutorService);
|
|
325
|
-
const result: any = await firstValueFrom(
|
|
326
|
-
executor.runReadiness().pipe(catchError((err: any) => of(err?.response ?? err?.getResponse?.() ?? err))),
|
|
327
|
-
);
|
|
328
|
-
|
|
329
|
-
expect(result?.status).toBe('ok');
|
|
330
|
-
const keyEntry = result?.details?.key ?? result?.info?.key;
|
|
331
|
-
expect(keyEntry?.status).toBe('up');
|
|
332
|
-
});
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
// ---------------------------------------------------------------------------
|
|
336
|
-
// AC-16: runReadiness() with one up and one down adapter returns status "error"
|
|
337
|
-
// ---------------------------------------------------------------------------
|
|
338
|
-
describe('AC-16: runReadiness with up + down adapters yields status error and down entry', () => {
|
|
339
|
-
it('result.status is error and error/details contains downKey with status down', async () => {
|
|
340
|
-
@Injectable()
|
|
341
|
-
class MockAdapterUpOnly {
|
|
342
|
-
async isHealthy() { return { upKey: { status: 'up' as const } }; }
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
@Injectable()
|
|
346
|
-
class MockAdapterDownOnly {
|
|
347
|
-
async isHealthy() { return { downKey: { status: 'down' as const } }; }
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
const moduleRef = await Test.createTestingModule({
|
|
351
|
-
imports: [TerminusModule],
|
|
352
|
-
providers: [
|
|
353
|
-
HealthExecutorService,
|
|
354
|
-
MockAdapterUpOnly,
|
|
355
|
-
MockAdapterDownOnly,
|
|
356
|
-
{
|
|
357
|
-
provide: HEALTH_MODULE_OPTIONS,
|
|
358
|
-
useValue: { readiness: [MockAdapterUpOnly, MockAdapterDownOnly] },
|
|
359
|
-
},
|
|
360
|
-
],
|
|
361
|
-
}).compile();
|
|
362
|
-
|
|
363
|
-
const executor = moduleRef.get(HealthExecutorService);
|
|
364
|
-
const result: any = await firstValueFrom(
|
|
365
|
-
executor.runReadiness().pipe(catchError((err: any) => of(err?.response ?? err?.getResponse?.() ?? err))),
|
|
366
|
-
);
|
|
367
|
-
|
|
368
|
-
expect(result?.status).toBe('error');
|
|
369
|
-
const downEntry = result?.error?.downKey ?? result?.details?.downKey;
|
|
370
|
-
expect(downEntry?.status).toBe('down');
|
|
371
|
-
});
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
// ---------------------------------------------------------------------------
|
|
375
|
-
// AC-17: runLiveness() with empty liveness array returns status "ok"
|
|
376
|
-
// ---------------------------------------------------------------------------
|
|
377
|
-
describe('AC-17: runLiveness with empty liveness array returns status ok', () => {
|
|
378
|
-
it('result.status is ok', async () => {
|
|
379
|
-
const moduleRef = await Test.createTestingModule({
|
|
380
|
-
imports: [TerminusModule],
|
|
381
|
-
providers: [
|
|
382
|
-
HealthExecutorService,
|
|
383
|
-
{ provide: HEALTH_MODULE_OPTIONS, useValue: { liveness: [] } },
|
|
384
|
-
],
|
|
385
|
-
}).compile();
|
|
386
|
-
|
|
387
|
-
const executor = moduleRef.get(HealthExecutorService);
|
|
388
|
-
const result: any = await firstValueFrom(
|
|
389
|
-
executor.runLiveness().pipe(catchError((err: any) => of(err?.response ?? err?.getResponse?.() ?? err))),
|
|
390
|
-
);
|
|
391
|
-
expect(result?.status).toBe('ok');
|
|
392
|
-
});
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
// ---------------------------------------------------------------------------
|
|
396
|
-
// AC-18: createHealthController returns a NestJS controller class
|
|
397
|
-
// ---------------------------------------------------------------------------
|
|
398
|
-
describe('AC-18: createHealthController returns a Type with controller and route metadata', () => {
|
|
399
|
-
it('returns a class with @Controller path and a @Get handler for "live"', () => {
|
|
400
|
-
const ReturnedClass = createHealthController({
|
|
401
|
-
livenessPath: 'health/live',
|
|
402
|
-
readinessPath: 'health/ready',
|
|
403
|
-
});
|
|
404
|
-
|
|
405
|
-
expect(typeof ReturnedClass).toBe('function');
|
|
406
|
-
|
|
407
|
-
// Verify controller decorator was applied (NestJS stores path metadata)
|
|
408
|
-
const controllerPath = Reflect.getMetadata('path', ReturnedClass);
|
|
409
|
-
expect(controllerPath).toBeDefined();
|
|
410
|
-
expect(typeof controllerPath).toBe('string');
|
|
411
|
-
|
|
412
|
-
// Verify there's a route handler named 'live' with HTTP GET method.
|
|
413
|
-
// NestJS RequestMapping stores METHOD_METADATA on the descriptor's
|
|
414
|
-
// value (the method function), not on the prototype+key pair.
|
|
415
|
-
// NestJS RequestMethod.GET = 0
|
|
416
|
-
const METHOD_METADATA = 'method';
|
|
417
|
-
const liveMethodName = Object.getOwnPropertyNames(ReturnedClass.prototype).find(
|
|
418
|
-
(name) => {
|
|
419
|
-
if (name === 'constructor') return false;
|
|
420
|
-
const fn = (ReturnedClass.prototype as any)[name];
|
|
421
|
-
return Reflect.getMetadata(METHOD_METADATA, fn) === 0;
|
|
422
|
-
},
|
|
423
|
-
);
|
|
424
|
-
expect(liveMethodName).toBeDefined();
|
|
425
|
-
});
|
|
426
|
-
});
|
|
427
|
-
|
|
428
|
-
// ---------------------------------------------------------------------------
|
|
429
|
-
// AC-19: GET /health/live returns 200 from createHealthController (integration)
|
|
430
|
-
// ---------------------------------------------------------------------------
|
|
431
|
-
describe('AC-19: mounted createHealthController serves GET /health/live with 200', () => {
|
|
432
|
-
let app: INestApplication;
|
|
433
|
-
|
|
434
|
-
beforeAll(async () => {
|
|
435
|
-
@Injectable()
|
|
436
|
-
class MockExecutor {
|
|
437
|
-
runLiveness() { return of({ status: 'ok' as const, info: {}, error: {}, details: {} }); }
|
|
438
|
-
runReadiness() { return of({ status: 'ok' as const, info: {}, error: {}, details: {} }); }
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
const HealthController = createHealthController({
|
|
442
|
-
livenessPath: 'health/live',
|
|
443
|
-
readinessPath: 'health/ready',
|
|
444
|
-
});
|
|
445
|
-
|
|
446
|
-
const moduleRef = await Test.createTestingModule({
|
|
447
|
-
controllers: [HealthController],
|
|
448
|
-
providers: [
|
|
449
|
-
{ provide: HealthExecutorService, useClass: MockExecutor },
|
|
450
|
-
],
|
|
451
|
-
}).compile();
|
|
452
|
-
|
|
453
|
-
app = moduleRef.createNestApplication();
|
|
454
|
-
await app.init();
|
|
455
|
-
});
|
|
456
|
-
|
|
457
|
-
afterAll(async () => {
|
|
458
|
-
await app?.close();
|
|
459
|
-
});
|
|
460
|
-
|
|
461
|
-
it('GET /health/live returns HTTP 200', async () => {
|
|
462
|
-
const supertest = require('supertest');
|
|
463
|
-
const res = await supertest(app.getHttpServer()).get('/health/live');
|
|
464
|
-
expect(res.status).toBe(200);
|
|
465
|
-
});
|
|
466
|
-
});
|
|
467
|
-
|
|
468
|
-
// ---------------------------------------------------------------------------
|
|
469
|
-
// AC-20: GET /health/ready returns 503 when readiness executor reports down
|
|
470
|
-
// ---------------------------------------------------------------------------
|
|
471
|
-
describe('AC-20: mounted createHealthController serves GET /health/ready with 503 when executor reports down', () => {
|
|
472
|
-
let app: INestApplication;
|
|
473
|
-
|
|
474
|
-
beforeAll(async () => {
|
|
475
|
-
const { ServiceUnavailableException } = require('@nestjs/common');
|
|
476
|
-
|
|
477
|
-
@Injectable()
|
|
478
|
-
class MockDownExecutor {
|
|
479
|
-
runLiveness() { return of({ status: 'ok' as const, info: {}, error: {}, details: {} }); }
|
|
480
|
-
runReadiness() {
|
|
481
|
-
const result = { status: 'error' as const, info: {}, error: { adapter: { status: 'down' as const } }, details: { adapter: { status: 'down' as const } } };
|
|
482
|
-
throw new ServiceUnavailableException(result);
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
const HealthController = createHealthController({
|
|
487
|
-
livenessPath: 'health/live',
|
|
488
|
-
readinessPath: 'health/ready',
|
|
489
|
-
});
|
|
490
|
-
|
|
491
|
-
const moduleRef = await Test.createTestingModule({
|
|
492
|
-
controllers: [HealthController],
|
|
493
|
-
providers: [
|
|
494
|
-
{ provide: HealthExecutorService, useClass: MockDownExecutor },
|
|
495
|
-
],
|
|
496
|
-
}).compile();
|
|
497
|
-
|
|
498
|
-
app = moduleRef.createNestApplication();
|
|
499
|
-
await app.init();
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
afterAll(async () => {
|
|
503
|
-
await app?.close();
|
|
504
|
-
});
|
|
505
|
-
|
|
506
|
-
it('GET /health/ready returns HTTP 503', async () => {
|
|
507
|
-
const supertest = require('supertest');
|
|
508
|
-
const res = await supertest(app.getHttpServer()).get('/health/ready');
|
|
509
|
-
expect(res.status).toBe(503);
|
|
510
|
-
});
|
|
511
|
-
});
|
|
512
|
-
|
|
513
|
-
// ---------------------------------------------------------------------------
|
|
514
|
-
// AC-21: HealthModule.register() returns a global DynamicModule
|
|
515
|
-
// ---------------------------------------------------------------------------
|
|
516
|
-
describe('AC-21: HealthModule.register({}) returns a global DynamicModule', () => {
|
|
517
|
-
it('returns object with global: true and module === HealthModule', () => {
|
|
518
|
-
const result: any = HealthModule.register({});
|
|
519
|
-
expect(result).toBeDefined();
|
|
520
|
-
expect(result.global).toBe(true);
|
|
521
|
-
expect(result.module).toBe(HealthModule);
|
|
522
|
-
});
|
|
523
|
-
});
|
|
524
|
-
|
|
525
|
-
// ---------------------------------------------------------------------------
|
|
526
|
-
// AC-22: Full HealthModule.register({}) app serves GET /health/live with 200
|
|
527
|
-
// ---------------------------------------------------------------------------
|
|
528
|
-
describe('AC-22: HealthModule.register({}) app serves GET /health/live returning 200', () => {
|
|
529
|
-
let app: INestApplication;
|
|
530
|
-
|
|
531
|
-
beforeAll(async () => {
|
|
532
|
-
const moduleRef = await Test.createTestingModule({
|
|
533
|
-
imports: [HealthModule.register({})],
|
|
534
|
-
}).compile();
|
|
535
|
-
|
|
536
|
-
app = moduleRef.createNestApplication();
|
|
537
|
-
await app.init();
|
|
538
|
-
});
|
|
539
|
-
|
|
540
|
-
afterAll(async () => {
|
|
541
|
-
await app?.close();
|
|
542
|
-
});
|
|
543
|
-
|
|
544
|
-
it('GET /health/live returns 200 and body status ok or up', async () => {
|
|
545
|
-
const supertest = require('supertest');
|
|
546
|
-
const res = await supertest(app.getHttpServer()).get('/health/live');
|
|
547
|
-
expect(res.status).toBe(200);
|
|
548
|
-
const status = res.body?.status;
|
|
549
|
-
expect(['ok', 'up']).toContain(status);
|
|
550
|
-
});
|
|
551
|
-
});
|
|
552
|
-
|
|
553
|
-
// ---------------------------------------------------------------------------
|
|
554
|
-
// AC-23: HealthModule.register({ readiness: [] }) app serves GET /health/ready with 200 and status ok
|
|
555
|
-
// ---------------------------------------------------------------------------
|
|
556
|
-
describe('AC-23: HealthModule.register({ readiness: [] }) app serves GET /health/ready returning 200 status ok', () => {
|
|
557
|
-
let app: INestApplication;
|
|
558
|
-
|
|
559
|
-
beforeAll(async () => {
|
|
560
|
-
const moduleRef = await Test.createTestingModule({
|
|
561
|
-
imports: [HealthModule.register({ readiness: [] })],
|
|
562
|
-
}).compile();
|
|
563
|
-
|
|
564
|
-
app = moduleRef.createNestApplication();
|
|
565
|
-
await app.init();
|
|
566
|
-
});
|
|
567
|
-
|
|
568
|
-
afterAll(async () => {
|
|
569
|
-
await app?.close();
|
|
570
|
-
});
|
|
571
|
-
|
|
572
|
-
it('GET /health/ready returns 200 and body status ok', async () => {
|
|
573
|
-
const supertest = require('supertest');
|
|
574
|
-
const res = await supertest(app.getHttpServer()).get('/health/ready');
|
|
575
|
-
expect(res.status).toBe(200);
|
|
576
|
-
expect(res.body?.status).toBe('ok');
|
|
577
|
-
});
|
|
578
|
-
});
|
|
579
|
-
|
|
580
|
-
// ---------------------------------------------------------------------------
|
|
581
|
-
// AC-24: HealthModule with CacheHealthCheck + down source returns 503 on GET /health/ready
|
|
582
|
-
// ---------------------------------------------------------------------------
|
|
583
|
-
describe('AC-24: HealthModule with CacheHealthCheck and down CACHE_HEALTH_SOURCE returns 503', () => {
|
|
584
|
-
let app: INestApplication;
|
|
585
|
-
|
|
586
|
-
beforeAll(async () => {
|
|
587
|
-
const downSource = {
|
|
588
|
-
isHealthy: () => Promise.resolve({ healthy: false, connections: [] }),
|
|
589
|
-
};
|
|
590
|
-
|
|
591
|
-
const moduleRef = await Test.createTestingModule({
|
|
592
|
-
imports: [HealthModule.register({ readiness: [CacheHealthCheck] })],
|
|
593
|
-
providers: [
|
|
594
|
-
{ provide: CACHE_HEALTH_SOURCE, useValue: downSource },
|
|
595
|
-
],
|
|
596
|
-
}).compile();
|
|
597
|
-
|
|
598
|
-
app = moduleRef.createNestApplication();
|
|
599
|
-
await app.init();
|
|
600
|
-
});
|
|
601
|
-
|
|
602
|
-
afterAll(async () => {
|
|
603
|
-
await app?.close();
|
|
604
|
-
});
|
|
605
|
-
|
|
606
|
-
it('GET /health/ready returns 503 with a down cache entry', async () => {
|
|
607
|
-
const supertest = require('supertest');
|
|
608
|
-
const res = await supertest(app.getHttpServer()).get('/health/ready');
|
|
609
|
-
expect(res.status).toBe(503);
|
|
610
|
-
const body = res.body;
|
|
611
|
-
const cacheEntry = body?.error?.cache ?? body?.details?.cache;
|
|
612
|
-
expect(cacheEntry?.status).toBe('down');
|
|
613
|
-
});
|
|
614
|
-
});
|
|
615
|
-
|
|
616
|
-
// ---------------------------------------------------------------------------
|
|
617
|
-
// AC-25: HealthModule with custom controller paths serves those paths and 404 on defaults
|
|
618
|
-
// ---------------------------------------------------------------------------
|
|
619
|
-
describe('AC-25: HealthModule with custom controller paths serves custom paths and 404 on old paths', () => {
|
|
620
|
-
let app: INestApplication;
|
|
621
|
-
|
|
622
|
-
beforeAll(async () => {
|
|
623
|
-
const moduleRef = await Test.createTestingModule({
|
|
624
|
-
imports: [
|
|
625
|
-
HealthModule.register({
|
|
626
|
-
controller: { livenessPath: 'healthz', readinessPath: 'readyz' },
|
|
627
|
-
}),
|
|
628
|
-
],
|
|
629
|
-
}).compile();
|
|
630
|
-
|
|
631
|
-
app = moduleRef.createNestApplication();
|
|
632
|
-
await app.init();
|
|
633
|
-
});
|
|
634
|
-
|
|
635
|
-
afterAll(async () => {
|
|
636
|
-
await app?.close();
|
|
637
|
-
});
|
|
638
|
-
|
|
639
|
-
it('GET /healthz returns 200', async () => {
|
|
640
|
-
const supertest = require('supertest');
|
|
641
|
-
const res = await supertest(app.getHttpServer()).get('/healthz');
|
|
642
|
-
expect(res.status).toBe(200);
|
|
643
|
-
});
|
|
644
|
-
|
|
645
|
-
it('GET /health/live returns 404', async () => {
|
|
646
|
-
const supertest = require('supertest');
|
|
647
|
-
const res = await supertest(app.getHttpServer()).get('/health/live');
|
|
648
|
-
expect(res.status).toBe(404);
|
|
649
|
-
});
|
|
650
|
-
|
|
651
|
-
it('GET /readyz returns 200', async () => {
|
|
652
|
-
const supertest = require('supertest');
|
|
653
|
-
const res = await supertest(app.getHttpServer()).get('/readyz');
|
|
654
|
-
expect(res.status).toBe(200);
|
|
655
|
-
});
|
|
656
|
-
});
|
|
657
|
-
|
|
658
|
-
// ---------------------------------------------------------------------------
|
|
659
|
-
// AC-26: HealthModule with controller: false binds no routes but HealthExecutorService resolves
|
|
660
|
-
// ---------------------------------------------------------------------------
|
|
661
|
-
describe('AC-26: HealthModule with controller: false exposes no routes but HealthExecutorService is available', () => {
|
|
662
|
-
let app: INestApplication;
|
|
663
|
-
|
|
664
|
-
beforeAll(async () => {
|
|
665
|
-
const moduleRef = await Test.createTestingModule({
|
|
666
|
-
imports: [HealthModule.register({ controller: false })],
|
|
667
|
-
}).compile();
|
|
668
|
-
|
|
669
|
-
app = moduleRef.createNestApplication();
|
|
670
|
-
await app.init();
|
|
671
|
-
});
|
|
672
|
-
|
|
673
|
-
afterAll(async () => {
|
|
674
|
-
await app?.close();
|
|
675
|
-
});
|
|
676
|
-
|
|
677
|
-
it('GET /health/live returns 404', async () => {
|
|
678
|
-
const supertest = require('supertest');
|
|
679
|
-
const res = await supertest(app.getHttpServer()).get('/health/live');
|
|
680
|
-
expect(res.status).toBe(404);
|
|
681
|
-
});
|
|
682
|
-
|
|
683
|
-
it('HealthExecutorService is resolvable from the module', async () => {
|
|
684
|
-
const moduleRef = await Test.createTestingModule({
|
|
685
|
-
imports: [HealthModule.register({ controller: false })],
|
|
686
|
-
}).compile();
|
|
687
|
-
|
|
688
|
-
const executor = moduleRef.get(HealthExecutorService);
|
|
689
|
-
expect(executor).toBeDefined();
|
|
690
|
-
expect(executor).toBeInstanceOf(HealthExecutorService);
|
|
691
|
-
});
|
|
692
|
-
});
|
|
693
|
-
|
|
694
|
-
// ---------------------------------------------------------------------------
|
|
695
|
-
// AC-27: HealthModule with CacheHealthCheck but no source token still boots;
|
|
696
|
-
// GET /health/ready returns 503 with cache entry down
|
|
697
|
-
// ---------------------------------------------------------------------------
|
|
698
|
-
describe('AC-27: HealthModule with CacheHealthCheck and no source token boots and returns 503', () => {
|
|
699
|
-
let app: INestApplication;
|
|
700
|
-
|
|
701
|
-
beforeAll(async () => {
|
|
702
|
-
const moduleRef = await Test.createTestingModule({
|
|
703
|
-
imports: [HealthModule.register({ readiness: [CacheHealthCheck] })],
|
|
704
|
-
// No CACHE_HEALTH_SOURCE provider — source is absent
|
|
705
|
-
}).compile();
|
|
706
|
-
|
|
707
|
-
app = moduleRef.createNestApplication();
|
|
708
|
-
await app.init();
|
|
709
|
-
});
|
|
710
|
-
|
|
711
|
-
afterAll(async () => {
|
|
712
|
-
await app?.close();
|
|
713
|
-
});
|
|
714
|
-
|
|
715
|
-
it('module initializes without throwing and GET /health/ready returns 503 with cache down', async () => {
|
|
716
|
-
const supertest = require('supertest');
|
|
717
|
-
const res = await supertest(app.getHttpServer()).get('/health/ready');
|
|
718
|
-
expect(res.status).toBe(503);
|
|
719
|
-
const body = res.body;
|
|
720
|
-
const cacheEntry = body?.error?.cache ?? body?.details?.cache;
|
|
721
|
-
expect(cacheEntry).toBeDefined();
|
|
722
|
-
const isDown = cacheEntry?.status === 'down' || cacheEntry?.healthy === false;
|
|
723
|
-
expect(isDown).toBe(true);
|
|
724
|
-
});
|
|
725
|
-
});
|
|
726
|
-
|
|
727
|
-
// ---------------------------------------------------------------------------
|
|
728
|
-
// AC-28: HealthModule.registerAsync with useFactory serves GET /health/ready with 200
|
|
729
|
-
// ---------------------------------------------------------------------------
|
|
730
|
-
describe('AC-28: HealthModule.registerAsync with useFactory produces app serving GET /health/ready with 200', () => {
|
|
731
|
-
let app: INestApplication;
|
|
732
|
-
|
|
733
|
-
beforeAll(async () => {
|
|
734
|
-
const moduleRef = await Test.createTestingModule({
|
|
735
|
-
imports: [
|
|
736
|
-
HealthModule.registerAsync({
|
|
737
|
-
useFactory: () => Promise.resolve({ readiness: [] }),
|
|
738
|
-
}),
|
|
739
|
-
],
|
|
740
|
-
}).compile();
|
|
741
|
-
|
|
742
|
-
app = moduleRef.createNestApplication();
|
|
743
|
-
await app.init();
|
|
744
|
-
});
|
|
745
|
-
|
|
746
|
-
afterAll(async () => {
|
|
747
|
-
await app?.close();
|
|
748
|
-
});
|
|
749
|
-
|
|
750
|
-
it('GET /health/ready returns 200 and body status ok', async () => {
|
|
751
|
-
const supertest = require('supertest');
|
|
752
|
-
const res = await supertest(app.getHttpServer()).get('/health/ready');
|
|
753
|
-
expect(res.status).toBe(200);
|
|
754
|
-
expect(res.body?.status).toBe('ok');
|
|
755
|
-
});
|
|
756
|
-
});
|