'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const fs = require('fs'); const path = require('path'); const LoginPrompt = require('../src/loginPrompt.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 buildSh = fs.readFileSync(path.join(root, 'src/build.sh'), 'utf8'); function memoryStore (initial) { const data = Object.assign({}, initial || {}); return { getItem (k) { return Object.prototype.hasOwnProperty.call(data, k) ? data[k] : null; }, setItem (k, v) { data[k] = String(v); }, _data: data }; } describe('LoginPrompt decision (t1246)', () => { it('shows the prompt only when logged out, not remembered, not an auth return', () => { assert.equal(LoginPrompt.shouldShow({}), true); assert.equal(LoginPrompt.shouldShow({ me: null }), true); assert.equal(LoginPrompt.shouldShow({ me: { id: 'u1', name: 'Tim' } }), false); assert.equal(LoginPrompt.shouldShow({ remembered: true }), false); assert.equal(LoginPrompt.shouldShow({ fromAuthReturn: true }), false); assert.equal(LoginPrompt.shouldShow({ me: { id: 'u1' }, remembered: true, fromAuthReturn: true }), false); }); it('stores the session choice in the given storage under a stable key', () => { const store = memoryStore(); assert.equal(LoginPrompt.STORAGE_KEY, 'meteors-login-prompt'); assert.equal(LoginPrompt.remembered(store), false); assert.equal(LoginPrompt.remember(store), true); assert.equal(store.getItem(LoginPrompt.STORAGE_KEY), '1'); assert.equal(LoginPrompt.remembered(store), true); assert.equal(LoginPrompt.remembered(memoryStore()), false); }); it('treats a missing or throwing storage as not remembered', () => { assert.equal(LoginPrompt.remembered(null), false); assert.equal(LoginPrompt.remember(null), false); const boom = { getItem () { throw new Error('blocked'); }, setItem () { throw new Error('blocked'); } }; assert.equal(LoginPrompt.remembered(boom), false); assert.equal(LoginPrompt.remember(boom), false); }); it('detects a return from the auth callback or login URL', () => { assert.equal(LoginPrompt.isAuthReturn('', ''), false); assert.equal(LoginPrompt.isAuthReturn('?debug=1', ''), false); assert.equal(LoginPrompt.isAuthReturn('?online=1', ''), true); assert.equal(LoginPrompt.isAuthReturn('online=1', ''), true); assert.equal(LoginPrompt.isAuthReturn('?foo=1&online=1', ''), true); assert.equal(LoginPrompt.isAuthReturn('?auth=unconfigured', ''), true); assert.equal(LoginPrompt.isAuthReturn('', 'https://projects.timfalken.com/meteors/callback.php'), true); assert.equal(LoginPrompt.isAuthReturn('', 'https://auth.timfalken.com/oauth/authorize?client_id=x'), true); assert.equal(LoginPrompt.isAuthReturn('', 'https://projects.timfalken.com/meteors/index.html'), false); }); }); describe('LoginPrompt wiring (t1246)', () => { it('is included in the game build before main.js', () => { assert.match(buildSh, /loginPrompt\.js/); const order = buildSh.indexOf('loginPrompt.js'); const mainAt = buildSh.indexOf('main.js'); assert.ok(order >= 0 && mainAt > order, 'loginPrompt.js is concatenated before main.js'); }); it('has a startup dialog with Log in and Play as Guest, in the game style', () => { assert.match(html, /id="login-prompt"[^>]*role="dialog"/); assert.match(html, /id="login-prompt"[^>]*aria-modal="true"/); assert.match(html, /id="login-prompt"[^>]*hidden/); assert.match(html, /id="login-prompt-title"[^>]*data-i18n="loginPrompt\.title"/); assert.match(html, /data-i18n="loginPrompt\.body"/); assert.match(html, /id="btn-login-prompt"[^>]*href="auth\.php"/); assert.match(html, /id="btn-login-prompt"[^>]*data-i18n="menu\.login"/); assert.match(html, /id="btn-play-guest"[^>]*data-i18n="loginPrompt\.guest"/); assert.match(html, /id="login-prompt-local"/); // Must be in the hidden-override list, otherwise .screen { display:flex } wins. assert.match(css, /#login-prompt\[hidden\]/); assert.match(css, /#login-prompt\s*\{[^}]*z-index:\s*[5-9]\d/); }); it('uses the existing login URL (menu, not the online-panel return) and remembers the session choice', () => { assert.match(main, /function maybeShowLoginPrompt/); assert.match(main, /function bindLoginPrompt/); assert.match(main, /LoginPrompt\.remember\(/); assert.match(main, /LoginPrompt\.shouldShow\(/); assert.match(main, /LoginPrompt\.isAuthReturn\(/); assert.match(main, /btn-play-guest/); assert.match(main, /btn-login-prompt/); assert.doesNotMatch(main, /btn-login-prompt[\s\S]{0,400}return=online/); // After Online.refresh, and also when Online is missing. assert.match(main, /Online\.refresh\(\)\.then\([\s\S]*?maybeShowLoginPrompt\(\)/); assert.match(main, /else\s*\n\s*\{\s*\n\s*refreshMenuAccount\(\);\s*\n\s*maybeShowLoginPrompt\(\);/); }); it('translates the prompt in every language', () => { const wantTitle = { en: 'Log in?', nl: 'Inloggen?', de: 'Anmelden?', fr: 'Se connecter ?', es: '¿Iniciar sesión?', pl: 'Zalogować się?', ru: 'Войти?' }; const wantGuest = { en: 'Play as Guest', nl: 'Speel als gast', de: 'Als Gast spielen', fr: 'Jouer en invité', es: 'Jugar como invitado', pl: 'Graj jako gość', ru: 'Играть как гость' }; for (const loc of catalog.localeIds) { const L = catalog.locales[loc]; assert.ok(L['loginPrompt.title'], loc + ' title'); assert.ok(L['loginPrompt.body'], loc + ' body'); assert.ok(L['loginPrompt.guest'], loc + ' guest'); if (wantTitle[loc]) { assert.equal(L['loginPrompt.title'], wantTitle[loc]); } if (wantGuest[loc]) { assert.equal(L['loginPrompt.guest'], wantGuest[loc]); } assert.notEqual(L['loginPrompt.guest'], L['menu.login'], loc); } }); });