const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const vm = require('node:vm'); const source = fs.readFileSync(require.resolve('../public/js/app.js'), 'utf8'); function fixture(fetch) { const context = vm.createContext({ window: { fetch }, fetch, console, URLSearchParams, document: { addEventListener() {}, getElementById: () => null } }); vm.runInContext(`${source}\nglobalThis.manager = Object.create(AccountManager.prototype);`, context); return Object.assign(context.manager, { apiBase: '/api', currentPage: 'notification-management', currentUser: { Role: 'admin' }, notificationItems: [], notificationPreviewItems: [], notificationPage: 1, notificationPageSize: 20, notificationFilter: 'all', notificationSourceFilter: '', notificationUnreadCount: 0, notificationPagination: { page: 1, total: 0, totalPages: 1 }, renderNotificationsPage() {}, notifyFailure() {} }); } test('a missing API or HTML fallback produces an actionable message without a JSON parser error', async () => { for (const status of [200, 404]) { const manager = fixture(async () => new Response('Cannot GET /api/notifications/manage', { status, headers: { 'Content-Type': 'text/html' } })); await assert.rejects(manager.fetchNotificationJson('/manage'), error => { assert.match(error.message, /khởi động lại backend/); assert.doesNotMatch(error.message, /Unexpected token| { for (const [response, expected] of [ [new Response('Bad Gateway', { status: 502, headers: { 'Content-Type': 'text/html' } }), /HTTP 502/], [new Response('{broken', { headers: { 'Content-Type': 'application/json' } }), /không hợp lệ/], [Response.json({ success: false, message: 'Thông báo đã kết thúc' }, { status: 404 }), /Thông báo đã kết thúc/], [Response.json({ success: false, code: 'API_NOT_FOUND' }, { status: 404 }), /khởi động lại backend/] ]) { const manager = fixture(async () => response); await assert.rejects(manager.fetchNotificationJson('/1/dismiss', { method: 'PUT' }), expected); } }); test('notification requests preserve JSON headers and never retry a write automatically', async () => { const calls = []; const manager = fixture(async (url, options) => { calls.push({ url, options }); return Response.json({ success: true }); }); await manager.fetchNotificationJson('', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: '{"title":"Notice"}' }); assert.equal(calls.length, 1); assert.equal(calls[0].url, '/api/notifications'); assert.equal(calls[0].options.headers.Accept, 'application/json'); assert.equal(calls[0].options.headers['Content-Type'], 'application/json'); assert.equal(calls[0].options.method, 'POST'); assert.equal(calls[0].options.cache, 'no-store'); }); test('a failed management load shows an error and retry control, then clears both after recovery', async () => { let offline = true; const manager = fixture(async () => offline ? new Response('Not found', { status: 404, headers: { 'Content-Type': 'text/html' } }) : Response.json({ success: true, data: [], pagination: { page: 1, total: 0, totalPages: 1 } })); await manager.refreshNotificationsPage(); assert.equal(manager.notificationLoading, false); assert.match(manager.getNotificationsContent(), /role="alert"/); assert.match(manager.getNotificationsContent(), /id="retryNotificationsBtn"/); assert.doesNotMatch(manager.getNotificationsContent(), /Không có thông báo phù hợp/); offline = false; await manager.refreshNotificationsPage(); assert.equal(manager.notificationLoadError, ''); assert.doesNotMatch(manager.getNotificationsContent(), /retryNotificationsBtn/); assert.match(manager.getNotificationsContent(), /Không có thông báo phù hợp/); });