SHA256
Обновить окно ошибки запуска UI
This commit is contained in:
@@ -91,3 +91,108 @@ test('конфиг сервера изолирует client-errors.log на 14
|
||||
assert.match(xml, /<logger name="client\.frontend" level="error" additivity="false">/);
|
||||
assert.match(xml, /<totalSizeCap>150MB<\/totalSizeCap>/);
|
||||
});
|
||||
|
||||
|
||||
test('ClientErrorLog подтверждается по payload.accepted, а не по корневому полю', async () => {
|
||||
const src = read('../js/services/auth-service.js');
|
||||
const match = src.match(/async reportClientError\(details\) \{([\s\S]*?)\n \}/);
|
||||
assert.ok(match, 'reportClientError method exists');
|
||||
const report = new Function('details', `return (async function () {${match[1]}\n}).call(this);`);
|
||||
globalThis.WebSocket = { OPEN: 1 };
|
||||
const request = async () => ({ status: 200, payload: { accepted: true } });
|
||||
const ws = { ws: { readyState: 1 }, request };
|
||||
assert.equal(await report.call({ ws }, { kind: 'global_error', message: 'Test' }), true);
|
||||
ws.request = async () => ({ status: 200, payload: { accepted: false } });
|
||||
assert.equal(await report.call({ ws }, { kind: 'global_error', message: 'Test' }), false);
|
||||
ws.request = async () => ({ status: 500, payload: { accepted: true } });
|
||||
assert.equal(await report.call({ ws }, { kind: 'global_error', message: 'Test' }), false);
|
||||
ws.ws.readyState = 3;
|
||||
assert.equal(await report.call({ ws }, { kind: 'global_error', message: 'Test' }), false);
|
||||
});
|
||||
|
||||
test('BOOT ERROR отключает аварийную плашку после подключения штатного обработчика', () => {
|
||||
const index = read('../index.html');
|
||||
const app = read('../js/app.js');
|
||||
assert.match(app, /window\.__SHINE_APP_ERROR_HANDLERS_READY__ = true;/);
|
||||
const handlers = [...index.matchAll(/window\.addEventListener\('(error|unhandledrejection)', \(e\) => \{\s*([\s\S]*?)\n \}\);/g)];
|
||||
assert.equal(handlers.length, 2);
|
||||
for (const match of handlers) {
|
||||
assert.match(match[2], /if \(window\.__SHINE_APP_ERROR_HANDLERS_READY__ === true\) return;/);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
test('аварийный экран нейтральный, с причиной, источником и кнопкой перезагрузки', () => {
|
||||
const index = read('../index.html');
|
||||
assert.match(index, /Не удалось запустить SHiNE/);
|
||||
assert.match(index, /Причина: \$/);
|
||||
assert.match(index, /Источник: \$/);
|
||||
assert.match(index, /Обновить страницу/);
|
||||
assert.match(index, /Подробности/);
|
||||
assert.match(index, /Отправка отчёта не подтверждена/);
|
||||
assert.match(index, /state\.sentToServer === true/);
|
||||
assert.match(index, /sentToServer: true/);
|
||||
assert.doesNotMatch(index, /rgba\(120, 0, 0/);
|
||||
assert.doesNotMatch(index, /\[BOOT ERROR\] \$/);
|
||||
});
|
||||
|
||||
test('аварийный экран не показывает stack и произвольный текст исключения', () => {
|
||||
const index = read('../index.html');
|
||||
const match = index.match(/const updateBootOverlay = \(\) => \{([\s\S]*?)\n \};/);
|
||||
assert.ok(match, 'есть функция обновления аварийного экрана');
|
||||
assert.doesNotMatch(match[1], /\.stack|state\.message\s*;/);
|
||||
assert.match(index, /Ошибка выполнения JavaScript \(TypeError\)/);
|
||||
assert.match(index, /if \(window\.__SHINE_APP_ERROR_HANDLERS_READY__ === true\) return;/);
|
||||
});
|
||||
|
||||
test('BOOT ERROR показывает краткую причину без приватного текста и позволяет перезагрузить страницу', async () => {
|
||||
const { runInNewContext } = await import('node:vm');
|
||||
const index = read('../index.html');
|
||||
const match = index.match(/\(function attachBootErrorOverlay\(\) \{[\s\S]*?\}\(\)\);/);
|
||||
assert.ok(match, 'загрузочный обработчик найден');
|
||||
class FakeElement {
|
||||
constructor(tag = 'div') { this.tag = tag; this.children = []; this.style = {}; this.dataset = {}; this.handlers = {}; this.attributes = {}; this.textContent = ''; }
|
||||
setAttribute(name, value) { this.attributes[name] = value; }
|
||||
append(...els) { this.children.push(...els); }
|
||||
appendChild(el) { this.children.push(el); }
|
||||
addEventListener(name, handler) { this.handlers[name] = handler; }
|
||||
querySelector(selector) {
|
||||
const key = selector.match(/^\[data-boot-error="(.*?)"\]$/)?.[1];
|
||||
const descend = (el) => {
|
||||
if (key && el.dataset?.bootError === key) return el;
|
||||
for (const child of el.children) { const found = descend(child); if (found) return found; }
|
||||
return null;
|
||||
};
|
||||
return descend(this);
|
||||
}
|
||||
}
|
||||
const body = new FakeElement('body');
|
||||
const handlers = {};
|
||||
let reloads = 0;
|
||||
const fakeWindow = {
|
||||
addEventListener: (type, handler) => { handlers[type] = handler; },
|
||||
location: { origin: 'https://shineup.me', reload: () => reloads++ },
|
||||
};
|
||||
const fakeDocument = {
|
||||
documentElement: { dataset: { theme: 'dark' } }, body,
|
||||
createElement: (tag) => new FakeElement(tag),
|
||||
getElementById: (id) => body.children.find((el) => el.id === id) || null,
|
||||
};
|
||||
runInNewContext(match[0], { window: fakeWindow, document: fakeDocument, navigator: { userAgent: 'test' }, console, URL, Date, Number, String, Math });
|
||||
handlers.error({
|
||||
message: 'TypeError: private message text: NEVER_SHOW_ME',
|
||||
filename: 'https://shineup.me/js/app.js?password=NEVER_SHOW_ME', lineno: 42, colno: 9,
|
||||
error: { stack: 'TypeError: NEVER_SHOW_ME' },
|
||||
});
|
||||
const overlay = fakeDocument.getElementById('boot-error-overlay');
|
||||
assert.ok(overlay);
|
||||
assert.equal(overlay.querySelector('[data-boot-error="cause"]').textContent, 'Причина: Ошибка выполнения JavaScript (TypeError).');
|
||||
assert.equal(overlay.querySelector('[data-boot-error="source"]').textContent, 'Источник: app.js:42');
|
||||
assert.match(overlay.querySelector('[data-boot-error="status"]').textContent, /не подтверждена/);
|
||||
const rendered = JSON.stringify(body, (k, v) => k === 'handlers' ? undefined : v);
|
||||
assert.ok(!rendered.includes('NEVER_SHOW_ME'));
|
||||
const buttons = overlay.children[0].children.at(-1).children;
|
||||
assert.equal(buttons[0].textContent, 'Обновить страницу');
|
||||
buttons[0].handlers.click();
|
||||
assert.equal(reloads, 1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user