This repository was archived by the owner on Feb 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathapiSession.test.ts
More file actions
99 lines (83 loc) · 3.02 KB
/
apiSession.test.ts
File metadata and controls
99 lines (83 loc) · 3.02 KB
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
94
95
96
97
98
99
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { ApiSessionClient } from './apiSession';
import type { RawJSONLines } from '@/claude/types';
// Use vi.hoisted to ensure mock function is available when vi.mock factory runs
const { mockIo } = vi.hoisted(() => ({
mockIo: vi.fn()
}));
vi.mock('socket.io-client', () => ({
io: mockIo
}));
describe('ApiSessionClient connection handling', () => {
let mockSocket: any;
let consoleSpy: any;
let mockSession: any;
beforeEach(() => {
consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
// Mock socket.io client
mockSocket = {
connected: false,
connect: vi.fn(),
on: vi.fn(),
off: vi.fn(),
disconnect: vi.fn(),
emit: vi.fn(),
};
mockIo.mockReturnValue(mockSocket);
// Create a proper mock session with metadata
mockSession = {
id: 'test-session-id',
seq: 0,
metadata: {
path: '/tmp',
host: 'localhost',
homeDir: '/home/user',
happyHomeDir: '/home/user/.happy',
happyLibDir: '/home/user/.happy/lib',
happyToolsDir: '/home/user/.happy/tools'
},
metadataVersion: 0,
agentState: null,
agentStateVersion: 0,
encryptionKey: new Uint8Array(32),
encryptionVariant: 'legacy' as const
};
});
it('should handle socket connection failure gracefully', async () => {
// Should not throw during client creation
// Note: socket is created with autoConnect: false, so connection happens later
expect(() => {
new ApiSessionClient('fake-token', mockSession);
}).not.toThrow();
});
it('should emit correct events on socket connection', () => {
const client = new ApiSessionClient('fake-token', mockSession);
// Should have set up event listeners
expect(mockSocket.on).toHaveBeenCalledWith('connect', expect.any(Function));
expect(mockSocket.on).toHaveBeenCalledWith('disconnect', expect.any(Function));
expect(mockSocket.on).toHaveBeenCalledWith('error', expect.any(Function));
});
it('emits messages even when disconnected (socket.io will buffer)', () => {
mockSocket.connected = false;
const client = new ApiSessionClient('fake-token', mockSession);
const payload: RawJSONLines = {
type: 'user',
uuid: 'test-uuid',
message: {
content: 'hello',
},
} as const;
client.sendClaudeSessionMessage(payload);
expect(mockSocket.emit).toHaveBeenCalledWith(
'message',
expect.objectContaining({
sid: mockSession.id,
message: expect.any(String),
})
);
});
afterEach(() => {
consoleSpy.mockRestore();
vi.restoreAllMocks();
});
});