'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const fs = require('fs'); const path = require('path'); const Avatar = require('../src/avatar.js'); const catalog = require('../server/lib/i18n.json'); const root = path.join(__dirname, '..'); const html = fs.readFileSync(path.join(root, 'build/index.html'), 'utf8'); const css = fs.readFileSync(path.join(root, 'build/style.css'), 'utf8'); const main = fs.readFileSync(path.join(root, 'src/main.js'), 'utf8'); const online = fs.readFileSync(path.join(root, 'src/online.js'), 'utf8'); const api = fs.readFileSync(path.join(root, 'server/api.php'), 'utf8'); const boot = fs.readFileSync(path.join(root, 'server/lib/bootstrap.php'), 'utf8'); const buildSh = fs.readFileSync(path.join(root, 'src/build.sh'), 'utf8'); describe('Leaderboard wiring (t1247)', () => { it('places the Leaderboard button under Play online', () => { const onlineAt = html.indexOf('id="btn-online"'); const boardAt = html.indexOf('id="btn-leaderboard"'); const designerAt = html.indexOf('id="btn-designer"'); assert.ok(onlineAt >= 0 && boardAt > onlineAt && designerAt > boardAt, 'button order'); assert.match(html, /id="btn-leaderboard"[^>]*data-i18n="menu\.leaderboard"/); }); it('has a two-tab leaderboard screen with login prompt for guests', () => { assert.match(html, /id="leaderboard"[^>]*class="screen"/); assert.match(html, /id="lb-tab-global"[^>]*data-i18n="leaderboard\.global"/); assert.match(html, /id="lb-tab-friends"[^>]*data-i18n="leaderboard\.friends"/); assert.match(html, /id="lb-login"/); assert.match(html, /id="lb-login-link"[^>]*href="auth\.php"/); assert.match(html, /id="lb-list"/); assert.match(css, /#leaderboard\[hidden\]/); assert.match(css, /\.lb-row\.you/); assert.match(css, /\.player-avatar/); }); it('loads the public API and highlights the current player', () => { assert.match(online, /Online\.leaderboard\s*=\s*function/); assert.match(online, /action=leaderboard/); assert.match(main, /function openLeaderboard/); assert.match(main, /function renderLeaderboard/); assert.match(main, /p\.you \|\| \(meId && p\.id === meId\)/); assert.match(main, /Avatar\.piece/); assert.match(main, /tab === 'friends' && !loggedIn/); assert.match(api, /action === 'leaderboard'/); assert.match(api, /leaderboardKnownIdentities\(/); assert.match(boot, /authRequest\('\/api\/friends'/); assert.match(buildSh, /avatar\.js/); }); it('translates the leaderboard in every language', () => { const want = { en: 'Leaderboard', nl: 'Ranglijst', de: 'Rangliste', fr: 'Classement', es: 'Clasificación' }; for (const loc of catalog.localeIds) { const L = catalog.locales[loc]; assert.ok(L['menu.leaderboard'], loc); assert.ok(L['leaderboard.friends'], loc); assert.ok(L['leaderboard.loginPrompt'], loc); assert.match(L['leaderboard.points'], /\{n\}/, loc + ' points placeholder'); if (want[loc]) { assert.equal(L['menu.leaderboard'], want[loc]); } } }); }); describe('Avatar helpers (t1247)', () => { it('builds initials and rejects a javascript picture URL', () => { assert.equal(Avatar.initials('Tim Falken'), 'TF'); assert.equal(Avatar.initials('timfa'), 'TI'); assert.equal(Avatar.initials(''), '?'); assert.equal(Avatar.usableUrl('https://auth.timfalken.com/avatar/1'), 'https://auth.timfalken.com/avatar/1'); assert.equal(Avatar.usableUrl('javascript:alert(1)'), null); assert.equal(Avatar.usableUrl(''), null); }); }); describe('leaderboard button (t1256)', () => { it('binds click and a mobile pointerup path', () => { const main = fs.readFileSync(path.join(root, 'src/main.js'), 'utf8'); const bind = main.split('function bindLeaderboard')[1] || ''; assert.match(bind, /btn-leaderboard/); assert.match(bind, /pointerup/); assert.match(bind, /pointerType === 'touch'/); }); }); describe('leaderboard public profiles (t1253)', () => { it('resolves names via the public auth profiles API', () => { assert.match(boot, /\/api\/profiles/); assert.match(boot, /fetchAuthProfiles\(/); assert.match(api, /leaderboardKnownIdentities\(/); }); });