Skip to content

Commit 99d52e0

Browse files
author
georgiy.rusanov
committed
chore: added tests
1 parent 4205b26 commit 99d52e0

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

test/admin-app.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { expect, test, describe } from 'vitest'
2+
import { build } from '../src/server/admin-app.js'
3+
4+
describe('admin-app', () => {
5+
test('should register metrics endpoint', async () => {
6+
const app = build()
7+
8+
// Test that the app can be started (this will trigger plugin registration)
9+
await app.ready()
10+
11+
// Verify that metrics endpoint is available
12+
const routes = app.printRoutes()
13+
expect(routes).toContain('metrics')
14+
15+
await app.close()
16+
})
17+
})

test/utils.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { expect, test, describe } from 'vitest'
2+
import { extractRequestForLogging, translateErrorToResponseCode } from '../src/server/utils.js'
3+
import { FastifyRequest } from 'fastify'
4+
5+
describe('server utils', () => {
6+
describe('extractRequestForLogging', () => {
7+
test('should handle valid pg connection string', () => {
8+
const mockRequest = {
9+
method: 'POST',
10+
url: '/test',
11+
headers: {
12+
pg: 'postgresql://user:pass@localhost:5432/db',
13+
'x-supabase-info': 'test-info',
14+
},
15+
} as FastifyRequest
16+
17+
const result = extractRequestForLogging(mockRequest)
18+
19+
expect(result).toEqual({
20+
method: 'POST',
21+
url: '/test',
22+
pg: 'localhost',
23+
opt: 'test-info',
24+
})
25+
})
26+
27+
test('should handle missing x-supabase-info header', () => {
28+
const mockRequest = {
29+
method: 'DELETE',
30+
url: '/test',
31+
headers: {
32+
pg: 'postgresql://user:pass@localhost:5432/db',
33+
},
34+
} as FastifyRequest
35+
36+
const result = extractRequestForLogging(mockRequest)
37+
38+
expect(result).toEqual({
39+
method: 'DELETE',
40+
url: '/test',
41+
pg: 'localhost',
42+
opt: '',
43+
})
44+
})
45+
})
46+
47+
describe('translateErrorToResponseCode', () => {
48+
test('should return 504 for connection timeout', () => {
49+
const error = { message: 'Connection terminated due to connection timeout' }
50+
const result = translateErrorToResponseCode(error)
51+
expect(result).toBe(504)
52+
})
53+
54+
test('should return 503 for too many clients', () => {
55+
const error = { message: 'sorry, too many clients already' }
56+
const result = translateErrorToResponseCode(error)
57+
expect(result).toBe(503)
58+
})
59+
})
60+
})

0 commit comments

Comments
 (0)