// SPDX-License-Identifier: GPL-2.0-or-later // Copyright (C) 2026 Bryan Joshua Pedini import assert from 'node:assert/strict'; import test from 'node:test'; import { ApiError, handleError } from '../../static/js/common.js'; class Element { children = []; attributes = {}; handlers = {}; constructor(tag) { this.tag = tag; } append(...nodes) { this.children.push(...nodes); } prepend(node) { this.children.unshift(node); } querySelector(selector) { return this.children.find((node) => node.className?.split(' ').includes(selector.slice(1))); } setAttribute(name, value) { this.attributes[name] = value; } addEventListener(name, callback) { this.handlers[name] = callback; } } test('a stale edit puts a usable reload button inside its form', () => { globalThis.Node = Element; globalThis.document = { createElement: (tag) => new Element(tag) }; const form = new Element('form'); let reloads = 0; handleError(new ApiError(412, { error: 'Stale record.' }), { form, reload: () => reloads++ }); const alert = form.querySelector('.form-error'); assert.equal(alert.attributes.role, 'alert'); assert.equal(alert.textContent, 'Stale record. Reload to see the current data.'); const button = alert.children.find((node) => node.tag === 'button'); assert.equal(button.textContent, 'Reload'); assert.equal(button.attributes.type, 'button'); button.handlers.click(); assert.equal(reloads, 1); });