const test = require('node:test'); const assert = require('node:assert/strict'); const { normalizeIdentifier, recordPlayerHeartbeat, upsertPlayerRegistration } = require('../src/data/player-registry'); test('normalizeIdentifier trims and limits friendly player labels', () => { assert.equal(normalizeIdentifier(' Shop2 '), 'Shop2'); assert.equal(normalizeIdentifier(''), ''); assert.equal(normalizeIdentifier('x'.repeat(300)).length, 255); }); test('player registry upserts include the identifier field', async () => { const calls = []; const pool = { async query(sql, params) { calls.push({ sql, params }); if (String(sql || '').includes('information_schema.COLUMNS')) { return [[{ column_count: 1 }]]; } if (String(sql || '').includes('UPDATE d_players')) { return [{ affectedRows: 1 }]; } if (String(sql || '').includes('SELECT id, identifier, public_base_url, internal_base_url, last_seen_at')) { return [[{ id: 1, identifier: 'Shop2', public_base_url: 'http://player.local', internal_base_url: 'http://player:8081', last_seen_at: '2026-08-07 00:00:00' }]]; } return [[]]; } }; const registration = await upsertPlayerRegistration(pool, { deviceId: 'device-01', identifier: 'Shop2', publicBaseUrl: 'http://player.local/', internalBaseUrl: 'http://player:8081/' }); const heartbeat = await recordPlayerHeartbeat(pool, { deviceId: 'device-01', identifier: 'Shop2', publicBaseUrl: 'http://player.local/', internalBaseUrl: 'http://player:8081/' }); assert.deepEqual(registration, { id: 1, identifier: 'Shop2', public_base_url: 'http://player.local', internal_base_url: 'http://player:8081', last_seen_at: '2026-08-07 00:00:00' }); assert.deepEqual(heartbeat, registration); assert.ok(calls.some(function (call) { return /INSERT INTO d_players/.test(String(call.sql || '')) && /identifier/.test(String(call.sql || '')); })); assert.ok(calls.some(function (call) { return /SELECT .*identifier/.test(String(call.sql || '')); })); assert.deepEqual((calls.find(function (call) { return /INSERT INTO d_players/.test(String(call.sql || '')); }) || {}).params, ['Shop2', 'http://player.local', 'http://player:8081', 'Shop2']); });