diff --git a/server/src/nest/app.module.ts b/server/src/nest/app.module.ts index e06396c4d..a91ccada9 100644 --- a/server/src/nest/app.module.ts +++ b/server/src/nest/app.module.ts @@ -4,8 +4,7 @@ import { ZodValidationPipe } from './common/zod-validation.pipe'; import { AppConfigModule } from './app-config/app-config.module'; import { DatabaseModule } from './database/database.module'; import { RealtimeModule } from './realtime/realtime.module'; -import { HealthController } from './health/health.controller'; -import { HealthService } from './health/health.service'; +import { HealthModule } from './health/health.module'; import { WeatherModule } from './weather/weather.module'; import { HelpModule } from './help/help.module'; import { AirportsModule } from './airports/airports.module'; @@ -61,10 +60,8 @@ import { IdempotencyInterceptor } from './common/idempotency.interceptor'; * migrated. */ @Module({ - imports: [AppConfigModule, DatabaseModule, RealtimeModule, McpModule.forRoot({ accessPolicy: trekMcpAccessPolicy, validateAccess: trekMcpValidateAccess }), WeatherModule, HelpModule, AirportsModule, ConfigModule, SystemNoticesModule, MapsModule, PlaceEnrichmentModule, CategoriesModule, TagsModule, NotificationsModule, AtlasModule, VacayModule, PackingModule, TodoModule, BudgetModule, ReservationsModule, DaysModule, AssignmentsModule, PlacesModule, TripsModule, CollabModule, FilesModule, PhotosModule, MemoriesModule, AirtrailModule, JourneyModule, CollectionsModule, ShareModule, TripInviteModule, TransitModule, FeedsModule, SettingsModule, BackupModule, AuthModule, OidcModule, OauthModule, AdminModule, AddonsModule, AuditModule, PermissionsModule, PluginsModule, BookingImportModule, LlmParseModule], - controllers: [HealthController], + imports: [AppConfigModule, DatabaseModule, RealtimeModule, McpModule.forRoot({ accessPolicy: trekMcpAccessPolicy, validateAccess: trekMcpValidateAccess }), HealthModule, WeatherModule, HelpModule, AirportsModule, ConfigModule, SystemNoticesModule, MapsModule, PlaceEnrichmentModule, CategoriesModule, TagsModule, NotificationsModule, AtlasModule, VacayModule, PackingModule, TodoModule, BudgetModule, ReservationsModule, DaysModule, AssignmentsModule, PlacesModule, TripsModule, CollabModule, FilesModule, PhotosModule, MemoriesModule, AirtrailModule, JourneyModule, CollectionsModule, ShareModule, TripInviteModule, TransitModule, FeedsModule, SettingsModule, BackupModule, AuthModule, OidcModule, OauthModule, AdminModule, AddonsModule, AuditModule, PermissionsModule, PluginsModule, BookingImportModule, LlmParseModule], providers: [ - HealthService, // Global error-envelope normaliser (DI-registered so it also catches // framework-level exceptions like the not-found handler). { provide: APP_FILTER, useClass: TrekExceptionFilter }, diff --git a/server/src/nest/booking-import/booking-import.module.ts b/server/src/nest/booking-import/booking-import.module.ts index 8dd697a03..c4a6e9ac7 100644 --- a/server/src/nest/booking-import/booking-import.module.ts +++ b/server/src/nest/booking-import/booking-import.module.ts @@ -2,8 +2,7 @@ import { Module } from '@nestjs/common'; import { BookingImportController } from './booking-import.controller'; import { BookingImportService } from './booking-import.service'; import { ImportJobsService } from './import-jobs.service'; -import { KitineraryExtractorService } from './kitinerary-extractor.service'; -import { FeaturesController } from './features.controller'; +import { KitineraryExtractorModule } from './kitinerary-extractor.module'; import { LlmParseModule } from '../llm-parse/llm-parse.module'; import { ReservationsModule } from '../reservations/reservations.module'; import { PermissionsModule } from '../permissions/permissions.module'; @@ -13,8 +12,8 @@ import { MapsModule } from '../maps/maps.module'; import { PlacesModule } from '../places/places.module'; @Module({ - imports: [LlmParseModule, ReservationsModule, PermissionsModule, BudgetModule, AddonsModule, MapsModule, PlacesModule], - controllers: [BookingImportController, FeaturesController], - providers: [BookingImportService, KitineraryExtractorService, ImportJobsService], + imports: [KitineraryExtractorModule, LlmParseModule, ReservationsModule, PermissionsModule, BudgetModule, AddonsModule, MapsModule, PlacesModule], + controllers: [BookingImportController], + providers: [BookingImportService, ImportJobsService], }) export class BookingImportModule {} diff --git a/server/src/nest/booking-import/kitinerary-extractor.module.ts b/server/src/nest/booking-import/kitinerary-extractor.module.ts new file mode 100644 index 000000000..8cb88c244 --- /dev/null +++ b/server/src/nest/booking-import/kitinerary-extractor.module.ts @@ -0,0 +1,12 @@ +import { Module } from '@nestjs/common'; +import { KitineraryExtractorService } from './kitinerary-extractor.service'; + +/** The kitinerary binary probe on its own. It has no dependencies, and the + * feature-flag endpoint in health/ needs nothing else from booking-import — + * importing the whole domain there would drag llm-parse, reservations, budget, + * maps and places along for one `isAvailable()`. */ +@Module({ + providers: [KitineraryExtractorService], + exports: [KitineraryExtractorService], +}) +export class KitineraryExtractorModule {} diff --git a/server/src/nest/booking-import/features.controller.ts b/server/src/nest/health/features.controller.ts similarity index 89% rename from server/src/nest/booking-import/features.controller.ts rename to server/src/nest/health/features.controller.ts index fd7507852..19abdb254 100644 --- a/server/src/nest/booking-import/features.controller.ts +++ b/server/src/nest/health/features.controller.ts @@ -1,5 +1,5 @@ import { Controller, Get } from '@nestjs/common'; -import { KitineraryExtractorService } from './kitinerary-extractor.service'; +import { KitineraryExtractorService } from '../booking-import/kitinerary-extractor.service'; import { AddonsService } from '../addons/addons.service'; import { ADDON_IDS } from '../../addons'; diff --git a/server/src/nest/health/health.controller.ts b/server/src/nest/health/health.controller.ts deleted file mode 100644 index 63db5cd23..000000000 --- a/server/src/nest/health/health.controller.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common'; -import { z } from 'zod'; -import { createZodDto } from 'nestjs-zod'; -import type { User } from '../../types'; -import { HealthService } from './health.service'; -import { JwtAuthGuard } from '../auth/jwt-auth.guard'; -import { CurrentUser } from '../auth/current-user.decorator'; - -// Local demo schema (real domains import their schema from @trek/shared and -// wrap it in a colocated .dto.ts). The DTO class is what the global -// ZodValidationPipe (APP_PIPE) discovers by metatype. -const echoSchema = z.object({ name: z.string().min(1) }); -class EchoDto extends createZodDto(echoSchema) {} - -/** - * Foundation smoke endpoints for the co-hosted NestJS app. - * Proves: boot, routing, type-based DI, the shared SQLite connection, the - * JWT-cookie auth guard, and the Zod validation pipe + error-envelope parity. - * - * Lives under /api/_nest/* so it never collides with the legacy Express API. - */ -@Controller('api/_nest') -export class HealthController { - constructor(private readonly healthService: HealthService) {} - - @Get('health') - getHealth() { - return { ok: true, ...this.healthService.info() }; - } - - /** Guarded: returns the authenticated user, proving JwtAuthGuard + @CurrentUser. */ - @Get('me') - @UseGuards(JwtAuthGuard) - me(@CurrentUser() user: User) { - return user; - } - - /** Validated: proves the global Zod pipe (400 + { error } on failure) and body parsing. */ - @Post('echo') - @UseGuards(JwtAuthGuard) - echo(@Body() body: EchoDto) { - return { youSent: body }; - } -} diff --git a/server/src/nest/health/health.module.ts b/server/src/nest/health/health.module.ts new file mode 100644 index 000000000..5693c1bbe --- /dev/null +++ b/server/src/nest/health/health.module.ts @@ -0,0 +1,15 @@ +import { Module } from '@nestjs/common'; +import { FeaturesController } from './features.controller'; +import { KitineraryExtractorModule } from '../booking-import/kitinerary-extractor.module'; +import { AddonsModule } from '../addons/addons.module'; + +/** Server capability reporting. `GET /api/health/features` tells the client which + * optional server-side features are usable, so it can hide the affordances it + * cannot back. `GET /api/health` itself is the container probe and stays on the + * raw express layer (`platform.routes.ts`) — it must answer before Nest routing + * and before the HTTPS redirect. */ +@Module({ + imports: [KitineraryExtractorModule, AddonsModule], + controllers: [FeaturesController], +}) +export class HealthModule {} diff --git a/server/src/nest/health/health.service.ts b/server/src/nest/health/health.service.ts deleted file mode 100644 index 4e5b6335c..000000000 --- a/server/src/nest/health/health.service.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { DatabaseService } from '../database/database.service'; - -/** - * Smoke service proving NestJS DI works under the chosen runtime AND that the - * injected DatabaseService talks to TREK's existing SQLite connection. - */ -@Injectable() -export class HealthService { - constructor(private readonly database: DatabaseService) {} - - info() { - const row = this.database.get<{ n: number }>('SELECT COUNT(*) AS n FROM users'); - return { - runtime: 'nestjs', - diInjected: true, - // Proof the shared connection works: real row count from the existing DB. - userCount: row?.n ?? null, - }; - } -} diff --git a/server/tests/unit/nest/features.controller.test.ts b/server/tests/unit/nest/features.controller.test.ts new file mode 100644 index 000000000..b3dc0d226 --- /dev/null +++ b/server/tests/unit/nest/features.controller.test.ts @@ -0,0 +1,73 @@ +import { describe, it, expect, vi } from 'vitest'; +import { FeaturesController } from '../../../src/nest/health/features.controller'; +import { HealthModule } from '../../../src/nest/health/health.module'; +import { KitineraryExtractorModule } from '../../../src/nest/booking-import/kitinerary-extractor.module'; +import { KitineraryExtractorService } from '../../../src/nest/booking-import/kitinerary-extractor.service'; +import { BookingImportModule } from '../../../src/nest/booking-import/booking-import.module'; +import { expectRegisteredController, expectRegisteredProvider } from '../../helpers/module-providers'; +import { ADDON_IDS } from '../../../src/addons'; + +type Extractor = Pick; +type Addons = { isAddonEnabled: (id: string) => boolean }; + +function make(available: boolean, aiEnabled: boolean) { + const extractor = { isAvailable: vi.fn(() => available) }; + const addons = { isAddonEnabled: vi.fn(() => aiEnabled) }; + return { + extractor, + addons, + controller: new FeaturesController(extractor as Extractor as KitineraryExtractorService, addons as never), + }; +} + +describe('FeaturesController (GET /api/health/features)', () => { + it('FEAT-001: reports both flags on', () => { + const { controller } = make(true, true); + expect(controller.features()).toEqual({ bookingImport: true, aiParsing: true }); + }); + + it('FEAT-002: reports both flags off', () => { + const { controller } = make(false, false); + expect(controller.features()).toEqual({ bookingImport: false, aiParsing: false }); + }); + + it('FEAT-003: the two flags are independent', () => { + expect(make(true, false).controller.features()).toEqual({ bookingImport: true, aiParsing: false }); + expect(make(false, true).controller.features()).toEqual({ bookingImport: false, aiParsing: true }); + }); + + it('FEAT-004: aiParsing asks the addons service for the LLM parsing addon specifically', () => { + const { controller, addons } = make(true, true); + controller.features(); + expect(addons.isAddonEnabled).toHaveBeenCalledWith(ADDON_IDS.LLM_PARSING); + }); + + it('FEAT-005: bookingImport is read live, not cached at construction', () => { + const { controller, extractor } = make(false, false); + controller.features(); + extractor.isAvailable.mockReturnValue(true); + expect(controller.features().bookingImport).toBe(true); + expect(extractor.isAvailable).toHaveBeenCalledTimes(2); + }); + + it('FEAT-006: the route lives on the health module, not on booking-import', () => { + expectRegisteredController(HealthModule, FeaturesController); + const bookingControllers = Reflect.getMetadata('controllers', BookingImportModule) as unknown[]; + expect(bookingControllers).not.toContain(FeaturesController); + }); + + it('FEAT-007: the extractor is reachable from health without importing the booking-import domain', () => { + // The whole point of the leaf module: health depends on the probe, not on + // llm-parse/reservations/budget/maps/places behind BookingImportModule. + expectRegisteredProvider(KitineraryExtractorModule, KitineraryExtractorService); + const healthImports = Reflect.getMetadata('imports', HealthModule) as unknown[]; + expect(Array.isArray(healthImports)).toBe(true); + expect(healthImports).toEqual(expect.arrayContaining([KitineraryExtractorModule])); + expect(healthImports).not.toContain(BookingImportModule); + }); + + it('FEAT-008: the controller keeps its public path', () => { + expect(Reflect.getMetadata('path', FeaturesController)).toBe('api/health'); + expect(Reflect.getMetadata('path', FeaturesController.prototype.features)).toBe('features'); + }); +}); diff --git a/server/tests/unit/nest/health.controller.test.ts b/server/tests/unit/nest/health.controller.test.ts deleted file mode 100644 index a18d9a75a..000000000 --- a/server/tests/unit/nest/health.controller.test.ts +++ /dev/null @@ -1,65 +0,0 @@ -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 { - 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(); - }); -});