UNPKG

@joystick.js/db-canary

Version:

JoystickDB - A minimalist database server for the Joystick framework

108 lines (82 loc) 2.98 kB
import test from 'ava'; import { load_settings, get_settings, reload_settings, clear_settings_cache } from '../../../src/server/lib/load_settings.js'; const valid_settings = { authentication: { password_hash: '$2b$10$example.hash.here' }, database: { max_connections: 100 } }; test.beforeEach(() => { // NOTE: Clean up any cached settings and environment variables before each test. clear_settings_cache(); delete process.env.JOYSTICK_DB_SETTINGS; }); test.afterEach(() => { // NOTE: Clean up environment variables after each test. delete process.env.JOYSTICK_DB_SETTINGS; }); test('load_settings loads valid JSON from environment variable', (t) => { process.env.JOYSTICK_DB_SETTINGS = JSON.stringify(valid_settings); const settings = load_settings(); t.deepEqual(settings, valid_settings); t.is(settings.authentication.password_hash, '$2b$10$example.hash.here'); }); test('load_settings throws error for missing environment variable', (t) => { const error = t.throws(() => { load_settings(); }); t.true(error.message.includes('JOYSTICK_DB_SETTINGS environment variable is not set')); }); test('load_settings throws error for invalid JSON in environment variable', (t) => { process.env.JOYSTICK_DB_SETTINGS = '{ invalid json }'; const error = t.throws(() => { load_settings(); }); t.true(error.message.includes('Invalid JSON in JOYSTICK_DB_SETTINGS environment variable')); }); test('get_settings returns cached settings', (t) => { process.env.JOYSTICK_DB_SETTINGS = JSON.stringify(valid_settings); load_settings(); const settings = get_settings(); t.deepEqual(settings, valid_settings); }); test('get_settings throws error when settings not loaded', (t) => { const error = t.throws(() => { get_settings(); }); t.true(error.message.includes('Settings not loaded')); }); test('reload_settings reloads from environment variable', (t) => { const initial_settings = { test: 'initial' }; const updated_settings = { test: 'updated' }; process.env.JOYSTICK_DB_SETTINGS = JSON.stringify(initial_settings); load_settings(); t.deepEqual(get_settings(), initial_settings); process.env.JOYSTICK_DB_SETTINGS = JSON.stringify(updated_settings); const reloaded = reload_settings(); t.deepEqual(reloaded, updated_settings); t.deepEqual(get_settings(), updated_settings); }); test('load_settings works with complex nested settings', (t) => { const complex_settings = { authentication: { password_hash: '$2b$10$example.hash.here', created_at: '2023-01-01T00:00:00.000Z', failed_attempts: {}, rate_limits: {} }, database: { max_connections: 100, timeout: 5000 }, cluster: { enabled: true, nodes: ['node1', 'node2'] } }; process.env.JOYSTICK_DB_SETTINGS = JSON.stringify(complex_settings); const settings = load_settings(); t.deepEqual(settings, complex_settings); });