Skip to content

Commit

Permalink
fix(operators): log test completed
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanGerbeth committed Dec 6, 2024
1 parent 0402eb1 commit df028a7
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 43 deletions.
37 changes: 21 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/operators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"dependencies": {
"@rxjs-collection/observables": "*",
"ansi-colors": "4.1.3",
"consola": "3.2.3",
"ascii-table3": "^0.9.0",
"debug": "^4.3.7",
"fast-equals": "5.0.1",
"rxjs": "7.8.1"
},
Expand Down
67 changes: 57 additions & 10 deletions packages/operators/src/log.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,74 @@
import { bgGreen } from 'ansi-colors';
import { AsciiTable3, AlignmentEnum } from 'ascii-table3';
import chalk from 'chalk';
import debug from 'debug';
import { connectable, finalize, Observable, Subject } from 'rxjs';
import { connectable, finalize, Observable, Subject, tap, toArray } from 'rxjs';

Check warning on line 4 in packages/operators/src/log.js

View workflow job for this annotation

GitHub Actions / Install (ubuntu-latest, 20)

'tap' is defined but never used
import util from 'util';

export const enableLog = tag => debug.enable(tag);
const getAlignments = list => {
return list.map(v => {
switch (typeof v) {
case 'number':
return AlignmentEnum.RIGHT;
case 'string':
return AlignmentEnum.LEFT;
default:
return AlignmentEnum.LEFT;
}
});
};

export const defaultLogger = tag => {
const logger = debug(tag);
logger.log = global.console.log.bind(console);
return {
default: logger,
error: debug(`${tag}:error`),
complete: () => logger(chalk.white.bgGreen.bold('complete!'))
};
};

const tableLogger = tag => {
debug.formatters.t = data => {
if (!data.length) return;
const table = new AsciiTable3();
table.setHeading.apply(table, ['index', ...Object.keys(data[0])]);
table.setAligns(getAlignments([0, ...Object.values(data[0])]));
table.addRowMatrix(
data.map((entry, index) => [
index,
...Object.values(entry).map(item => util.inspect(item, { colors: true, depth: 0 }))
])
);
return table.toString();
};

export const log = tag => {
const logger = debug(tag);
logger.log = global.console.log.bind(console);
const error = debug(`${tag}:error`);

return {
default: val => logger('%t', val),
error: debug(`${tag}:error`),
complete: () => logger(chalk.bgGreen.bold('complete!'))
};
};

export const enableLog = tag => debug.enable(tag);

export const log = (tag, logger = defaultLogger(tag)) => {
if (debug.enabled(tag)) {
return source => {
return new Observable(observer => {
return source.subscribe({
next: val => {
logger(val);
logger.default(val);
observer.next(val);
},
error: err => {
error(err);
logger.error(err);
observer.error(err);
},
complete: () => {
logger(bgGreen.bold('complete!'));
logger.complete();
observer.complete();
}
});
Expand All @@ -33,11 +79,12 @@ export const log = tag => {
return source => source;
};

export const logResult = (tag, observable) => {
export const logResult = (tag, observable, logger = tableLogger) => {
return new Promise(done => {
connectable(
observable.pipe(
log(tag),
toArray(),
log(tag, logger(tag)),
finalize(() => done())
),
{ connector: () => new Subject() }
Expand Down
42 changes: 27 additions & 15 deletions packages/operators/src/log.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { from, map } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import stripAnsi from 'strip-ansi';
import { afterAll, beforeEach, describe, expect, test, vi } from 'vitest';

import { enableLog, log, logResult } from './log';
import { defaultLogger, enableLog, log, logResult } from './log';

describe('log', () => {
let testScheduler;
Expand Down Expand Up @@ -39,7 +40,7 @@ describe('log', () => {

const actual = [];
vi.spyOn(console, 'log').mockImplementation(v => {
actual.push(replaceDateTimeISOString(stripAnsiCodes(v)));
actual.push(replaceDateTimeISOString(stripAnsi(v)));
return v;
});

Expand All @@ -55,36 +56,47 @@ describe('log', () => {
});
});

test('logResult', async () => {
test('logResult - defaultLogger', async () => {
const actual = [];

vi.spyOn(console, 'log').mockImplementation(v => {
actual.push(replaceDateTimeISOString(stripAnsiCodes(v)));
actual.push(replaceDateTimeISOString(stripAnsi(v)));
return v;
});

const expectedVal = [
' operators:log:result content a',
' operators:log:result content b',
' operators:log:result content c',
" operators:log:result [ 'content a', 'content b', 'content c' ]",
' operators:log:result complete!'
];

const triggerVal = ['content a', 'content b', 'content c'];

enableLog('operators:log:result');
await logResult('operators:log:result', from(triggerVal), defaultLogger);
expect(actual).deep.equal(expectedVal);
});

test('logResult - tableLogger', async () => {
const actual = [];

vi.spyOn(console, 'log').mockImplementation(v => {
actual.push(replaceDateTimeISOString(stripAnsi(v)));
return v;
});

const expectedVal = [
` operators:log:result +-------+-------------+\n| index | text |\n+-------+-------------+\n| 0 | 'content a' |\n| 1 | 'content b' |\n| 2 | 'content c' |\n+-------+-------------+\n`,
' operators:log:result complete!'
];

const triggerVal = [{ text: 'content a' }, { text: 'content b' }, { text: 'content c' }];

enableLog('operators:log:result');
await logResult('operators:log:result', from(triggerVal));
expect(actual).deep.equal(expectedVal);
});
});

const stripAnsiCodes = str => {
return str.replace(
// eslint-disable-next-line security/detect-unsafe-regex, no-control-regex
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
''
);
};

const replaceDateTimeISOString = str => {
return str.replace(
/^[0-9]{4}-((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])|(0[469]|11)-(0[1-9]|[12][0-9]|30)|(02)-(0[1-9]|[12][0-9]))T(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])\.[0-9]{3}Z/,
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/src/demo/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { delay, from } from 'rxjs';
import { describe, test } from 'vitest';

describe('playground stephan', () => {
describe.skip('playground stephan', () => {
test('basics', async () => {
const s = from([1, 2, 3, 4, 5, 6, 7, 8, 9])

Check warning on line 6 in packages/playground/src/demo/index.test.js

View workflow job for this annotation

GitHub Actions / Install (ubuntu-latest, 20)

's' is assigned a value but never used
.pipe(delay(4000))
Expand Down
1 change: 1 addition & 0 deletions vitest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default defineConfig({
'commitlint.config.js',
'eslint.ignores.js',
'vitest.workspace.js',
'./coverage/**/*.*',
'./packages/mocks/',
'./packages/*/src/index.js'
]
Expand Down

0 comments on commit df028a7

Please sign in to comment.