client/tests/browser_accounts.mjs (10609B)
1 import assert from 'node:assert/strict'; 2 import fs from 'node:fs'; 3 import vm from 'node:vm'; 4 import test from 'node:test'; 5 6 function accountPage() { 7 const elements = new Map(), requests = [], replies = [], reloads = [], timers = []; 8 class Element extends EventTarget { 9 hidden = false; 10 disabled = false; 11 open = true; 12 value = ''; 13 style = {}; 14 dataset = {}; 15 parentElement = {hidden: false}; 16 classList = {add() {}, remove() {}}; 17 close() { this.open = false; } 18 focus() { this.focused = true; } 19 replaceChildren(...children) { this.children = children; this.value = String(children[0]?.value ?? ''); } 20 } 21 const document = new EventTarget(); 22 document.querySelector = selector => { 23 if (!elements.has(selector)) elements.set(selector, new Element()); 24 return elements.get(selector); 25 }; 26 const window = new EventTarget(); 27 const context = vm.createContext({ 28 document, window, navigator: {userAgent: 'Desktop'}, console, URLSearchParams, crossOriginIsolated: false, 29 Option: class { constructor(text, value) { this.text = text; this.value = String(value); } }, 30 localStorage: {getItem() { return null; }}, 31 location: {reload: () => reloads.push(true), assign: url => reloads.push(url)}, 32 setTimeout: callback => timers.push(callback), 33 updateTouchControls() {}, 34 fetch: async (url, options) => { 35 requests.push({url, options}); 36 const reply = replies.shift(); 37 if (reply instanceof Error) throw reply; 38 return await reply; 39 } 40 }); 41 const html = fs.readFileSync(new URL('../browser/index.html', import.meta.url), 'utf8'); 42 vm.runInContext(html.match(/<script>([\s\S]*?)<\/script>/)[1], context); 43 vm.runInContext(html.match(/<script id="account-controls">([\s\S]*?)<\/script>/)[1], context); 44 return {context, requests, replies, reloads, timers, element: document.querySelector, 45 open: () => context.Module.onOptionsOpen(), 46 load: () => context.Module.loadAccount(), 47 choose: action => document.querySelector('#' + action + '-button').onclick(), 48 submit: () => document.querySelector('#account-form').onsubmit({preventDefault() {}})}; 49 } 50 51 function response(status, body) { 52 return {ok: status >= 200 && status < 300, json: async () => body, text: async () => body}; 53 } 54 55 test('Options offers visitor account flows with credentials hidden until a choice', async () => { 56 const page = accountPage(); 57 page.replies.push(response(200, {name: 'VisitorLupez', visitor: true})); 58 59 await page.load(); 60 page.open(); 61 62 assert.equal(page.element('#claim-button').hidden, false); 63 assert.equal(page.element('#password-button').hidden, true); 64 assert.equal(page.element('#account-choices').disabled, false); 65 assert.equal(page.element('#account-form').hidden, true); 66 assert.match(page.element('#account-current').textContent, /Playing as VisitorLupez/); 67 assert.equal(page.element('#account-password').value, ''); 68 assert.equal(page.requests[0].url, 'browser-account'); 69 page.choose('claim'); 70 assert.equal(page.element('#account-name').value, 'Lupez'); 71 assert.equal(page.element('#account-name').disabled, false); 72 }); 73 74 test('an adopted player can change password with adoption hidden', async () => { 75 const page = accountPage(); 76 page.replies.push(response(200, {name: 'Alice', visitor: false, adopted: true})); 77 78 await page.load(); 79 page.open(); 80 81 assert.equal(page.element('#claim-button').hidden, true); 82 assert.equal(page.element('#password-button').hidden, false); 83 assert.equal(page.element('#account-choices').disabled, false); 84 assert.equal(page.element('#account-current').textContent, 'Playing as Alice.'); 85 }); 86 87 for (const password of ['updated password', 'valid']) { 88 test(`password change accepts ${password} without reloading`, async () => { 89 const page = accountPage(); 90 page.choose('password'); 91 page.element('#account-password').value = password; 92 page.replies.push(response(204, '')); 93 94 await page.submit(); 95 96 assert.equal(page.requests[0].url, 'browser-account/password'); 97 assert.equal(page.requests[0].options.body.get('password'), password); 98 assert.deepEqual(page.reloads, []); 99 assert.equal(page.element('#account-form').hidden, true); 100 assert.equal(page.element('#account-choices').hidden, false); 101 assert.equal(page.element('#account-password').value, ''); 102 assert.equal(page.element('#account-error').textContent, 'Password changed.'); 103 assert.equal(page.element('#password-button').focused, true); 104 105 // Choosing another flow restores the ordinary name/password fields. 106 page.choose('login'); 107 assert.equal(page.element('#account-name').disabled, false); 108 assert.equal(page.element('#account-name').parentElement.hidden, false); 109 assert.equal(page.element('#account-password-label').textContent, 'Password'); 110 assert.equal(page.element('#account-error').textContent, ''); 111 }); 112 } 113 114 for (const action of ['claim', 'login']) { 115 test(`${action} posts exact credentials and reloads after success despite a session replacement notification`, async () => { 116 const page = accountPage(); 117 page.choose(action); 118 page.element('#account-name').value = 'Alice'; 119 page.element('#account-password').value = ' spaced password '; 120 let finish; 121 page.replies.push(new Promise(resolve => { finish = resolve; })); 122 123 const pending = page.submit(); 124 page.context.Module.onGameClosed(true); 125 126 assert.equal(page.element('#player-menu').open, true); 127 assert.equal(page.element('#account-fields').disabled, true); 128 assert.deepEqual(page.reloads, []); 129 assert.equal(page.requests[0].url, 'browser-account/' + action); 130 assert.equal(page.requests[0].options.method, 'POST'); 131 assert.equal(page.requests[0].options.body.get('name'), 'Alice'); 132 assert.equal(page.requests[0].options.body.get('password'), ' spaced password '); 133 134 await page.submit(); 135 finish(response(204, '')); 136 await pending; 137 138 assert.equal(page.requests.length, 1); 139 assert.deepEqual(page.reloads, [true]); 140 assert.deepEqual(page.timers, []); 141 }); 142 } 143 144 test('failed login keeps the current game and form available without reloading', async () => { 145 const page = accountPage(); 146 page.choose('login'); 147 page.replies.push(response(401, 'Incorrect player name or password.')); 148 149 await page.submit(); 150 151 assert.deepEqual(page.reloads, []); 152 assert.equal(page.element('#account-error').textContent, 'Incorrect player name or password.'); 153 assert.equal(page.element('#player-menu').open, true); 154 assert.equal(page.element('#account-fields').disabled, false); 155 }); 156 157 test('network failure reports a retryable error and preserves a pending takeover', async () => { 158 const page = accountPage(); 159 page.choose('login'); 160 let fail; 161 page.replies.push(new Promise((_, reject) => { fail = reject; })); 162 163 const pending = page.submit(); 164 page.context.Module.onGameClosed(true); 165 fail(new Error('Connection lost')); 166 await pending; 167 168 assert.match(page.element('#account-error').textContent, /Please try again/); 169 assert.equal(page.element('#player-menu').open, false); 170 assert.equal(page.element('#loading').textContent, 'This character connected elsewhere.'); 171 assert.equal(page.element('#reconnect').hidden, false); 172 assert.deepEqual(page.reloads, []); 173 }); 174 175 for (const reply of [response(401, 'Reconnect to continue.'), new Error('Offline')]) { 176 test('an unavailable account lookup leaves Create and Log in usable', async () => { 177 const page = accountPage(); 178 page.element('#account-form').hidden = true; 179 page.replies.push(reply); 180 181 await page.load(); 182 page.open(); 183 184 assert.equal(page.element('#account-panel').hidden, false); 185 assert.equal(page.element('#account-form').hidden, true); 186 assert.equal(page.element('#account-choices').disabled, false); 187 assert(page.element('#account-error').textContent.length > 0); 188 assert.deepEqual(page.reloads, []); 189 }); 190 } 191 192 test('Options opens immediately during loading and reuses identity on every opening', async () => { 193 const page = accountPage(); 194 let finish; 195 page.replies.push(new Promise(resolve => { finish = resolve; })); 196 const loading = page.load(); 197 198 page.open(); 199 assert.equal(page.element('#account-choices').disabled, false); 200 page.choose('login'); 201 assert.equal(page.element('#account-form').hidden, false); 202 finish(response(200, {name: 'Alice', visitor: false, adopted: true})); 203 await loading; 204 page.open(); 205 page.open(); 206 207 assert.equal(page.requests.length, 1); 208 assert.equal(page.element('#password-button').hidden, false); 209 assert.equal(page.element('#account-choices').disabled, false); 210 }); 211 212 test('Create launches the native creator without submitting account fields', async () => { 213 const page = accountPage(); 214 page.replies.push(response(200, {name: 'VisitorLupez', visitor: true})); 215 await page.load(); 216 page.open(); 217 218 page.choose('create'); 219 220 assert.deepEqual(page.reloads, ['?create=1']); 221 assert.equal(page.element('#account-form').hidden, true); 222 assert.equal(page.requests.length, 1); 223 }); 224 225 test('Back clears credentials before switching account flows', async () => { 226 const page = accountPage(); 227 page.choose('claim'); 228 page.element('#account-name').value = 'Alice'; 229 page.element('#account-password').value = 'secret'; 230 231 page.element('#account-back').onclick(); 232 await page.submit(); 233 234 assert.equal(page.element('#account-form').hidden, true); 235 assert.equal(page.element('#account-choices').hidden, false); 236 assert.equal(page.element('#account-name').value, ''); 237 assert.equal(page.element('#account-password').value, ''); 238 assert.equal(page.element('#claim-button').focused, true); 239 assert.equal(page.requests.length, 0); 240 241 page.choose('login'); 242 243 assert.equal(page.element('#account-password').autocomplete, 'current-password'); 244 }); 245 246 for (const action of ['claim', 'password']) { 247 for (const password of ['', 'abcd', '\u00e9\u00e9\u00e9\u00e9']) { 248 test(`${action} refuses short passwords: ${JSON.stringify(password)}`, async () => { 249 const page = accountPage(); 250 page.choose(action); 251 page.element('#account-password').value = password; 252 253 await page.submit(); 254 255 assert.equal(page.requests.length, 0); 256 assert.equal(page.element('#account-error').textContent, 'Use a password of at least 5 characters.'); 257 assert.equal(page.element('#account-password').required, true); 258 assert.equal(page.element('#account-password').minLength, 5); 259 page.choose('login'); 260 assert.equal(page.element('#account-password').required, false); 261 assert.equal(page.element('#account-password').minLength, 0); 262 }); 263 } 264 }