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/AdminAcceptedTaskBehaviorTe...

39 lines
2.6 KiB
JavaScript

import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
const source = await readFile(new URL('../public/cloud/admin.js', import.meta.url), 'utf8');
globalThis.location = { href: 'https://panel.test/admin/cloud', origin: 'https://panel.test' };
const { taskUrl } = await import('../public/cloud/task-observer.js');
// Only endpoints that are defined as asynchronous task mutations use this
// strict helper. Existing synchronous and hybrid endpoints continue through
// api(), so their 200 responses remain valid.
assert.match(source, /async function acceptedTask\(path, options = \{\}\)/);
assert.match(source, /status !== 202 \|\| !taskUrl\(payload\)/);
assert.match(source, /import \{ announceTaskAccepted, observeTask, taskUrl \}/);
assert.equal(taskUrl({ task_id: 42, status_url: '/api/v1/tasks/42' }), '/api/v1/tasks/42');
assert.equal(taskUrl({ task_id: 42, status_url: '/api/v1/tasks/42', status: 'active' }), '');
assert.equal(taskUrl({ task_id: 42, status_url: 'https://other.test/api/v1/tasks/42' }), '');
const generalApi = source.slice(source.indexOf('async function api('), source.indexOf('async function acceptedTask('));
assert.doesNotMatch(generalApi, /status !== 202/,
'The general API helper must not reject synchronous 200 or hybrid 200/202 endpoints.');
const storage = source.slice(source.indexOf('async function saveHostStorage'), source.indexOf('function schedulerLabel'));
assert.match(storage, /acceptedTask\(`\/api\/v1\/admin\/hosts\/\$\{encodeURIComponent\(hostId\)\}\/storage-configuration`/);
assert.match(storage, /announceTaskAccepted\(receipt/);
assert.match(storage, /observeTask\(receipt/);
assert.match(storage, /task\.status === 'succeeded'/);
assert.match(storage, /api\(`\/api\/v1\/admin\/hosts\/\$\{encodeURIComponent\(hostId\)\}\/storage-configuration`\)/);
assert.match(storage, /renderHostStorage\(payload\)/);
assert.match(storage, /taskFailureMessage\(task/);
assert.match(storage, /task\.safe_to_resubmit !== true/);
assert.doesNotMatch(storage, /setInterval\(/);
const catalog = source.slice(source.indexOf('async function loadCatalog'), source.indexOf('function taskFailureMessage'));
assert.match(catalog, /acceptedTask\(`\$\{catalogPath\(\)\}\/\$\{encodeURIComponent\(entry\.id\)\}`/);
assert.match(catalog, /importProgress\(receipt, '同步镜像名称'/);
assert.match(catalog, /await loadCatalog\(\);\s*notice\('名称已保存。'\);/);
assert.doesNotMatch(catalog, /await api\([^;]+method: 'PATCH'[\s\S]*?await loadCatalog\(\);\s*notice\('名称已保存。'\);/);
console.log('Admin strict 202 task receipt, bounded observation and terminal refresh behavior passed.');