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
40 changes: 40 additions & 0 deletions packages/api/src/routes/health/health.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ describe('Health Router', () => {
});

describe('GET /api/health', () => {
const savedRailwaySha = process.env['RAILWAY_GIT_COMMIT_SHA'];
const savedVercelSha = process.env['VERCEL_GIT_COMMIT_SHA'];
const savedGitCommit = process.env['GIT_COMMIT'];

afterEach(() => {
if (savedRailwaySha === undefined) delete process.env['RAILWAY_GIT_COMMIT_SHA'];
else process.env['RAILWAY_GIT_COMMIT_SHA'] = savedRailwaySha;
if (savedVercelSha === undefined) delete process.env['VERCEL_GIT_COMMIT_SHA'];
else process.env['VERCEL_GIT_COMMIT_SHA'] = savedVercelSha;
if (savedGitCommit === undefined) delete process.env['GIT_COMMIT'];
else process.env['GIT_COMMIT'] = savedGitCommit;
});

it('should return 200 with health status', async () => {
// Act
const response = await request(app).get('/api/health');
Expand All @@ -20,7 +33,12 @@ describe('Health Router', () => {
expect(response.status).toBe(200);
expect(response.body).toEqual({
status: 'ok',
ok: true,
service: 'scholarmancy-api',
commit: expect.any(String),
env: expect.any(String),
timestamp: expect.any(String),
utc: expect.any(String),
});
});

Expand All @@ -31,6 +49,28 @@ describe('Health Router', () => {
// Assert
expect(response.body.timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/);
expect(() => new Date(response.body.timestamp)).not.toThrow();
expect(response.body.utc).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/);
expect(() => new Date(response.body.utc)).not.toThrow();
});

it('should return commit from RAILWAY_GIT_COMMIT_SHA when set', async () => {
process.env['RAILWAY_GIT_COMMIT_SHA'] = 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef';

const response = await request(app).get('/api/health');

expect(response.status).toBe(200);
expect(response.body.commit).toBe('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef');
});

it('should report unknown commit when deploy env vars are absent', async () => {
delete process.env['RAILWAY_GIT_COMMIT_SHA'];
delete process.env['VERCEL_GIT_COMMIT_SHA'];
delete process.env['GIT_COMMIT'];

const response = await request(app).get('/api/health');

expect(response.status).toBe(200);
expect(response.body.commit).toBe('unknown');
});
});

Expand Down
12 changes: 11 additions & 1 deletion packages/api/src/routes/health/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,19 @@ export function readBuiltAt(): string {
* Returns server status and current timestamp.
*/
healthRouter.get('/', (_req: Request, res: Response) => {
const now = new Date().toISOString();
res.status(200).json({
status: 'ok',
timestamp: new Date().toISOString(),
ok: true,
service: 'scholarmancy-api',
commit:
process.env['RAILWAY_GIT_COMMIT_SHA'] ??
process.env['VERCEL_GIT_COMMIT_SHA'] ??
process.env['GIT_COMMIT'] ??
'unknown',
env: process.env['APP_ENV'] ?? process.env['RAILWAY_ENVIRONMENT_NAME'] ?? 'dev',
timestamp: now,
utc: now,
});
});

Expand Down
48 changes: 48 additions & 0 deletions packages/web/app/api/health/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { GET } from './route';

describe('GET /api/health', () => {
const savedRailwaySha = process.env['RAILWAY_GIT_COMMIT_SHA'];
const savedVercelSha = process.env['VERCEL_GIT_COMMIT_SHA'];
const savedGitCommit = process.env['GIT_COMMIT'];

afterEach(() => {
if (savedRailwaySha === undefined) delete process.env['RAILWAY_GIT_COMMIT_SHA'];
else process.env['RAILWAY_GIT_COMMIT_SHA'] = savedRailwaySha;
if (savedVercelSha === undefined) delete process.env['VERCEL_GIT_COMMIT_SHA'];
else process.env['VERCEL_GIT_COMMIT_SHA'] = savedVercelSha;
if (savedGitCommit === undefined) delete process.env['GIT_COMMIT'];
else process.env['GIT_COMMIT'] = savedGitCommit;
});

it('returns fleet health metadata with scholarmancy-web service', async () => {
const res = await GET();
expect(res.status).toBe(200);
const body = await res.json();
expect(body).toEqual({
status: 'ok',
ok: true,
service: 'scholarmancy-web',
commit: expect.any(String),
env: expect.any(String),
utc: expect.stringMatching(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/),
});
});

it('returns commit from RAILWAY_GIT_COMMIT_SHA when set', async () => {
process.env['RAILWAY_GIT_COMMIT_SHA'] = 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef';

const res = await GET();
const body = await res.json();
expect(body.commit).toBe('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef');
});

it('reports unknown commit when deploy env vars are absent', async () => {
delete process.env['RAILWAY_GIT_COMMIT_SHA'];
delete process.env['VERCEL_GIT_COMMIT_SHA'];
delete process.env['GIT_COMMIT'];

const res = await GET();
const body = await res.json();
expect(body.commit).toBe('unknown');
});
});
17 changes: 16 additions & 1 deletion packages/web/app/api/health/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,20 @@ export const dynamic = 'force-dynamic';
export const runtime = 'nodejs';

export async function GET() {
return NextResponse.json({ status: 'ok' }, { status: 200 });
const utc = new Date().toISOString();
return NextResponse.json(
{
status: 'ok',
ok: true,
service: 'scholarmancy-web',
commit:
process.env['RAILWAY_GIT_COMMIT_SHA'] ??
process.env['VERCEL_GIT_COMMIT_SHA'] ??
process.env['GIT_COMMIT'] ??
'unknown',
env: process.env['APP_ENV'] ?? process.env['RAILWAY_ENVIRONMENT_NAME'] ?? 'dev',
utc,
},
{ status: 200 }
);
}
Loading