Repository navigation
Expand file tree
/
Copy pathaudio_spec.js
More file actions
141 lines (119 loc) · 5.3 KB
/
Copy pathaudio_spec.js
File metadata and controls
141 lines (119 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// These coverage tests were written by Codex, an AI agent.
import { INSTREAM, OUTSTREAM, ORTB_AUDIO_PARAMS, isValidAudioBid } from 'src/audio.js';
import { auctionManager } from 'src/auctionManager.js';
import { config } from 'src/config.js';
import { hook } from 'src/hook.js';
import * as utils from 'src/utils.js';
import { stubAuctionIndex } from '../helpers/indexStub.js';
describe('audio.js', () => {
let sandbox;
let logError;
before(() => {
hook.ready();
});
beforeEach(() => {
sandbox = sinon.createSandbox();
logError = sandbox.stub(utils, 'logError');
});
afterEach(() => {
config.resetConfig();
sandbox.restore();
});
function validate(bid = {}, audio = {}, adUnitProperties = {}) {
const adUnits = [{
adUnitId: 'audio-unit',
mediaTypes: { audio },
...adUnitProperties
}];
return isValidAudioBid({ adUnitId: 'audio-unit', ...bid }, {
index: stubAuctionIndex({ adUnits })
});
}
describe('isValidAudioBid', () => {
[INSTREAM, undefined].forEach(context => {
describe(`with ${context === undefined ? 'the default' : context} context`, () => {
it('accepts a VAST URL without requiring a cache', () => {
expect(validate({ vastUrl: 'https://example.com/ad.xml' }, { context })).to.equal(true);
sinon.assert.notCalled(logError);
});
it('rejects a bid without VAST XML or a VAST URL', () => {
expect(validate({}, { context })).to.equal(false);
});
it('rejects VAST XML alone when no cache is configured', () => {
expect(validate({ vastXml: '<VAST/>' }, { context })).to.equal(false);
sinon.assert.calledOnce(logError);
expect(logError.firstCall.args[0]).to.include('vastXml').and.include('useLocal');
});
});
});
[
{ url: 'https://cache.example.com' },
{ useLocal: true }
].forEach(cache => {
it(`accepts VAST XML alone with ${cache.useLocal ? 'local' : 'remote'} caching`, () => {
config.setConfig({ cache });
expect(validate({ vastXml: '<VAST/>' })).to.equal(true);
sinon.assert.notCalled(logError);
});
});
it('rejects VAST XML alone when the cache configuration is empty', () => {
config.setConfig({ cache: {} });
expect(validate({ vastXml: '<VAST/>' })).to.equal(false);
sinon.assert.calledOnce(logError);
});
it('accepts VAST XML accompanied by a URL without a cache', () => {
expect(validate({ vastXml: '<VAST/>', vastUrl: 'https://example.com/ad.xml' })).to.equal(true);
sinon.assert.notCalled(logError);
});
it('does not require VAST assets when the ad unit has no audio media type', () => {
expect(validate({}, undefined, { mediaTypes: {} })).to.equal(true);
});
it('uses the auction manager index when no index is supplied', () => {
const bid = { adUnitId: 'audio-unit', vastUrl: 'https://example.com/ad.xml' };
const getMediaTypes = sandbox.stub(auctionManager.index, 'getMediaTypes').returns({ audio: {} });
const getAdUnit = sandbox.stub(auctionManager.index, 'getAdUnit').returns({ adUnitId: 'audio-unit' });
expect(isValidAudioBid(bid)).to.equal(true);
sinon.assert.calledOnceWithExactly(getMediaTypes, bid);
sinon.assert.calledOnceWithExactly(getAdUnit, bid);
});
describe('with outstream context', () => {
const renderer = { url: 'https://example.com/renderer.js', render: () => {} };
it('accepts a renderer supplied on the bid', () => {
expect(validate({ renderer }, { context: OUTSTREAM })).to.equal(true);
});
it('accepts a renderer supplied on the ad unit', () => {
expect(validate({}, { context: OUTSTREAM }, { renderer })).to.equal(true);
});
it('accepts a renderer supplied in the audio media type', () => {
expect(validate({}, { context: OUTSTREAM, renderer })).to.equal(true);
});
it('rejects a bid without a renderer even when it has a VAST URL', () => {
expect(validate({ vastUrl: 'https://example.com/ad.xml' }, { context: OUTSTREAM })).to.equal(false);
});
it('requires VAST assets instead of a renderer when useCacheKey is enabled', () => {
expect(validate({ renderer }, { context: OUTSTREAM, useCacheKey: true })).to.equal(false);
expect(validate({ vastUrl: 'https://example.com/ad.xml' }, { context: OUTSTREAM, useCacheKey: true })).to.equal(true);
});
it('requires caching for VAST XML alone when useCacheKey is enabled', () => {
const audio = { context: OUTSTREAM, useCacheKey: true };
expect(validate({ vastXml: '<VAST/>' }, audio)).to.equal(false);
config.setConfig({ cache: { useLocal: true } });
expect(validate({ vastXml: '<VAST/>' }, audio)).to.equal(true);
});
});
});
describe('OpenRTB audio MIME validation', () => {
[
{ value: ['audio/mpeg'], valid: true },
{ value: ['audio/mpeg', 'audio/mp4'], valid: true },
{ value: [], valid: false },
{ value: 'audio/mpeg', valid: false },
{ value: ['audio/mpeg', 1], valid: false },
{ value: null, valid: false }
].forEach(({ value, valid }) => {
it(`${valid ? 'accepts' : 'rejects'} ${JSON.stringify(value)}`, () => {
expect(ORTB_AUDIO_PARAMS.get('mimes')(value)).to.equal(valid);
});
});
});
});