<commit_before>import snooplogg from '../src/index.js';
import util from 'util';
import { expect } from 'chai';
import {
	createInstanceWithDefaults,
	Format,
	SnoopLogg,
	StripColors
} from '../src/index.js';

import { Console } from 'console';
import { Transform, Writable } from 'stream';

process.env.FORCE_COLOR = 1;

describe('SnoopLogg', () => {
	before(() => {
		// silence stdio
		snooplogg.enable(null);
	});

	it('should be instance of SnoopLogg', () => {
		expect(snooplogg).to.be.a('function');
	});

	it('should error if SnoopLogg options are invalid', () => {
		expect(() => {
			new SnoopLogg('foo');
		}).to.throw(TypeError, 'Expected options to be an object');

		expect(() => {
			new SnoopLogg(null);
		}).to.throw(TypeError, 'Expected options to be an object');
	});

	it('should update config', () => {
		const instance = createInstanceWithDefaults();

		instance.config();

		expect(() => {
			instance.config('foo');
		}).to.throw(TypeError, 'Expected config options to be an object');

		expect(() => {
			instance.config({
				colors: 123
			});
		}).to.throw(TypeError, 'Expected colors to be a string or array');

		instance.config({
			colors: 'red,blue'
		});

		instance.config({
			colors: [ 'green', 'yellow' ]
		});

		expect(() => {
			instance.config({
				inspectOptions: 'foo'
			});
		}).to.throw(TypeError, 'Expected inspect options to be an object');

		expect(() => {
			instance.config({
				minBrightness: 'foo'
			});
		}).to.throw(TypeError, 'Expected minimum brightness to be a number');

		expect(() => {
			instance.config({
				minBrightness: -1
			});
		}).to.throw(RangeError, 'Minimum brightness must be between 0 and 255');

		expect(() => {
			instance.config({
				minBrightness: 666
			});
		}).to.throw(RangeError, 'Minimum brightness must be between 0 and 255');

		expect(() => {
			instance.config({
				maxBrightness: 'foo'
			});
		}).to.throw(TypeError, 'Expected maximum brightness to be a number');

		expect(() => {
			instance.config({
				maxBrightness: -1
			});
		}).to.throw(RangeError, 'Maximum brightness must be between 0 and 255');

		expect(() => {
			instance.config({
				maxBrightness: 666
			});
		}).to.throw(RangeError, 'Maximum brightness must be between 0 and 255');

		expect(() => {
			instance.config({
				minBrightness: 100,
				maxBrightness: 50
			});
		}).to.throw(RangeError, 'Maximum brightness must greater than or equal to the minimum brightness');

		expect(() => {
			instance.config({
				theme: 123
			});
		}).to.throw(TypeError, 'Expected theme to be a string');

		instance.config({
			theme: 'foo'
		});

		expect(() => {
			instance.config({
				maxBufferSize: 'hi'
			});
		}).to.throw(TypeError, 'Invalid max buffer size: Expected new max size to be a number');

		expect(() => {
			instance.config({
				maxBufferSize: NaN
			});
		}).to.throw(RangeError, 'Invalid max buffer size: Expected new max size to be zero or greater');

		expect(() => {
			instance.config({
				maxBufferSize: -123
			});
		}).to.throw(RangeError, 'Invalid max buffer size: Expected new max size to be zero or greater');
	});

	it('should log to stream', () => {
		class MockOutputStream extends Writable {
			_write(msg, enc, cb) {
				try {
					expect(msg).to.be.instanceof(Buffer);
					expect(msg.toString()).to.equal('log() test\n');
					cb();
				} catch (e) {
					cb(e);
				}
			}
		}

		createInstanceWithDefaults()
			.config({ theme: 'minimal' })
			.enable('*')
			.pipe(new MockOutputStream)
			.log('log() test');
	});

	it('should output log types', () => {
		const types = [ 'trace', 'debug', 'info', 'warn', 'error', 'fatal' ];
		const expected = [
			/^\u001b\[90mtrace\u001b\[39m Trace: trace\(\) test\n/,
			'\u001b[35mdebug\u001b[39m debug() test\n',
			'\u001b[32minfo\u001b[39m info() test\n',
			'\u001b[33mwarn\u001b[39m warn() test\n',
			'\u001b[31merror\u001b[39m error() test\n',
			'\u001b[41m\u001b[37mfatal\u001b[39m\u001b[49m fatal() test\n'
		];
		let i = 0;

		// 1b 5b 39 30 6d 74 72 61 63 65 1b 5b 33 39 6d 20 54 72 61 63 65 3a 20 74 72 61 63 65 28 29 20 74 65 73 74 0a 1b 5b 39 30 6d 74 72 61 63 65 1b 5b 33 39
		//                74 72 61 63 65                20 54 72 61 63 65 3a 20 74 72 61 63 65 28 29 20 74 65 73 74 0a 74 72 61 63 65 20 20 20 20 20 61 74 20 43 6f 6e 74 65 78 74 2e 3c 61 6e
		class MockOutputStream extends Writable {
			_write(msg, enc, cb) {
				try {
					expect(msg).to.be.instanceof(Buffer);
					const value = expected[i++];
					if (value instanceof RegExp) {
						const s = msg.toString();
						console.log(Buffer.from(s));
						expect(s).to.match(value);
					} else {
						expect(msg.toString()).to.equal(value);
					}<commit_msg>More test debugging.<commit_after>import snooplogg from '../src/index.js';
import util from 'util';
import { expect } from 'chai';
import {
	createInstanceWithDefaults,
	Format,
	SnoopLogg,
	StripColors
} from '../src/index.js';

import { Console } from 'console';
import { Transform, Writable } from 'stream';

describe('SnoopLogg', () => {
	before(() => {
		// silence stdio
		snooplogg.enable(null);
	});

	it('should be instance of SnoopLogg', () => {
		expect(snooplogg).to.be.a('function');
	});

	it('should error if SnoopLogg options are invalid', () => {
		expect(() => {
			new SnoopLogg('foo');
		}).to.throw(TypeError, 'Expected options to be an object');

		expect(() => {
			new SnoopLogg(null);
		}).to.throw(TypeError, 'Expected options to be an object');
	});

	it('should update config', () => {
		const instance = createInstanceWithDefaults();

		instance.config();

		expect(() => {
			instance.config('foo');
		}).to.throw(TypeError, 'Expected config options to be an object');

		expect(() => {
			instance.config({
				colors: 123
			});
		}).to.throw(TypeError, 'Expected colors to be a string or array');

		instance.config({
			colors: 'red,blue'
		});

		instance.config({
			colors: [ 'green', 'yellow' ]
		});

		expect(() => {
			instance.config({
				inspectOptions: 'foo'
			});
		}).to.throw(TypeError, 'Expected inspect options to be an object');

		expect(() => {
			instance.config({
				minBrightness: 'foo'
			});
		}).to.throw(TypeError, 'Expected minimum brightness to be a number');

		expect(() => {
			instance.config({
				minBrightness: -1
			});
		}).to.throw(RangeError, 'Minimum brightness must be between 0 and 255');

		expect(() => {
			instance.config({
				minBrightness: 666
			});
		}).to.throw(RangeError, 'Minimum brightness must be between 0 and 255');

		expect(() => {
			instance.config({
				maxBrightness: 'foo'
			});
		}).to.throw(TypeError, 'Expected maximum brightness to be a number');

		expect(() => {
			instance.config({
				maxBrightness: -1
			});
		}).to.throw(RangeError, 'Maximum brightness must be between 0 and 255');

		expect(() => {
			instance.config({
				maxBrightness: 666
			});
		}).to.throw(RangeError, 'Maximum brightness must be between 0 and 255');

		expect(() => {
			instance.config({
				minBrightness: 100,
				maxBrightness: 50
			});
		}).to.throw(RangeError, 'Maximum brightness must greater than or equal to the minimum brightness');

		expect(() => {
			instance.config({
				theme: 123
			});
		}).to.throw(TypeError, 'Expected theme to be a string');

		instance.config({
			theme: 'foo'
		});

		expect(() => {
			instance.config({
				maxBufferSize: 'hi'
			});
		}).to.throw(TypeError, 'Invalid max buffer size: Expected new max size to be a number');

		expect(() => {
			instance.config({
				maxBufferSize: NaN
			});
		}).to.throw(RangeError, 'Invalid max buffer size: Expected new max size to be zero or greater');

		expect(() => {
			instance.config({
				maxBufferSize: -123
			});
		}).to.throw(RangeError, 'Invalid max buffer size: Expected new max size to be zero or greater');
	});

	it('should log to stream', () => {
		class MockOutputStream extends Writable {
			_write(msg, enc, cb) {
				try {
					expect(msg).to.be.instanceof(Buffer);
					expect(msg.toString()).to.equal('log() test\n');
					cb();
				} catch (e) {
					cb(e);
				}
			}
		}

		createInstanceWithDefaults()
			.config({ theme: 'minimal' })
			.enable('*')
			.pipe(new MockOutputStream)
			.log('log() test');
	});

	it('should output log types', () => {
		const types = [ /* 'trace', */ 'debug', 'info', 'warn', 'error', 'fatal' ];
		const expected = [
			// /^\u001b\[90mtrace\u001b\[39m Trace: trace\(\) test\n/,
			'\u001b[35mdebug\u001b[39m debug() test\n',
			'\u001b[32minfo\u001b[39m info() test\n',
			'\u001b[33mwarn\u001b[39m warn() test\n',
			'\u001b[31merror\u001b[39m error() test\n',
			'\u001b[41m\u001b[37mfatal\u001b[39m\u001b[49m fatal() test\n'
		];
		let i = 0;

		// 1b 5b 39 30 6d 74 72 61 63 65 1b 5b 33 39 6d 20 54 72 61 63 65 3a 20 74 72 61 63 65 28 29 20 74 65 73 74 0a 1b 5b 39 30 6d 74 72 61 63 65 1b 5b 33 39
		//                74 72 61 63 65                20 54 72 61 63 65 3a 20 74 72 61 63 65 28 29 20 74 65 73 74 0a                74 72 61 63 65 20 20 20 20 20 61 74 20 43 6f 6e 74 65 78 74 2e 3c 61 6e
		//                t  r  a  c  e                 ?  T  r  a  c  e

		class MockOutputStream extends Writable {
			_write(msg, enc, cb) {
				try {
					expect(msg).to.be.instanceof(Buffer);
					const value = expected[i++];
					if (value instanceof RegExp) {
						const s = msg.toString();
						console.log(Buffer.from(s));
						expect(s).to.match(value);
					} else {
						expect(msg.toString()).to.equal(value);
					}
{'fraction_non_alphanumeric': 0.16601459236544358, 'fraction_numerical': 0.061224489795918366, 'mean_word_length': 2.9589786521557135, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6094354', 'n_tokens_mistral': 4299, 'n_tokens_neox': 3165, 'n_words': 940}