'use strict'; // t1278: the multiplayer end path must produce a tally (earned > 0) // after a rematch in the same lobby, using the server's online_awarded // the same way play_finish uses awarded. const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const fs = require('fs'); const path = require('path'); const Online = require('../src/online.js'); const Reward = require('../src/reward.js'); const main = fs.readFileSync(path.join(__dirname, '../src/main.js'), 'utf8'); const store = fs.readFileSync(path.join(__dirname, '../server/lib/Store.php'), 'utf8'); describe('Online reward totals (t1278)', () => { it('rewardResult uses awarded the same way as play_finish', () => { assert.deepEqual( Online.rewardResult({ lifetimePoints: 250, awarded: 70, before: 100 }), { before: 180, after: 250 } ); assert.deepEqual( Online.rewardResult({ lifetimePoints: 90, awarded: 90, before: 0 }), { before: 0, after: 90 } ); assert.deepEqual( Online.rewardResult({ lifetimePoints: '150', awarded: '25', before: '0' }), { before: 125, after: 150 }, 'string payloads from JSON still count' ); }); it('falls back to the pre-match snapshot when awarded is unknown', () => { assert.deepEqual( Online.rewardResult({ lifetimePoints: 190, before: 100 }), { before: 100, after: 190 } ); assert.deepEqual( Online.rewardResult({ lifetimePoints: 100, before: 100 }), { before: 100, after: 100 }, 'equal totals (the rematch bug) yield earned 0' ); assert.equal(Online.rewardResult({ lifetimePoints: 'nope' }), null); }); it('_apply stores online_awarded from a finished poll', () => { Online.lifetimePoints = 0; Online.onlineAwarded = null; Online._apply({ ok: true, lifetime_points: 160, online_awarded: 70 }); assert.equal(Online.lifetimePoints, 160); assert.equal(Online.onlineAwarded, 70); const res = Online.rewardResult({ lifetimePoints: Online.lifetimePoints, awarded: Online.onlineAwarded, before: 90 }); assert.deepEqual(res, { before: 90, after: 160 }); const seq = Reward.createSequence({ intro: 0, rise: 0, flight: 0, hold: 0 }); seq.setResult(res); assert.equal(seq.earned, 70); const out = seq.tick(0); assert.equal(out.phase, 'count'); assert.ok(out.spawn.length >= 1, 'tally starts (blocks spawn)'); }); it('onFinished in main.js builds the result via Online.rewardResult', () => { const i = main.indexOf('Online.onFinished = function'); assert.ok(i >= 0); const body = main.slice(i, i + 900); assert.match(body, /Online\.rewardResult/); assert.match(body, /Online\.onlineAwarded/); assert.match(body, /showReward\(/); }); it('the server award key includes the match start time', () => { assert.match(store, /'online:' \. \$id \. ':' \. \$started \. ':' \. \$userId/); assert.match(store, /function onlineAwardKeyForMatch/); }); });