File size: 3,693 Bytes
529090e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
 * ChatService Test Suite
 * Tests for Neural Chat messaging service
 */

import { describe, it, expect, beforeEach, vi } from 'vitest';

// Mock Neo4j
vi.mock('../../adapters/Neo4jAdapter.js', () => ({
    neo4jAdapter: {
        runQuery: vi.fn().mockResolvedValue([]),
        executeQuery: vi.fn().mockResolvedValue([])
    }
}));

describe('ChatService', () => {
    let chatService: any;

    beforeEach(async () => {
        vi.clearAllMocks();
        const module = await import('../../services/NeuralChat/ChatService.js');
        chatService = module.neuralChatService;
        (chatService as any).channels = new Map();
    });

    describe('sendMessage()', () => {
        it('should create message with correct structure', async () => {
            const { neo4jAdapter } = await import('../../adapters/Neo4jAdapter.js');
            
            const result = await chatService.sendMessage({
                channel: 'core-dev',
                body: 'Test message',
                from: 'claude',
                type: 'chat'
            });

            expect(neo4jAdapter.executeQuery).toHaveBeenCalled();
            expect(result).toHaveProperty('id');
            expect(result.body).toBe('Test message');
            expect(result.from).toBe('claude');
        });

        it('should extract mentions from message body', async () => {
            const result = await chatService.sendMessage({
                channel: 'core-dev',
                body: 'Hey @gemini, check this out @deepseek',
                from: 'claude',
                type: 'chat'
            });

            expect(result.mentions).toContain('gemini');
            expect(result.mentions).toContain('deepseek');
        });
    });

    describe('getMessages()', () => {
        it('should filter messages by channel', async () => {
            const { neo4jAdapter } = await import('../../adapters/Neo4jAdapter.js');
            (neo4jAdapter.executeQuery as any).mockResolvedValue([
                { m: { properties: { id: 'msg-1', channel: 'core-dev', body: 'Test 1', from_agent: 'codex', to_agent: '', type: 'chat', priority: 'normal', subject: '', mentions: [], reply_to: null, timestamp: Date.now() } } },
                { m: { properties: { id: 'msg-2', channel: 'core-dev', body: 'Test 2', from_agent: 'codex', to_agent: '', type: 'chat', priority: 'normal', subject: '', mentions: [], reply_to: null, timestamp: Date.now() } } }
            ]);

            const messages = await chatService.getMessages({ channel: 'core-dev' });

            expect(messages).toHaveLength(2);
            expect(messages[0].channel).toBe('core-dev');
        });

        it('should respect limit parameter', async () => {
            await chatService.getMessages({ channel: 'core-dev', limit: 5 });
            
            const { neo4jAdapter } = await import('../../adapters/Neo4jAdapter.js');
            const lastCall = (neo4jAdapter.executeQuery as any).mock.calls.pop();
            expect(lastCall[1].limit).toBe(5);
        });
    });

    describe('getChannels()', () => {
        it('should return list of channels', async () => {
            (chatService as any).channels = new Map([
                ['core-dev', { id: 'core-dev', name: 'core-dev', description: 'Core development', members: [], created_at: '', is_private: false }],
                ['standup', { id: 'standup', name: 'standup', description: 'Daily standup', members: [], created_at: '', is_private: false }]
            ]);

            const channels = chatService.getChannels();

            expect(channels).toHaveLength(2);
            expect(channels[0].name).toBe('core-dev');
        });
    });
});