|
| 1 | +/** |
| 2 | + * Copyright (C) 2024-present Puter Technologies Inc. |
| 3 | + * |
| 4 | + * This file is part of Puter. |
| 5 | + * |
| 6 | + * Puter is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published |
| 8 | + * by the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 21 | +import { installJsonConsole } from './jsonConsole.js'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Capture what the patched console writes to stdout/stderr. Returns the raw |
| 25 | + * chunks plus helpers to parse them, then uninstall() restores everything. |
| 26 | + */ |
| 27 | +const withInstalledConsole = ( |
| 28 | + options?: Parameters<typeof installJsonConsole>[0], |
| 29 | +) => { |
| 30 | + const out: string[] = []; |
| 31 | + const err: string[] = []; |
| 32 | + const stdout = vi |
| 33 | + .spyOn(process.stdout, 'write') |
| 34 | + .mockImplementation((chunk: unknown) => { |
| 35 | + out.push(String(chunk)); |
| 36 | + return true; |
| 37 | + }); |
| 38 | + const stderr = vi |
| 39 | + .spyOn(process.stderr, 'write') |
| 40 | + .mockImplementation((chunk: unknown) => { |
| 41 | + err.push(String(chunk)); |
| 42 | + return true; |
| 43 | + }); |
| 44 | + const uninstall = installJsonConsole(options); |
| 45 | + return { |
| 46 | + out, |
| 47 | + err, |
| 48 | + uninstall: () => { |
| 49 | + uninstall(); |
| 50 | + stdout.mockRestore(); |
| 51 | + stderr.mockRestore(); |
| 52 | + }, |
| 53 | + }; |
| 54 | +}; |
| 55 | + |
| 56 | +describe('installJsonConsole', () => { |
| 57 | + afterEach(() => { |
| 58 | + vi.restoreAllMocks(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('emits one JSON line per call with level, timestamp and msg', () => { |
| 62 | + const { out, uninstall } = withInstalledConsole(); |
| 63 | + try { |
| 64 | + console.log('hello world'); |
| 65 | + } finally { |
| 66 | + uninstall(); |
| 67 | + } |
| 68 | + |
| 69 | + expect(out).toHaveLength(1); |
| 70 | + expect(out[0].endsWith('\n')).toBe(true); |
| 71 | + const entry = JSON.parse(out[0]); |
| 72 | + expect(entry.level).toBe('info'); |
| 73 | + expect(entry.msg).toBe('hello world'); |
| 74 | + expect(() => new Date(entry.timestamp).toISOString()).not.toThrow(); |
| 75 | + expect(entry.timestamp).toBe(new Date(entry.timestamp).toISOString()); |
| 76 | + }); |
| 77 | + |
| 78 | + it('maps each console method to the expected level and stream', () => { |
| 79 | + const { out, err, uninstall } = withInstalledConsole(); |
| 80 | + try { |
| 81 | + console.info('i'); |
| 82 | + console.debug('d'); |
| 83 | + console.warn('w'); |
| 84 | + console.error('e'); |
| 85 | + } finally { |
| 86 | + uninstall(); |
| 87 | + } |
| 88 | + |
| 89 | + expect(out.map((l) => JSON.parse(l).level)).toEqual(['info', 'debug']); |
| 90 | + expect(err.map((l) => JSON.parse(l).level)).toEqual(['warn', 'error']); |
| 91 | + }); |
| 92 | + |
| 93 | + it('formats non-string args like console does (objects preserved)', () => { |
| 94 | + const { out, uninstall } = withInstalledConsole(); |
| 95 | + try { |
| 96 | + console.log('user', { id: 5, roles: ['a'] }, [1, 2]); |
| 97 | + } finally { |
| 98 | + uninstall(); |
| 99 | + } |
| 100 | + |
| 101 | + const entry = JSON.parse(out[0]); |
| 102 | + expect(entry.msg).toBe("user { id: 5, roles: [ 'a' ] } [ 1, 2 ]"); |
| 103 | + }); |
| 104 | + |
| 105 | + it('collapses a multi-line stack trace into a single log event', () => { |
| 106 | + const { err, uninstall } = withInstalledConsole(); |
| 107 | + try { |
| 108 | + console.error(new Error('boom')); |
| 109 | + } finally { |
| 110 | + uninstall(); |
| 111 | + } |
| 112 | + |
| 113 | + // Exactly one write, one trailing newline, no interior raw newlines |
| 114 | + // (the stack lives inside the JSON-escaped `msg` string). |
| 115 | + expect(err).toHaveLength(1); |
| 116 | + expect(err[0].match(/\n/g)).toHaveLength(1); |
| 117 | + const entry = JSON.parse(err[0]); |
| 118 | + expect(entry.level).toBe('error'); |
| 119 | + expect(entry.msg).toContain('Error: boom'); |
| 120 | + expect(entry.msg).toContain('\n at '); // stack frames survive in msg |
| 121 | + }); |
| 122 | + |
| 123 | + it('attaches traceId/spanId only when a span is active', () => { |
| 124 | + let ctx: { traceId: string; spanId?: string } | undefined; |
| 125 | + const { out, uninstall } = withInstalledConsole({ |
| 126 | + getTraceContext: () => ctx, |
| 127 | + }); |
| 128 | + try { |
| 129 | + console.log('no span'); |
| 130 | + ctx = { traceId: 'abc123', spanId: 'def456' }; |
| 131 | + console.log('with span'); |
| 132 | + } finally { |
| 133 | + uninstall(); |
| 134 | + } |
| 135 | + |
| 136 | + const first = JSON.parse(out[0]); |
| 137 | + expect(first).not.toHaveProperty('traceId'); |
| 138 | + const second = JSON.parse(out[1]); |
| 139 | + expect(second.traceId).toBe('abc123'); |
| 140 | + expect(second.spanId).toBe('def456'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('restores the original console methods on uninstall', () => { |
| 144 | + const before = console.log; |
| 145 | + const { uninstall } = withInstalledConsole(); |
| 146 | + expect(console.log).not.toBe(before); |
| 147 | + uninstall(); |
| 148 | + expect(console.log).toBe(before); |
| 149 | + }); |
| 150 | + |
| 151 | + it('is idempotent — a second install is a no-op', () => { |
| 152 | + const { out, uninstall } = withInstalledConsole(); |
| 153 | + const second = installJsonConsole(); |
| 154 | + try { |
| 155 | + console.log('once'); |
| 156 | + } finally { |
| 157 | + second(); |
| 158 | + uninstall(); |
| 159 | + } |
| 160 | + // Still a single JSON line, not double-patched. |
| 161 | + expect(out).toHaveLength(1); |
| 162 | + expect(JSON.parse(out[0]).msg).toBe('once'); |
| 163 | + }); |
| 164 | +}); |
0 commit comments