Skip to content

Commit 9433e68

Browse files
committed
feat: add more tests to improve coverage
1 parent 3131ecc commit 9433e68

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*!
2+
* UDPClusterManager parseBroadcastedMessage and startListening branch tests
3+
*/
4+
import './mocks';
5+
import { expect } from 'chai';
6+
import { UDPClusterManager } from '../src';
7+
8+
/**
9+
* Covers default parameters in startListening(options = {}) and
10+
* default destructuring for address = '' and timeout = '0' in
11+
* parseBroadcastedMessage().
12+
*/
13+
describe('UDPClusterManager parse/start branches', () => {
14+
it('parseBroadcastedMessage: should apply defaults for empty address and timeout', () => {
15+
const parse = (UDPClusterManager as any).parseBroadcastedMessage as Function;
16+
const buf = Buffer.from(['name', 'id', 'UP'].join('\t'));
17+
const msg = parse(buf);
18+
expect(msg).to.include({ name: 'name', id: 'id', type: 'up' });
19+
// address default => '' leads to host '' and port NaN
20+
expect(msg.host).to.equal('');
21+
expect(Number.isNaN(msg.port)).to.equal(true);
22+
// timeout default '0' => 0 ms
23+
expect(msg.timeout).to.equal(0);
24+
});
25+
26+
it('startListening: should call listenBroadcastedMessages when called without options', () => {
27+
const mgr: any = new (UDPClusterManager as any)();
28+
let called = false;
29+
const original = mgr.listenBroadcastedMessages;
30+
mgr.listenBroadcastedMessages = (..._args: any[]) => { called = true; };
31+
try {
32+
(mgr as any).startListening();
33+
expect(called).to.equal(true);
34+
} finally {
35+
mgr.listenBroadcastedMessages = original;
36+
}
37+
});
38+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*!
2+
* Additional tests to cover remaining branches in profile decorator
3+
*/
4+
import './mocks';
5+
import { expect } from 'chai';
6+
import { profile, LogLevel } from '..';
7+
8+
// Note: We intentionally call decorated methods without a "this" context
9+
// to exercise the (this || target) branches inside the decorator wrapper.
10+
11+
describe('profile decorator extra branches', () => {
12+
it('should return early via original.apply(target, ...) when both debug flags are false and this is undefined', () => {
13+
class T1 {
14+
@profile({ enableDebugTime: false, enableDebugArgs: false, logLevel: LogLevel.LOG })
15+
public m(...args: any[]) { return args; }
16+
}
17+
const o = new T1();
18+
const fn = Object.getPrototypeOf(o).m as Function; // wrapper
19+
const res = fn.call(undefined, 1, 2, 3);
20+
expect(res).to.deep.equal([1, 2, 3]);
21+
});
22+
23+
it('should execute debug path with (this || target) picking target and logLevel fallback to IMQ_LOG_LEVEL', () => {
24+
class T2 {
25+
// no logger on prototype; calling with undefined this picks target
26+
@profile({ enableDebugTime: true, enableDebugArgs: true, logLevel: undefined as any })
27+
public m(..._args: any[]) { /* noop */ }
28+
}
29+
const o = new T2();
30+
const fn = Object.getPrototypeOf(o).m as Function; // wrapper
31+
// provide serializable args to avoid logger.error path when logger is undefined
32+
expect(() => fn.call(undefined, 1, { a: 2 }, 'x')).to.not.throw();
33+
});
34+
});

0 commit comments

Comments
 (0)