Files
accounted/app/api/health/__tests__/route.test.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

71 lines
2.3 KiB
TypeScript

/**
* /api/health: the version field is the build identifier (commit SHA prefix)
* when one is inlined at build, and the '1.0.0' fallback otherwise, so
* self-hosted Docker healthchecks keep a stable value.
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
let selectResult: { error: { code?: string; message?: string } | null } = { error: null }
vi.mock('@/lib/supabase/service-client', () => ({
createServiceRoleClient: () => ({
from: () => ({
select: () => ({
limit: async () => selectResult,
}),
}),
}),
}))
vi.mock('@/lib/logger', () => ({
createLogger: () => ({ error: vi.fn(), warn: vi.fn(), info: vi.fn(), debug: vi.fn() }),
}))
async function loadRoute() {
vi.resetModules()
return import('../route')
}
beforeEach(() => {
selectResult = { error: null }
vi.stubEnv('NEXT_PUBLIC_SUPABASE_URL', 'https://example.supabase.co')
vi.stubEnv('SUPABASE_SERVICE_ROLE_KEY', 'service-role-key')
vi.stubEnv('VERCEL_GIT_COMMIT_SHA', '')
vi.stubEnv('NEXT_PUBLIC_BUILD_ID', '')
})
afterEach(() => {
vi.unstubAllEnvs()
})
describe('GET /api/health', () => {
it('reports healthy with the 1.0.0 fallback when no build id is inlined', async () => {
const { GET } = await loadRoute()
const res = await GET()
expect(res.status).toBe(200)
const body = await res.json()
expect(body.status).toBe('healthy')
expect(body.version).toBe('1.0.0')
expect(typeof body.timestamp).toBe('string')
})
it('reports the commit SHA prefix as version when VERCEL_GIT_COMMIT_SHA is set', async () => {
vi.stubEnv('VERCEL_GIT_COMMIT_SHA', 'abcdef1234567890abcdef1234567890abcdef12')
const { GET } = await loadRoute()
const body = await (await GET()).json()
expect(body.status).toBe('healthy')
expect(body.version).toBe('abcdef123456')
})
it('keeps the same version on the unhealthy path', async () => {
vi.stubEnv('VERCEL_GIT_COMMIT_SHA', 'abcdef1234567890abcdef1234567890abcdef12')
selectResult = { error: { code: '57P01', message: 'terminating connection' } }
const { GET } = await loadRoute()
const res = await GET()
expect(res.status).toBe(503)
const body = await res.json()
expect(body.status).toBe('unhealthy')
expect(body.version).toBe('abcdef123456')
})
})