diff --git a/apps/backend/lambdas/AGENTS.md b/apps/backend/lambdas/AGENTS.md index 6dcc52e8..175eda96 100644 --- a/apps/backend/lambdas/AGENTS.md +++ b/apps/backend/lambdas/AGENTS.md @@ -50,7 +50,7 @@ export const getDonor: RouteHandler = async ({ params }) => { return json(200, { ... }); }; ``` -- `dispatch()` handles OPTIONS preflight, `GET //health`, 404 and 500 centrally — routes.ts only lists real endpoints. +- `dispatch()` handles OPTIONS preflight, `GET //health` (after awaiting `warmup`), 404 and 500 centrally — routes.ts only lists real endpoints. - **A caught error is an error Sentry cannot see.** The Sentry layer wraps the handler and records uncaught throws only, and we always catch so API Gateway returns a JSON 500 instead of a 502. `dispatch()` reports what reaches its catch; a controller that returns its own 500 must use `serverError(err, 'Failed to …')` from `@branch/lambda-http` rather than `console.error` + `json(500, …)`. Same status to the caller, one less invisible failure. - **NEVER remove or modify the `ROUTES-START` / `ROUTES-END` markers** — the CLI inserts new table entries between them. - Patterns are always full-prefixed (`/donors/:id`, never `/:id`) so one table works whether the event arrives via API Gateway's `{proxy+}` (full path) or the shared dev-server (prefix stripped) — `dispatch()` canonicalizes both to the prefixed form. @@ -84,6 +84,8 @@ export const onImpersonatedWrite = recordImpersonationAction; // from @branch/st A route that acts on the caller's own Cognito session (MFA, today) must declare `impersonation: 'blocked'`: while impersonating, that session is the staff account's. +`auth.ts` also exports `warmup = () => warmAuth(db)` (Cognito JWKS + DB connection). `handler.ts` wraps it in `prewarm()` at module scope, so the work starts during Lambda init, and passes it to `dispatch` as `warmup`. `GET //health` waits for it, and the frontend pings every service's `/health` on page load (`warmServices` in `apps/frontend/src/lib/api.ts`) to absorb cold starts early. + ### Two layers, and which one to use 1. **Route level** — declared in `routes.ts`, enforced by `dispatch` before the diff --git a/apps/backend/lambdas/auth/auth.ts b/apps/backend/lambdas/auth/auth.ts index 74ab0e8e..98be497e 100644 --- a/apps/backend/lambdas/auth/auth.ts +++ b/apps/backend/lambdas/auth/auth.ts @@ -1,4 +1,8 @@ -import { authenticateRequest as _authenticateRequest, loadRbacSubject } from '@branch/lambda-auth'; +import { + authenticateRequest as _authenticateRequest, + loadRbacSubject, + warmAuth, +} from '@branch/lambda-auth'; import { createAuthResolver } from '@branch/lambda-http'; import { db, recordImpersonationAction } from '@branch/store'; @@ -19,3 +23,5 @@ export const resolveAuth = createAuthResolver(authenticateRequest, (context) => ); export const onImpersonatedWrite = recordImpersonationAction; + +export const warmup = () => warmAuth(db); diff --git a/apps/backend/lambdas/auth/handler.ts b/apps/backend/lambdas/auth/handler.ts index 8918ec3d..8f85bc39 100644 --- a/apps/backend/lambdas/auth/handler.ts +++ b/apps/backend/lambdas/auth/handler.ts @@ -1,6 +1,9 @@ -import { dispatch } from '@branch/lambda-http'; -import { onImpersonatedWrite, resolveAuth } from './auth'; +import { dispatch, prewarm } from '@branch/lambda-http'; +import { onImpersonatedWrite, resolveAuth, warmup } from './auth'; import { routes } from './routes'; +// Module scope, so it starts during init rather than on the first request. +const warm = prewarm(warmup); + export const handler = (event: any) => - dispatch(event, { prefix: 'auth', routes, resolveAuth, onImpersonatedWrite }); + dispatch(event, { prefix: 'auth', routes, resolveAuth, onImpersonatedWrite, warmup: warm }); diff --git a/apps/backend/lambdas/donors/auth.ts b/apps/backend/lambdas/donors/auth.ts index 74ab0e8e..98be497e 100644 --- a/apps/backend/lambdas/donors/auth.ts +++ b/apps/backend/lambdas/donors/auth.ts @@ -1,4 +1,8 @@ -import { authenticateRequest as _authenticateRequest, loadRbacSubject } from '@branch/lambda-auth'; +import { + authenticateRequest as _authenticateRequest, + loadRbacSubject, + warmAuth, +} from '@branch/lambda-auth'; import { createAuthResolver } from '@branch/lambda-http'; import { db, recordImpersonationAction } from '@branch/store'; @@ -19,3 +23,5 @@ export const resolveAuth = createAuthResolver(authenticateRequest, (context) => ); export const onImpersonatedWrite = recordImpersonationAction; + +export const warmup = () => warmAuth(db); diff --git a/apps/backend/lambdas/donors/handler.ts b/apps/backend/lambdas/donors/handler.ts index 5b47cf7c..70363c8e 100644 --- a/apps/backend/lambdas/donors/handler.ts +++ b/apps/backend/lambdas/donors/handler.ts @@ -1,6 +1,9 @@ -import { dispatch } from '@branch/lambda-http'; -import { onImpersonatedWrite, resolveAuth } from './auth'; +import { dispatch, prewarm } from '@branch/lambda-http'; +import { onImpersonatedWrite, resolveAuth, warmup } from './auth'; import { routes } from './routes'; +// Module scope, so it starts during init rather than on the first request. +const warm = prewarm(warmup); + export const handler = (event: any) => - dispatch(event, { prefix: 'donors', routes, resolveAuth, onImpersonatedWrite }); + dispatch(event, { prefix: 'donors', routes, resolveAuth, onImpersonatedWrite, warmup: warm }); diff --git a/apps/backend/lambdas/expenditures/auth.ts b/apps/backend/lambdas/expenditures/auth.ts index 74ab0e8e..98be497e 100644 --- a/apps/backend/lambdas/expenditures/auth.ts +++ b/apps/backend/lambdas/expenditures/auth.ts @@ -1,4 +1,8 @@ -import { authenticateRequest as _authenticateRequest, loadRbacSubject } from '@branch/lambda-auth'; +import { + authenticateRequest as _authenticateRequest, + loadRbacSubject, + warmAuth, +} from '@branch/lambda-auth'; import { createAuthResolver } from '@branch/lambda-http'; import { db, recordImpersonationAction } from '@branch/store'; @@ -19,3 +23,5 @@ export const resolveAuth = createAuthResolver(authenticateRequest, (context) => ); export const onImpersonatedWrite = recordImpersonationAction; + +export const warmup = () => warmAuth(db); diff --git a/apps/backend/lambdas/expenditures/handler.ts b/apps/backend/lambdas/expenditures/handler.ts index 0002312d..cb08836d 100644 --- a/apps/backend/lambdas/expenditures/handler.ts +++ b/apps/backend/lambdas/expenditures/handler.ts @@ -1,6 +1,9 @@ -import { dispatch } from '@branch/lambda-http'; -import { onImpersonatedWrite, resolveAuth } from './auth'; +import { dispatch, prewarm } from '@branch/lambda-http'; +import { onImpersonatedWrite, resolveAuth, warmup } from './auth'; import { routes } from './routes'; +// Module scope, so it starts during init rather than on the first request. +const warm = prewarm(warmup); + export const handler = (event: any) => - dispatch(event, { prefix: 'expenditures', routes, resolveAuth, onImpersonatedWrite }); \ No newline at end of file + dispatch(event, { prefix: 'expenditures', routes, resolveAuth, onImpersonatedWrite, warmup: warm }); diff --git a/apps/backend/lambdas/projects/auth.ts b/apps/backend/lambdas/projects/auth.ts index 74ab0e8e..98be497e 100644 --- a/apps/backend/lambdas/projects/auth.ts +++ b/apps/backend/lambdas/projects/auth.ts @@ -1,4 +1,8 @@ -import { authenticateRequest as _authenticateRequest, loadRbacSubject } from '@branch/lambda-auth'; +import { + authenticateRequest as _authenticateRequest, + loadRbacSubject, + warmAuth, +} from '@branch/lambda-auth'; import { createAuthResolver } from '@branch/lambda-http'; import { db, recordImpersonationAction } from '@branch/store'; @@ -19,3 +23,5 @@ export const resolveAuth = createAuthResolver(authenticateRequest, (context) => ); export const onImpersonatedWrite = recordImpersonationAction; + +export const warmup = () => warmAuth(db); diff --git a/apps/backend/lambdas/projects/handler.ts b/apps/backend/lambdas/projects/handler.ts index 239e27fe..43d41456 100644 --- a/apps/backend/lambdas/projects/handler.ts +++ b/apps/backend/lambdas/projects/handler.ts @@ -1,6 +1,9 @@ -import { dispatch } from '@branch/lambda-http'; -import { onImpersonatedWrite, resolveAuth } from './auth'; +import { dispatch, prewarm } from '@branch/lambda-http'; +import { onImpersonatedWrite, resolveAuth, warmup } from './auth'; import { routes } from './routes'; +// Module scope, so it starts during init rather than on the first request. +const warm = prewarm(warmup); + export const handler = (event: any) => - dispatch(event, { prefix: 'projects', routes, resolveAuth, onImpersonatedWrite }); + dispatch(event, { prefix: 'projects', routes, resolveAuth, onImpersonatedWrite, warmup: warm }); diff --git a/apps/backend/lambdas/reports/auth.ts b/apps/backend/lambdas/reports/auth.ts index 74ab0e8e..98be497e 100644 --- a/apps/backend/lambdas/reports/auth.ts +++ b/apps/backend/lambdas/reports/auth.ts @@ -1,4 +1,8 @@ -import { authenticateRequest as _authenticateRequest, loadRbacSubject } from '@branch/lambda-auth'; +import { + authenticateRequest as _authenticateRequest, + loadRbacSubject, + warmAuth, +} from '@branch/lambda-auth'; import { createAuthResolver } from '@branch/lambda-http'; import { db, recordImpersonationAction } from '@branch/store'; @@ -19,3 +23,5 @@ export const resolveAuth = createAuthResolver(authenticateRequest, (context) => ); export const onImpersonatedWrite = recordImpersonationAction; + +export const warmup = () => warmAuth(db); diff --git a/apps/backend/lambdas/reports/handler.ts b/apps/backend/lambdas/reports/handler.ts index c5628074..670223c6 100644 --- a/apps/backend/lambdas/reports/handler.ts +++ b/apps/backend/lambdas/reports/handler.ts @@ -1,6 +1,9 @@ -import { dispatch } from '@branch/lambda-http'; -import { onImpersonatedWrite, resolveAuth } from './auth'; +import { dispatch, prewarm } from '@branch/lambda-http'; +import { onImpersonatedWrite, resolveAuth, warmup } from './auth'; import { routes } from './routes'; +// Module scope, so it starts during init rather than on the first request. +const warm = prewarm(warmup); + export const handler = (event: any) => - dispatch(event, { prefix: 'reports', routes, resolveAuth, onImpersonatedWrite }); + dispatch(event, { prefix: 'reports', routes, resolveAuth, onImpersonatedWrite, warmup: warm }); diff --git a/apps/backend/lambdas/users/auth.ts b/apps/backend/lambdas/users/auth.ts index 74ab0e8e..98be497e 100644 --- a/apps/backend/lambdas/users/auth.ts +++ b/apps/backend/lambdas/users/auth.ts @@ -1,4 +1,8 @@ -import { authenticateRequest as _authenticateRequest, loadRbacSubject } from '@branch/lambda-auth'; +import { + authenticateRequest as _authenticateRequest, + loadRbacSubject, + warmAuth, +} from '@branch/lambda-auth'; import { createAuthResolver } from '@branch/lambda-http'; import { db, recordImpersonationAction } from '@branch/store'; @@ -19,3 +23,5 @@ export const resolveAuth = createAuthResolver(authenticateRequest, (context) => ); export const onImpersonatedWrite = recordImpersonationAction; + +export const warmup = () => warmAuth(db); diff --git a/apps/backend/lambdas/users/handler.ts b/apps/backend/lambdas/users/handler.ts index ee5b3215..9dce09b1 100644 --- a/apps/backend/lambdas/users/handler.ts +++ b/apps/backend/lambdas/users/handler.ts @@ -1,6 +1,9 @@ -import { dispatch } from '@branch/lambda-http'; -import { onImpersonatedWrite, resolveAuth } from './auth'; +import { dispatch, prewarm } from '@branch/lambda-http'; +import { onImpersonatedWrite, resolveAuth, warmup } from './auth'; import { routes } from './routes'; +// Module scope, so it starts during init rather than on the first request. +const warm = prewarm(warmup); + export const handler = (event: any) => - dispatch(event, { prefix: 'users', routes, resolveAuth, onImpersonatedWrite }); + dispatch(event, { prefix: 'users', routes, resolveAuth, onImpersonatedWrite, warmup: warm }); diff --git a/apps/frontend/src/app/providers.tsx b/apps/frontend/src/app/providers.tsx index e39cd2da..65e3c306 100644 --- a/apps/frontend/src/app/providers.tsx +++ b/apps/frontend/src/app/providers.tsx @@ -1,12 +1,13 @@ 'use client'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { usePathname } from 'next/navigation'; import { classifyRoute } from '@/lib/routes'; import { ChakraProvider, defaultSystem } from '@chakra-ui/react'; import { QueryClientProvider } from '@tanstack/react-query'; import { AuthProvider } from '@/context/AuthContext'; import { makeQueryClient } from '@/lib/queryClient'; +import { warmServices } from '@/lib/api'; import AuthGate from './components/AuthGate'; import ImpersonationBanner from './components/ImpersonationBanner'; import RoutePrefetcher from './components/RoutePrefetcher'; @@ -25,6 +26,8 @@ export function Providers({ children }: { children: React.ReactNode }) { const [queryClient] = useState(makeQueryClient); const pathname = usePathname() ?? '/'; + useEffect(warmServices, []); + // Chakra's Emotion styles break hydration on prerendered open pages. if (classifyRoute(pathname) === 'open') return <>{children}; diff --git a/apps/frontend/src/lib/api.ts b/apps/frontend/src/lib/api.ts index 3134f0c2..ad8da9d7 100644 --- a/apps/frontend/src/lib/api.ts +++ b/apps/frontend/src/lib/api.ts @@ -15,6 +15,20 @@ function resolveBaseUrl(path: string): string { return `http://localhost:${port}`; } +let servicesWarmed = false; + +export function warmServices(): void { + if (servicesWarmed) return; + servicesWarmed = true; + for (const service of Object.keys(SERVICE_PORTS)) { + const path = `/${service}/health`; + // no-cors GET is a CORS simple request: no preflight. + fetch(`${resolveBaseUrl(path)}${path}`, { mode: 'no-cors' }).catch( + () => {}, + ); + } +} + /** * A failed HTTP response, carrying the status so callers can branch on it. * diff --git a/apps/frontend/test/lib/api.test.ts b/apps/frontend/test/lib/api.test.ts index b9422120..e7e3b7fb 100644 --- a/apps/frontend/test/lib/api.test.ts +++ b/apps/frontend/test/lib/api.test.ts @@ -49,3 +49,20 @@ describe('apiFetch', () => { await expect(apiFetch('/auth/login')).rejects.toThrow('Bad Request'); }); }); + +describe('warmServices', () => { + it('pings every service health endpoint once, without a preflight', async () => { + const { warmServices } = await import('@/lib/api'); + global.fetch = jest.fn().mockResolvedValue({} as Response); + warmServices(); + warmServices(); + + const calls = (global.fetch as jest.Mock).mock.calls; + expect(calls.map(([url]) => new URL(url as string).pathname).sort()).toEqual( + ['auth', 'donors', 'expenditures', 'projects', 'reports', 'users'].map( + (s) => `/${s}/health`, + ), + ); + for (const [, init] of calls) expect(init).toEqual({ mode: 'no-cors' }); + }); +}); diff --git a/shared/lambda-auth/src/authenticate.ts b/shared/lambda-auth/src/authenticate.ts index 3883aa50..e75b0865 100644 --- a/shared/lambda-auth/src/authenticate.ts +++ b/shared/lambda-auth/src/authenticate.ts @@ -38,6 +38,18 @@ function getVerifier() { return verifier; } +export async function warmAuth(db: QueryableDb): Promise { + await Promise.all([ + getVerifier().hydrate(), + db + .selectFrom('branch.users') + .select('user_id') + .where('deleted_at', 'is', null) + .limit(1) + .execute(), + ]); +} + export function extractToken(event: any): string | null { const authHeader = event.headers?.Authorization || event.headers?.authorization; if (!authHeader) return null; diff --git a/shared/lambda-auth/src/index.ts b/shared/lambda-auth/src/index.ts index 6791207c..2f1ad841 100644 --- a/shared/lambda-auth/src/index.ts +++ b/shared/lambda-auth/src/index.ts @@ -4,6 +4,7 @@ export { authenticateRequest, extractImpersonationSession, IMPERSONATION_HEADER, + warmAuth, } from './authenticate'; export { loadRbacSubject, preloadedSubject } from './rbac'; // The policy itself is re-exported so a lambda needs one import for auth and diff --git a/shared/lambda-auth/test/authenticate.test.ts b/shared/lambda-auth/test/authenticate.test.ts index f8dd4b72..4fc1a01a 100644 --- a/shared/lambda-auth/test/authenticate.test.ts +++ b/shared/lambda-auth/test/authenticate.test.ts @@ -360,6 +360,23 @@ describe('authenticateRequest', () => { }); }); +describe('warmAuth', () => { + it('hydrates the JWKS and opens the DB connection in parallel', async () => { + const hydrate = jest.fn().mockResolvedValue(undefined); + mockCreate.mockReturnValueOnce({ verify: mockVerify, hydrate } as any); + const execute = jest.fn().mockResolvedValue([]); + const chain = { select: () => chain, where: () => chain, limit: () => chain, execute }; + const db = { selectFrom: jest.fn(() => chain) }; + + const { warmAuth } = await loadModule(); + await warmAuth(db); + + expect(hydrate).toHaveBeenCalledTimes(1); + expect(db.selectFrom).toHaveBeenCalledWith('branch.users'); + expect(execute).toHaveBeenCalledTimes(1); + }); +}); + function makeImpersonationDb(users: any[], sessions: any[]) { const chain = (run: (filters: [string, unknown][]) => unknown[]) => { const filters: [string, unknown][] = []; diff --git a/shared/lambda-http/README.md b/shared/lambda-http/README.md index a1657814..f47a227a 100644 --- a/shared/lambda-http/README.md +++ b/shared/lambda-http/README.md @@ -56,7 +56,9 @@ the row is loaded. while impersonating is the staff account's. Every non-GET impersonated request, refused or not, is then handed to `onImpersonatedWrite`. Each lambda passes `@branch/store`'s `recordImpersonationAction`. -- OPTIONS preflight, `GET //health`, 404, and 500. +- OPTIONS preflight, `GET //health`, 404, and 500. Health awaits the + optional `warmup` first, so the frontend's load-time ping leaves a cold + container with its JWKS and DB connection ready. - CORS headers on every response, via `json`. - **Sentry reporting for the 500 path.** The Sentry Lambda layer wraps the handler and records *uncaught throws* only. Every lambda catches instead, so @@ -69,7 +71,8 @@ the row is loaded. | Export | Purpose | | --- | --- | -| `dispatch(event, { prefix, routes, resolveAuth, onImpersonatedWrite })` | Route an event; returns a response. | +| `dispatch(event, { prefix, routes, resolveAuth, onImpersonatedWrite, warmup })` | Route an event; returns a response. | +| `prewarm(warm)` | Start `warm` at module scope (Lambda only) and return the `warmup` health awaits. | | `json(status, body)` | JSON response with CORS headers. | | `parseBody(event)` | Parse a JSON body; `null` when malformed. | | `requirePermission(subject, action, resource?)` | 403 carrying the policy's own reason, or `undefined` when allowed. | diff --git a/shared/lambda-http/src/dispatch.ts b/shared/lambda-http/src/dispatch.ts index e5e26430..4c557cc4 100644 --- a/shared/lambda-http/src/dispatch.ts +++ b/shared/lambda-http/src/dispatch.ts @@ -22,7 +22,7 @@ import type { DispatchOptions, RequestAuth, Route } from './types'; */ export async function dispatch( event: any, - { prefix, routes, resolveAuth, onImpersonatedWrite }: DispatchOptions, + { prefix, routes, resolveAuth, onImpersonatedWrite, warmup }: DispatchOptions, ): Promise { // Hoisted out of the try so the catch can name the route that failed. const rawPath: string = event?.rawPath || event?.path || '/'; @@ -43,6 +43,7 @@ export async function dispatch( if (method === 'OPTIONS') return json(200, {}); if (path === `${base}/health` && method === 'GET') { + await warmup?.(); return json(200, { ok: true, timestamp: new Date().toISOString() }); } diff --git a/shared/lambda-http/src/index.ts b/shared/lambda-http/src/index.ts index a3b5ff0f..bca73914 100644 --- a/shared/lambda-http/src/index.ts +++ b/shared/lambda-http/src/index.ts @@ -5,3 +5,4 @@ export { dispatch } from './dispatch'; export { parseBody } from './body'; export { requirePermission, createAuthResolver, ANONYMOUS_AUTH } from './authz'; export { reportError, serverError } from './errors'; +export { prewarm } from './warmup'; diff --git a/shared/lambda-http/src/types.ts b/shared/lambda-http/src/types.ts index ca3c986e..9bb31d0d 100644 --- a/shared/lambda-http/src/types.ts +++ b/shared/lambda-http/src/types.ts @@ -72,4 +72,5 @@ export interface DispatchOptions { path: string, statusCode: number, ) => Promise; + warmup?: () => Promise; } diff --git a/shared/lambda-http/src/warmup.ts b/shared/lambda-http/src/warmup.ts new file mode 100644 index 00000000..d554090e --- /dev/null +++ b/shared/lambda-http/src/warmup.ts @@ -0,0 +1,9 @@ +// /health awaits this so Lambda does not freeze the container mid-warm-up. +export function prewarm(warm: () => Promise): () => Promise { + if (!process.env.AWS_LAMBDA_FUNCTION_NAME) return async () => {}; + const pending = warm().then( + () => undefined, + (err) => console.warn('Warm-up failed:', err), + ); + return () => pending; +} diff --git a/shared/lambda-http/test/dispatch.test.ts b/shared/lambda-http/test/dispatch.test.ts index acb20f08..62bd9486 100644 --- a/shared/lambda-http/test/dispatch.test.ts +++ b/shared/lambda-http/test/dispatch.test.ts @@ -113,6 +113,29 @@ describe('dispatch routing', () => { } }); + it('holds the health response until warmup settles', async () => { + let release!: () => void; + const warmup = jest.fn(() => new Promise((r) => (release = r))); + let settled = false; + const pending = dispatch(event('GET', '/projects/health'), { + prefix: 'projects', + routes, + warmup, + }).then((res) => ((settled = true), res)); + + await Promise.resolve(); + expect(warmup).toHaveBeenCalledTimes(1); + expect(settled).toBe(false); + release(); + expect((await pending).statusCode).toBe(200); + }); + + it('does not run warmup on ordinary routes', async () => { + const warmup = jest.fn(async () => {}); + await dispatch(event('GET', '/projects'), { ...opts(), warmup }); + expect(warmup).not.toHaveBeenCalled(); + }); + it('404s an unmatched path', async () => { const res = await dispatch(event('GET', '/projects/7/nope'), opts()); expect(res.statusCode).toBe(404); diff --git a/shared/lambda-http/test/warmup.test.ts b/shared/lambda-http/test/warmup.test.ts new file mode 100644 index 00000000..33f1094e --- /dev/null +++ b/shared/lambda-http/test/warmup.test.ts @@ -0,0 +1,34 @@ +import { prewarm } from '../src/warmup'; + +describe('prewarm', () => { + const original = process.env.AWS_LAMBDA_FUNCTION_NAME; + afterEach(() => { + if (original === undefined) delete process.env.AWS_LAMBDA_FUNCTION_NAME; + else process.env.AWS_LAMBDA_FUNCTION_NAME = original; + }); + + it('does nothing outside Lambda', async () => { + delete process.env.AWS_LAMBDA_FUNCTION_NAME; + const warm = jest.fn(async () => {}); + await prewarm(warm)(); + expect(warm).not.toHaveBeenCalled(); + }); + + it('starts immediately in Lambda and runs once', async () => { + process.env.AWS_LAMBDA_FUNCTION_NAME = 'branch-projects'; + const warm = jest.fn(async () => {}); + const run = prewarm(warm); + expect(warm).toHaveBeenCalledTimes(1); + await run(); + await run(); + expect(warm).toHaveBeenCalledTimes(1); + }); + + it('swallows a failure so health still answers', async () => { + process.env.AWS_LAMBDA_FUNCTION_NAME = 'branch-projects'; + const spy = jest.spyOn(console, 'warn').mockImplementation(() => {}); + await expect(prewarm(async () => Promise.reject(new Error('boom')))()).resolves.toBeUndefined(); + expect(spy).toHaveBeenCalled(); + spy.mockRestore(); + }); +});