"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("@testing-library/react");
const useCollaboration_1 = require("../client/useCollaboration");
// Mock Y.js
jest.mock('yjs', () => ({
    Doc: jest.fn().mockImplementation(() => ({
        getMap: jest.fn().mockReturnValue({
            set: jest.fn(),
            get: jest.fn(),
            observe: jest.fn()
        }),
        on: jest.fn(),
        transact: jest.fn((fn, origin) => fn()),
        destroy: jest.fn()
    })),
    Text: jest.fn().mockImplementation(() => ({
        toString: jest.fn().mockReturnValue(''),
        insert: jest.fn(),
        delete: jest.fn(),
        length: 0
    })),
    Array: jest.fn().mockImplementation(() => ({
        toArray: jest.fn().mockReturnValue([]),
        insert: jest.fn(),
        delete: jest.fn(),
        length: 0
    })),
    Map: jest.fn().mockImplementation(() => ({
        forEach: jest.fn(),
        set: jest.fn(),
        delete: jest.fn()
    })),
    AbstractType: jest.fn()
}));
// Mock IndexedDB persistence
jest.mock('y-indexeddb', () => ({
    IndexeddbPersistence: jest.fn().mockImplementation(() => ({
        on: jest.fn(),
        destroy: jest.fn()
    }))
}));
// Mock Socket.io client
jest.mock('socket.io-client', () => ({
    io: jest.fn().mockReturnValue({
        connected: false,
        on: jest.fn(),
        emit: jest.fn(),
        disconnect: jest.fn()
    })
}));
describe('useCollaboration', () => {
    const mockOptions = {
        documentId: 'test-doc-1',
        documentType: 'task',
        userId: 'user1',
        userInfo: {
            id: 'user1',
            name: 'Test User',
            email: 'test@example.com',
            color: '#FF6B6B'
        },
        serverUrl: 'http://localhost:3000',
        autoConnect: false
    };
    beforeEach(() => {
        jest.clearAllMocks();
    });
    test('should initialize with correct default state', () => {
        const { result } = (0, react_1.renderHook)(() => (0, useCollaboration_1.useCollaboration)(mockOptions));
        expect(result.current.state.connected).toBe(false);
        expect(result.current.state.activeUsers).toEqual([]);
        expect(result.current.state.localUser.id).toBe('user1');
        expect(result.current.state.localUser.name).toBe('Test User');
        expect(result.current.state.isSyncing).toBe(false);
        expect(result.current.state.hasUnsavedChanges).toBe(false);
    });
    test('should provide connection methods', () => {
        const { result } = (0, react_1.renderHook)(() => (0, useCollaboration_1.useCollaboration)(mockOptions));
        expect(typeof result.current.connect).toBe('function');
        expect(typeof result.current.disconnect).toBe('function');
        expect(result.current.isConnected).toBe(false);
    });
    test('should provide document manipulation methods', () => {
        const { result } = (0, react_1.renderHook)(() => (0, useCollaboration_1.useCollaboration)(mockOptions));
        expect(typeof result.current.getDocument).toBe('function');
        expect(typeof result.current.getYType).toBe('function');
        expect(typeof result.current.getValue).toBe('function');
        expect(typeof result.current.updateField).toBe('function');
    });
    test('should provide cursor and awareness methods', () => {
        const { result } = (0, react_1.renderHook)(() => (0, useCollaboration_1.useCollaboration)(mockOptions));
        expect(typeof result.current.updateCursor).toBe('function');
        expect(typeof result.current.updateAwareness).toBe('function');
    });
    test('should handle field updates', () => {
        const { result } = (0, react_1.renderHook)(() => (0, useCollaboration_1.useCollaboration)(mockOptions));
        (0, react_1.act)(() => {
            result.current.updateField('title', 'New Title');
        });
        // Should not throw and method should exist
        expect(result.current.updateField).toBeDefined();
    });
    test('should handle cursor updates', () => {
        const { result } = (0, react_1.renderHook)(() => (0, useCollaboration_1.useCollaboration)(mockOptions));
        (0, react_1.act)(() => {
            result.current.updateCursor('title', 5, { start: 5, end: 10 });
        });
        // Should not throw and method should exist
        expect(result.current.updateCursor).toBeDefined();
    });
    test('should handle awareness updates', () => {
        const { result } = (0, react_1.renderHook)(() => (0, useCollaboration_1.useCollaboration)(mockOptions));
        (0, react_1.act)(() => {
            result.current.updateAwareness({ section: 'test-section', activity: 'editing' });
        });
        // Should not throw and method should exist
        expect(result.current.updateAwareness).toBeDefined();
    });
    test('should auto-connect when autoConnect is true', () => {
        const optionsWithAutoConnect = { ...mockOptions, autoConnect: true };
        const { result } = (0, react_1.renderHook)(() => (0, useCollaboration_1.useCollaboration)(optionsWithAutoConnect));
        // Should attempt to connect
        expect(result.current.connect).toBeDefined();
    });
    test('should handle document type correctly', () => {
        const taskOptions = { ...mockOptions, documentType: 'task' };
        const { result: taskResult } = (0, react_1.renderHook)(() => (0, useCollaboration_1.useCollaboration)(taskOptions));
        const projectOptions = { ...mockOptions, documentType: 'project' };
        const { result: projectResult } = (0, react_1.renderHook)(() => (0, useCollaboration_1.useCollaboration)(projectOptions));
        expect(taskResult.current.getDocument).toBeDefined();
        expect(projectResult.current.getDocument).toBeDefined();
    });
    test('should provide utility properties', () => {
        const { result } = (0, react_1.renderHook)(() => (0, useCollaboration_1.useCollaboration)(mockOptions));
        expect(typeof result.current.isConnected).toBe('boolean');
        expect(Array.isArray(result.current.activeUsers)).toBe(true);
        expect(typeof result.current.hasUnsavedChanges).toBe('boolean');
    });
    test('should handle getValue for different field types', () => {
        const { result } = (0, react_1.renderHook)(() => (0, useCollaboration_1.useCollaboration)(mockOptions));
        // Mock different return types
        const stringValue = result.current.getValue('title');
        const arrayValue = result.current.getValue('assignees');
        const objectValue = result.current.getValue('customFields');
        expect(result.current.getValue).toBeDefined();
    });
    test('should handle cleanup on unmount', () => {
        const { unmount } = (0, react_1.renderHook)(() => (0, useCollaboration_1.useCollaboration)(mockOptions));
        expect(() => unmount()).not.toThrow();
    });
});
//# sourceMappingURL=useCollaboration.test.js.map