Files
edr-platform/apps/edr-freight-api/src/modules/health/health.controller.spec.ts
Nathnael 0ac85ebc1f fix
2026-08-31 11:45:55 +00:00

101 lines
3.2 KiB
TypeScript

import 'reflect-metadata';
import type { Response } from 'express';
import type { DataSource } from 'typeorm';
import type { MatrixClient } from '../chat/matrix.client';
import type { EmailClientService } from '../notifications/email-client.service';
import type { SmsClientService } from '../notifications/sms-client.service';
import { HealthController } from './health.controller';
type ReadinessBody = {
status: string;
checks: {
chat: { status: string; enabled: boolean; actingAs?: string; error?: string };
};
};
/** Captures what the controller wrote, in place of an express Response. */
function recorder() {
const sent: { code?: number; body?: ReadinessBody } = {};
const res = {
status(code: number) {
sent.code = code;
return this;
},
json(body: ReadinessBody) {
sent.body = body;
return this;
},
};
return { sent, res: res as unknown as Response };
}
function controllerWith(matrix: Partial<MatrixClient>) {
const dataSource = { query: jest.fn(async () => [{ '?column?': 1 }]) };
return new HealthController(
dataSource as unknown as DataSource,
{ brokerConnected: true } as unknown as SmsClientService,
{ brokerConnected: true } as unknown as EmailClientService,
matrix as MatrixClient,
);
}
describe('HealthController readiness — chat check', () => {
it('reports degraded, not 503, when MATRIX_ADMIN_TOKEN is not a server admin', async () => {
// The dev outage. Chat is broken, but chat is not worth pulling the pod
// out of the load balancer for — bookings and billing still work.
const controller = controllerWith({
enabled: true,
adminCheck: jest.fn(async () => ({
ok: false,
actingAs: '@super-admin.f15347:matrixdev.edrsc.com',
error: 'Matrix GET /_synapse/admin/v2/users?limit=1 -> 403: not a server admin',
})),
});
const { sent, res } = recorder();
await controller.readiness(res);
expect(sent.code).toBe(200);
expect(sent.body?.status).toBe('degraded');
expect(sent.body?.checks.chat.status).toBe('error');
// The account name is the actionable half — it says *which* token is wired up.
expect(sent.body?.checks.chat.actingAs).toBe(
'@super-admin.f15347:matrixdev.edrsc.com',
);
});
it('reports ok when the token really is a server admin', async () => {
const controller = controllerWith({
enabled: true,
adminCheck: jest.fn(async () => ({
ok: true,
actingAs: '@edrbot:matrixdev.edrsc.com',
})),
});
const { sent, res } = recorder();
await controller.readiness(res);
expect(sent.body?.status).toBe('ok');
expect(sent.body?.checks.chat).toMatchObject({
status: 'ok',
enabled: true,
actingAs: '@edrbot:matrixdev.edrsc.com',
});
});
it('does not call Synapse, or degrade, when chat is switched off', async () => {
const adminCheck = jest.fn();
const controller = controllerWith({ enabled: false, adminCheck });
const { sent, res } = recorder();
await controller.readiness(res);
expect(adminCheck).not.toHaveBeenCalled();
expect(sent.body?.status).toBe('ok');
expect(sent.body?.checks.chat).toEqual({ status: 'unknown', enabled: false });
});
});