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-danger-dialog.js', import.meta.url),
'utf8',
);
class FakeNode {
constructor(tag, documentRef) {
this.tagName = String(tag).toUpperCase();
this.ownerDocument = documentRef;
this.children = [];
this.parentNode = null;
this.listeners = new Map();
this.attributes = new Map();
this.dataset = {};
this.hidden = false;
this.disabled = false;
this.open = false;
this.value = '';
this._text = '';
const classes = new Set();
this.classList = {
add: (...names) => names.forEach(name => classes.add(name)),
remove: (...names) => names.forEach(name => classes.delete(name)),
contains: name => classes.has(name),
toggle: (name, force) => {
const active = force === undefined ? !classes.has(name) : Boolean(force);
if (active) classes.add(name); else classes.delete(name);
return active;
},
};
}
set id(value) { this.setAttribute('id', value); }
get id() { return this.getAttribute('id') || ''; }
set className(value) {
this.setAttribute('class', value);
String(value).split(/\s+/).filter(Boolean).forEach(name => this.classList.add(name));
}
get className() { return this.getAttribute('class') || ''; }
set textContent(value) { this._text = String(value ?? ''); }
get textContent() { return this._text; }
get isConnected() {
let node = this;
while (node) {
if (node === this.ownerDocument?.documentElement) return true;
node = node.parentNode;
}
return false;
}
append(...nodes) {
nodes.forEach(node => {
node.parentNode = this;
this.children.push(node);
});
}
setAttribute(name, value) {
this.attributes.set(String(name), String(value));
if (name === 'hidden') this.hidden = true;
}
getAttribute(name) { return this.attributes.get(String(name)) ?? null; }
hasAttribute(name) { return this.attributes.has(String(name)); }
removeAttribute(name) {
this.attributes.delete(String(name));
if (name === 'hidden') this.hidden = false;
}
addEventListener(type, listener) {
const listeners = this.listeners.get(type) || [];
listeners.push(listener);
this.listeners.set(type, listeners);
}
dispatch(type, fields = {}) {
const event = {
type,
target: fields.target || this,
currentTarget: this,
key: fields.key,
shiftKey: fields.shiftKey === true,
defaultPrevented: false,
preventDefault() { this.defaultPrevented = true; },
};
for (const listener of [...(this.listeners.get(type) || [])]) listener.call(this, event);
return event;
}
matches(selector) {
if (selector.startsWith('.')) return this.classList.contains(selector.slice(1));
return false;
}
querySelector(selector) {
for (const child of this.children) {
if (child.matches(selector)) return child;
const nested = child.querySelector(selector);
if (nested) return nested;
}
return null;
}
focus(options) {
this.ownerDocument.activeElement = this;
this.lastFocusOptions = options;
}
showModal() { this.open = true; this.setAttribute('open', ''); }
close() {
this.open = false;
this.removeAttribute('open');
this.dispatch('close');
}
}
class FakeDocument {
constructor() {
this.documentElement = new FakeNode('html', this);
this.body = new FakeNode('body', this);
this.documentElement.append(this.body);
this.activeElement = this.body;
}
createElement(tag) { return new FakeNode(tag, this); }
getElementById(id) {
const visit = node => {
if (node.id === id) return node;
for (const child of node.children) {
const match = visit(child);
if (match) return match;
}
return null;
};
return visit(this.documentElement);
}
}
function fixture() {
const document = new FakeDocument();
const make = (tag, id, className = '') => {
const node = document.createElement(tag);
node.id = id;
node.className = className;
return node;
};
const dialog = make('dialog', 'admin-danger-dialog', 'admin-danger-dialog');
const form = make('form', 'admin-danger-dialog-form');
const kicker = make('span', '', 'admin-danger-dialog-kicker');
const mark = make('span', '', 'admin-danger-dialog-mark');
const title = make('h2', 'admin-danger-dialog-title');
const message = make('p', 'admin-danger-dialog-message');
const close = make('button', 'admin-danger-dialog-close');
const cancel = make('button', 'admin-danger-dialog-cancel');
const confirm = make('button', 'admin-danger-dialog-confirm');
const verification = make('div', 'admin-danger-dialog-verification');
const input = make('input', 'admin-danger-dialog-verification-input');
const label = make('label', 'admin-danger-dialog-verification-label');
const hint = make('small', 'admin-danger-dialog-verification-hint');
verification.hidden = true;
verification.append(label, input, hint);
form.append(mark, kicker, title, message, close, verification, cancel, confirm);
dialog.append(form);
document.body.append(dialog);
const context = { document, console, Promise, Object, String, Error };
context.window = context;
context.globalThis = context;
vm.runInNewContext(source, context, { filename: 'admin-danger-dialog.js' });
return { document, dialog, form, title, message, close, cancel, confirm, verification, input, hint,
api: context.VenixAdminDangerDialog };
}
async function settled(promise) {
let result = { done: false };
promise.then(value => { result = { done: true, value }; });
await Promise.resolve();
return () => result;
}
// A normal dialog starts on the safe action, fails closed on cancellation and
// restores focus to the control that opened it.
{
const test = fixture();
const opener = test.document.createElement('button');
test.document.body.append(opener);
opener.focus();
const promise = test.api.confirm({
tone: 'warning', title: '创建修复任务', message: '不会重放旧任务。', confirmText: '继续',
});
assert.equal(test.dialog.open, true);
assert.equal(test.document.activeElement, test.cancel);
assert.equal(test.title.textContent, '创建修复任务');
assert.equal(test.message.textContent, '不会重放旧任务。');
test.cancel.dispatch('click');
assert.equal(await promise, false);
assert.equal(test.document.activeElement, opener);
}
// Typed confirmation is literal, stays disabled for near matches, renders
// caller copy as text and resolves only after an exact match.
{
const test = fixture();
const dangerous = '
';
const promise = test.api.confirm({
title: dangerous,
message: '',
requiredText: 'vm-prod-01',
confirmText: '永久删除',
});
assert.equal(test.title.textContent, dangerous);
assert.equal(test.verification.hidden, false);
assert.equal(test.confirm.disabled, true);
assert.equal(test.document.activeElement, test.input);
test.input.value = 'vm-prod-01 ';
test.input.dispatch('input');
assert.equal(test.confirm.disabled, true);
assert.equal(test.input.getAttribute('aria-invalid'), 'true');
test.input.value = 'vm-prod-01';
test.input.dispatch('input');
assert.equal(test.confirm.disabled, false);
assert.match(test.hint.textContent, /名称已匹配/);
test.form.dispatch('submit');
assert.equal(await promise, true);
}
// A concurrent confirmation cannot replace the active high-risk decision.
{
const test = fixture();
const first = test.api.confirm({ title: '第一项', message: '保持打开' });
const firstState = await settled(first);
const second = test.api.confirm({ title: '第二项', message: '不能覆盖' });
assert.equal(await second, false);
assert.equal(firstState().done, false);
assert.equal(test.title.textContent, '第一项');
test.dialog.dispatch('cancel');
assert.equal(await first, false);
}
console.log('Administrator danger dialog behavior checks passed.');