Files
TREK/server/tests/unit/nest/health.controller.test.ts
T
Maurice 7266ad99ae Restore nest coverage to >=80% after the #1209 dep bump (istanbul provider + branch tests) (#1213)
* fix(server): set oxc:false in vitest so the SWC transform survives the Vite 8 bump

* fix(server): switch coverage to the istanbul provider (v8 under-reports branches on Vite 8 + Vitest 4)

* test(nest): cover controller/service branches to clear the 80% coverage gate
2026-06-16 21:36:39 +02:00

66 lines
2.2 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import { HealthController } from '../../../src/nest/health/health.controller';
import { HealthService } from '../../../src/nest/health/health.service';
import { DatabaseService } from '../../../src/nest/database/database.service';
import type { User } from '../../../src/types';
const user = { id: 1, role: 'user', email: 'u@example.test' } as User;
function makeService(overrides: Partial<HealthService> = {}): HealthService {
return {
info: vi.fn().mockReturnValue({ runtime: 'nestjs', diInjected: true, userCount: 0 }),
...overrides,
} as unknown as HealthService;
}
describe('HealthController (foundation smoke endpoints under /api/_nest)', () => {
it('GET /health merges ok:true with the service info', () => {
const svc = makeService({
info: vi.fn().mockReturnValue({ runtime: 'nestjs', diInjected: true, userCount: 7 }),
});
expect(new HealthController(svc).getHealth()).toEqual({
ok: true,
runtime: 'nestjs',
diInjected: true,
userCount: 7,
});
});
it('GET /me returns the authenticated user as-is', () => {
const svc = makeService();
expect(new HealthController(svc).me(user)).toBe(user);
});
it('POST /echo wraps the validated body', () => {
const svc = makeService();
expect(new HealthController(svc).echo({ name: 'Maurice' })).toEqual({
youSent: { name: 'Maurice' },
});
});
});
describe('HealthService.info (shared SQLite connection proof)', () => {
function makeDb(get: () => unknown): DatabaseService {
return { get: vi.fn(get) } as unknown as DatabaseService;
}
it('returns the real user count when the row resolves', () => {
const service = new HealthService(makeDb(() => ({ n: 42 })));
expect(service.info()).toEqual({
runtime: 'nestjs',
diInjected: true,
userCount: 42,
});
});
it('falls back to null when the row is undefined', () => {
const service = new HealthService(makeDb(() => undefined));
expect(service.info().userCount).toBeNull();
});
it('falls back to null when the count column is null', () => {
const service = new HealthService(makeDb(() => ({ n: null })));
expect(service.info().userCount).toBeNull();
});
});