Skip to content

Commit 78639d7

Browse files
committed
chore: cleanup console
1 parent 873088f commit 78639d7

File tree

3 files changed

+82
-107
lines changed

3 files changed

+82
-107
lines changed

src/formatters/consoleFormatter.ts

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,49 @@
66

77
import type {ConsoleMessageData} from '../McpResponse.js';
88

9-
const logLevels: Record<string, string> = {
10-
log: 'Log',
11-
info: 'Info',
12-
warning: 'Warning',
13-
error: 'Error',
14-
exception: 'Exception',
15-
assert: 'Assert',
16-
};
17-
189
// The short format for a console message, based on a previous format.
1910
export function formatConsoleEventShort(msg: ConsoleMessageData): string {
20-
const args = msg.args ? formatArgs(msg, false) : '';
21-
return `msgid=${msg.consoleMessageStableId} [${msg.type}] ${msg.message}${args}`;
11+
return `msgid=${msg.consoleMessageStableId} [${msg.type}] ${msg.message} (${msg.args?.length ?? 0} args)`;
2212
}
2313

24-
// The verbose format for a console message, including all details.
25-
export function formatConsoleEventVerbose(msg: ConsoleMessageData): string {
26-
const logLevel = msg.type ? (logLevels[msg.type] ?? 'Log') : 'Log';
27-
let result = `${logLevel}> ${msg.message}`;
14+
function getArgs(msg: ConsoleMessageData) {
15+
const args = [...(msg.args ?? [])];
2816

29-
if (msg.args && msg.args.length > 0) {
30-
result += formatArgs(msg, true);
17+
// If there is no text, the first argument serves as text (see formatMessage).
18+
if (!msg.message) {
19+
args.shift();
3120
}
3221

33-
result += `
34-
ID: ${msg.consoleMessageStableId}`;
35-
result += `
36-
Type: ${msg.type}`;
22+
return args;
23+
}
24+
25+
// The verbose format for a console message, including all details.
26+
export function formatConsoleEventVerbose(msg: ConsoleMessageData): string {
27+
const result = [
28+
`ID: ${msg.consoleMessageStableId}`,
29+
`Message: ${msg.type}> ${msg.message}`,
30+
formatArgs(msg),
31+
];
32+
33+
return result.join('\n');
34+
}
3735

38-
return result;
36+
function formatArg(arg: unknown) {
37+
return typeof arg === 'object' ? JSON.stringify(arg) : String(arg);
3938
}
4039

41-
// If `includeAllArgs` is false, only includes the first arg and indicates that there are more args.
42-
function formatArgs(
43-
consoleData: ConsoleMessageData,
44-
includeAllArgs = false,
45-
): string {
46-
if (!consoleData.args || consoleData.args.length === 0) {
40+
function formatArgs(consoleData: ConsoleMessageData): string {
41+
const args = getArgs(consoleData);
42+
43+
if (!args.length) {
4744
return '';
4845
}
4946

50-
let formattedArgs = '';
51-
// In the short format version, we only include the first arg.
52-
const messageArgsToFormat = includeAllArgs
53-
? consoleData.args
54-
: [consoleData.args[0]];
55-
56-
for (const arg of messageArgsToFormat) {
57-
if (arg !== consoleData.message) {
58-
formattedArgs += ' ';
59-
formattedArgs +=
60-
typeof arg === 'object' ? JSON.stringify(arg) : String(arg);
61-
}
62-
}
47+
const result = ['### Arguments'];
6348

64-
if (!includeAllArgs && consoleData.args.length > 1) {
65-
formattedArgs += ` ...`;
49+
for (const [key, arg] of args.entries()) {
50+
result.push(`Arg #${key}: ${formatArg(arg)}`);
6651
}
6752

68-
return formattedArgs.length > 0 ? ` Args:${formattedArgs}` : '';
53+
return result.join('\n');
6954
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
exports[`consoleFormatter > formatConsoleEventShort > formats a console.log message 1`] = `
2+
msgid=1 [log] Hello, world! (0 args)
3+
`;
4+
5+
exports[`consoleFormatter > formatConsoleEventShort > formats a console.log message with multiple arguments 1`] = `
6+
msgid=3 [log] Processing file: (2 args)
7+
`;
8+
9+
exports[`consoleFormatter > formatConsoleEventShort > formats a console.log message with one argument 1`] = `
10+
msgid=2 [log] Processing file: (1 args)
11+
`;
12+
13+
exports[`consoleFormatter > formatConsoleEventVerbose > formats a console.error message 1`] = `
14+
ID: 4
15+
Message: error> Something went wrong
16+
17+
`;
18+
19+
exports[`consoleFormatter > formatConsoleEventVerbose > formats a console.log message 1`] = `
20+
ID: 1
21+
Message: log> Hello, world!
22+
23+
`;
24+
25+
exports[`consoleFormatter > formatConsoleEventVerbose > formats a console.log message with multiple arguments 1`] = `
26+
ID: 3
27+
Message: log> Processing file:
28+
### Arguments
29+
Arg #0: file.txt
30+
Arg #1: another file
31+
`;
32+
33+
exports[`consoleFormatter > formatConsoleEventVerbose > formats a console.log message with one argument 1`] = `
34+
ID: 2
35+
Message: log> Processing file:
36+
### Arguments
37+
Arg #0: file.txt
38+
`;

tests/formatters/consoleFormatter.test.ts

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import assert from 'node:assert';
87
import {describe, it} from 'node:test';
98

109
import {
@@ -13,131 +12,84 @@ import {
1312
} from '../../src/formatters/consoleFormatter.js';
1413
import type {ConsoleMessageData} from '../../src/McpResponse.js';
1514

16-
describe('consoleFormatter', () => {
15+
describe.only('consoleFormatter', () => {
1716
describe('formatConsoleEventShort', () => {
18-
it('formats a console.log message', () => {
17+
it('formats a console.log message', t => {
1918
const message: ConsoleMessageData = {
2019
consoleMessageStableId: 1,
2120
type: 'log',
2221
message: 'Hello, world!',
2322
args: [],
2423
};
2524
const result = formatConsoleEventShort(message);
26-
assert.equal(result, 'msgid=1 [log] Hello, world!');
25+
t.assert.snapshot?.(result);
2726
});
2827

29-
it('formats a console.log message with one argument', () => {
28+
it('formats a console.log message with one argument', t => {
3029
const message: ConsoleMessageData = {
3130
consoleMessageStableId: 2,
3231
type: 'log',
3332
message: 'Processing file:',
3433
args: ['file.txt'],
3534
};
3635
const result = formatConsoleEventShort(message);
37-
assert.equal(result, 'msgid=2 [log] Processing file: Args: file.txt');
36+
t.assert.snapshot?.(result);
3837
});
3938

40-
it('formats a console.log message with multiple arguments', () => {
39+
it('formats a console.log message with multiple arguments', t => {
4140
const message: ConsoleMessageData = {
4241
consoleMessageStableId: 3,
4342
type: 'log',
4443
message: 'Processing file:',
4544
args: ['file.txt', 'another file'],
4645
};
4746
const result = formatConsoleEventShort(message);
48-
assert.equal(result, 'msgid=3 [log] Processing file: Args: file.txt ...');
49-
});
50-
51-
it('does not include args if message is the same as arg', () => {
52-
const message: ConsoleMessageData = {
53-
consoleMessageStableId: 4,
54-
type: 'log',
55-
message: 'Hello',
56-
args: ['Hello'],
57-
};
58-
const result = formatConsoleEventShort(message);
59-
assert.equal(result, 'msgid=4 [log] Hello');
47+
t.assert.snapshot?.(result);
6048
});
6149
});
6250

6351
describe('formatConsoleEventVerbose', () => {
64-
it('formats a console.log message', () => {
52+
it('formats a console.log message', t => {
6553
const message: ConsoleMessageData = {
6654
consoleMessageStableId: 1,
6755
type: 'log',
6856
message: 'Hello, world!',
6957
args: [],
7058
};
7159
const result = formatConsoleEventVerbose(message);
72-
assert.equal(
73-
result,
74-
`Log> Hello, world!
75-
ID: 1
76-
Type: log`,
77-
);
60+
t.assert.snapshot?.(result);
7861
});
7962

80-
it('formats a console.log message with one argument', () => {
63+
it('formats a console.log message with one argument', t => {
8164
const message: ConsoleMessageData = {
8265
consoleMessageStableId: 2,
8366
type: 'log',
8467
message: 'Processing file:',
8568
args: ['file.txt'],
8669
};
8770
const result = formatConsoleEventVerbose(message);
88-
assert.equal(
89-
result,
90-
`Log> Processing file: Args: file.txt
91-
ID: 2
92-
Type: log`,
93-
);
71+
t.assert.snapshot?.(result);
9472
});
9573

96-
it('formats a console.log message with multiple arguments', () => {
74+
it('formats a console.log message with multiple arguments', t => {
9775
const message: ConsoleMessageData = {
9876
consoleMessageStableId: 3,
9977
type: 'log',
10078
message: 'Processing file:',
10179
args: ['file.txt', 'another file'],
10280
};
10381
const result = formatConsoleEventVerbose(message);
104-
assert.equal(
105-
result,
106-
`Log> Processing file: Args: file.txt another file
107-
ID: 3
108-
Type: log`,
109-
);
82+
t.assert.snapshot?.(result);
11083
});
11184

112-
it('formats a console.error message', () => {
85+
it('formats a console.error message', t => {
11386
const message: ConsoleMessageData = {
11487
consoleMessageStableId: 4,
11588
type: 'error',
11689
message: 'Something went wrong',
11790
};
11891
const result = formatConsoleEventVerbose(message);
119-
assert.equal(
120-
result,
121-
`Error> Something went wrong
122-
ID: 4
123-
Type: error`,
124-
);
125-
});
126-
127-
it('does not include args if message is the same as arg', () => {
128-
const message: ConsoleMessageData = {
129-
consoleMessageStableId: 5,
130-
type: 'log',
131-
message: 'Hello',
132-
args: ['Hello', 'World'],
133-
};
134-
const result = formatConsoleEventVerbose(message);
135-
assert.equal(
136-
result,
137-
`Log> Hello Args: World
138-
ID: 5
139-
Type: log`,
140-
);
92+
t.assert.snapshot?.(result);
14193
});
14294
});
14395
});

0 commit comments

Comments
 (0)