You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/AdminFabricWizardBehaviorTe...

781 lines
40 KiB
JavaScript

import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import vm from 'node:vm';
const source = await readFile(new URL('../public/cloud/admin-fabric.js', import.meta.url), 'utf8');
class Node {
constructor(tag = 'div') {
this.tagName = tag.toUpperCase(); this.children = []; this.dataset = {};
this.listeners = new Map(); this.attributes = new Map(); this.hidden = false;
this.disabled = false; this.value = ''; this.className = ''; this._text = '';
const classes = new Set();
this.classList = {
add: (...values) => values.forEach(value => classes.add(value)),
remove: (...values) => values.forEach(value => classes.delete(value)),
contains: value => classes.has(value),
};
}
set textContent(value) { this._text = String(value); this.children = []; }
get textContent() { return this._text + this.children.map(child => child.textContent).join(''); }
set innerHTML(_) { throw new Error('Fabric content must never be interpreted as HTML.'); }
get options() { return this.children; }
append(...values) { this.children.push(...values); }
replaceChildren(...values) { this._text = ''; this.children = []; this.append(...values); }
setAttribute(key, value) { this.attributes.set(key, String(value)); }
removeAttribute(key) { this.attributes.delete(key); }
getAttribute(key) { return this.attributes.get(key) ?? null; }
addEventListener(key, handler) {
const handlers = this.listeners.get(key) || []; handlers.push(handler);
this.listeners.set(key, handlers);
}
dispatch(key, fields = {}) {
const event = { target: this, currentTarget: this, preventDefault() {}, ...fields };
for (const handler of this.listeners.get(key) || []) handler(event);
}
}
const flush = async () => { for (let index = 0; index < 30; index += 1) await Promise.resolve(); };
function workflow(stage, changes = {}) {
return {
stage, can_probe: stage === 'needs_probe' || stage === 'blocked',
can_apply: stage === 'ready_to_apply',
can_reconcile: stage === 'needs_reconcile' || stage === 'active'
|| stage === 'operation_running',
blocking_reasons: stage === 'blocked' ? ['firewall_owner_not_nftables'] : [],
action_mode: stage === 'ready_to_apply' ? 'apply'
: stage === 'needs_reconcile' || stage === 'active'
|| stage === 'operation_running' ? 'reconcile' : 'probe',
...changes,
};
}
function region(id, stage, changes = {}) {
return {
region_id: id, region_code: id === 1 ? 'hk' : 'sg',
region_name: id === 1 ? 'Hong Kong' : 'Singapore', region_status: 'active',
host_count: 2, required_host_count: 2, ready_host_count: 0,
blocked_host_count: 0, hosts: [], ready: stage === 'active',
mapping_state: stage === 'active' ? 'active' : 'unconfigured',
fabric_state: stage === 'active' ? 'active' : null,
workflow: workflow(stage), plan_preview: null,
...changes,
};
}
function preview(code = 'hk') {
return {
mode: 'automatic', fabric_id: `fabric-${code}`, fabric_region: code,
revision: 1, listen_port: 51820, overlay_cidr: '100.64.7.0/24', host_count: 2,
};
}
function operation(status = 'running', kind = 'apply', changes = {}) {
return {
id: 'a'.repeat(32), kind, status,
started_at: 1791501000, updated_at: 1791501010,
finished_at: ['succeeded', 'failed', 'uncertain', 'interrupted'].includes(status)
? 1791501020 : null,
...changes,
};
}
function fixture(initialRegions, canManage = true) {
const ids = [...source.matchAll(/byId\('([^']+)'\)/g)].map(match => match[1]);
ids.push('fabric-detect-hosts', 'fabric-auto-configure', 'fabric-reconcile');
const nodes = Object.fromEntries([...new Set(ids)].map(id => [id,
new Node(id === 'fabric-region-select' ? 'select' : 'div')]));
const actions = ['probe', 'apply', 'reconcile'].map(action => {
const id = { probe: 'fabric-detect-hosts', apply: 'fabric-auto-configure',
reconcile: 'fabric-reconcile' }[action];
const button = nodes[id]; button.tagName = 'BUTTON';
button.dataset.fabricPrimaryAction = ''; button.dataset.fabricAction = action;
return button;
});
const steps = ['select', 'probe', 'apply', 'reconcile'].map(step => {
const item = new Node('li'); item.dataset.fabricWizardStep = step; return item;
});
const root = new Node('main'); root.dataset.canManage = canManage ? '1' : '0';
const document = {
getElementById: id => nodes[id] || null,
createElement: tag => new Node(tag),
querySelector: selector => selector === '[data-admin-fabric]' ? root : null,
querySelectorAll: selector => selector === '[data-fabric-primary-action]'
? actions : selector === '[data-fabric-wizard-step]' ? steps : [],
};
let now = 0; let clockOffset = 0; let timerId = 0;
const timers = new Map(); const requests = [];
const setTimeout = (callback, delay) => {
const id = ++timerId; timers.set(id, { callback, due: now + delay }); return id;
};
const clearTimeout = id => timers.delete(id);
class ClockDate extends Date {
static now() { return 1791501100000 + now + clockOffset; }
}
const window = { ADMIN_CSRF_TOKEN: 'csrf-test', setTimeout, clearTimeout };
const fetch = (url, options) => new Promise((resolve, reject) => {
const request = { url: String(url), options, resolve, reject, settled: false };
requests.push(request);
options.signal.addEventListener('abort', () => {
if (!request.settled) { request.settled = true; reject(new Error('aborted')); }
});
});
vm.runInNewContext(source, { document, window, fetch, AbortController, Date: ClockDate, console },
{ filename: 'admin-fabric.js' });
async function respond(request, payload, status = 200) {
assert.ok(request, 'Expected an outstanding Fabric request.');
request.settled = true;
request.resolve({ ok: status >= 200 && status < 300, status, json: async () => payload });
await flush();
}
async function reject(request, error = new TypeError('network failed')) {
assert.ok(request, 'Expected an outstanding Fabric request.');
request.settled = true; request.reject(error); await flush();
}
async function advance(milliseconds) {
const end = now + milliseconds;
for (;;) {
const next = [...timers.entries()].filter(([, item]) => item.due <= end)
.sort((left, right) => left[1].due - right[1].due)[0];
if (!next) break;
now = next[1].due; timers.delete(next[0]); next[1].callback(); await flush();
}
now = end; await flush();
}
return { nodes, actions, steps, requests, respond, reject, advance, initialRegions,
setClockOffset(value) { clockOffset = value; } };
}
// The server workflow, not browser-invented conditions, owns every enabled
// action and the read-only plan rendered after a successful fresh probe.
{
const test = fixture([region(1, 'needs_probe')]);
await test.respond(test.requests[0], { regions: test.initialRegions, engine_available: true });
assert.equal(test.nodes['fabric-detect-hosts'].disabled, false);
assert.equal(test.nodes['fabric-auto-configure'].disabled, true);
assert.equal(test.nodes['fabric-reconcile'].disabled, true);
test.nodes['fabric-detect-hosts'].dispatch('click'); await flush();
const mutation = test.requests[1];
assert.equal(mutation.url, '/api/v1/admin/fabric/regions/1/probe');
assert.equal(mutation.options.body, '{}');
assert.equal(mutation.options.headers['X-CSRF-Token'], 'csrf-test');
const ready = region(1, 'ready_to_apply', { ready_host_count: 2,
plan_preview: preview() });
await test.respond(mutation, { region: ready, engine_available: true });
assert.equal(test.nodes['fabric-auto-configure'].disabled, false);
assert.equal(test.nodes['fabric-plan-id'].textContent, 'fabric-hk');
assert.equal(test.nodes['fabric-plan-overlay'].textContent, '100.64.7.0/24');
assert.match(test.nodes['fabric-notice'].textContent, /检测通过/);
}
// rp_filter diagnostics expose the bounded values that affect the policy
// decision. Alma/RHEL strict defaults and the public underlay stay visible as
// preserved OS/operator policy; an absent owned Fabric interface is pending
// automatic configuration, not a failed vnet/tap probe.
{
const host = {
host_id: '1', host_name: 'alma', engine_region: 'hk', host_enabled: true,
scheduler_state: 'active', pool_name: 'hk', pool_status: 'active',
line_name: 'Hong Kong', line_status: 'active', platform_ready: true,
configured: true, reachable: true, endpoint_ip: '203.0.113.11',
endpoint_source: 'ssh_host_ipv4', probe_checked: true, probed_at: 1770000100,
fabric_member: false, fabric_state: 'unchecked', transport_verified: false,
observed_revision: 0, readiness_state: 'ready', reasons: [],
capabilities: {
ip_forward: 1, rp_filter: 0, rp_filter_all: 0,
rp_filter_default: 1, rp_filter_default_effective: 1,
underlay_interface: 'br0', rp_filter_underlay_raw: 1,
rp_filter_underlay_effective: 1, rp_filter_fabric_raw: null,
rp_filter_fabric_effective: null, public_selector: 'bridge:br0',
public_verified: true, interfaces: ['br0', 'vnet3', 'vnet10'],
},
};
const strictFabricHost = {
...host, host_id: '2', host_name: 'existing-strict',
capabilities: {
...host.capabilities, rp_filter_fabric_raw: 1,
rp_filter_fabric_effective: 1,
},
};
const test = fixture([region(1, 'ready_to_apply', {
host_count: 2, required_host_count: 2, ready_host_count: 2,
hosts: [host, strictFabricHost], plan_preview: preview(),
})]);
await test.respond(test.requests[0], { regions: test.initialRegions, engine_available: true });
const copy = test.nodes['fabric-hosts-body'].textContent;
assert.match(copy, /rp_filter all:raw 0 · effective 0(关闭,兼容)/);
assert.match(copy, /rp_filter default:raw 1 · effective 1(strict,发行版默认保留)/);
assert.match(copy, /Endpoint br0:raw 1 · effective 1(strict,公网接口保留)/);
assert.match(copy, /wg-fabric0:未创建(自动配置时由平台设为 2)/);
assert.match(copy, /wg-fabric0:raw 1 · effective 1(strict,自动配置将收敛为 2)/);
assert.match(copy, /仅平台自有 wg-fabric0 与 VPC bridge 设为 2/);
assert.doesNotMatch(copy, /vnet3|vnet10/);
}
// Mixed-version responses keep the legacy all value useful without claiming
// that omitted detailed evidence was checked.
{
const host = {
host_id: '1', host_name: 'legacy', engine_region: 'hk',
capabilities: { ip_forward: 1, rp_filter: 2, underlay_interface: 'br0' },
};
const test = fixture([region(1, 'needs_probe', { host_count: 1, hosts: [host] })]);
await test.respond(test.requests[0], { regions: test.initialRegions, engine_available: true });
const copy = test.nodes['fabric-hosts-body'].textContent;
assert.match(copy, /rp_filter all:raw 2 · effective 2(loose,兼容)/);
assert.match(copy, /rp_filter default:未检测/);
assert.match(copy, /Endpoint br0:未检测/);
assert.match(copy, /wg-fabric0:未检测/);
}
// A persisted Engine failure phase is shown as historical Fabric evidence,
// not as the outcome of the fresh capability probe rendered beside it. Older
// Engine responses without the optional field remain quiet.
{
const failedHost = {
host_id: '1', host_name: 'failed', engine_region: 'hk',
fabric_member: true, fabric_state: 'uncertain', transport_verified: false,
observed_revision: 0, readiness_state: 'uncertain',
last_error_code: 'fabric_remote_command_failed', last_error_phase: 'apply',
reasons: ['fabric_remote_command_failed'], capabilities: {},
};
const legacyHost = { ...failedHost, host_id: '2', host_name: 'legacy' };
delete legacyHost.last_error_phase;
const staleSuccessHost = {
...failedHost, host_id: '3', host_name: 'active', fabric_state: 'active',
transport_verified: true, last_error_code: null, last_error_phase: 'apply',
reasons: [],
};
const test = fixture([region(1, 'needs_reconcile', {
hosts: [failedHost, legacyHost, staleSuccessHost],
plan_preview: { ...preview(), mode: 'existing' },
})]);
await test.respond(test.requests[0], { regions: test.initialRegions, engine_available: true });
const copy = test.nodes['fabric-hosts-body'].textContent;
assert.equal((copy.match(/上次 Fabric 失败阶段:/g) || []).length, 1);
assert.match(copy, /上次 Fabric 失败阶段:apply(应用)/);
assert.match(copy, /宿主机远程配置命令失败/);
assert.doesNotMatch(copy, /本次探测失败阶段/);
}
// Double activation emits one apply only. A 200 response that has not reached
// the authoritative active stage is never painted as success and advances to
// the server-selected reconcile step.
{
const ready = region(1, 'ready_to_apply', { plan_preview: preview() });
const test = fixture([ready]);
await test.respond(test.requests[0], { regions: test.initialRegions, engine_available: true });
test.nodes['fabric-auto-configure'].dispatch('click');
test.nodes['fabric-auto-configure'].dispatch('click'); await flush();
assert.equal(test.requests.filter(request => request.options.method === 'POST').length, 1);
const pending = region(1, 'needs_reconcile', { plan_preview: { ...preview(), mode: 'existing' } });
await test.respond(test.requests[1], { regions: [pending], engine_available: true });
assert.match(test.nodes['fabric-notice'].textContent, /尚未 active/);
assert.ok(!test.nodes['fabric-notice'].className.includes('is-success'));
assert.equal(test.nodes['fabric-auto-configure'].disabled, true);
assert.equal(test.nodes['fabric-reconcile'].disabled, false);
}
// Apply/reconcile host writes are accepted as asynchronous operations. The
// page correlates node evidence to the root operation id, exposes only fixed
// labels, permits only same-operation reconcile recovery, and polls the
// existing read endpoint until a terminal state is observed.
{
const ready = region(1, 'ready_to_apply', { plan_preview: preview() });
const test = fixture([ready]);
await test.respond(test.requests[0], { regions: test.initialRegions, engine_available: true });
test.nodes['fabric-auto-configure'].dispatch('click'); await flush();
const rootOperation = operation('running');
const matchedHost = {
host_id: '1', host_name: 'matched', fabric_member: true,
fabric_state: 'applying', readiness_state: 'uncertain', capabilities: {},
operation: {
id: rootOperation.id, status: 'running', phase: 'apply',
checkpoint: 'future_untrusted_checkpoint', reason: 'future_untrusted_reason',
started_at: 1791501000, updated_at: 1791501010, finished_at: null,
},
};
const staleHost = {
...matchedHost, host_id: '2', host_name: 'stale',
operation: { ...matchedHost.operation, id: 'b'.repeat(32), checkpoint: 'nft_install' },
};
const running = region(1, 'operation_running', {
operation: rootOperation, hosts: [matchedHost, staleHost],
plan_preview: { ...preview(), mode: 'existing' },
});
await test.respond(test.requests[1], {
operation: rootOperation, regions: [running], engine_available: true,
}, 202);
assert.match(test.nodes['fabric-notice'].textContent,
new RegExp('已受理 operation ' + rootOperation.id));
assert.equal(test.nodes['fabric-detect-hosts'].disabled, true);
assert.equal(test.nodes['fabric-auto-configure'].disabled, true);
assert.equal(test.nodes['fabric-reconcile'].disabled, false);
const runningCopy = test.nodes['fabric-hosts-body'].textContent;
assert.equal((runningCopy.match(/本轮 operation:/g) || []).length, 1);
assert.match(runningCopy, /本轮检查点:未知检查点/);
assert.match(runningCopy, /本轮原因:未识别原因(已隐藏原始值)/);
assert.doesNotMatch(runningCopy, /future_untrusted/);
test.nodes['fabric-reconcile'].dispatch('click');
test.nodes['fabric-reconcile'].dispatch('click'); await flush();
assert.equal(test.requests.filter(request =>
request.options.method === 'POST').length, 2);
assert.equal(test.requests[2].url,
'/api/v1/admin/fabric/regions/1/reconcile');
assert.equal(test.actions.every(button => button.disabled), true);
await test.respond(test.requests[2], {
operation: rootOperation, regions: [running], engine_available: true,
}, 202);
assert.equal(test.nodes['fabric-detect-hosts'].disabled, true);
assert.equal(test.nodes['fabric-auto-configure'].disabled, true);
assert.equal(test.nodes['fabric-reconcile'].disabled, false);
await test.advance(2000);
assert.equal(test.requests[3].url, '/api/v1/admin/fabric');
await test.respond(test.requests[3], { regions: [running], engine_available: true });
await test.advance(2000);
const completedOperation = operation('succeeded');
const completed = region(1, 'needs_reconcile', {
operation: completedOperation,
plan_preview: { ...preview(), mode: 'existing' },
workflow: workflow('needs_reconcile', { can_probe: true }),
});
await test.respond(test.requests[4], { regions: [completed], engine_available: true });
assert.match(test.nodes['fabric-notice'].textContent, /宿主机任务已成功/);
assert.match(test.nodes['fabric-notice'].textContent, /完成最终回读确认/);
assert.doesNotMatch(test.nodes['fabric-notice'].textContent, /重新检测/,
'A succeeded apply must not direct the administrator back to host detection.');
assert.equal(test.nodes['fabric-detect-hosts'].disabled, false,
'A succeeded apply may retain the optional read-only host detection action.');
assert.equal(test.nodes['fabric-reconcile'].disabled, false,
'A succeeded apply must advance to the explicit final reconcile.');
assert.match(test.nodes['fabric-readback-summary'].textContent,
new RegExp(completedOperation.id));
assert.match(test.nodes['fabric-readback-summary'].textContent, /succeeded(成功)/);
assert.match(test.nodes['fabric-readback-summary'].textContent,
new RegExp('journalctl --utc -u virt-cloud-fabric-apply-'
+ completedOperation.id + '\\.service --no-pager'));
}
// A completed asynchronous reconcile is not the final region mapping
// publication. The server keeps the fixed plan at needs_reconcile (without
// sending the wizard back to a redundant host probe), and only a new explicit
// reconcile followed by an authoritative load may advance the UI to active.
{
const pending = region(1, 'needs_reconcile', {
plan_preview: { ...preview(), mode: 'existing' },
});
const test = fixture([pending]);
await test.respond(test.requests[0], { regions: [pending], engine_available: true });
test.nodes['fabric-reconcile'].dispatch('click'); await flush();
const runningOperation = operation('running', 'reconcile', { id: 'c'.repeat(32) });
const running = region(1, 'operation_running', {
operation: runningOperation,
plan_preview: { ...preview(), mode: 'existing' },
});
await test.respond(test.requests[1], {
operation: runningOperation, regions: [running], engine_available: true,
}, 202);
await test.advance(2000);
assert.equal(test.requests[2].url, '/api/v1/admin/fabric');
const succeededOperation = {
...runningOperation, status: 'succeeded', finished_at: runningOperation.updated_at + 10,
};
const awaitingFinalReadback = region(1, 'needs_reconcile', {
operation: succeededOperation,
plan_preview: { ...preview(), mode: 'existing' },
workflow: workflow('needs_reconcile', { can_probe: true }),
});
await test.respond(test.requests[2], {
regions: [awaitingFinalReadback], engine_available: true,
});
assert.match(test.nodes['fabric-notice'].textContent, /完成最终回读确认/);
assert.match(test.nodes['fabric-wizard-stage'].textContent, /配置需要重新核对/);
assert.equal(test.nodes['fabric-step-probe-state'].textContent, '检测已通过');
assert.equal(test.nodes['fabric-detect-hosts'].disabled, false,
'A succeeded reconcile may retain the optional read-only host detection action.');
assert.equal(test.nodes['fabric-reconcile'].disabled, false,
'The server-selected final reconcile must remain available.');
assert.equal(test.requests.filter(request => request.options.method === 'POST').length, 1,
'Polling a succeeded reconcile must not publish the mapping automatically.');
test.nodes['fabric-reconcile'].dispatch('click'); await flush();
assert.equal(test.requests[3].url, '/api/v1/admin/fabric/regions/1/reconcile');
await test.respond(test.requests[3], {});
assert.equal(test.requests[4].url, '/api/v1/admin/fabric');
assert.equal(test.requests[4].options.method, 'GET');
const active = region(1, 'active', {
operation: succeededOperation,
plan_preview: { ...preview(), mode: 'existing' },
});
await test.respond(test.requests[4], { regions: [active], engine_available: true });
assert.match(test.nodes['fabric-wizard-stage'].textContent, /地区私网已就绪/);
assert.match(test.nodes['fabric-notice'].textContent, /重新核对并通过全部门禁/);
assert.equal(test.nodes['fabric-reconcile'].disabled, false);
assert.equal(test.requests.filter(request => request.options.method === 'POST').length, 2);
}
// A missing or replaced authoritative operation cannot leave the browser
// polling a stale id forever. It stops the old read-only loop, marks the
// outcome unknown, and never adopts or POSTs the replacement operation.
{
const rootOperation = operation('running');
const running = region(1, 'operation_running', {
operation: rootOperation, plan_preview: { ...preview(), mode: 'existing' },
});
const test = fixture([running]);
await test.respond(test.requests[0], { regions: [running], engine_available: true });
await test.advance(2000);
const replacement = operation('running', 'reconcile', { id: 'b'.repeat(32) });
const replaced = region(1, 'operation_running', {
operation: replacement, plan_preview: { ...preview(), mode: 'existing' },
});
await test.respond(test.requests[1], { regions: [replaced], engine_available: true });
assert.match(test.nodes['fabric-notice'].textContent,
new RegExp('operation ' + rootOperation.id + ' 已无法与服务端当前回读对应'));
assert.match(test.nodes['fabric-notice'].textContent, /不会重复提交/);
assert.equal(test.requests.filter(request => request.options.method === 'POST').length, 0);
await test.advance(10000);
assert.equal(test.requests.length, 2);
}
// An empty successful region projection is authoritative too. If the region
// was removed while its operation was running, polling must stop instead of
// retaining the stale in-memory row forever.
{
const rootOperation = operation('running');
const running = region(1, 'operation_running', {
operation: rootOperation, plan_preview: { ...preview(), mode: 'existing' },
});
const test = fixture([running]);
await test.respond(test.requests[0], { regions: [running], engine_available: true });
await test.advance(2000);
await test.respond(test.requests[1], { regions: [], engine_available: true });
assert.match(test.nodes['fabric-notice'].textContent,
new RegExp('operation ' + rootOperation.id + ' 已无法与服务端当前回读对应'));
assert.equal(test.requests.filter(request => request.options.method === 'POST').length, 0);
await test.advance(10000);
assert.equal(test.requests.length, 2);
}
// A region change while a request is in flight cannot redirect the returned
// projection into the newly selected region or redraw that region as active.
{
const first = region(1, 'ready_to_apply', { plan_preview: preview() });
const second = region(2, 'needs_probe');
const test = fixture([first, second]);
await test.respond(test.requests[0], { regions: test.initialRegions, engine_available: true });
test.nodes['fabric-auto-configure'].dispatch('click'); await flush();
test.nodes['fabric-region-select'].value = '2';
test.nodes['fabric-region-select'].dispatch('change'); await flush();
await test.respond(test.requests[1], { regions: [region(1, 'active'), second], engine_available: true });
assert.equal(test.nodes['fabric-region-select'].value, '2');
assert.match(test.nodes['fabric-wizard-stage'].textContent, /等待检测/);
assert.match(test.nodes['fabric-notice'].textContent, /后来选择的地区/);
}
// A write timeout is an unknown outcome. It releases the in-flight lock but
// prevents replay of automatic apply from the stale preflight projection.
{
const ready = region(1, 'ready_to_apply', { plan_preview: preview() });
const test = fixture([ready]);
await test.respond(test.requests[0], { regions: test.initialRegions, engine_available: true });
test.nodes['fabric-auto-configure'].dispatch('click'); await flush();
await test.advance(240000);
assert.match(test.nodes['fabric-notice'].textContent, /结果尚未确认/);
assert.match(test.nodes['fabric-wizard-stage'].textContent, /尚未确认/);
assert.equal(test.nodes['fabric-auto-configure'].disabled, true);
assert.equal(test.nodes['fabric-reconcile'].disabled, true,
'A lost receipt must not open another host-writing reconcile request.');
test.nodes['fabric-auto-configure'].dispatch('click'); await flush();
assert.equal(test.requests.filter(request => request.options.method === 'POST').length, 1,
'An unknown apply outcome must never be replayed from stale browser state.');
test.nodes['fabric-reconcile'].dispatch('click'); await flush();
assert.equal(test.requests.filter(request => request.options.method === 'POST').length, 1);
await test.advance(5000);
assert.equal(test.requests[2].url, '/api/v1/admin/fabric');
assert.equal(test.requests[2].options.method, 'GET');
const finished = operation('succeeded');
await test.respond(test.requests[2], {
regions: [region(1, 'active', { operation: finished })], engine_available: true,
});
assert.match(test.nodes['fabric-notice'].textContent, /成功并通过全部门禁/);
}
// Once a write has been dispatched, a non-HTTP fetch failure and every HTTP
// 5xx response are unknown outcomes. An explicit 4xx rejection remains
// definitive and does not invent an in-flight apply.
{
const ready = region(1, 'ready_to_apply', { plan_preview: preview() });
const test = fixture([ready]);
await test.respond(test.requests[0], { regions: test.initialRegions, engine_available: true });
test.nodes['fabric-auto-configure'].dispatch('click'); await flush();
await test.reject(test.requests[1]);
assert.match(test.nodes['fabric-notice'].textContent, /网络连接中断.*尚未确认/);
assert.equal(test.nodes['fabric-auto-configure'].disabled, true);
assert.equal(test.nodes['fabric-reconcile'].disabled, true);
}
{
const ready = region(1, 'ready_to_apply', { plan_preview: preview() });
const test = fixture([ready]);
await test.respond(test.requests[0], { regions: test.initialRegions, engine_available: true });
test.nodes['fabric-auto-configure'].dispatch('click'); await flush();
await test.respond(test.requests[1], { detail: '网关等待引擎超时' }, 504);
assert.match(test.nodes['fabric-notice'].textContent, /504.*结果尚未确认/);
assert.equal(test.nodes['fabric-auto-configure'].disabled, true,
'A gateway timeout must never leave stale automatic apply available.');
assert.equal(test.nodes['fabric-reconcile'].disabled, true,
'A gateway timeout must recover only through GET, without enabling another POST.');
}
{
const ready = region(1, 'ready_to_apply', { plan_preview: preview() });
const test = fixture([ready]);
await test.respond(test.requests[0], { regions: test.initialRegions, engine_available: true });
test.nodes['fabric-auto-configure'].dispatch('click'); await flush();
await test.respond(test.requests[1], { detail: '地区宿主机集合已经变化' }, 409);
assert.match(test.nodes['fabric-notice'].textContent, /集合已经变化/);
assert.equal(test.nodes['fabric-auto-configure'].disabled, false,
'A definitive HTTP rejection must not be converted into an unknown write outcome.');
assert.equal(test.nodes['fabric-reconcile'].disabled, true);
await test.advance(10000);
assert.equal(test.requests.length, 2, 'A definitive 409 must not start lost-receipt GET recovery.');
}
// Unknown outcomes are isolated by region. A later successful action for a
// different region must not clear the first region's stale-write guard.
{
const first = region(1, 'ready_to_apply', { plan_preview: preview() });
const second = region(2, 'ready_to_apply', { plan_preview: preview('sg') });
const test = fixture([first, second]);
await test.respond(test.requests[0], { regions: test.initialRegions, engine_available: true });
test.nodes['fabric-auto-configure'].dispatch('click'); await flush();
await test.reject(test.requests[1]);
assert.equal(test.nodes['fabric-auto-configure'].disabled, true);
assert.equal(test.nodes['fabric-reconcile'].disabled, true);
test.nodes['fabric-region-select'].value = '2';
test.nodes['fabric-region-select'].dispatch('change'); await flush();
assert.equal(test.nodes['fabric-auto-configure'].disabled, false);
test.nodes['fabric-auto-configure'].dispatch('click'); await flush();
await test.respond(test.requests[2], {
regions: [first, region(2, 'active', { plan_preview: { ...preview('sg'), mode: 'existing' } })],
engine_available: true,
});
assert.match(test.nodes['fabric-notice'].textContent, /通过权威回读/);
test.nodes['fabric-region-select'].value = '1';
test.nodes['fabric-region-select'].dispatch('change'); await flush();
assert.match(test.nodes['fabric-wizard-stage'].textContent, /尚未确认/);
assert.equal(test.nodes['fabric-auto-configure'].disabled, true,
'A successful action in region 2 must not unlock region 1 stale apply.');
assert.equal(test.nodes['fabric-reconcile'].disabled, true,
'Region 1 must keep its own GET-only lost-receipt guard.');
}
// The real gateway-502 case has no accepted-operation receipt. GET may still
// show an old failed id while admission waits behind a host observer. It must
// neither finish this request nor release writes. A later new id resumes the
// normal exact-id poller, without generating another POST.
{
const old = operation('failed', 'reconcile');
const initial = region(1, 'needs_reconcile', { operation: old });
const test = fixture([initial]);
await test.respond(test.requests[0], { regions: [initial], engine_available: true });
test.nodes['fabric-reconcile'].dispatch('click'); await flush();
await test.respond(test.requests[1], { detail: 'gateway failed' }, 502);
assert.equal(test.actions.every(button => button.disabled), true);
await test.advance(4999);
assert.equal(test.requests.length, 2, 'Lost-receipt GET recovery has a 5-second interval.');
await test.advance(1);
await test.respond(test.requests[2], { regions: [initial], engine_available: true });
assert.match(test.nodes['fabric-notice'].textContent, /不会把旧任务状态当成本轮结果/);
assert.equal(test.actions.every(button => button.disabled), true);
const fresh = operation('running', 'reconcile', {
id: 'b'.repeat(32), started_at: old.updated_at + 1,
});
const running = region(1, 'operation_running', { operation: fresh });
await test.advance(5000);
await test.respond(test.requests[3], { regions: [running], engine_available: true });
assert.match(test.nodes['fabric-notice'].textContent, /只读发现 operation b{32}/);
assert.equal(test.actions.every(button => button.disabled), true,
'Discovering an in-flight job must not enable another host write.');
await test.advance(2000);
const succeeded = region(1, 'active', {
operation: { ...fresh, status: 'succeeded', finished_at: old.updated_at + 20 },
});
await test.respond(test.requests[4], { regions: [succeeded], engine_available: true });
assert.match(test.nodes['fabric-notice'].textContent, /成功并通过全部门禁/);
assert.equal(test.requests.filter(request => request.options.method === 'POST').length, 1);
await test.advance(10000);
assert.equal(test.requests.length, 5);
}
// Reconcile is allowed to resume an already-known unsettled id. An old
// terminal id or a new wrong-kind/older operation is not a recovered receipt.
{
const old = operation('running', 'apply');
const initial = region(1, 'operation_running', { operation: old });
const test = fixture([initial]);
await test.respond(test.requests[0], { regions: [initial], engine_available: true });
test.nodes['fabric-reconcile'].dispatch('click'); await flush();
await test.reject(test.requests[1]);
await test.advance(5000);
await test.respond(test.requests[2], { regions: [initial], engine_available: true });
assert.match(test.nodes['fabric-notice'].textContent,
new RegExp('只读发现 operation ' + old.id));
assert.equal(test.actions.every(button => button.disabled), true);
await test.advance(2000);
await test.respond(test.requests[3], {
regions: [region(1, 'needs_reconcile', {
operation: { ...old, status: 'failed', finished_at: old.updated_at + 10 },
})], engine_available: true,
});
assert.match(test.nodes['fabric-notice'].textContent, /failed(失败)/);
assert.equal(test.requests.filter(request => request.options.method === 'POST').length, 1);
}
// A backwards client clock cannot evade the independent finite read count.
{
const old = operation('failed', 'reconcile');
const initial = region(1, 'needs_reconcile', { operation: old });
const test = fixture([initial]);
await test.respond(test.requests[0], { regions: [initial], engine_available: true });
test.nodes['fabric-reconcile'].dispatch('click'); await flush();
await test.respond(test.requests[1], {}, 502);
test.setClockOffset(-86400000);
for (let index = 0; index < 36; index += 1) {
await test.advance(5000);
await test.respond(test.requests.at(-1), { regions: [initial], engine_available: true });
}
await test.advance(5000);
assert.match(test.nodes['fabric-notice'].textContent, /只读恢复已达到上限/);
assert.equal(test.requests.filter(request => request.options.method === 'GET').length, 37,
'Only the initial load plus at most 36 recovery GETs may run.');
const reads = test.requests.length;
await test.advance(60000);
assert.equal(test.requests.length, reads);
assert.equal(test.actions.every(button => button.disabled), true);
}
// Selection changes cannot redirect a recovered receipt or unlock another
// unknown region. A late origin-region GET remains a read-only origin result.
{
const first = region(1, 'needs_reconcile', { operation: operation('failed', 'reconcile') });
const second = region(2, 'ready_to_apply', { plan_preview: preview('sg') });
const test = fixture([first, second]);
await test.respond(test.requests[0], { regions: [first, second], engine_available: true });
test.nodes['fabric-reconcile'].dispatch('click'); await flush();
await test.respond(test.requests[1], {}, 502);
test.nodes['fabric-region-select'].value = '2';
test.nodes['fabric-region-select'].dispatch('change'); await flush();
await test.advance(5000);
const fresh = operation('running', 'reconcile', {
id: 'c'.repeat(32), started_at: first.operation.updated_at + 1,
});
await test.respond(test.requests[2], {
regions: [region(1, 'operation_running', { operation: fresh }), second], engine_available: true,
});
assert.equal(test.nodes['fabric-region-select'].value, '2');
assert.match(test.nodes['fabric-wizard-stage'].textContent, /检测通过/);
assert.equal(test.nodes['fabric-auto-configure'].disabled, false);
test.nodes['fabric-region-select'].value = '1';
test.nodes['fabric-region-select'].dispatch('change'); await flush();
assert.equal(test.actions.every(button => button.disabled), true);
assert.match(test.nodes['fabric-readback-summary'].textContent, /c{32}/);
assert.equal(test.requests.filter(request => request.options.method === 'POST').length, 1);
}
{
const old = operation('failed', 'reconcile');
const initial = region(1, 'needs_reconcile', { operation: old });
const test = fixture([initial]);
await test.respond(test.requests[0], { regions: [initial], engine_available: true });
test.nodes['fabric-reconcile'].dispatch('click'); await flush();
await test.respond(test.requests[1], {}, 502);
for (const invalid of [
{ ...old, status: 'succeeded', updated_at: old.updated_at + 20 },
operation('succeeded', 'apply', { id: 'b'.repeat(32), started_at: old.updated_at + 1 }),
operation('succeeded', 'reconcile', { id: 'b'.repeat(32), started_at: old.updated_at - 1 }),
operation('succeeded', 'reconcile', { id: 'b'.repeat(32), started_at: null }),
]) {
await test.advance(5000);
await test.respond(test.requests.at(-1), {
regions: [region(1, 'active', { operation: invalid })], engine_available: true,
});
assert.equal(test.actions.every(button => button.disabled), true);
assert.match(test.nodes['fabric-wizard-stage'].textContent, /尚未确认/);
assert.doesNotMatch(test.nodes['fabric-notice'].textContent, /成功并通过/);
}
assert.equal(test.requests.filter(request => request.options.method === 'POST').length, 1);
}
// Recovery remains bounded even if GET succeeds forever with a stale failed
// id. Missing regions, diagnostic engine-unavailable shells and read errors
// do not release the stale-write guard or silently trigger a second POST.
{
const old = operation('failed', 'reconcile');
const initial = region(1, 'needs_reconcile', { operation: old });
const test = fixture([initial]);
await test.respond(test.requests[0], { regions: [initial], engine_available: true });
test.nodes['fabric-reconcile'].dispatch('click'); await flush();
await test.respond(test.requests[1], {}, 502);
for (let index = 0; index < 35; index += 1) {
await test.advance(5000);
await test.respond(test.requests.at(-1), { regions: [initial], engine_available: true });
}
await test.advance(5000);
assert.match(test.nodes['fabric-notice'].textContent, /只读恢复已达到上限/);
assert.equal(test.actions.every(button => button.disabled), true);
const reads = test.requests.length;
await test.advance(60000);
assert.equal(test.requests.length, reads);
assert.equal(test.requests.filter(request => request.options.method === 'POST').length, 1);
}
{
const initial = region(1, 'ready_to_apply', { plan_preview: preview() });
const test = fixture([initial]);
await test.respond(test.requests[0], { regions: [initial], engine_available: true });
test.nodes['fabric-auto-configure'].dispatch('click'); await flush();
await test.reject(test.requests[1]);
await test.advance(5000);
await test.respond(test.requests[2], { regions: [], engine_available: true });
await test.advance(5000);
await test.respond(test.requests[3], {
regions: [region(1, 'active', { operation: operation('succeeded') })],
engine_available: false,
});
assert.equal(test.actions.every(button => button.disabled), true);
assert.doesNotMatch(test.nodes['fabric-notice'].textContent, /成功并通过/);
for (let index = 0; index < 11; index += 1) {
await test.advance(5000);
await test.reject(test.requests.at(-1));
}
assert.match(test.nodes['fabric-notice'].textContent, /状态连续读取失败/);
const reads = test.requests.length;
await test.advance(60000);
assert.equal(test.requests.length, reads);
assert.equal(test.requests.filter(request => request.options.method === 'POST').length, 1);
}
// The diagnostic HTTP-200 shell is not a successful load and never enables a
// write, even when a stale region projection is present.
{
const stale = region(1, 'engine_unavailable', {
workflow: workflow('engine_unavailable', { can_probe: false, action_mode: 'none',
blocking_reasons: ['engine_unavailable'] }),
});
const test = fixture([stale]);
await test.respond(test.requests[0], { regions: test.initialRegions,
engine_available: false, engine_error: 'Fabric 引擎状态暂时无法读取。' });
assert.equal(test.actions.every(button => button.disabled), true);
assert.ok(test.nodes['fabric-notice'].className.includes('is-error'));
}
console.log('Administrator Fabric wizard gating, bounded GET-only lost-receipt recovery, exact operation identity and timeout safety passed.');