feat: tests

This commit is contained in:
2023-04-21 12:08:35 +02:00
parent 8cb9285462
commit d8c79256c9
140 changed files with 2100 additions and 2693 deletions

View File

@@ -13,18 +13,15 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {suite, test} from '@testdeck/mocha';
import {
deleteUndefinedProperties,
isNodeEnvironment,
isProductiveEnvironment,
isProductiveNodeEnvironment,
} from '../src/common.js';
} from '../src/index.js';
@suite()
export class CommonSpec {
@test
deleteUndefinedProperties1() {
describe('common', function () {
it('should should delete undefined properties (1)', function () {
expect(
deleteUndefinedProperties({
a: 2,
@@ -39,20 +36,18 @@ export class CommonSpec {
c: 3,
},
});
}
});
@test
deleteUndefinedProperties2() {
it('should delete undefined properties (2)', function () {
expect(
deleteUndefinedProperties({
a: undefined,
b: undefined,
}),
).to.deep.equal({});
}
});
@test
deleteUndefinedProperties3() {
it('should delete undefined properties (3)', function () {
expect(
deleteUndefinedProperties({
a: 2,
@@ -64,49 +59,41 @@ export class CommonSpec {
b: 'foo',
c: 'bar',
});
}
});
@test
isNodeEnvironment() {
it('should detect node environment', function () {
expect(isNodeEnvironment()).to.be.equal(true);
const savedProcess = process;
// @ts-ignore
// @ts-expect-error override this
process = undefined;
expect(isNodeEnvironment()).to.be.equal(false);
process = savedProcess;
}
});
@test
isProductiveEnvironment() {
const nodeEnv = process.env.NODE_ENV;
it('should detect production environment', function () {
const nodeEnvironment = process.env.NODE_ENV;
process.env.NODE_ENV = '';
expect(isProductiveEnvironment()).to.be.equal(false);
process.env.NODE_ENV = 'production';
expect(isProductiveEnvironment()).to.be.equal(true);
process.env.NODE_ENV = nodeEnv;
}
process.env.NODE_ENV = nodeEnvironment;
});
@test
isProductiveNodeEnvironment() {
const nodeEnv = process.env.NODE_ENV;
it('should detect node production environment', function () {
const nodeEnvironment = process.env.NODE_ENV;
process.env.NODE_ENV = '';
expect(isProductiveNodeEnvironment()).to.be.equal(false);
process.env.NODE_ENV = 'production';
expect(isProductiveNodeEnvironment()).to.be.equal(true);
process.env.NODE_ENV = nodeEnv;
}
}
process.env.NODE_ENV = nodeEnvironment;
});
});

View File

@@ -1,4 +1,4 @@
import {Transport, VerifiableTransport} from '../src/transport.js';
import {Transport, VerifiableTransport} from '../src/index.js';
export class DummyTransport extends Transport {
send(subject: string, message: string): Promise<string> {

View File

@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/*
* Copyright (C) 2018, 2020 StApps
* This program is free software: you can redistribute it and/or modify it
@@ -16,41 +17,35 @@ import chai from 'chai';
import {expect} from 'chai';
import chaiAsPromised from 'chai-as-promised';
import chaiSpies from 'chai-spies';
import {suite, test} from '@testdeck/mocha';
import {Logger} from '../src/logger.js';
import {AddLogLevel} from '../src/transformations/add-log-level.js';
import {Colorize} from '../src/transformations/colorize.js';
import {DummyTransport} from './dummyTransport.js';
import {Logger, AddLogLevel, Colorize} from '../src/index.js';
import {DummyTransport} from './dummy-transport.js';
import path from 'node:path';
import chalk from 'chalk';
chai.should();
chai.use(chaiSpies);
chai.use(chaiAsPromised);
@suite()
export class LoggerSpec {
static 'sandbox': ChaiSpies.Sandbox;
chalk.level = 2;
static 'before'() {
LoggerSpec.sandbox = chai.spy.sandbox();
}
describe('Logger', function () {
const sandbox = chai.spy.sandbox();
'before'() {
beforeEach(function () {
Logger.setTransformations([new AddLogLevel()]);
}
});
'after'() {
LoggerSpec.sandbox.restore();
}
afterEach(function () {
sandbox.restore();
});
@test
async 'default log level'() {
it('should read default log level', async function () {
expect((Logger as any).getLevel('LOG')).to.be.equal(31);
expect((Logger as any).getLevel('EXIT')).to.be.equal(0);
}
});
@test
async 'error'() {
const spy = LoggerSpec.sandbox.on(console, 'error', () => {
it('should error', async function () {
const spy = sandbox.on(console, 'error', () => {
// noop
});
@@ -59,15 +54,14 @@ export class LoggerSpec {
expect(spy).to.have.been.called();
expect(spy.__spy.calls[0][0]).to.contain('[ERROR]');
expect(spy.__spy.calls[0][0]).to.contain('Foobar');
}
});
@test
async 'error in productive environment'() {
const spy = LoggerSpec.sandbox.on(console, 'error', () => {
it('should error in productive environment', async function () {
const spy = sandbox.on(console, 'error', () => {
// noop
});
const nodeEnv = process.env.NODE_ENV;
const nodeEnvironment = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
await Logger.error('Foobar').should.be.rejectedWith(Error);
@@ -87,12 +81,11 @@ export class LoggerSpec {
Logger.setTransport();
process.env.NODE_ENV = nodeEnv;
}
process.env.NODE_ENV = nodeEnvironment;
});
@test
async 'error without output'() {
const spy = LoggerSpec.sandbox.on(console, 'error', () => {
it('should error without output', async function () {
const spy = sandbox.on(console, 'error', () => {
// noop
});
@@ -103,24 +96,24 @@ export class LoggerSpec {
delete process.env.STAPPS_LOG_LEVEL;
expect(spy).not.to.have.been.called();
}
});
@test
async 'error with Error'() {
const spy = LoggerSpec.sandbox.on(console, 'error', () => {
it('should error with Error', async function () {
const spy = sandbox.on(console, 'error', () => {
// noop
});
// eslint-disable-next-line unicorn/error-message
await Logger.error(new Error());
expect(spy).to.have.been.called();
expect(spy.__spy.calls[0][0]).to.contain('Error');
expect(spy.__spy.calls[0][0]).to.contain(process.cwd());
}
const directory = process.cwd().replaceAll(path.sep, path.posix.sep);
expect(spy.__spy.calls[0][0]).to.contain(directory);
});
@test
'info'() {
const spy = LoggerSpec.sandbox.on(console, 'info', () => {
it('should info', function () {
const spy = sandbox.on(console, 'info', () => {
// noop
});
@@ -129,23 +122,22 @@ export class LoggerSpec {
expect(spy).to.have.been.called();
expect(spy.__spy.calls[0][0]).to.contain('[INFO]');
expect(spy.__spy.calls[0][0]).to.contain('Foobar');
}
});
@test
async 'exits'() {
const infoSpy = LoggerSpec.sandbox.on(console, 'info', () => {
it('should exit', async function () {
const infoSpy = sandbox.on(console, 'info', () => {
// noop
});
const logSpy = LoggerSpec.sandbox.on(console, 'log', () => {
const logSpy = sandbox.on(console, 'log', () => {
// noop
});
const warnSpy = LoggerSpec.sandbox.on(console, 'warn', () => {
const warnSpy = sandbox.on(console, 'warn', () => {
// noop
});
const errorSpy = LoggerSpec.sandbox.on(console, 'error', () => {
const errorSpy = sandbox.on(console, 'error', () => {
// noop
});
const processSpy = LoggerSpec.sandbox.on(process, 'exit', () => {
const processSpy = sandbox.on(process, 'exit', () => {
// noop
});
@@ -173,11 +165,10 @@ export class LoggerSpec {
expect(processSpy).to.have.been.called.exactly(5);
process.env.STAPPS_EXIT_LEVEL = exitLevel;
}
});
@test
'info without output'() {
const spy = LoggerSpec.sandbox.on(console, 'info', () => {
it('should info without output', function () {
const spy = sandbox.on(console, 'info', () => {
// noop
});
@@ -188,10 +179,9 @@ export class LoggerSpec {
delete process.env.STAPPS_LOG_LEVEL;
expect(spy).not.to.have.been.called;
}
});
@test
'initialized'() {
it('should be initialized', function () {
Logger.setTransport(new DummyTransport());
expect(() => {
@@ -199,11 +189,10 @@ export class LoggerSpec {
}).not.to.throw();
Logger.setTransport();
}
});
@test
'initialized in productive environment'() {
const nodeEnv = process.env.NODE_ENV;
it('should be initialized in productive environment', function () {
const nodeEnvironment = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
Logger.setTransport(new DummyTransport());
@@ -218,7 +207,7 @@ export class LoggerSpec {
Logger.initialized();
}).to.throw();
const spy = LoggerSpec.sandbox.on(console, 'warn', () => {
const spy = sandbox.on(console, 'warn', () => {
// noop
});
@@ -232,14 +221,13 @@ export class LoggerSpec {
expect(spy).to.have.been.called();
process.env.NODE_ENV = nodeEnv;
}
process.env.NODE_ENV = nodeEnvironment;
});
@test
'is compatible with log aggregation in productive environment'() {
it('should be compatible with log aggregation in productive environment', function () {
Logger.setTransformations([new AddLogLevel(), new Colorize()]);
let spy = LoggerSpec.sandbox.on(console, 'log', () => {
const spy = sandbox.on(console, 'log', () => {
// noop
});
@@ -248,7 +236,7 @@ export class LoggerSpec {
expect(spy).to.have.been.called.once;
expect(spy.__spy.calls[0][0]).to.equal('\u001B[37m[LOG] Foo\u001B[39m\n\u001B[37mbar\u001B[39m');
const nodeEnv = process.env.NODE_ENV;
const nodeEnvironment = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
process.env.ALLOW_NO_TRANSPORT = 'true';
@@ -259,13 +247,12 @@ export class LoggerSpec {
expect(spy).to.have.been.called.twice;
expect(spy.__spy.calls[1][0]).to.equal('[LOG] Foo bar');
process.env.NODE_ENV = nodeEnv;
process.env.NODE_ENV = nodeEnvironment;
delete process.env.ALLOW_NO_TRANSPORT;
}
});
@test
'log'() {
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
it('should log', function () {
const spy = sandbox.on(console, 'log', () => {
// noop
});
@@ -274,11 +261,10 @@ export class LoggerSpec {
expect(spy).to.have.been.called();
expect(spy.__spy.calls[0][0]).to.contain('[LOG]');
expect(spy.__spy.calls[0][0]).to.contain('Foobar');
}
});
@test
'log without output'() {
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
it('should log without output', function () {
const spy = sandbox.on(console, 'log', () => {
// noop
});
@@ -289,11 +275,10 @@ export class LoggerSpec {
delete process.env.STAPPS_LOG_LEVEL;
expect(spy).to.not.have.been.called();
}
});
@test
'ok'() {
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
it('should ok', function () {
const spy = sandbox.on(console, 'log', () => {
// noop
});
@@ -302,11 +287,10 @@ export class LoggerSpec {
expect(spy).to.have.been.called();
expect(spy.__spy.calls[0][0]).to.contain('[OK]');
expect(spy.__spy.calls[0][0]).to.contain('Foobar');
}
});
@test
'ok without output'() {
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
it('should ok without output', function () {
const spy = sandbox.on(console, 'log', () => {
// noop
});
@@ -317,19 +301,17 @@ export class LoggerSpec {
delete process.env.STAPPS_LOG_LEVEL;
expect(spy).not.to.have.been.called();
}
});
@test
'setTransport'() {
it('should set transport', function () {
expect(() => {
Logger.setTransport(new DummyTransport());
Logger.setTransport();
}).not.to.throw();
}
});
@test
'stringify'() {
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
it('should stringify', function () {
const spy = sandbox.on(console, 'log', () => {
// noop
});
@@ -338,11 +320,10 @@ export class LoggerSpec {
expect(spy).to.have.been.called();
expect(spy.__spy.calls[0][0]).to.contain('foo');
expect(spy.__spy.calls[0][0]).to.contain('bar');
}
});
@test
'stringify object'() {
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
it('should stringify object', function () {
const spy = sandbox.on(console, 'log', () => {
// noop
});
@@ -353,11 +334,10 @@ export class LoggerSpec {
expect(spy).to.have.been.called();
expect(spy.__spy.calls[0][0]).to.contain('foo');
expect(spy.__spy.calls[0][0]).to.contain('bar');
}
});
@test
'warn'() {
const spy = LoggerSpec.sandbox.on(console, 'warn', () => {
it('should warn', function () {
const spy = sandbox.on(console, 'warn', () => {
// noop
});
@@ -366,11 +346,10 @@ export class LoggerSpec {
expect(spy).to.have.been.called();
expect(spy.__spy.calls[0][0]).to.contain('[WARN]');
expect(spy.__spy.calls[0][0]).to.contain('Foobar');
}
});
@test
'warn without output'() {
const spy = LoggerSpec.sandbox.on(console, 'warn', () => {
it('should warn without output', function () {
const spy = sandbox.on(console, 'warn', () => {
// noop
});
@@ -381,17 +360,16 @@ export class LoggerSpec {
delete process.env.STAPPS_LOG_LEVEL;
expect(spy).not.to.have.been.called();
}
});
@test
'log level exclusiveness'() {
const warnSpy = LoggerSpec.sandbox.on(console, 'warn', () => {
it('should log level exclusiveness', function () {
const warnSpy = sandbox.on(console, 'warn', () => {
// noop WARN
});
const infoSpy = LoggerSpec.sandbox.on(console, 'info', () => {
const infoSpy = sandbox.on(console, 'info', () => {
// noop INFO
});
const okSpy = LoggerSpec.sandbox.on(console, 'log', () => {
const okSpy = sandbox.on(console, 'log', () => {
// noop OK
});
@@ -413,20 +391,19 @@ export class LoggerSpec {
expect(okSpy).to.not.have.been.called();
delete process.env.STAPPS_LOG_LEVEL;
}
});
@test
'getExitLevel'() {
it('should getExitLevel', function () {
const savedProcess = process;
// @ts-ignore
// @ts-expect-error ignore
process = undefined;
(global as any).window = {
STAPPS_EXIT_LEVEL: 0,
};
const stub = LoggerSpec.sandbox.on(console, 'info', () => {
const stub = sandbox.on(console, 'info', () => {
// noop
});
@@ -437,20 +414,19 @@ export class LoggerSpec {
delete (global as any).window;
expect(stub).not.to.have.been.called();
}
});
@test
'getLogLevel'() {
it('should getLogLevel', function () {
const savedProcess = process;
// @ts-ignore
// @ts-expect-error ignore
process = undefined;
(global as any).window = {
STAPPS_LOG_LEVEL: 0,
};
const stub = LoggerSpec.sandbox.on(console, 'info', () => {
const stub = sandbox.on(console, 'info', () => {
// noop
});
@@ -461,20 +437,19 @@ export class LoggerSpec {
delete (global as any).window;
expect(stub).not.to.have.been.called();
}
});
@test
'output without transformations'() {
it('should output without transformations', function () {
Logger.setTransformations([]);
const stub = LoggerSpec.sandbox.on(console, 'log', () => {
const stub = sandbox.on(console, 'log', () => {
// noop
});
const applyTransformationsSpy = LoggerSpec.sandbox.on(new Logger(), 'applyTransformations');
const applyTransformationsSpy = sandbox.on(new Logger(), 'applyTransformations');
Logger.log('Foobar');
expect(stub).to.have.been.called.with('Foobar');
expect(applyTransformationsSpy).not.to.have.been.called;
}
}
});
});

View File

@@ -13,96 +13,42 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {suite, test} from '@testdeck/mocha';
import {SMTP} from '../src/smtp.js';
import {SMTP} from '../src/index.js';
@suite()
export class SMTPSpec {
/* tslint:disable:member-ordering */
@test
mailValidation1() {
expect(SMTP.isValidEmailAddress('stordeur@campus.tu-berlin.de')).to.be.true;
}
const validEmails = [
'stordeur@campus.tu-berlin.de',
'foo@bar.com',
'test@test.cz',
'info@beispiel.to',
'stördeur@campus.tu-berlin.de',
'stordeur@campus.tu-berlin.de+a',
];
@test
mailValidation2() {
expect(SMTP.isValidEmailAddress('foo@bar.com')).to.be.true;
}
const invalidEmails = [
'stordeurcampus.tu-berlin.de',
'@campus.tu-berlin.de',
'',
'@',
' stordeur@campus.tu-berlin.de',
'stord+eur@campus.tu-berlin.de ',
'stordeur@campus..tu-berlin.de',
'stordeur@campus',
];
@test
mailValidation3() {
expect(SMTP.isValidEmailAddress('test@test.cz')).to.be.true;
}
describe('isValidEmailAddress', function () {
describe('valid emails', function () {
for (const email of validEmails) {
it(`should detect "${email}" as valid`, function () {
expect(SMTP.isValidEmailAddress(email)).to.be.true;
});
}
});
@test
mailValidation4() {
expect(SMTP.isValidEmailAddress('info@beispiel.to')).to.be.true;
}
@test
mailValidation5() {
expect(SMTP.isValidEmailAddress('stördeur@campus.tu-berlin.de')).to.be.true;
}
@test
mailValidation6() {
expect(SMTP.isValidEmailAddress('stordeur@campus.tu-berlin.de+a')).to.be.true;
}
@test
mailValidation7() {
expect(SMTP.isValidEmailAddress('stordeurcampus.tu-berlin.de')).to.be.false;
}
@test
mailValidation8() {
expect(SMTP.isValidEmailAddress('@campus.tu-berlin.de')).to.be.false;
}
@test
mailValidation9() {
expect(SMTP.isValidEmailAddress('')).to.be.false;
}
@test
mailValidation10() {
expect(SMTP.isValidEmailAddress('@')).to.be.false;
}
@test
mailValidation11() {
expect(SMTP.isValidEmailAddress('@')).to.be.false;
}
@test
mailValidation12() {
expect(SMTP.isValidEmailAddress(' stordeur@campus.tu-berlin.de')).to.be.false;
}
@test
mailValidation13() {
expect(SMTP.isValidEmailAddress('stordeur@campus.tu-berlin.de ')).to.be.false;
}
@test
mailValidation14() {
expect(SMTP.isValidEmailAddress('stord+eur@campus.tu-berlin.de ')).to.be.false;
}
@test
mailValidation15() {
expect(SMTP.isValidEmailAddress('anselm..stordeur@campus.tu-berlin.de')).to.be.false;
}
@test
mailValidation16() {
expect(SMTP.isValidEmailAddress('stordeur@campus..tu-berlin.de')).to.be.false;
}
@test
mailValidation17() {
expect(SMTP.isValidEmailAddress('stordeur@campus')).to.be.false;
}
/* tslint:enable:member-ordering */
}
describe('invalid emails', function () {
for (const email of invalidEmails) {
it(`should detect "${email}" as invalid`, function () {
expect(SMTP.isValidEmailAddress(email)).to.be.false;
});
}
});
});

View File

@@ -13,15 +13,12 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {suite, test} from '@testdeck/mocha';
import {AddLogLevel} from '../../src/transformations/add-log-level.js';
import {AddLogLevel} from '../../src/index.js';
@suite()
export class AddLogLevelSpec {
@test
transform() {
describe('add log level', function () {
it('should transform', function () {
const transformation = new AddLogLevel();
expect(transformation.transform('ERROR', 'Foobar')).to.be.equal('[ERROR] Foobar');
}
}
});
});

View File

@@ -13,18 +13,18 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {suite, test} from '@testdeck/mocha';
import {Colorize} from '../../src/transformations/colorize.js';
import {Colorize} from '../../src/index.js';
import chalk from 'chalk';
@suite()
export class ColorizeSpec {
@test
transform() {
chalk.level = 2;
describe('colorize', function () {
it('should transform', function () {
const transformation = new Colorize();
expect(transformation.transform('ERROR', 'Foobar')).to.be.equal(
'\u001b[1m\u001b[31mFoobar\u001b[39m\u001b[22m',
'\u001B[1m\u001B[31mFoobar\u001B[39m\u001B[22m',
);
expect(transformation.transform('LOG', 'Foobar')).to.be.equal('\u001b[37mFoobar\u001b[39m');
}
}
expect(transformation.transform('LOG', 'Foobar')).to.be.equal('\u001B[37mFoobar\u001B[39m');
});
});

View File

@@ -13,15 +13,12 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {suite, test} from '@testdeck/mocha';
import {Timestamp} from '../../src/transformations/timestamp.js';
import {Timestamp} from '../../src/index.js';
@suite()
export class TimeStampSpec {
@test
default() {
describe('timestamp', function () {
it('should contain Z', function () {
const transformation = new Timestamp();
expect(transformation.transform('ERROR', 'Foobar')).to.be.contain(`Z`);
}
}
});
});

View File

@@ -13,19 +13,15 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {suite, test} from '@testdeck/mocha';
import {isTransportWithVerification} from '../src/common.js';
import {DummyTransport, VerifiableDummyTransport} from './dummyTransport.js';
import {isTransportWithVerification} from '../src/index.js';
import {DummyTransport, VerifiableDummyTransport} from './dummy-transport.js';
@suite()
export class TransportSpec {
@test
isNotTransportWithVerification() {
return expect(isTransportWithVerification(new DummyTransport())).to.be.false;
}
describe('transport', function () {
it('should not have transport with verification', function () {
expect(isTransportWithVerification(new DummyTransport())).to.be.false;
});
@test
isTransportWithVerification() {
return expect(isTransportWithVerification(new VerifiableDummyTransport())).to.be.true;
}
}
it('should have transport with verification', function () {
expect(isTransportWithVerification(new VerifiableDummyTransport())).to.be.true;
});
});