76 lines
2.6 KiB
TypeScript
76 lines
2.6 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
import { getWebsiteApiBaseUrl } from '../../../apps/website/lib/config/apiBaseUrl';
|
|
|
|
describe('getWebsiteApiBaseUrl()', () => {
|
|
const originalEnv = process.env;
|
|
|
|
beforeEach(() => {
|
|
vi.resetModules();
|
|
process.env = { ...originalEnv };
|
|
// Clear relevant env vars
|
|
delete process.env.NEXT_PUBLIC_API_BASE_URL;
|
|
delete process.env.API_BASE_URL;
|
|
delete process.env.NODE_ENV;
|
|
delete process.env.CI;
|
|
delete process.env.DOCKER;
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.env = originalEnv;
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('Browser Context', () => {
|
|
beforeEach(() => {
|
|
vi.stubGlobal('window', {});
|
|
});
|
|
|
|
it('should use NEXT_PUBLIC_API_BASE_URL if provided', () => {
|
|
process.env.NEXT_PUBLIC_API_BASE_URL = 'https://api.example.com/';
|
|
expect(getWebsiteApiBaseUrl()).toBe('https://api.example.com');
|
|
});
|
|
|
|
it('should throw if missing env in test-like environment (CI)', () => {
|
|
process.env.CI = 'true';
|
|
expect(() => getWebsiteApiBaseUrl()).toThrow(/Missing NEXT_PUBLIC_API_BASE_URL/);
|
|
});
|
|
|
|
it('should throw if missing env in test-like environment (DOCKER)', () => {
|
|
process.env.DOCKER = 'true';
|
|
expect(() => getWebsiteApiBaseUrl()).toThrow(/Missing NEXT_PUBLIC_API_BASE_URL/);
|
|
});
|
|
|
|
it('should fallback to localhost in development (non-docker)', () => {
|
|
process.env.NODE_ENV = 'development';
|
|
expect(getWebsiteApiBaseUrl()).toBe('http://localhost:3001');
|
|
});
|
|
});
|
|
|
|
describe('Server Context', () => {
|
|
beforeEach(() => {
|
|
vi.stubGlobal('window', undefined);
|
|
});
|
|
|
|
it('should prioritize API_BASE_URL over NEXT_PUBLIC_API_BASE_URL', () => {
|
|
process.env.API_BASE_URL = 'https://internal-api.example.com';
|
|
process.env.NEXT_PUBLIC_API_BASE_URL = 'https://public-api.example.com';
|
|
expect(getWebsiteApiBaseUrl()).toBe('https://internal-api.example.com');
|
|
});
|
|
|
|
it('should use NEXT_PUBLIC_API_BASE_URL if API_BASE_URL is missing', () => {
|
|
process.env.NEXT_PUBLIC_API_BASE_URL = 'https://public-api.example.com';
|
|
expect(getWebsiteApiBaseUrl()).toBe('https://public-api.example.com');
|
|
});
|
|
|
|
it('should throw if missing env in test-like environment (CI)', () => {
|
|
process.env.CI = 'true';
|
|
expect(() => getWebsiteApiBaseUrl()).toThrow(/Missing API_BASE_URL/);
|
|
});
|
|
|
|
it('should fallback to api:3000 in production (non-test environment)', () => {
|
|
process.env.NODE_ENV = 'production';
|
|
expect(getWebsiteApiBaseUrl()).toBe('http://api:3000');
|
|
});
|
|
});
|
|
});
|