UNPKG

@joystick.js/db-canary

Version:

JoystickDB - A minimalist database server for the Joystick framework

244 lines (191 loc) 6.29 kB
import test from 'ava'; import { load_settings, get_settings, get_port_configuration, clear_settings_cache } from '../../../src/server/lib/load_settings.js'; test.beforeEach(() => { // Clean up environment variables before each test delete process.env.JOYSTICK_DB_SETTINGS; clear_settings_cache(); }); test.afterEach(() => { // Clean up environment variables after each test delete process.env.JOYSTICK_DB_SETTINGS; clear_settings_cache(); }); test('get_port_configuration returns default ports when no settings', (t) => { const { tcp_port, http_port } = get_port_configuration(); t.is(tcp_port, 1983); t.is(http_port, 1984); }); test('get_port_configuration uses configured port from settings', (t) => { process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ port: 3000, authentication: {} }); const { tcp_port, http_port } = get_port_configuration(); t.is(tcp_port, 3000); t.is(http_port, 3001); }); test('load_settings validates port configuration - valid port', (t) => { process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ port: 2000, authentication: {} }); t.notThrows(() => { load_settings(); }); const settings = get_settings(); t.is(settings.port, 2000); }); test('load_settings validates port configuration - port too low', (t) => { process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ port: 1023, authentication: {} }); const error = t.throws(() => { load_settings(); }); t.true(error.message.includes('Port must be between 1024 and 65534')); }); test('load_settings validates port configuration - port too high', (t) => { process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ port: 70000, authentication: {} }); const error = t.throws(() => { load_settings(); }); t.true(error.message.includes('HTTP port (70001) would exceed maximum port number')); }); test('load_settings validates port configuration - HTTP port overflow', (t) => { process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ port: 65535, authentication: {} }); const error = t.throws(() => { load_settings(); }); t.true(error.message.includes('HTTP port (65536) would exceed maximum port number')); }); test('load_settings validates port configuration - non-integer port', (t) => { process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ port: 1983.5, authentication: {} }); const error = t.throws(() => { load_settings(); }); t.true(error.message.includes('Port must be an integer')); }); test('load_settings validates port configuration - string port', (t) => { process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ port: "1983", authentication: {} }); const error = t.throws(() => { load_settings(); }); t.true(error.message.includes('Port must be an integer')); }); test('load_settings allows valid port range - minimum valid port', (t) => { process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ port: 1024, authentication: {} }); t.notThrows(() => { load_settings(); }); const settings = get_settings(); t.is(settings.port, 1024); }); test('load_settings allows valid port range - maximum valid port', (t) => { process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ port: 65534, authentication: {} }); t.notThrows(() => { load_settings(); }); const settings = get_settings(); t.is(settings.port, 65534); }); test('load_settings allows settings without port configuration', (t) => { process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ authentication: {}, s3: { bucket: 'test' } }); t.notThrows(() => { load_settings(); }); const settings = get_settings(); t.is(settings.port, undefined); }); test('get_port_configuration handles mixed settings scenarios', (t) => { // Test with settings that don't include port process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ authentication: {}, s3: { bucket: 'test' } }); const { tcp_port, http_port } = get_port_configuration(); t.is(tcp_port, 1983); t.is(http_port, 1984); }); test('port configuration works with complex settings', (t) => { process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ port: 5000, authentication: { password_hash: 'test-hash' }, s3: { bucket: 'test-bucket', region: 'us-east-1' }, replication: { enabled: true, nodes: ['node1', 'node2'] } }); t.notThrows(() => { load_settings(); }); const settings = get_settings(); t.is(settings.port, 5000); const { tcp_port, http_port } = get_port_configuration(); t.is(tcp_port, 5000); t.is(http_port, 5001); }); test('port validation error messages are descriptive', (t) => { // Test port too low process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ port: 500 }); let error = t.throws(() => load_settings()); t.true(error.message.includes('Port must be between 1024 and 65534')); t.true(error.message.includes('HTTP port will be port + 1')); clear_settings_cache(); // Test HTTP port overflow process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ port: 65535 }); error = t.throws(() => load_settings()); t.true(error.message.includes('HTTP port (65536) would exceed maximum port number (65535)')); t.true(error.message.includes('Use a lower TCP port')); }); test('get_port_configuration is resilient to settings loading errors', (t) => { // Set invalid JSON to cause settings loading to fail process.env.JOYSTICK_DB_SETTINGS = '{ invalid json }'; // Should not throw and should return defaults const { tcp_port, http_port } = get_port_configuration(); t.is(tcp_port, 1983); t.is(http_port, 1984); }); test('port configuration updates when settings are reloaded', (t) => { // Start with default configuration let { tcp_port, http_port } = get_port_configuration(); t.is(tcp_port, 1983); t.is(http_port, 1984); // Set new port configuration process.env.JOYSTICK_DB_SETTINGS = JSON.stringify({ port: 4000, authentication: {} }); // Configuration should update after loading settings load_settings(); ({ tcp_port, http_port } = get_port_configuration()); t.is(tcp_port, 4000); t.is(http_port, 4001); });