Skip to content

Commit 9e9f609

Browse files
committed
Is json shouldn't do the slicing, that's an implementation detail the logs should have
Signed-off-by: Adameska <[email protected]>
1 parent 1c1ac4c commit 9e9f609

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

packages/webview/src/component/pods/PodLogs.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,45 @@ describe('PodLogs', () => {
258258
const nineSpaces = ' '; // 9 spaces to pad 'a' to length of 'longername'
259259
expect(writtenLog).equals(nineSpaces + '\u001b[36ma\u001b[0m|short name log\r');
260260
});
261+
262+
test('should only sample first 20 lines for JSON detection', () => {
263+
const pod = createPod(['containerName']);
264+
const mockedTerminal = createMockTerminal();
265+
setupTerminalMock(mockedTerminal);
266+
267+
render(PodLogs, { object: pod });
268+
269+
// Send 20 JSON lines followed by non-JSON lines
270+
// If sampling is limited to 20 lines, JSON detection should return true (100% JSON)
271+
// If all lines are checked, it would be ~71% JSON and fail the 80% threshold
272+
const jsonLines = Array.from(
273+
{ length: 20 },
274+
(_, i) => `{"timestamp":"2025-11-18T10:00:0${i}Z","level":"info","message":"Line ${i + 1}"}`,
275+
).join('\n');
276+
277+
const nonJsonLines = Array.from({ length: 8 }, (_, i) => `Not JSON line ${i + 1}`).join('\n');
278+
279+
// Send JSON lines first (should fill buffer and detect as JSON)
280+
streamPodLogsMock.sendData({
281+
podName: 'podName',
282+
namespace: 'namespace',
283+
containerName: 'containerName',
284+
data: jsonLines,
285+
});
286+
287+
// Send non-JSON lines (should be ignored for detection since buffer already filled)
288+
streamPodLogsMock.sendData({
289+
podName: 'podName',
290+
namespace: 'namespace',
291+
containerName: 'containerName',
292+
data: nonJsonLines,
293+
});
294+
295+
// The non-JSON lines should still be colorized as JSON since detection happened on first 20 lines
296+
const calls = vi.mocked(mockedTerminal.write).mock.calls;
297+
// First call should have JSON colorization (yellow braces)
298+
expect(calls[0][0]).toContain('\u001b[33m{\u001b[0m');
299+
});
261300
});
262301

263302
describe('terminal initialization', () => {

packages/webview/src/component/terminal/json-colorizer.spec.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -276,41 +276,6 @@ describe('detectJsonLogs', () => {
276276
expect(detectJsonLogs(logs)).toBe(true);
277277
});
278278

279-
test('should only check first 20 non-empty lines', () => {
280-
const logs = [
281-
'{"timestamp":"2025-11-18T10:00:00Z","level":"info","message":"Line 1"}',
282-
'{"timestamp":"2025-11-18T10:00:01Z","level":"info","message":"Line 2"}',
283-
'{"timestamp":"2025-11-18T10:00:02Z","level":"info","message":"Line 3"}',
284-
'{"timestamp":"2025-11-18T10:00:03Z","level":"info","message":"Line 4"}',
285-
'{"timestamp":"2025-11-18T10:00:04Z","level":"info","message":"Line 5"}',
286-
'{"timestamp":"2025-11-18T10:00:05Z","level":"info","message":"Line 6"}',
287-
'{"timestamp":"2025-11-18T10:00:06Z","level":"info","message":"Line 7"}',
288-
'{"timestamp":"2025-11-18T10:00:07Z","level":"info","message":"Line 8"}',
289-
'{"timestamp":"2025-11-18T10:00:08Z","level":"info","message":"Line 9"}',
290-
'{"timestamp":"2025-11-18T10:00:09Z","level":"info","message":"Line 11"}',
291-
'{"timestamp":"2025-11-18T10:00:09Z","level":"info","message":"Line 12"}',
292-
'{"timestamp":"2025-11-18T10:00:09Z","level":"info","message":"Line 13"}',
293-
'{"timestamp":"2025-11-18T10:00:09Z","level":"info","message":"Line 14"}',
294-
'{"timestamp":"2025-11-18T10:00:09Z","level":"info","message":"Line 15"}',
295-
'{"timestamp":"2025-11-18T10:00:09Z","level":"info","message":"Line 16"}',
296-
'{"timestamp":"2025-11-18T10:00:09Z","level":"info","message":"Line 17"}',
297-
'{"timestamp":"2025-11-18T10:00:09Z","level":"info","message":"Line 18"}',
298-
'{"timestamp":"2025-11-18T10:00:09Z","level":"info","message":"Line 19"}',
299-
'{"timestamp":"2025-11-18T10:00:09Z","level":"info","message":"Line 20"}',
300-
// These lines after the 20th should be ignored
301-
'Not JSON line 1',
302-
'Not JSON line 2',
303-
'Not JSON line 3',
304-
'Not JSON line 4',
305-
'Not JSON line 5',
306-
'Not JSON line 6',
307-
'Not JSON line 7',
308-
'Not JSON line 8',
309-
];
310-
311-
expect(detectJsonLogs(logs)).toBe(true);
312-
});
313-
314279
test('should detect JSON with nested objects', () => {
315280
const logs = [
316281
'{"user":{"id":123,"name":"John"},"action":"login"}',

0 commit comments

Comments
 (0)