Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions apps/api/__tests__/integration/health.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,12 @@ describe('Health Endpoint', () => {
expect(response.body.checks).toBeDefined();
expect(response.body.checks.database).toBeDefined();
expect(response.body.checks.redis).toBeDefined();

expect(typeof response.body.ok).toBe('boolean');
expect(response.body.service).toBe('fieldview-api');
expect(typeof response.body.commit).toBe('string');
expect(typeof response.body.env).toBe('string');
expect(typeof response.body.utc).toBe('string');
expect(response.body.ok).toBe(response.body.status === 'healthy');
});
});
45 changes: 45 additions & 0 deletions apps/api/src/lib/__tests__/health-fleet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { resolveHealthCommit } from '../health-fleet';

const COMMIT_KEYS = [
'RAILWAY_GIT_COMMIT_SHA',
'VERCEL_GIT_COMMIT_SHA',
'GIT_COMMIT',
] as const;

describe('resolveHealthCommit', () => {
const saved: Partial<Record<(typeof COMMIT_KEYS)[number], string | undefined>> = {};

beforeEach(() => {
for (const key of COMMIT_KEYS) {
saved[key] = process.env[key];
delete process.env[key];
}
});

afterEach(() => {
for (const key of COMMIT_KEYS) {
const value = saved[key];
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
}
});

it('returns RAILWAY_GIT_COMMIT_SHA when set', () => {
process.env.RAILWAY_GIT_COMMIT_SHA = 'abc123fullsha';
expect(resolveHealthCommit()).toBe('abc123fullsha');
});

it('returns unknown when all commit env vars are unset', () => {
expect(resolveHealthCommit()).toBe('unknown');
});

it('falls back to GIT_COMMIT when Railway and Vercel are unset', () => {
process.env.GIT_COMMIT = 'localsha';
expect(resolveHealthCommit()).toBe('localsha');
});
});
30 changes: 30 additions & 0 deletions apps/api/src/lib/health-fleet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/** Fleet health contract: build identity fields for GET /health. */

export function resolveHealthCommit(): string {
return (
process.env.RAILWAY_GIT_COMMIT_SHA ??
process.env.VERCEL_GIT_COMMIT_SHA ??
process.env.GIT_COMMIT ??
'unknown'
);
}

export function resolveHealthEnv(): string {
return process.env.APP_ENV ?? process.env.RAILWAY_ENVIRONMENT_NAME ?? 'dev';
}

export function buildFleetHealthFields(ok: boolean, service: string): {
ok: boolean;
service: string;
commit: string;
env: string;
utc: string;
} {
return {
ok,
service,
commit: resolveHealthCommit(),
env: resolveHealthEnv(),
utc: new Date().toISOString(),
};
}
2 changes: 2 additions & 0 deletions apps/api/src/routes/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import express, { type Request, Response, Router } from 'express';

import { buildFleetHealthFields } from '../lib/health-fleet';
import { prisma } from '../lib/prisma';
import { redisClient } from '../lib/redis';

Expand Down Expand Up @@ -90,6 +91,7 @@ export function createHealthRouter(): Router {
timestamp: new Date().toISOString(),
checks,
email: checkEmailProvider(),
...buildFleetHealthFields(isHealthy, 'fieldview-api'),
});
});

Expand Down
13 changes: 13 additions & 0 deletions apps/web/app/api/health/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NextResponse } from 'next/server';

import { resolveHealthCommit, resolveHealthEnv } from '@/lib/health-fleet';

export async function GET(): Promise<NextResponse> {
return NextResponse.json({
ok: true,
service: 'fieldview-web',
commit: resolveHealthCommit(),
env: resolveHealthEnv(),
utc: new Date().toISOString(),
});
}
40 changes: 40 additions & 0 deletions apps/web/lib/health-fleet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { resolveHealthCommit } from './health-fleet';

const COMMIT_KEYS = [
'RAILWAY_GIT_COMMIT_SHA',
'VERCEL_GIT_COMMIT_SHA',
'GIT_COMMIT',
] as const;

describe('resolveHealthCommit', () => {
const saved: Partial<Record<(typeof COMMIT_KEYS)[number], string | undefined>> = {};

beforeEach(() => {
for (const key of COMMIT_KEYS) {
saved[key] = process.env[key];
delete process.env[key];
}
});

afterEach(() => {
for (const key of COMMIT_KEYS) {
const value = saved[key];
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
}
});

it('returns RAILWAY_GIT_COMMIT_SHA when set', () => {
process.env.RAILWAY_GIT_COMMIT_SHA = 'abc123fullsha';
expect(resolveHealthCommit()).toBe('abc123fullsha');
});

it('returns unknown when all commit env vars are unset', () => {
expect(resolveHealthCommit()).toBe('unknown');
});
});
14 changes: 14 additions & 0 deletions apps/web/lib/health-fleet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** Fleet health contract: build identity fields for GET /api/health. */

export function resolveHealthCommit(): string {
return (
process.env.RAILWAY_GIT_COMMIT_SHA ??
process.env.VERCEL_GIT_COMMIT_SHA ??
process.env.GIT_COMMIT ??
'unknown'
);
}

export function resolveHealthEnv(): string {
return process.env.APP_ENV ?? process.env.RAILWAY_ENVIRONMENT_NAME ?? 'dev';
}
Loading