| 1 | 'use strict'; |
| 2 | |
| 3 | const { describe, it } = require('node:test'); |
| 4 | const assert = require('node:assert/strict'); |
| 5 | const fs = require('fs'); |
| 6 | const path = require('path'); |
| 7 | const Avatar = require('../src/avatar.js'); |
| 8 | const catalog = require('../server/lib/i18n.json'); |
| 9 | |
| 10 | const root = path.join(__dirname, '..'); |
| 11 | const html = fs.readFileSync(path.join(root, 'build/index.html'), 'utf8'); |
| 12 | const css = fs.readFileSync(path.join(root, 'build/style.css'), 'utf8'); |
| 13 | const main = fs.readFileSync(path.join(root, 'src/main.js'), 'utf8'); |
| 14 | const online = fs.readFileSync(path.join(root, 'src/online.js'), 'utf8'); |
| 15 | const api = fs.readFileSync(path.join(root, 'server/api.php'), 'utf8'); |
| 16 | const boot = fs.readFileSync(path.join(root, 'server/lib/bootstrap.php'), 'utf8'); |
| 17 | const buildSh = fs.readFileSync(path.join(root, 'src/build.sh'), 'utf8'); |
| 18 | |
| 19 | describe('Leaderboard wiring (t1247)', () => |
| 20 | { |
| 21 | it('places the Leaderboard button under Play online', () => |
| 22 | { |
| 23 | const onlineAt = html.indexOf('id="btn-online"'); |
| 24 | const boardAt = html.indexOf('id="btn-leaderboard"'); |
| 25 | const designerAt = html.indexOf('id="btn-designer"'); |
| 26 | assert.ok(onlineAt >= 0 && boardAt > onlineAt && designerAt > boardAt, 'button order'); |
| 27 | assert.match(html, /id="btn-leaderboard"[^>]*data-i18n="menu\.leaderboard"/); |
| 28 | }); |
| 29 | |
| 30 | it('has a two-tab leaderboard screen with login prompt for guests', () => |
| 31 | { |
| 32 | assert.match(html, /id="leaderboard"[^>]*class="screen"/); |
| 33 | assert.match(html, /id="lb-tab-global"[^>]*data-i18n="leaderboard\.global"/); |
| 34 | assert.match(html, /id="lb-tab-friends"[^>]*data-i18n="leaderboard\.friends"/); |
| 35 | assert.match(html, /id="lb-login"/); |
| 36 | assert.match(html, /id="lb-login-link"[^>]*href="auth\.php"/); |
| 37 | assert.match(html, /id="lb-list"/); |
| 38 | assert.match(css, /#leaderboard\[hidden\]/); |
| 39 | assert.match(css, /\.lb-row\.you/); |
| 40 | assert.match(css, /\.player-avatar/); |
| 41 | }); |
| 42 | |
| 43 | it('loads the public API and highlights the current player', () => |
| 44 | { |
| 45 | assert.match(online, /Online\.leaderboard\s*=\s*function/); |
| 46 | assert.match(online, /action=leaderboard/); |
| 47 | assert.match(main, /function openLeaderboard/); |
| 48 | assert.match(main, /function renderLeaderboard/); |
| 49 | assert.match(main, /p\.you \|\| \(meId && p\.id === meId\)/); |
| 50 | assert.match(main, /Avatar\.piece/); |
| 51 | assert.match(main, /tab === 'friends' && !loggedIn/); |
| 52 | assert.match(api, /action === 'leaderboard'/); |
| 53 | assert.match(api, /leaderboardKnownIdentities\(/); |
| 54 | assert.match(boot, /authRequest\('\/api\/friends'/); |
| 55 | assert.match(buildSh, /avatar\.js/); |
| 56 | }); |
| 57 | |
| 58 | it('translates the leaderboard in every language', () => |
| 59 | { |
| 60 | const want = { en: 'Leaderboard', nl: 'Ranglijst', de: 'Rangliste', fr: 'Classement', es: 'Clasificación' }; |
| 61 | for (const loc of catalog.localeIds) |
| 62 | { |
| 63 | const L = catalog.locales[loc]; |
| 64 | assert.ok(L['menu.leaderboard'], loc); |
| 65 | assert.ok(L['leaderboard.friends'], loc); |
| 66 | assert.ok(L['leaderboard.loginPrompt'], loc); |
| 67 | assert.match(L['leaderboard.points'], /\{n\}/, loc + ' points placeholder'); |
| 68 | if (want[loc]) |
| 69 | { assert.equal(L['menu.leaderboard'], want[loc]); } |
| 70 | } |
| 71 | }); |
| 72 | }); |
| 73 | |
| 74 | describe('Avatar helpers (t1247)', () => |
| 75 | { |
| 76 | it('builds initials and rejects a javascript picture URL', () => |
| 77 | { |
| 78 | assert.equal(Avatar.initials('Tim Falken'), 'TF'); |
| 79 | assert.equal(Avatar.initials('timfa'), 'TI'); |
| 80 | assert.equal(Avatar.initials(''), '?'); |
| 81 | assert.equal(Avatar.usableUrl('https://auth.timfalken.com/avatar/1'), 'https://auth.timfalken.com/avatar/1'); |
| 82 | assert.equal(Avatar.usableUrl('javascript:alert(1)'), null); |
| 83 | assert.equal(Avatar.usableUrl(''), null); |
| 84 | }); |
| 85 | }); |
| 86 | |
| 87 | describe('leaderboard button (t1256)', () => |
| 88 | { |
| 89 | it('binds click and a mobile pointerup path', () => |
| 90 | { |
| 91 | const main = fs.readFileSync(path.join(root, 'src/main.js'), 'utf8'); |
| 92 | const bind = main.split('function bindLeaderboard')[1] || ''; |
| 93 | assert.match(bind, /btn-leaderboard/); |
| 94 | assert.match(bind, /pointerup/); |
| 95 | assert.match(bind, /pointerType === 'touch'/); |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | describe('leaderboard public profiles (t1253)', () => |
| 100 | { |
| 101 | it('resolves names via the public auth profiles API', () => |
| 102 | { |
| 103 | assert.match(boot, /\/api\/profiles/); |
| 104 | assert.match(boot, /fetchAuthProfiles\(/); |
| 105 | assert.match(api, /leaderboardKnownIdentities\(/); |
| 106 | }); |
| 107 | }); |
projects-timfalken-com meteors