Skip to content

Commit

Permalink
Debugging module: fix bug where mocked bidders always time out with a…
Browse files Browse the repository at this point in the history
…uctions (#12177)
  • Loading branch information
dgirardi authored Aug 28, 2024
1 parent 8932942 commit 06ead4c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
6 changes: 4 additions & 2 deletions modules/debugging/debugging.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ export function bidderBidInterceptor(next, interceptBids, spec, bids, bidRequest
({bids, bidRequest} = interceptBids({
bids,
bidRequest,
addBid: cbs.onBid,
addPaapiConfig: (config, bidRequest) => cbs.onPaapi({bidId: bidRequest.bidId, ...config}),
addBid: wrapCallback(cbs.onBid),
addPaapiConfig: wrapCallback((config, bidRequest) => cbs.onPaapi({bidId: bidRequest.bidId, ...config})),
done
}));
if (bids.length === 0) {
// eslint-disable-next-line no-unused-expressions
cbs.onResponse?.({}); // trigger onResponse so that the bidder may be marked as "timely" if necessary
done();
} else {
next(spec, bids, bidRequest, ajax, wrapCallback, {...cbs, onCompletion: done});
Expand Down
40 changes: 36 additions & 4 deletions test/spec/modules/debugging_mod_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,31 +354,57 @@ describe('Debugging config', () => {
});

describe('bidderBidInterceptor', () => {
let next, interceptBids, onCompletion, interceptResult, done, addBid;
let next, interceptBids, onCompletion, interceptResult, done, addBid, wrapCallback, addPaapiConfig, wrapped;

function interceptorArgs({spec = {}, bids = [], bidRequest = {}, ajax = {}, wrapCallback = {}, cbs = {}} = {}) {
function interceptorArgs({spec = {}, bids = [], bidRequest = {}, ajax = {}, cbs = {}} = {}) {
return [next, interceptBids, spec, bids, bidRequest, ajax, wrapCallback, Object.assign({onCompletion}, cbs)];
}

beforeEach(() => {
next = sinon.spy();
wrapped = false;
wrapCallback = sinon.stub().callsFake(cb => {
if (cb == null) return cb;
return function () {
wrapped = true;
try {
return cb.apply(this, arguments)
} finally {
wrapped = false;
}
}
});
interceptBids = sinon.stub().callsFake((opts) => {
done = opts.done;
addBid = opts.addBid;
addPaapiConfig = opts.addPaapiConfig;
return interceptResult;
});
onCompletion = sinon.spy();
interceptResult = {bids: [], bidRequest: {}};
});

it('should pass to interceptBid an addBid that triggers onBid', () => {
const onBid = sinon.spy();
const onBid = sinon.stub().callsFake(() => {
expect(wrapped).to.be.true;
});
bidderBidInterceptor(...interceptorArgs({cbs: {onBid}}));
const bid = {};
const bid = {
bidder: 'bidder'
};
addBid(bid);
expect(onBid.calledWith(sinon.match.same(bid))).to.be.true;
});

it('should pass addPaapiConfig that triggers onPaapi', () => {
const onPaapi = sinon.stub().callsFake(() => {
expect(wrapped).to.be.true;
});
bidderBidInterceptor(...interceptorArgs({cbs: {onPaapi}}));
addPaapiConfig({paapi: 'config'}, {bidId: 'bidId'});
sinon.assert.calledWith(onPaapi, {paapi: 'config', bidId: 'bidId'})
})

describe('with no remaining bids', () => {
it('should pass a done callback that triggers onCompletion', () => {
bidderBidInterceptor(...interceptorArgs());
Expand All @@ -387,6 +413,12 @@ describe('bidderBidInterceptor', () => {
expect(onCompletion.calledOnce).to.be.true;
});

it('should call onResponse', () => {
const onResponse = sinon.stub();
bidderBidInterceptor(...interceptorArgs({cbs: {onResponse}}));
sinon.assert.called(onResponse);
})

it('should not call next()', () => {
bidderBidInterceptor(...interceptorArgs());
expect(next.called).to.be.false;
Expand Down

0 comments on commit 06ead4c

Please sign in to comment.