"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const testing_1 = require("@nestjs/testing");
const health_controller_1 = require("../health.controller");
const terminus_1 = require("@nestjs/terminus");
describe('HealthController', () => {
    let controller;
    let healthCheckService;
    let typeOrmHealthIndicator;
    beforeEach(async () => {
        const module = await testing_1.Test.createTestingModule({
            controllers: [health_controller_1.HealthController],
            providers: [
                {
                    provide: terminus_1.HealthCheckService,
                    useValue: {
                        check: jest.fn(),
                    },
                },
                {
                    provide: terminus_1.TypeOrmHealthIndicator,
                    useValue: {
                        pingCheck: jest.fn(),
                    },
                },
            ],
        }).compile();
        controller = module.get(health_controller_1.HealthController);
        healthCheckService = module.get(terminus_1.HealthCheckService);
        typeOrmHealthIndicator = module.get(terminus_1.TypeOrmHealthIndicator);
    });
    afterEach(() => {
        jest.clearAllMocks();
    });
    describe('check', () => {
        it('should return health check result', async () => {
            const mockResult = {
                status: 'ok',
                info: { database: { status: 'up' } },
                error: {},
                details: { database: { status: 'up' } },
            };
            healthCheckService.check.mockResolvedValue(mockResult);
            const result = await controller.check();
            expect(result).toEqual(mockResult);
            expect(healthCheckService.check).toHaveBeenCalled();
        });
        it('should check database health', async () => {
            healthCheckService.check.mockImplementation(async (checks) => {
                for (const check of checks) {
                    await check();
                }
                return { status: 'ok', info: {}, error: {}, details: {} };
            });
            typeOrmHealthIndicator.pingCheck.mockResolvedValue({ database: { status: 'up' } });
            await controller.check();
            expect(typeOrmHealthIndicator.pingCheck).toHaveBeenCalledWith('database');
        });
    });
    describe('live', () => {
        it('should return liveness status', () => {
            const result = controller.live();
            expect(result.status).toBe('ok');
            expect(result.timestamp).toBeDefined();
        });
        it('should include current timestamp', () => {
            const before = new Date().getTime();
            const result = controller.live();
            const after = new Date().getTime();
            const timestamp = new Date(result.timestamp).getTime();
            expect(timestamp).toBeGreaterThanOrEqual(before);
            expect(timestamp).toBeLessThanOrEqual(after);
        });
    });
    describe('ready', () => {
        it('should return readiness check result', async () => {
            const mockResult = {
                status: 'ok',
                info: { database: { status: 'up' } },
                error: {},
                details: { database: { status: 'up' } },
            };
            healthCheckService.check.mockResolvedValue(mockResult);
            const result = await controller.ready();
            expect(result).toEqual(mockResult);
            expect(healthCheckService.check).toHaveBeenCalled();
        });
        it('should check database for readiness', async () => {
            healthCheckService.check.mockImplementation(async (checks) => {
                for (const check of checks) {
                    await check();
                }
                return { status: 'ok', info: {}, error: {}, details: {} };
            });
            typeOrmHealthIndicator.pingCheck.mockResolvedValue({ database: { status: 'up' } });
            await controller.ready();
            expect(typeOrmHealthIndicator.pingCheck).toHaveBeenCalledWith('database');
        });
    });
});
//# sourceMappingURL=health.controller.spec.js.map