aboutsummaryrefslogtreecommitdiff
path: root/test/sponsors-test.ts
blob: 216af311e13f77fdd75735271674fdf3d5892b0d (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (c) 2020, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import fs from 'fs';

import {unwrap} from '../lib/assert.js';
import {Sponsor} from '../lib/sponsors.interfaces.js';
import {loadSponsorsFromString, makeIconSets, parse} from '../lib/sponsors.js';

import {resolvePathFromTestRoot, should} from './utils.js';

describe('Sponsors', () => {
    it('should expand names to objects', () => {
        parse('moo').name.should.eq('moo');
    });
    it('should handle just names', () => {
        parse({name: 'moo'}).name.should.eq('moo');
    });
    it('should default empty params', () => {
        const obj = parse('moo');
        should.equal(obj.description, undefined);
        should.equal(obj.url, undefined);
        obj.onclick.should.eq('');
        should.equal(obj.img, undefined);
        should.equal(obj.icon, undefined);
        should.equal(obj.icon_dark, undefined);
        obj.topIconShowEvery.should.eq(0);
        obj.sideBySide.should.be.false;
        should.equal(obj.statsId, undefined);
    });
    it('should make descriptions always one-sized arrays', () => {
        unwrap(parse({name: 'moo', description: 'desc'}).description).should.deep.eq(['desc']);
    });
    it('should pass through descriptions', () => {
        unwrap(parse({name: 'moo', description: ['desc1', 'desc2']}).description).should.deep.eq(['desc1', 'desc2']);
    });
    it('should pass through icons', () => {
        unwrap(parse({name: 'bob', icon: 'icon'}).icon).should.eq('icon');
    });
    it('should pick icons over images', () => {
        unwrap(parse({name: 'bob', img: 'img', icon: 'icon'}).icon).should.eq('icon');
    });
    it('should pick icons if not img', () => {
        unwrap(parse({name: 'bob', img: 'img'}).icon).should.eq('img');
    });
    it('should pick dark icons if specified', () => {
        unwrap(parse({name: 'bob', icon: 'icon', icon_dark: 'icon_dark'}).icon_dark).should.eq('icon_dark');
    });
    it('should handle topIcons', () => {
        parse({name: 'bob', topIconShowEvery: 2}).topIconShowEvery.should.eq(2);
    });
    it('should handle clicks', () => {
        parse({
            name: 'bob',
            url: 'https://some.host/click',
        }).onclick.should.eq('window.onSponsorClick("https://some.host/click");');
    });

    it('should load a simple example', () => {
        const sample = loadSponsorsFromString(`
---
levels:
  - name: Patreon Legends
    description: 'These amazing people have pledged at the highest level of support. Huge thanks to these fine folks:'
    sponsors:
      - name: Kitty Kat
        url: https://meow.meow
      - name: Aardvark
        url: https://ard.vark
  - name: Patreons
    description: 'Thanks to all my patrons:'
    sponsors:
      - Lovely
      - People
      - Yay
`);
        sample.should.not.be.null;
        const levels = sample.getLevels();
        levels.length.should.eq(2);
        levels[0].name.should.eq('Patreon Legends');
        levels[1].name.should.eq('Patreons');
    });

    it('should sort sponsors by name', () => {
        const peeps = loadSponsorsFromString(`
---
levels:
  - name: a
    description: d
    sponsors:
    - D
    - C
    - A
    - B
        `).getLevels()[0].sponsors;
        peeps.map(sponsor => sponsor.name).should.deep.equals(['A', 'B', 'C', 'D']);
    });
    it('should sort sponsors by priority then name', () => {
        const peeps = loadSponsorsFromString(`
---
levels:
  - name: a
    description: d
    sponsors:
    - name: D
      priority: 100
    - name: C
      priority: 50
    - name: B
      priority: 50
        `).getLevels()[0].sponsors;
        peeps
            .map(sponsor => {
                return {name: sponsor.name, priority: sponsor.priority};
            })
            .should.deep.equals([
                {name: 'D', priority: 100},
                {name: 'B', priority: 50},
                {name: 'C', priority: 50},
            ]);
    });

    it('should pick out all the top level icons', () => {
        const icons = loadSponsorsFromString(`
---
levels:
  - name: a
    description: d
    sponsors:
    - name: one
      img: pick_me
      topIconShowEvery: 1
    - name: two
      img: not_me
  - name: another level
    description: more
    sponsors:
    - name: three
      img: not_me_either
      topIconShowEvery: 0
    - name: four
      img: pick_me_also
      topIconShowEvery: 2
    - name: five
      topIconShowEvery: 3
        `).getAllTopIcons();
        icons.map(s => s.name).should.deep.equals(['one', 'four']);
    });

    it('should pick icons appropriately when all required every 3', () => {
        const sponsor1 = parse({name: 'Sponsor1', topIconShowEvery: 3, icon: '1'});
        const sponsor2 = parse({name: 'Sponsor2', topIconShowEvery: 3, icon: '2'});
        const sponsor3 = parse({name: 'Sponsor3', topIconShowEvery: 3, icon: '3'});
        const icons = [sponsor1, sponsor2, sponsor3];
        makeIconSets(icons, 10).should.deep.eq([icons]);
        makeIconSets(icons, 3).should.deep.eq([icons]);
        makeIconSets(icons, 2).should.deep.eq([
            [sponsor1, sponsor2],
            [sponsor1, sponsor3],
            [sponsor2, sponsor3],
        ]);
        makeIconSets(icons, 1).should.deep.eq([[sponsor1], [sponsor2], [sponsor3]]);
    });
    it('should pick icons appropriately when not required on different schedules', () => {
        const sponsor1 = parse({name: 'Sponsor1', topIconShowEvery: 1, icon: '1'});
        const sponsor2 = parse({name: 'Sponsor2', topIconShowEvery: 2, icon: '2'});
        const sponsor3 = parse({name: 'Sponsor3', topIconShowEvery: 3, icon: '3'});
        const icons = [sponsor1, sponsor2, sponsor3];
        makeIconSets(icons, 10).should.deep.eq([icons]);
        makeIconSets(icons, 3).should.deep.eq([icons]);
        makeIconSets(icons, 2).should.deep.eq([
            [sponsor1, sponsor2],
            [sponsor1, sponsor3],
        ]);
        (() => makeIconSets(icons, 1)).should.throw();
    });
    it('should pick icons appropriately with a lot of sponsors on representative schedules', () => {
        const sponsor1 = parse({name: 'Sponsor1', topIconShowEvery: 1, icon: '1'});
        const sponsor2 = parse({name: 'Sponsor2', topIconShowEvery: 3, icon: '2'});
        const sponsor3 = parse({name: 'Sponsor3', topIconShowEvery: 3, icon: '3'});
        const sponsor4 = parse({name: 'Sponsor4', topIconShowEvery: 3, icon: '3'});
        const sponsor5 = parse({name: 'Sponsor5', topIconShowEvery: 3, icon: '3'});
        const icons = [sponsor1, sponsor2, sponsor3, sponsor4, sponsor5];
        makeIconSets(icons, 10).should.deep.eq([icons]);
        makeIconSets(icons, 3).should.deep.eq([
            [sponsor1, sponsor2, sponsor3],
            [sponsor1, sponsor4, sponsor5],
        ]);
        (() => makeIconSets(icons, 1)).should.throw();
    });
    it('should handle alternating', () => {
        const sponsor1 = parse({name: 'Sponsor1', topIconShowEvery: 1, icon: '1'});
        const sponsor2 = parse({name: 'Sponsor2', topIconShowEvery: 1, icon: '2'});
        const sponsor3 = parse({name: 'Sponsor3', topIconShowEvery: 2, icon: '3'});
        const sponsor4 = parse({name: 'Sponsor4', topIconShowEvery: 2, icon: '4'});
        const icons = [sponsor1, sponsor2, sponsor3, sponsor4];
        makeIconSets(icons, 4).should.deep.eq([icons]);
        makeIconSets(icons, 3).should.deep.eq([
            [sponsor1, sponsor2, sponsor3],
            [sponsor1, sponsor2, sponsor4],
        ]);
        (() => makeIconSets(icons, 2)).should.throw();
    });
});

describe('Our specific sponsor file', () => {
    const stringConfig = fs.readFileSync(resolvePathFromTestRoot('../etc/config/sponsors.yaml')).toString();
    it('should parse the current config', () => {
        loadSponsorsFromString(stringConfig);
    });
    it('should pick appropriate sponsor icons', () => {
        const numLoads = 100;
        const expectedNumIcons = 2;

        const sponsors = loadSponsorsFromString(stringConfig);
        const picks: Sponsor[][] = [];
        for (let load = 0; load < numLoads; ++load) {
            picks.push(sponsors.pickTopIcons());
        }
        const countBySponsor = new Map();
        for (const pick of picks) {
            for (const sponsor of pick) {
                countBySponsor.set(sponsor, (countBySponsor.get(sponsor) || 0) + 1);
            }
            pick.length.should.eq(expectedNumIcons);
        }
        for (const topIcon of sponsors.getAllTopIcons()) {
            const appearsEvery = countBySponsor.get(topIcon) / numLoads;
            appearsEvery.should.lte(topIcon.topIconShowEvery);
        }
    });
});