import assert from 'node:assert/strict'; import { createAdminInstanceMigration } from '../public/cloud/admin-instance-migration.js'; class Element { constructor(tag = 'div') { this.tag = tag; this.children = []; this.listeners = new Map(); this.dataset = {}; this.value = ''; this.checked = false; this.disabled = false; this.hidden = false; this.textContent = ''; } append(...children) { this.children.push(...children); } addEventListener(name, listener) { this.listeners.set(name, listener); } querySelectorAll(selector) { const tags = selector.split(',').map(value => value.trim()); const descendants = this.children.flatMap(child => child instanceof Element ? [child, ...child.querySelectorAll(selector)] : []); return descendants.filter(child => tags.includes(child.tag)); } get selectedOptions() { return this.tag === 'select' ? this.children.filter(child => child instanceof Element && child.tag === 'option' && String(child.value) === String(this.value)) : []; } } globalThis.document = { createElement: tag => new Element(tag), createTextNode: value => ({ textContent: String(value) }), }; globalThis.Option = class extends Element { constructor(label = '', value = '') { super('option'); this.textContent = String(label); this.value = String(value); } }; const submissions = []; const migration = createAdminInstanceMigration({ isBusy: () => false, async submitTask(value) { submissions.push(value); return { task_id: '42' }; }, notice() {}, refresh() {}, }); const section = migration.render({ can_migrate: true, migration_targets: [{ host_id: '9', host_name: 'HKG-B', region: 'Hong Kong', line: 'Premium', resource_pool_id: '77', available_ip_count: 3, }, { host_id: '11', host_name: 'No dual-stack capacity', region: 'Hong Kong', line: 'Premium', resource_pool_id: '77', available_ip_count: 0, }], }, '123'); const form = section.querySelectorAll('form')[0]; const selects = form.querySelectorAll('select'); const confirm = section.querySelectorAll('input')[0]; assert.equal(selects[0].children[1].dataset.resourcePoolId, '77'); assert.match(selects[0].children[1].textContent, /Premium/); assert.match(selects[0].children[1].textContent, /3 组可用双栈地址/); assert.equal(selects[0].children.length, 2, 'A target without a complete dual-stack address bundle was offered.'); selects[0].value = '9'; selects[1].value = 'delete'; confirm.checked = true; await form.listeners.get('submit')({ preventDefault() {} }); assert.equal(submissions.length, 1); assert.deepEqual(submissions[0].body, { target_host_id: 9, target_resource_pool_id: 77, cleanup_mode: 'delete', confirm_maintenance_migration: true, }); const invalid = migration.render({ can_migrate: true, migration_targets: [{ host_id: '10', host_name: 'Broken target', resource_pool_id: '', available_ip_count: 1, }], }, '123'); const invalidForm = invalid.querySelectorAll('form')[0]; const invalidSelects = invalidForm.querySelectorAll('select'); invalidSelects[0].value = '10'; invalid.querySelectorAll('input')[0].checked = true; await invalidForm.listeners.get('submit')({ preventDefault() {} }); assert.equal(submissions.length, 1, 'A target without its exact resource-pool identity reached the API.'); console.log('Administrator maintenance-migration target identity behavior passed.');