forked from patternfly/patternfly-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.logger.test.ts
More file actions
169 lines (134 loc) · 4.99 KB
/
server.logger.test.ts
File metadata and controls
169 lines (134 loc) · 4.99 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import diagnostics_channel from 'node:diagnostics_channel';
import { getLoggerOptions, setOptions } from '../options.context';
import { toMcpLevel, registerMcpSubscriber, createServerLogger } from '../server.logger';
import { log } from '../logger';
describe('toMcpLevel', () => {
it.each([
{
description: 'default',
level: undefined
},
{
description: 'debug',
level: 'debug'
},
{
description: 'info',
level: 'info'
},
{
description: 'warn',
level: 'warn'
},
{
description: 'error',
level: 'error'
},
{
description: 'anything',
level: 'lorem ipsum'
}
])('should return log severity, $description', ({ level }) => {
expect(toMcpLevel(level as any)).toMatchSnapshot();
});
});
describe('registerMcpSubscriber', () => {
let subscribeSpy: jest.SpyInstance;
let unsubscribeSpy: jest.SpyInstance;
beforeEach(() => {
setOptions({});
subscribeSpy = jest.spyOn(diagnostics_channel, 'subscribe');
unsubscribeSpy = jest.spyOn(diagnostics_channel, 'unsubscribe');
});
afterEach(() => {
subscribeSpy.mockRestore();
unsubscribeSpy.mockRestore();
});
it('should attempt to subscribe and unsubscribe from a channel', () => {
const loggingSession = getLoggerOptions();
const unsubscribe = registerMcpSubscriber((() => {}) as any, loggingSession);
unsubscribe();
expect({
subscribe: subscribeSpy.mock.calls,
unsubscribe: unsubscribeSpy.mock.calls
}).toMatchSnapshot('subscribe');
});
});
describe('createServerLogger', () => {
let subscribeSpy: jest.SpyInstance;
let unsubscribeSpy: jest.SpyInstance;
beforeEach(() => {
setOptions({});
subscribeSpy = jest.spyOn(diagnostics_channel, 'subscribe');
unsubscribeSpy = jest.spyOn(diagnostics_channel, 'unsubscribe');
});
afterEach(() => {
subscribeSpy.mockRestore();
unsubscribeSpy.mockRestore();
});
it.each([
{
description: 'with stderr, and emulated channel to pass checks',
options: { channelName: 'loremIpsum', stderr: true, protocol: false }
},
{
description: 'with stderr, protocol, and emulated channel to pass checks',
options: { channelName: 'loremIpsum', stderr: true, protocol: true }
},
{
description: 'with no logging options',
options: {},
stderr: false
}
])('should attempt to subscribe and unsubscribe from a channel, $description', ({ options }) => {
// Use channelName to pass conditions
const { unsubscribe } = createServerLogger((() => {}) as any, options as any);
unsubscribe();
expect({
subscribe: subscribeSpy.mock.calls,
unsubscribe: unsubscribeSpy.mock.calls
}).toMatchSnapshot('subscribe');
});
it('should return a memoized server logger that avoids duplicate sinks; teardown stops emissions', () => {
setOptions({ logging: { stderr: true, level: 'debug' } as any });
const stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => true as any);
class MockServer { sendLoggingMessage = jest.fn(async () => {}); }
const server = new MockServer() as any;
// Create a single memoized server logger with two server-level subscription handlers
const { subscribe: subscribeCallOne, unsubscribe: unsubscribeAllCallOne } = createServerLogger.memo(server);
const { subscribe: subscribeCallTwo, unsubscribe: unsubscribeAllCallTwo } = createServerLogger.memo(server);
// Create two lower-level subscription handlers
const mockHandlerOne = jest.fn();
const mockHandlerTwo = jest.fn();
const unsubscribeMockHandlerOne = subscribeCallOne(mockHandlerOne);
const unsubscribeMockHandlerTwo = subscribeCallTwo(mockHandlerTwo);
log.debug('a');
expect(mockHandlerOne).toHaveBeenCalledTimes(1);
expect(mockHandlerTwo).toHaveBeenCalledTimes(1);
// This removes the subscription for mockHandlerOne
unsubscribeMockHandlerOne();
log.debug('b');
// This was removed earlier by the "unsubscribeMockHandlerOne()" call above
expect(mockHandlerOne).toHaveBeenCalledTimes(1);
// This continues to be called
expect(mockHandlerTwo).toHaveBeenCalledTimes(2);
log.info('lorem ipsum, dolor sit info');
expect(unsubscribeAllCallOne).toBe(unsubscribeAllCallTwo);
log.info('dolor sit amet');
// This removes all subscriptions
unsubscribeAllCallOne();
log.info('hello world!');
// This shouldn't throw an error since all subscriptions were removed
unsubscribeAllCallTwo();
log.debug('c');
// This was removed earlier by the "unsubscribeMockHandlerOne()" call above
expect(mockHandlerOne).toHaveBeenCalledTimes(1);
// This was removed by the "unsubscribeAllCallOne()" call above
expect(mockHandlerTwo).toHaveBeenCalledTimes(4);
// This shouldn't throw an error since all subscriptions were removed
unsubscribeMockHandlerTwo();
log.info('goodbye world!');
expect(stderrSpy.mock.calls).toMatchSnapshot('stderr');
stderrSpy.mockRestore();
});
});