65 lines
4.6 KiB
JavaScript
65 lines
4.6 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import ts from 'typescript';
|
|
import React from 'react';
|
|
|
|
const tick = () => new Promise(resolve => setImmediate(resolve));
|
|
|
|
function fixture() {
|
|
const source = fs.readFileSync(new URL('../src/SystemUpdatePanel.tsx', import.meta.url), 'utf8')
|
|
.replace(/^import[\s\S]*?;\n/gm, '').replace(/export /g, '');
|
|
let cursor = 0, dirty = false, queued = [], polling, pollingEnabled, tree, failure;
|
|
const states = new Map(), refs = new Map(), memo = new Map(), effects = new Map(), authHandled = [];
|
|
let server = { enabled: true, state: 'available', available: true, version: '2', currentVersion: '1' };
|
|
const props = { onUnauthorized(error) { if ([401, 403].includes(error?.status)) authHandled.push(error.status); return error?.status === 401; } };
|
|
const hooks = {
|
|
useState(initial) { const i = cursor++; if (!states.has(i)) states.set(i, initial); return [states.get(i), next => { states.set(i, next); dirty = true; }]; },
|
|
useRef(initial) { const i = cursor++; if (!refs.has(i)) refs.set(i, { current: initial }); return refs.get(i); },
|
|
useCallback(fn, deps) { const i = cursor++, old = memo.get(i); if (!old || deps.some((value, j) => value !== old.deps[j])) memo.set(i, { fn, deps }); return memo.get(i).fn; },
|
|
useEffect(fn, deps) { const i = cursor++, old = effects.get(i); if (!old || deps.some((value, j) => value !== old.deps[j])) queued.push(() => { effects.set(i, { deps }); fn(); }); },
|
|
};
|
|
const api = {
|
|
async systemUpdateStatus() { if (failure !== undefined) { const status = failure; failure = undefined; throw Object.assign(Error('connection-or-session-error'), { status }); } return { ...server }; },
|
|
async applySystemUpdate() { server = { ...server, state: 'queued', available: false }; return { ...server }; },
|
|
async checkSystemUpdate() { return { ...server }; },
|
|
};
|
|
const prelude = 'const {useState,useRef,useCallback,useEffect}=hooks;';
|
|
const compiled = ts.transpileModule(prelude + source + '\nreturn SystemUpdatePanel;',
|
|
{ fileName: 'fixture.tsx', compilerOptions: { target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.React } }).outputText;
|
|
const component = new Function('React', 'hooks', 'api', 'usePageCache', 'useVisiblePolling', 'configurationDateTime',
|
|
'Alert', 'Button', 'Spin', 'Tag', 'ReloadOutlined', compiled)(React, hooks, api, () => ({ active: true }),
|
|
(callback, interval, enabled) => { polling = callback; pollingEnabled = enabled; }, value => value, 'Alert', 'Button', 'Spin', 'Tag', 'ReloadOutlined');
|
|
const render = () => { let attempts = 0; do { cursor = 0; dirty = false; queued = []; tree = component(props); queued.forEach(fn => fn()); assert.ok(++attempts < 15); } while (dirty); return tree; };
|
|
const buttons = () => { const result = []; function walk(node) { if (!node || typeof node !== 'object') return; if (node.type === 'Button') result.push(node); React.Children.forEach(node.props?.children, walk); } walk(tree); return result; };
|
|
return {
|
|
authHandled, reconnecting: () => states.get(3), error: () => states.get(2), pollingEnabled: () => pollingEnabled,
|
|
async start() { render(); await tick(); render(); },
|
|
async apply() { const button = buttons().find(node => node.props.children === '更新并重启'); assert.ok(button); button.props.onClick(); await tick(); render(); },
|
|
async failPoll(status) { failure = status; await polling(); render(); },
|
|
async complete() { server = { ...server, state: 'completed', message: '更新完成。', currentVersion: '2' }; await polling(); render(); },
|
|
};
|
|
}
|
|
|
|
for (const status of [401, 403]) test(`restart polling forwards ${status} to authentication handling`, async () => {
|
|
const ui = fixture(); await ui.start(); await ui.apply(); await ui.failPoll(status);
|
|
assert.deepEqual(ui.authHandled, [status]);
|
|
assert.equal(ui.reconnecting(), false);
|
|
assert.equal(ui.pollingEnabled(), false);
|
|
if (status === 403) assert.equal(ui.error(), 'connection-or-session-error');
|
|
});
|
|
|
|
test('temporary restart connection loss reconnects and clears after completion', async () => {
|
|
const ui = fixture(); await ui.start(); await ui.apply(); await ui.failPoll(503);
|
|
assert.equal(ui.reconnecting(), true);
|
|
assert.deepEqual(ui.authHandled, []);
|
|
await ui.complete();
|
|
assert.equal(ui.reconnecting(), false);
|
|
});
|
|
|
|
test('a permanent route failure is displayed rather than treated as a restart connection loss', async () => {
|
|
const ui = fixture(); await ui.start(); await ui.apply(); await ui.failPoll(404);
|
|
assert.equal(ui.reconnecting(), false);
|
|
assert.equal(ui.error(), 'connection-or-session-error');
|
|
});
|