'use strict'; const { describe, it, beforeEach } = require('node:test'); const assert = require('node:assert/strict'); const Avatar = require('../src/avatar.js'); // Minimal DOM stand-in: enough for Avatar.piece. function makeDoc() { function El(tag) { this.tagName = tag.toUpperCase(); this.children = []; this.parentNode = null; this.attrs = {}; this.listeners = {}; this._style = {}; const self = this; this.style = new Proxy(this._style, { get(o, k) { if (k === 'setProperty') { return (n, v) => { o[n] = v; }; } return o[k]; }, set(o, k, v) { o[k] = v; return true; } }); this._classes = new Set(); this.classList = { add: (...c) => c.forEach((x) => self._classes.add(x)), remove: (...c) => c.forEach((x) => self._classes.delete(x)), contains: (c) => self._classes.has(c) }; this.textContent = ''; } Object.defineProperty(El.prototype, 'className', { get() { return [...this._classes].join(' '); }, set(v) { this._classes = new Set(String(v).split(/\s+/).filter(Boolean)); } }); El.prototype.setAttribute = function (k, v) { this.attrs[k] = v; }; El.prototype.appendChild = function (c) { c.parentNode = this; this.children.push(c); return c; }; El.prototype.removeChild = function (c) { this.children = this.children.filter((x) => x !== c); c.parentNode = null; }; El.prototype.addEventListener = function (t, f) { (this.listeners[t] = this.listeners[t] || []).push(f); }; El.prototype.fire = function (t) { (this.listeners[t] || []).forEach((f) => f()); }; return { createElement: (t) => new El(t) }; } const find = (el, cls) => el.children.find((c) => c.classList.contains(cls)); describe('Avatar helpers', () => { beforeEach(() => { Avatar.failed = {}; Avatar.loaded = {}; }); it('makes initials from names', () => { assert.equal(Avatar.initials('Tim Falken'), 'TF'); assert.equal(Avatar.initials('timfa'), 'TI'); assert.equal(Avatar.initials('a'), 'A'); assert.equal(Avatar.initials('jan_de_vries'), 'JV'); assert.equal(Avatar.initials(''), '?'); assert.equal(Avatar.initials(' '), '?'); assert.equal(Avatar.initials(null), '?'); assert.equal(Avatar.initials('Émile Zola'), 'ÉZ'); }); it('rejects empty, whitespace, script and failed URLs', () => { assert.equal(Avatar.usableUrl(null), null); assert.equal(Avatar.usableUrl(''), null); assert.equal(Avatar.usableUrl(' '), null); assert.equal(Avatar.usableUrl('javascript:alert(1)'), null); assert.equal(Avatar.usableUrl('https://auth.example/avatar/1'), 'https://auth.example/avatar/1'); Avatar.failed['https://auth.example/avatar/1'] = true; assert.equal(Avatar.usableUrl('https://auth.example/avatar/1'), null); }); it('picks readable text colour', () => { assert.equal(Avatar.textColor('#e8c547'), '#111'); assert.equal(Avatar.textColor('#1a2a6c'), '#fff'); assert.equal(Avatar.textColor('bogus'), '#fff'); }); }); describe('Avatar.piece', () => { beforeEach(() => { Avatar.failed = {}; Avatar.loaded = {}; }); const doc = makeDoc(); it('without avatar: coloured circle with initials, no img', () => { const el = Avatar.piece({ id: 'u2', name: 'Opponent Two', color: '#4fa3e8', avatar: null }, { doc }); assert.ok(el.classList.contains('piece')); assert.equal(el.style.backgroundColor, '#4fa3e8'); assert.equal(el.style.borderColor, '#4fa3e8'); assert.equal(find(el, 'piece-initials').textContent, 'OT'); assert.equal(find(el, 'piece-img'), undefined); }); it('image error removes only the img; the token itself stays and keeps initials', () => { const url = 'https://auth.example/avatar/404'; const cell = doc.createElement('button'); const el = Avatar.piece({ id: 'u3', name: 'Bob', color: '#e85d5d', avatar: url }, { doc }); cell.appendChild(el); // token moved into a board cell (like renderBoard) const img = find(el, 'piece-img'); assert.ok(img); img.fire('error'); assert.equal(cell.children[0], el, 'token still in the cell'); assert.equal(find(el, 'piece-img'), undefined); assert.equal(find(el, 'piece-initials').textContent, 'BO'); assert.equal(Avatar.failed[url], true); // Next render (3 s poll) skips the broken URL entirely: no flicker. const again = Avatar.piece({ id: 'u3', name: 'Bob', color: '#e85d5d', avatar: url }, { doc }); assert.equal(find(again, 'piece-img'), undefined); }); it('loaded image hides initials via has-img; remembered across renders', () => { const url = 'https://auth.example/avatar/ok'; const el = Avatar.piece({ id: 'u1', name: 'Tim', color: '#5dd39e', avatar: url }, { doc }); const img = find(el, 'piece-img'); img.naturalWidth = 64; img.fire('load'); assert.ok(el.classList.contains('has-img')); const again = Avatar.piece({ id: 'u1', name: 'Tim', color: '#5dd39e', avatar: url }, { doc }); assert.ok(again.classList.contains('has-img')); }); it('dead players keep the dead style; missing colour gets a deterministic one', () => { const el = Avatar.piece({ id: 'x', name: 'Z', alive: false }, { doc }); assert.ok(el.classList.contains('dead')); assert.equal(el.style.backgroundColor, Avatar.colorFor('x')); }); });