"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const collaborationService_1 = require("../collaborationService");
// Mock all external dependencies
jest.mock('yjs');
jest.mock('y-indexeddb');
jest.mock('socket.io');
describe('CollaborationService Unit Tests', () => {
    let collaborationService;
    beforeEach(() => {
        collaborationService = new collaborationService_1.CollaborationService();
    });
    describe('Service Initialization', () => {
        test('should initialize without throwing', () => {
            expect(() => new collaborationService_1.CollaborationService()).not.toThrow();
        });
        test('should have required methods', () => {
            expect(typeof collaborationService.setSocketServer).toBe('function');
            expect(typeof collaborationService.getChangeHistory).toBe('function');
            expect(typeof collaborationService.getActiveUsers).toBe('function');
            expect(typeof collaborationService.createSnapshot).toBe('function');
            expect(typeof collaborationService.cleanupDocument).toBe('function');
        });
    });
    describe('Change History Management', () => {
        test('should return empty array for non-existent document', () => {
            const history = collaborationService.getChangeHistory('non-existent');
            expect(Array.isArray(history)).toBe(true);
            expect(history).toHaveLength(0);
        });
        test('should limit change history with limit parameter', () => {
            const history = collaborationService.getChangeHistory('test-doc', 5);
            expect(Array.isArray(history)).toBe(true);
        });
        test('should get detailed change history with filtering', () => {
            const filteredHistory = collaborationService.getDetailedChangeHistory('test-doc', {
                field: 'title',
                userId: 'user1',
                limit: 10
            });
            expect(Array.isArray(filteredHistory)).toBe(true);
        });
    });
    describe('User Management', () => {
        test('should return empty array for active users of non-existent document', () => {
            const users = collaborationService.getActiveUsers('non-existent');
            expect(Array.isArray(users)).toBe(true);
            expect(users).toHaveLength(0);
        });
        test('should get active users with info', () => {
            const usersWithInfo = collaborationService.getActiveUsersWithInfo('test-doc');
            expect(Array.isArray(usersWithInfo)).toBe(true);
        });
        test('should get section awareness data', () => {
            const awareness = collaborationService.getSectionAwareness('test-section');
            expect(Array.isArray(awareness)).toBe(true);
        });
    });
    describe('Snapshot Operations', () => {
        test('should return null for snapshot of non-existent document', () => {
            const snapshot = collaborationService.createSnapshot('non-existent');
            expect(snapshot).toBeNull();
        });
        test('should create detailed snapshot', () => {
            const detailedSnapshot = collaborationService.createDetailedSnapshot('test-doc', 'Test Snapshot', 'Description', 'user1');
            expect(detailedSnapshot).toBeNull(); // Because document doesn't exist
        });
        test('should create auto snapshot', () => {
            const autoSnapshot = collaborationService.createAutoSnapshot('test-doc', 'rollback', 'user1');
            expect(autoSnapshot).toBeNull(); // Because document doesn't exist
        });
    });
    describe('Version Comparison', () => {
        test('should compare versions and detect differences', () => {
            const versionA = {
                title: 'Original Title',
                description: 'Original Description',
                status: 'pending'
            };
            const versionB = {
                title: 'Updated Title',
                description: 'Original Description',
                priority: 'high'
            };
            const differences = collaborationService.compareVersions('test-doc', versionA, versionB);
            expect(Array.isArray(differences)).toBe(true);
            expect(differences).toHaveLength(3);
            const titleDiff = differences.find(d => d.field === 'title');
            expect(titleDiff?.type).toBe('modified');
            expect(titleDiff?.oldValue).toBe('Original Title');
            expect(titleDiff?.newValue).toBe('Updated Title');
            const statusDiff = differences.find(d => d.field === 'status');
            expect(statusDiff?.type).toBe('removed');
            const priorityDiff = differences.find(d => d.field === 'priority');
            expect(priorityDiff?.type).toBe('added');
        });
        test('should detect no differences when versions are identical', () => {
            const version = {
                title: 'Same Title',
                status: 'pending'
            };
            const differences = collaborationService.compareVersions('test-doc', version, version);
            expect(differences).toHaveLength(0);
        });
    });
    describe('Rollback Validation', () => {
        test('should validate rollback operation', () => {
            const targetSnapshot = {
                title: 'Old Title',
                status: 'pending'
            };
            const validation = collaborationService.validateRollback('test-doc', targetSnapshot);
            expect(typeof validation.isValid).toBe('boolean');
            expect(Array.isArray(validation.warnings)).toBe(true);
            expect(Array.isArray(validation.affectedFields)).toBe(true);
            expect(Array.isArray(validation.conflictingUsers)).toBe(true);
        });
        test('should return invalid for null snapshot', () => {
            const validation = collaborationService.validateRollback('test-doc', null);
            expect(validation.isValid).toBe(false);
            expect(validation.warnings).toContain('Invalid snapshot data');
        });
    });
    describe('Document Statistics', () => {
        test('should return stats for non-existent document', () => {
            const stats = collaborationService.getDocumentStats('non-existent');
            expect(typeof stats.activeUsers).toBe('number');
            expect(typeof stats.changeCount).toBe('number');
            expect(typeof stats.size).toBe('number');
            expect(stats.activeUsers).toBe(0);
            expect(stats.changeCount).toBe(0);
            expect(stats.size).toBe(0);
        });
    });
    describe('Cleanup Operations', () => {
        test('should cleanup document without throwing', () => {
            expect(() => {
                collaborationService.cleanupDocument('test-doc');
            }).not.toThrow();
        });
    });
    describe('Socket Server Integration', () => {
        test('should accept socket server without throwing', () => {
            const mockIo = {
                on: jest.fn(),
                to: jest.fn().mockReturnThis(),
                emit: jest.fn()
            };
            expect(() => {
                collaborationService.setSocketServer(mockIo);
            }).not.toThrow();
        });
    });
});
//# sourceMappingURL=collaboration.unit.test.js.map