| 1 | 'use strict'; |
| 2 | const { describe, it, beforeEach } = require('node:test'); |
| 3 | const assert = require('node:assert/strict'); |
| 4 | const Avatar = require('../src/avatar.js'); |
| 5 | |
| 6 | // Minimal DOM stand-in: enough for Avatar.piece. |
| 7 | function makeDoc() { |
| 8 | function El(tag) { |
| 9 | this.tagName = tag.toUpperCase(); |
| 10 | this.children = []; |
| 11 | this.parentNode = null; |
| 12 | this.attrs = {}; |
| 13 | this.listeners = {}; |
| 14 | this._style = {}; |
| 15 | const self = this; |
| 16 | this.style = new Proxy(this._style, { |
| 17 | get(o, k) { if (k === 'setProperty') { return (n, v) => { o[n] = v; }; } return o[k]; }, |
| 18 | set(o, k, v) { o[k] = v; return true; } |
| 19 | }); |
| 20 | this._classes = new Set(); |
| 21 | this.classList = { |
| 22 | add: (...c) => c.forEach((x) => self._classes.add(x)), |
| 23 | remove: (...c) => c.forEach((x) => self._classes.delete(x)), |
| 24 | contains: (c) => self._classes.has(c) |
| 25 | }; |
| 26 | this.textContent = ''; |
| 27 | } |
| 28 | Object.defineProperty(El.prototype, 'className', { |
| 29 | get() { return [...this._classes].join(' '); }, |
| 30 | set(v) { this._classes = new Set(String(v).split(/\s+/).filter(Boolean)); } |
| 31 | }); |
| 32 | El.prototype.setAttribute = function (k, v) { this.attrs[k] = v; }; |
| 33 | El.prototype.appendChild = function (c) { c.parentNode = this; this.children.push(c); return c; }; |
| 34 | El.prototype.removeChild = function (c) { this.children = this.children.filter((x) => x !== c); c.parentNode = null; }; |
| 35 | El.prototype.addEventListener = function (t, f) { (this.listeners[t] = this.listeners[t] || []).push(f); }; |
| 36 | El.prototype.fire = function (t) { (this.listeners[t] || []).forEach((f) => f()); }; |
| 37 | return { createElement: (t) => new El(t) }; |
| 38 | } |
| 39 | |
| 40 | const find = (el, cls) => el.children.find((c) => c.classList.contains(cls)); |
| 41 | |
| 42 | describe('Avatar helpers', () => { |
| 43 | beforeEach(() => { Avatar.failed = {}; Avatar.loaded = {}; }); |
| 44 | |
| 45 | it('makes initials from names', () => { |
| 46 | assert.equal(Avatar.initials('Tim Falken'), 'TF'); |
| 47 | assert.equal(Avatar.initials('timfa'), 'TI'); |
| 48 | assert.equal(Avatar.initials('a'), 'A'); |
| 49 | assert.equal(Avatar.initials('jan_de_vries'), 'JV'); |
| 50 | assert.equal(Avatar.initials(''), '?'); |
| 51 | assert.equal(Avatar.initials(' '), '?'); |
| 52 | assert.equal(Avatar.initials(null), '?'); |
| 53 | assert.equal(Avatar.initials('Émile Zola'), 'ÉZ'); |
| 54 | }); |
| 55 | |
| 56 | it('rejects empty, whitespace, script and failed URLs', () => { |
| 57 | assert.equal(Avatar.usableUrl(null), null); |
| 58 | assert.equal(Avatar.usableUrl(''), null); |
| 59 | assert.equal(Avatar.usableUrl(' '), null); |
| 60 | assert.equal(Avatar.usableUrl('javascript:alert(1)'), null); |
| 61 | assert.equal(Avatar.usableUrl('https://auth.example/avatar/1'), 'https://auth.example/avatar/1'); |
| 62 | Avatar.failed['https://auth.example/avatar/1'] = true; |
| 63 | assert.equal(Avatar.usableUrl('https://auth.example/avatar/1'), null); |
| 64 | }); |
| 65 | |
| 66 | it('picks readable text colour', () => { |
| 67 | assert.equal(Avatar.textColor('#e8c547'), '#111'); |
| 68 | assert.equal(Avatar.textColor('#1a2a6c'), '#fff'); |
| 69 | assert.equal(Avatar.textColor('bogus'), '#fff'); |
| 70 | }); |
| 71 | }); |
| 72 | |
| 73 | describe('Avatar.piece', () => { |
| 74 | beforeEach(() => { Avatar.failed = {}; Avatar.loaded = {}; }); |
| 75 | const doc = makeDoc(); |
| 76 | |
| 77 | it('without avatar: coloured circle with initials, no img', () => { |
| 78 | const el = Avatar.piece({ id: 'u2', name: 'Opponent Two', color: '#4fa3e8', avatar: null }, { doc }); |
| 79 | assert.ok(el.classList.contains('piece')); |
| 80 | assert.equal(el.style.backgroundColor, '#4fa3e8'); |
| 81 | assert.equal(el.style.borderColor, '#4fa3e8'); |
| 82 | assert.equal(find(el, 'piece-initials').textContent, 'OT'); |
| 83 | assert.equal(find(el, 'piece-img'), undefined); |
| 84 | }); |
| 85 | |
| 86 | it('image error removes only the img; the token itself stays and keeps initials', () => { |
| 87 | const url = 'https://auth.example/avatar/404'; |
| 88 | const cell = doc.createElement('button'); |
| 89 | const el = Avatar.piece({ id: 'u3', name: 'Bob', color: '#e85d5d', avatar: url }, { doc }); |
| 90 | cell.appendChild(el); // token moved into a board cell (like renderBoard) |
| 91 | const img = find(el, 'piece-img'); |
| 92 | assert.ok(img); |
| 93 | img.fire('error'); |
| 94 | assert.equal(cell.children[0], el, 'token still in the cell'); |
| 95 | assert.equal(find(el, 'piece-img'), undefined); |
| 96 | assert.equal(find(el, 'piece-initials').textContent, 'BO'); |
| 97 | assert.equal(Avatar.failed[url], true); |
| 98 | // Next render (3 s poll) skips the broken URL entirely: no flicker. |
| 99 | const again = Avatar.piece({ id: 'u3', name: 'Bob', color: '#e85d5d', avatar: url }, { doc }); |
| 100 | assert.equal(find(again, 'piece-img'), undefined); |
| 101 | }); |
| 102 | |
| 103 | it('loaded image hides initials via has-img; remembered across renders', () => { |
| 104 | const url = 'https://auth.example/avatar/ok'; |
| 105 | const el = Avatar.piece({ id: 'u1', name: 'Tim', color: '#5dd39e', avatar: url }, { doc }); |
| 106 | const img = find(el, 'piece-img'); |
| 107 | img.naturalWidth = 64; |
| 108 | img.fire('load'); |
| 109 | assert.ok(el.classList.contains('has-img')); |
| 110 | const again = Avatar.piece({ id: 'u1', name: 'Tim', color: '#5dd39e', avatar: url }, { doc }); |
| 111 | assert.ok(again.classList.contains('has-img')); |
| 112 | }); |
| 113 | |
| 114 | it('dead players keep the dead style; missing colour gets a deterministic one', () => { |
| 115 | const el = Avatar.piece({ id: 'x', name: 'Z', alive: false }, { doc }); |
| 116 | assert.ok(el.classList.contains('dead')); |
| 117 | assert.equal(el.style.backgroundColor, Avatar.colorFor('x')); |
| 118 | }); |
| 119 | }); |
projects-timfalken-com allianties