Files
stack/apps/api/src/app.controller.test.ts
Jason Woltje dd747a1d87 feat(#3): Add comprehensive tests and improve Prisma seed script
- Create comprehensive test suite for PrismaService (10 tests)
- Fix AppController tests with proper PrismaService mocking
- Wrap seed operations in transaction for atomicity
- Replace N+1 pattern with batch operations (createMany)
- Add concurrency warning to seed script
- All tests passing (14/14)
- Build successful
- Test coverage >85%

Fixes #3

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-28 16:24:25 -06:00

64 lines
2.0 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { AppService } from "./app.service";
import { AppController } from "./app.controller";
import { PrismaService } from "./prisma/prisma.service";
describe("AppController", () => {
const appService = new AppService();
// Mock PrismaService
const mockPrismaService = {
isHealthy: vi.fn(),
getConnectionInfo: vi.fn(),
$connect: vi.fn(),
$disconnect: vi.fn(),
} as unknown as PrismaService;
const controller = new AppController(appService, mockPrismaService);
describe("getHello", () => {
it('should return "Mosaic Stack API"', () => {
expect(controller.getHello()).toBe("Mosaic Stack API");
});
});
describe("getHealth", () => {
it("should return health status", async () => {
// Setup mocks
vi.mocked(mockPrismaService.isHealthy).mockResolvedValue(true);
vi.mocked(mockPrismaService.getConnectionInfo).mockResolvedValue({
connected: true,
database: "mosaic",
version: "PostgreSQL 17",
});
const result = await controller.getHealth();
expect(result.success).toBe(true);
expect(result.data.status).toBe("healthy");
expect(result.data.timestamp).toBeDefined();
expect(result.data.checks.database.status).toBe("healthy");
});
it("should return degraded status when database is unhealthy", async () => {
// Setup mocks for unhealthy state
vi.mocked(mockPrismaService.isHealthy).mockResolvedValue(false);
vi.mocked(mockPrismaService.getConnectionInfo).mockResolvedValue({
connected: false,
});
const result = await controller.getHealth();
expect(result.success).toBe(true);
expect(result.data.status).toBe("degraded");
expect(result.data.checks.database.status).toBe("unhealthy");
});
});
});
describe("AppService", () => {
const service = new AppService();
it('should return "Mosaic Stack API"', () => {
expect(service.getHello()).toBe("Mosaic Stack API");
});
});