All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 21m19s
85 lines
3.0 KiB
TypeScript
85 lines
3.0 KiB
TypeScript
// src/utils/serverUtils.test.ts
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import type { Logger } from 'pino';
|
|
import { getBaseUrl } from './serverUtils';
|
|
|
|
// Create a mock logger to spy on its methods
|
|
const createMockLogger = (): Logger =>
|
|
({
|
|
warn: vi.fn(),
|
|
// Add other logger methods if they were used, but only `warn` is relevant here.
|
|
info: vi.fn(),
|
|
error: vi.fn(),
|
|
debug: vi.fn(),
|
|
fatal: vi.fn(),
|
|
trace: vi.fn(),
|
|
silent: vi.fn(),
|
|
child: vi.fn(() => createMockLogger()),
|
|
level: 'info',
|
|
}) as unknown as Logger;
|
|
|
|
describe('serverUtils', () => {
|
|
describe('getBaseUrl', () => {
|
|
const originalEnv = process.env;
|
|
let mockLogger: Logger;
|
|
|
|
beforeEach(() => {
|
|
// Reset mocks and environment variables before each test for isolation
|
|
vi.resetModules();
|
|
process.env = { ...originalEnv };
|
|
mockLogger = createMockLogger();
|
|
});
|
|
|
|
afterEach(() => {
|
|
// Restore original environment variables after each test
|
|
process.env = originalEnv;
|
|
});
|
|
|
|
it('should use FRONTEND_URL if it is a valid URL', () => {
|
|
process.env.FRONTEND_URL = 'https://valid.example.com';
|
|
const baseUrl = getBaseUrl(mockLogger);
|
|
expect(baseUrl).toBe('https://valid.example.com');
|
|
expect(mockLogger.warn).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should trim a trailing slash from FRONTEND_URL', () => {
|
|
process.env.FRONTEND_URL = 'https://valid.example.com/';
|
|
const baseUrl = getBaseUrl(mockLogger);
|
|
expect(baseUrl).toBe('https://valid.example.com');
|
|
});
|
|
|
|
it('should use BASE_URL if FRONTEND_URL is not set', () => {
|
|
delete process.env.FRONTEND_URL;
|
|
process.env.BASE_URL = 'https://base.example.com';
|
|
const baseUrl = getBaseUrl(mockLogger);
|
|
expect(baseUrl).toBe('https://base.example.com');
|
|
expect(mockLogger.warn).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should fall back to localhost with default port 3000 if no URL is provided', () => {
|
|
delete process.env.FRONTEND_URL;
|
|
delete process.env.BASE_URL;
|
|
delete process.env.PORT;
|
|
const baseUrl = getBaseUrl(mockLogger);
|
|
expect(baseUrl).toBe('http://localhost:3000');
|
|
expect(mockLogger.warn).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should fall back to localhost with the specified PORT if no URL is provided', () => {
|
|
delete process.env.FRONTEND_URL;
|
|
delete process.env.BASE_URL;
|
|
process.env.PORT = '8888';
|
|
const baseUrl = getBaseUrl(mockLogger);
|
|
expect(baseUrl).toBe('http://localhost:8888');
|
|
});
|
|
|
|
it('should log a warning and fall back if FRONTEND_URL is invalid (does not start with http)', () => {
|
|
process.env.FRONTEND_URL = 'invalid.url.com';
|
|
const baseUrl = getBaseUrl(mockLogger);
|
|
expect(baseUrl).toBe('http://localhost:3000');
|
|
expect(mockLogger.warn).toHaveBeenCalledWith(
|
|
"[getBaseUrl] FRONTEND_URL/BASE_URL is invalid or incomplete ('invalid.url.com'). Falling back to default local URL: http://localhost:3000",
|
|
);
|
|
});
|
|
});
|
|
}); |