Skip to content

Commit 08bceec

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

File tree

2 files changed

+80
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)