BlueGit

projects-timfalken-com meteors

Files

meteors / test / online-reward.test.js

online-reward.test.js

online-reward.test.js 2.8 KiB Download Raw
1'use strict';
2
3// t1278: the multiplayer end path must produce a tally (earned > 0)
4// after a rematch in the same lobby, using the server's online_awarded
5// the same way play_finish uses awarded.
6
7const { describe, it } = require('node:test');
8const assert = require('node:assert/strict');
9const fs = require('fs');
10const path = require('path');
11const Online = require('../src/online.js');
12const Reward = require('../src/reward.js');
13
14const main = fs.readFileSync(path.join(__dirname, '../src/main.js'), 'utf8');
15const store = fs.readFileSync(path.join(__dirname, '../server/lib/Store.php'), 'utf8');
16
17describe('Online reward totals (t1278)', () =>
18{
19 it('rewardResult uses awarded the same way as play_finish', () =>
20 {
21 assert.deepEqual(
22 Online.rewardResult({ lifetimePoints: 250, awarded: 70, before: 100 }),
23 { before: 180, after: 250 }
24 );
25 assert.deepEqual(
26 Online.rewardResult({ lifetimePoints: 90, awarded: 90, before: 0 }),
27 { before: 0, after: 90 }
28 );
29 assert.deepEqual(
30 Online.rewardResult({ lifetimePoints: '150', awarded: '25', before: '0' }),
31 { before: 125, after: 150 },
32 'string payloads from JSON still count'
33 );
34 });
35
36 it('falls back to the pre-match snapshot when awarded is unknown', () =>
37 {
38 assert.deepEqual(
39 Online.rewardResult({ lifetimePoints: 190, before: 100 }),
40 { before: 100, after: 190 }
41 );
42 assert.deepEqual(
43 Online.rewardResult({ lifetimePoints: 100, before: 100 }),
44 { before: 100, after: 100 },
45 'equal totals (the rematch bug) yield earned 0'
46 );
47 assert.equal(Online.rewardResult({ lifetimePoints: 'nope' }), null);
48 });
49
50 it('_apply stores online_awarded from a finished poll', () =>
51 {
52 Online.lifetimePoints = 0;
53 Online.onlineAwarded = null;
54 Online._apply({ ok: true, lifetime_points: 160, online_awarded: 70 });
55 assert.equal(Online.lifetimePoints, 160);
56 assert.equal(Online.onlineAwarded, 70);
57 const res = Online.rewardResult({
58 lifetimePoints: Online.lifetimePoints,
59 awarded: Online.onlineAwarded,
60 before: 90
61 });
62 assert.deepEqual(res, { before: 90, after: 160 });
63 const seq = Reward.createSequence({ intro: 0, rise: 0, flight: 0, hold: 0 });
64 seq.setResult(res);
65 assert.equal(seq.earned, 70);
66 const out = seq.tick(0);
67 assert.equal(out.phase, 'count');
68 assert.ok(out.spawn.length >= 1, 'tally starts (blocks spawn)');
69 });
70
71 it('onFinished in main.js builds the result via Online.rewardResult', () =>
72 {
73 const i = main.indexOf('Online.onFinished = function');
74 assert.ok(i >= 0);
75 const body = main.slice(i, i + 900);
76 assert.match(body, /Online\.rewardResult/);
77 assert.match(body, /Online\.onlineAwarded/);
78 assert.match(body, /showReward\(/);
79 });
80
81 it('the server award key includes the match start time', () =>
82 {
83 assert.match(store, /'online:' \. \$id \. ':' \. \$started \. ':' \. \$userId/);
84 assert.match(store, /function onlineAwardKeyForMatch/);
85 });
86});