@joystick.js/db-canary
Version:
JoystickDB - A minimalist database server for the Joystick framework
125 lines (99 loc) • 4.29 kB
JavaScript
/**
* @fileoverview Basic tests for the simplified primary/secondary sync system.
* Tests API_KEY authentication, sync operations, and basic functionality.
*/
import test from 'ava';
import { get_simple_sync_manager } from '../../../src/server/lib/simple_sync_manager.js';
import { get_sync_receiver } from '../../../src/server/lib/sync_receiver.js';
test('simple sync manager should initialize correctly', t => {
const sync_manager = get_simple_sync_manager();
t.truthy(sync_manager);
t.is(typeof sync_manager.initialize, 'function');
t.is(typeof sync_manager.queue_sync, 'function');
t.is(typeof sync_manager.get_sync_status, 'function');
t.is(typeof sync_manager.shutdown, 'function');
});
test('sync receiver should initialize correctly', t => {
const sync_receiver = get_sync_receiver();
t.truthy(sync_receiver);
t.is(typeof sync_receiver.initialize, 'function');
t.is(typeof sync_receiver.should_block_client_operation, 'function');
t.is(typeof sync_receiver.get_sync_status, 'function');
t.is(typeof sync_receiver.shutdown, 'function');
});
test('sync receiver should block write operations on secondary nodes', t => {
const sync_receiver = get_sync_receiver();
// Mock secondary mode
sync_receiver.is_secondary = true;
// Should block write operations
t.true(sync_receiver.should_block_client_operation('insert_one'));
t.true(sync_receiver.should_block_client_operation('update_one'));
t.true(sync_receiver.should_block_client_operation('delete_one'));
t.true(sync_receiver.should_block_client_operation('bulk_write'));
// Should allow read operations
t.false(sync_receiver.should_block_client_operation('find'));
t.false(sync_receiver.should_block_client_operation('find_one'));
t.false(sync_receiver.should_block_client_operation('count_documents'));
t.false(sync_receiver.should_block_client_operation('get_indexes'));
// Reset
sync_receiver.is_secondary = false;
});
test('sync receiver should not block operations on primary nodes', t => {
const sync_receiver = get_sync_receiver();
// Mock primary mode (default)
sync_receiver.is_secondary = false;
// Should not block any operations on primary
t.false(sync_receiver.should_block_client_operation('insert_one'));
t.false(sync_receiver.should_block_client_operation('update_one'));
t.false(sync_receiver.should_block_client_operation('find'));
t.false(sync_receiver.should_block_client_operation('find_one'));
});
test('simple sync manager should provide status information', t => {
const sync_manager = get_simple_sync_manager();
const status = sync_manager.get_sync_status();
t.is(typeof status, 'object');
t.is(typeof status.is_primary, 'boolean');
t.is(typeof status.secondary_count, 'number');
t.is(typeof status.stats, 'object');
t.is(typeof status.secondaries, 'object');
});
test('sync receiver should provide status information', t => {
const sync_receiver = get_sync_receiver();
const status = sync_receiver.get_sync_status();
t.is(typeof status, 'object');
t.is(typeof status.is_secondary, 'boolean');
t.is(typeof status.api_key_loaded, 'boolean');
t.is(typeof status.server_running, 'boolean');
t.is(typeof status.sync_port, 'number');
t.is(typeof status.stats, 'object');
});
test('sync manager should handle empty secondary nodes list', t => {
const sync_manager = get_simple_sync_manager();
// Should not throw when updating with empty list
t.notThrows(() => {
sync_manager.update_secondary_nodes([]);
});
});
test('sync receiver should handle manual promotion to primary', t => {
const sync_receiver = get_sync_receiver();
// Mock secondary mode
sync_receiver.is_secondary = true;
sync_receiver.server = { close: () => {} }; // Mock server
// Should be able to promote to primary
t.notThrows(() => {
sync_receiver.promote_to_primary();
});
// Should now be in primary mode
t.false(sync_receiver.is_secondary);
});
test.after.always(async t => {
// Clean up any resources
try {
const sync_manager = get_simple_sync_manager();
const sync_receiver = get_sync_receiver();
await sync_manager.shutdown();
await sync_receiver.shutdown();
} catch (error) {
// Ignore cleanup errors in tests
}
});