BlueGit

projects-timfalken-com meteors

Files

meteors / test / login-prompt.test.js

login-prompt.test.js

login-prompt.test.js 5.9 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const fs = require('fs');
6const path = require('path');
7const LoginPrompt = require('../src/loginPrompt.js');
8const catalog = require('../server/lib/i18n.json');
9
10const root = path.join(__dirname, '..');
11const html = fs.readFileSync(path.join(root, 'build/index.html'), 'utf8');
12const css = fs.readFileSync(path.join(root, 'build/style.css'), 'utf8');
13const main = fs.readFileSync(path.join(root, 'src/main.js'), 'utf8');
14const buildSh = fs.readFileSync(path.join(root, 'src/build.sh'), 'utf8');
15
16function memoryStore (initial)
17{
18 const data = Object.assign({}, initial || {});
19 return {
20 getItem (k) { return Object.prototype.hasOwnProperty.call(data, k) ? data[k] : null; },
21 setItem (k, v) { data[k] = String(v); },
22 _data: data
23 };
24}
25
26describe('LoginPrompt decision (t1246)', () =>
27{
28 it('shows the prompt only when logged out, not remembered, not an auth return', () =>
29 {
30 assert.equal(LoginPrompt.shouldShow({}), true);
31 assert.equal(LoginPrompt.shouldShow({ me: null }), true);
32 assert.equal(LoginPrompt.shouldShow({ me: { id: 'u1', name: 'Tim' } }), false);
33 assert.equal(LoginPrompt.shouldShow({ remembered: true }), false);
34 assert.equal(LoginPrompt.shouldShow({ fromAuthReturn: true }), false);
35 assert.equal(LoginPrompt.shouldShow({
36 me: { id: 'u1' }, remembered: true, fromAuthReturn: true
37 }), false);
38 });
39
40 it('stores the session choice in the given storage under a stable key', () =>
41 {
42 const store = memoryStore();
43 assert.equal(LoginPrompt.STORAGE_KEY, 'meteors-login-prompt');
44 assert.equal(LoginPrompt.remembered(store), false);
45 assert.equal(LoginPrompt.remember(store), true);
46 assert.equal(store.getItem(LoginPrompt.STORAGE_KEY), '1');
47 assert.equal(LoginPrompt.remembered(store), true);
48 assert.equal(LoginPrompt.remembered(memoryStore()), false);
49 });
50
51 it('treats a missing or throwing storage as not remembered', () =>
52 {
53 assert.equal(LoginPrompt.remembered(null), false);
54 assert.equal(LoginPrompt.remember(null), false);
55 const boom = {
56 getItem () { throw new Error('blocked'); },
57 setItem () { throw new Error('blocked'); }
58 };
59 assert.equal(LoginPrompt.remembered(boom), false);
60 assert.equal(LoginPrompt.remember(boom), false);
61 });
62
63 it('detects a return from the auth callback or login URL', () =>
64 {
65 assert.equal(LoginPrompt.isAuthReturn('', ''), false);
66 assert.equal(LoginPrompt.isAuthReturn('?debug=1', ''), false);
67 assert.equal(LoginPrompt.isAuthReturn('?online=1', ''), true);
68 assert.equal(LoginPrompt.isAuthReturn('online=1', ''), true);
69 assert.equal(LoginPrompt.isAuthReturn('?foo=1&online=1', ''), true);
70 assert.equal(LoginPrompt.isAuthReturn('?auth=unconfigured', ''), true);
71 assert.equal(LoginPrompt.isAuthReturn('', 'https://projects.timfalken.com/meteors/callback.php'), true);
72 assert.equal(LoginPrompt.isAuthReturn('', 'https://auth.timfalken.com/oauth/authorize?client_id=x'), true);
73 assert.equal(LoginPrompt.isAuthReturn('', 'https://projects.timfalken.com/meteors/index.html'), false);
74 });
75});
76
77describe('LoginPrompt wiring (t1246)', () =>
78{
79 it('is included in the game build before main.js', () =>
80 {
81 assert.match(buildSh, /loginPrompt\.js/);
82 const order = buildSh.indexOf('loginPrompt.js');
83 const mainAt = buildSh.indexOf('main.js');
84 assert.ok(order >= 0 && mainAt > order, 'loginPrompt.js is concatenated before main.js');
85 });
86
87 it('has a startup dialog with Log in and Play as Guest, in the game style', () =>
88 {
89 assert.match(html, /id="login-prompt"[^>]*role="dialog"/);
90 assert.match(html, /id="login-prompt"[^>]*aria-modal="true"/);
91 assert.match(html, /id="login-prompt"[^>]*hidden/);
92 assert.match(html, /id="login-prompt-title"[^>]*data-i18n="loginPrompt\.title"/);
93 assert.match(html, /data-i18n="loginPrompt\.body"/);
94 assert.match(html, /id="btn-login-prompt"[^>]*href="auth\.php"/);
95 assert.match(html, /id="btn-login-prompt"[^>]*data-i18n="menu\.login"/);
96 assert.match(html, /id="btn-play-guest"[^>]*data-i18n="loginPrompt\.guest"/);
97 assert.match(html, /id="login-prompt-local"/);
98 // Must be in the hidden-override list, otherwise .screen { display:flex } wins.
99 assert.match(css, /#login-prompt\[hidden\]/);
100 assert.match(css, /#login-prompt\s*\{[^}]*z-index:\s*[5-9]\d/);
101 });
102
103 it('uses the existing login URL (menu, not the online-panel return) and remembers the session choice', () =>
104 {
105 assert.match(main, /function maybeShowLoginPrompt/);
106 assert.match(main, /function bindLoginPrompt/);
107 assert.match(main, /LoginPrompt\.remember\(/);
108 assert.match(main, /LoginPrompt\.shouldShow\(/);
109 assert.match(main, /LoginPrompt\.isAuthReturn\(/);
110 assert.match(main, /btn-play-guest/);
111 assert.match(main, /btn-login-prompt/);
112 assert.doesNotMatch(main, /btn-login-prompt[\s\S]{0,400}return=online/);
113 // After Online.refresh, and also when Online is missing.
114 assert.match(main, /Online\.refresh\(\)\.then\([\s\S]*?maybeShowLoginPrompt\(\)/);
115 assert.match(main, /else\s*\n\s*\{\s*\n\s*refreshMenuAccount\(\);\s*\n\s*maybeShowLoginPrompt\(\);/);
116 });
117
118 it('translates the prompt in every language', () =>
119 {
120 const wantTitle = {
121 en: 'Log in?', nl: 'Inloggen?', de: 'Anmelden?', fr: 'Se connecter ?',
122 es: '¿Iniciar sesión?', pl: 'Zalogować się?', ru: 'Войти?'
123 };
124 const wantGuest = {
125 en: 'Play as Guest', nl: 'Speel als gast', de: 'Als Gast spielen',
126 fr: 'Jouer en invité', es: 'Jugar como invitado', pl: 'Graj jako gość',
127 ru: 'Играть как гость'
128 };
129 for (const loc of catalog.localeIds)
130 {
131 const L = catalog.locales[loc];
132 assert.ok(L['loginPrompt.title'], loc + ' title');
133 assert.ok(L['loginPrompt.body'], loc + ' body');
134 assert.ok(L['loginPrompt.guest'], loc + ' guest');
135 if (wantTitle[loc])
136 { assert.equal(L['loginPrompt.title'], wantTitle[loc]); }
137 if (wantGuest[loc])
138 { assert.equal(L['loginPrompt.guest'], wantGuest[loc]); }
139 assert.notEqual(L['loginPrompt.guest'], L['menu.login'], loc);
140 }
141 });
142});