import {expect} from 'chai'; import type {LogLevel} from '@rocicorp/logger'; import {newLogContext} from './logger.js'; import * as sinon from 'sinon'; import {test, suite} from 'mocha'; const mockConsoleMethod = (level: LogLevel) => { const fake = sinon.fake(); sinon.replace(console, level, fake); return fake; }; suite('Server: Logger', () => { beforeEach(() => { sinon.restore(); }); test('prepends level', () => { const mockDebug = mockConsoleMethod('debug'); const mockInfo = mockConsoleMethod('info'); const mockError = mockConsoleMethod('error'); { sinon.reset(); const l = newLogContext('debug'); expect(l.debug).to.be.instanceOf(Function); expect(l.info).to.be.instanceOf(Function); expect(l.error).to.be.instanceOf(Function); l.debug?.('ggg'); l.info?.('hhh'); l.error?.('iii'); expect(mockDebug.lastCall.args).to.deep.equal(['DBG', 'ggg']); expect(mockInfo.lastCall.args).to.deep.equal(['INF', 'hhh']); expect(mockError.lastCall.args).to.deep.equal(['ERR', 'iii']); } }); test('stringifies objects, with newlines', () => { const mockDebug = mockConsoleMethod('debug'); { sinon.reset(); const l = newLogContext('debug'); const o = {foo: 'bar'}; l.debug?.(o); expect(mockDebug.lastCall.args.slice(1)).to.deep.equal([ '{\n "foo": "bar"\n}', ]); } }); test('stringifies objects, without newlines', () => { const mockDebug = mockConsoleMethod('debug'); { sinon.reset(); const l = newLogContext('debug', false /* don't allow newlines */); const o = {foo: 'bar'}; l.debug?.(o); expect(mockDebug.lastCall.args.slice(1)).to.deep.equal([ '{ "foo": "bar"}', ]); } }); });