const test = require('node:test'); const assert = require('node:assert/strict'); const registerManageRoutes = require('../src/web/routes/admin/manage'); test('screen update redirects and forwards redirect when the slug changes', async () => { const handlers = {}; const app = { post(path, ...routeHandlers) { handlers[path] = routeHandlers; }, get() {} }; const pool = { async query(sql) { if (sql.includes('SELECT slug FROM d_screens ORDER BY slug ASC')) { return [[{ slug: 'alpha' }, { slug: 'beta' }]]; } if (sql.includes('SELECT id, name, slug, playlist_id')) { return [[{ id: 42, name: 'Old Screen', slug: 'alpha', playlist_id: null }]]; } if (sql.includes('UPDATE d_screens SET name = ?, slug = ?, playlist_id = ?, player_id = ?, modified_by = ? WHERE id = ?')) { return [{ affectedRows: 1 }]; } return [[]]; } }; const calls = []; const pages = { renderScreenFormPage() {}, renderScreenEditPage() {} }; const deps = { pool, common: { slugify(value) { return String(value || '').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, ''); }, uniqueScreenSlug: async () => 'beta', fetchDuplicateName: async () => null, fetchScreenById: async () => ({ id: 42, name: 'Old Screen', slug: 'alpha', playlist_id: null }), fetchScreenPlayerRecord: async () => ({ public_base_url: 'http://player.local' }) }, pages, getAuditUserId() { return 7; }, redirectAfterSave(req, res, url) { calls.push({ kind: 'redirectAfterSave', url }); res.redirectedTo = url; }, notifyPlayerScreens: async (slugs, payload) => { calls.push({ kind: 'notifyPlayerScreens', slugs, payload }); return 1; }, broadcastDashboardState: async () => { calls.push({ kind: 'broadcastDashboardState' }); }, getScreenDeleteBlockMessage: async () => '', getScreenConnections: async () => [], forwardPlayerCommand: async (slug, payload) => { calls.push({ kind: 'forwardPlayerCommand', slug, payload }); return { ok: true }; }, playerPublicBaseUrl: 'http://player.example', requirePermission() { return function (_req, _res, next) { next(); }; } }; registerManageRoutes(app, deps); const routeHandlers = handlers['/screens/:id']; assert.equal(Array.isArray(routeHandlers), true); const req = { params: { id: '42' }, body: { name: 'Updated Screen', slug: 'beta' } }; const res = { redirect(url) { this.redirectedTo = url; }, status() { return this; }, send() { return this; } }; await routeHandlers[1](req, res, () => {}); assert.equal(res.redirectedTo, '/screens?edit=42'); assert.deepEqual(calls, [ { kind: 'forwardPlayerCommand', slug: 'alpha', payload: { command: 'redirect', url: 'http://player.local/screen/beta' } }, { kind: 'redirectAfterSave', url: '/screens?edit=42' } ]); });