/**
 * Unit tests for connection utility
 */
import { buildConnection } from './connection';

describe('buildConnection', () => {
    describe('when key is provided and existingConnection has no accountKey', () => {
        it('should create connection with accountKey from URL key', () => {
            const result = buildConnection('url-key-123', undefined);

            expect(result).toEqual({ accountKey: 'url-key-123' });
        });

        it('should handle null existingConnection', () => {
            const result = buildConnection('url-key-123', null);

            expect(result).toEqual({ accountKey: 'url-key-123' });
        });

        it('should preserve existing serverUrl when adding accountKey', () => {
            const result = buildConnection('url-key-123', {
                serverUrl: 'https://api.example.com'
            });

            expect(result).toEqual({
                serverUrl: 'https://api.example.com',
                accountKey: 'url-key-123'
            });
        });

        it('should preserve other connection properties', () => {
            const existing = {
                serverUrl: 'https://api.example.com',
                otherProp: 'value'
            } as any;

            const result = buildConnection('url-key-123', existing);

            expect(result).toEqual({
                serverUrl: 'https://api.example.com',
                otherProp: 'value',
                accountKey: 'url-key-123'
            });
        });
    });

    describe('when existingConnection already has accountKey', () => {
        it('should preserve existing accountKey and not override', () => {
            const result = buildConnection('url-key-123', {
                accountKey: 'existing-key',
                serverUrl: 'https://api.example.com'
            });

            expect(result).toEqual({
                accountKey: 'existing-key',
                serverUrl: 'https://api.example.com'
            });
        });
    });

    describe('when key is not provided', () => {
        it('should return existingConnection unchanged when key is null', () => {
            const existing = {
                accountKey: 'existing-key',
                serverUrl: 'https://api.example.com'
            };

            const result = buildConnection(null, existing);

            expect(result).toEqual(existing);
        });

        it('should return existingConnection unchanged when key is undefined', () => {
            const existing = {
                serverUrl: 'https://api.example.com'
            };

            const result = buildConnection(undefined, existing);

            expect(result).toEqual(existing);
        });

        it('should return existingConnection unchanged when key is empty string', () => {
            const existing = {
                serverUrl: 'https://api.example.com'
            };

            const result = buildConnection('', existing);

            expect(result).toEqual(existing);
        });

        it('should return undefined when both key and existingConnection are missing', () => {
            const result = buildConnection(null, undefined);

            expect(result).toBeUndefined();
        });

        it('should return undefined when key is empty and existingConnection is null', () => {
            const result = buildConnection('', null);

            expect(result).toBeUndefined();
        });
    });

    describe('edge cases', () => {
        it('should handle empty existingConnection object', () => {
            const result = buildConnection('url-key-123', {});

            expect(result).toEqual({ accountKey: 'url-key-123' });
        });

        it('should handle existingConnection with empty accountKey', () => {
            const result = buildConnection('url-key-123', { accountKey: '' });

            expect(result).toEqual({ accountKey: 'url-key-123' });
        });
    });
});
