92 lines
6.2 KiB
JavaScript
92 lines
6.2 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import {readFileSync} from 'node:fs';
|
|
import {runInNewContext} from 'node:vm';
|
|
import test from 'node:test';
|
|
|
|
// Small DOM adapter: execute the shipped script unchanged, without network access.
|
|
async function fixture() {
|
|
const elements = [];
|
|
class Element {
|
|
constructor(tag) { this.tag = tag; this.children = []; this.dataset = {}; this.attrs = new Map(); this.listeners = {}; elements.push(this); }
|
|
append(...nodes) { this.children.push(...nodes); }
|
|
replaceChildren() { this.children = []; }
|
|
querySelectorAll(tag) { return this.children.flatMap(e => [e, ...e.querySelectorAll(tag)]).filter(e => e.tag === tag); }
|
|
setAttribute(k, v) { this.attrs.set(k, v); }
|
|
removeAttribute(k) { this.attrs.delete(k); }
|
|
toggleAttribute(k, on) { if (on) this.attrs.set(k, ''); else this.attrs.delete(k); }
|
|
addEventListener(name, fn) { this.listeners[name] = fn; }
|
|
set checked(on) { if (on) for (const e of elements) if (e.tag === 'input' && e.name === this.name) e._checked = false; this._checked = on; }
|
|
get checked() { return !!this._checked; }
|
|
}
|
|
const routes = new Element('fieldset'), panel = new Element('section'), status = new Element('span'), enter = new Element('a'), recheck = new Element('button');
|
|
const selectors = {'.routes': routes, '.entries': panel, '#route-status': status, '#enter': enter, '.recheck': recheck};
|
|
const record = (code, access) => ({code, hostname: code + '.example.com', url: 'https://' + code + '.example.com/', role: 'direct_access', priority: 0, activated_at: '2026-01-01', selectable: true, probe_enabled: access === 'available', eligible: access === 'available', access, reason: access === 'available' ? null : access});
|
|
const state = {records: [record('slow', 'available'), record('fast', 'available'), record('blocked', 'blocked'), record('suspected', 'suspected_blocked'), record('proxy', 'requires_proxy')], pending: [], reports: [], now: 0};
|
|
const flush = () => new Promise(resolve => setImmediate(resolve));
|
|
runInNewContext(readFileSync(new URL('../../theme/Fuc/assets/navigation-domainhub.js', import.meta.url), 'utf8'), {
|
|
document: {querySelector: key => selectors[key], createElement: tag => new Element(tag), hidden: false},
|
|
location: {hostname: 'navigation.example.com', search: '?code=invite_1', origin: 'https://navigation.example.com'},
|
|
URL, URLSearchParams, AbortSignal, Date, performance: {now: () => state.now},
|
|
setInterval: fn => { state.refresh = fn; },
|
|
fetch: async (url, options = {}) => {
|
|
const value = new URL(url, 'https://navigation.example.com');
|
|
if (value.pathname === '/_nav-routes') return {ok: true, json: async () => ({entries: structuredClone(state.records)})};
|
|
if (value.pathname === '/_nav-stats') { state.reports.push(JSON.parse(options.body)); return {ok: true}; }
|
|
return new Promise((resolve, reject) => state.pending.push({host: value.hostname, resolve, reject}));
|
|
},
|
|
});
|
|
await flush();
|
|
const row = code => routes.children.find(e => e.children[0].dataset.domainhubCode === code);
|
|
async function complete(code, now, ok = true) {
|
|
const index = state.pending.findIndex(e => e.host === code + '.example.com'); assert.notEqual(index, -1);
|
|
const request = state.pending.splice(index, 1)[0]; state.now = now;
|
|
if (ok) request.resolve({ok: true, json: async () => ({data: {app_name: 'FUC'}})});
|
|
else request.reject(Error('unreachable'));
|
|
await flush();
|
|
}
|
|
return {state, row, routes, recheck, enter, complete, flush};
|
|
}
|
|
|
|
test('manual warning routes stay selectable without probes; fastest highlight survives selection and refresh', async () => {
|
|
const f = await fixture();
|
|
assert.deepEqual(f.state.pending.map(e => e.host).sort(), ['fast.example.com', 'slow.example.com']);
|
|
for (const code of ['blocked', 'suspected', 'proxy']) {
|
|
assert.equal(f.row(code).children[0].disabled, false);
|
|
assert.equal(f.row(code).children[2].textContent, '不测速');
|
|
}
|
|
await f.complete('fast', 100); await f.complete('slow', 300);
|
|
assert.equal(f.row('fast').children[2].attrs.has('data-fastest'), true);
|
|
assert.equal(f.row('slow').children[2].attrs.has('data-fastest'), false);
|
|
f.row('blocked').children[0].checked = true; f.routes.listeners.change();
|
|
assert.equal(f.enter.href, '/_nav-go?route=blocked&code=invite_1');
|
|
f.state.refresh(); await f.flush();
|
|
assert.equal(f.row('blocked').children[0].checked, true);
|
|
assert.equal(f.row('fast').children[2].textContent, '100 ms');
|
|
assert.equal(f.row('fast').children[2].attrs.has('data-fastest'), true);
|
|
assert.deepEqual(f.state.reports[0], {slow: 'success', fast: 'success'});
|
|
f.recheck.listeners.click(); await f.flush();
|
|
assert.equal(f.row('fast').children[2].attrs.has('data-fastest'), false);
|
|
await f.complete('slow', 450); await f.complete('fast', 500);
|
|
assert.equal(f.row('slow').children[2].attrs.has('data-fastest'), true);
|
|
assert.equal(f.row('blocked').children[0].checked, true);
|
|
});
|
|
|
|
test('policy and URL changes clear old highlights; failures do not gain fastest status', async () => {
|
|
const f = await fixture(); await f.complete('fast', 100); await f.complete('slow', 300);
|
|
Object.assign(f.state.records.find(e => e.code === 'fast'), {eligible: false, probe_enabled: false, access: 'blocked', reason: 'blocked'});
|
|
f.state.refresh(); await f.flush();
|
|
assert.equal(f.row('fast').children[0].disabled, false);
|
|
assert.equal(f.row('fast').children[2].textContent, '不测速');
|
|
assert.equal(f.row('fast').children[2].attrs.has('data-fastest'), false);
|
|
assert.equal(f.row('slow').children[2].attrs.has('data-fastest'), true);
|
|
Object.assign(f.state.records.find(e => e.code === 'fast'), {selectable: false, reason: 'disabled'});
|
|
f.state.refresh(); await f.flush();
|
|
assert.equal(f.row('fast').children[0].disabled, true);
|
|
Object.assign(f.state.records.find(e => e.code === 'slow'), {url: 'https://new.example.com/', hostname: 'new.example.com'});
|
|
f.state.refresh(); await f.flush();
|
|
assert.equal(f.row('slow').children[2].textContent, '—');
|
|
assert.equal(f.row('slow').children[2].attrs.has('data-fastest'), false);
|
|
f.recheck.listeners.click(); await f.flush(); await f.complete('new', 400, false);
|
|
assert.equal(f.routes.children.some(e => e.children[2].attrs.has('data-fastest')), false);
|
|
});
|