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.
59 lines
3.6 KiB
JavaScript
59 lines
3.6 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { readFile } from 'node:fs/promises';
|
|
import vm from 'node:vm';
|
|
|
|
globalThis.location = new URL('https://panel.test/admin/instances');
|
|
const tasks = await import('../public/cloud/task-observer.js');
|
|
const { createAdminInstanceActions } = await import('../public/cloud/admin-instance-actions.js');
|
|
const context = { Headers, AbortController, setTimeout, clearTimeout };
|
|
vm.runInNewContext(await readFile(new URL('../public/cloud/admin-request-runtime.js', import.meta.url), 'utf8'), context);
|
|
|
|
const calls = [];
|
|
let response = { status: 202, ok: true, json: async () => ({ task_id: '42', status_url: '/api/v1/tasks/42' }) };
|
|
let observation;
|
|
let announced = 0;
|
|
const actions = createAdminInstanceActions({
|
|
runtimeFactory: context.VenixAdminRequestRuntime.create,
|
|
fetchImpl: async (path, options) => { calls.push({ path, options }); return response; },
|
|
locationImpl: globalThis.location,
|
|
csrfToken: () => 'a'.repeat(64),
|
|
tasks: { ...tasks, announceTaskAccepted() { announced += 1; },
|
|
observeTask(receipt, options) { observation = options; return { url: receipt.status_url, stop() {} }; } },
|
|
});
|
|
const controller = new AbortController();
|
|
const receipt = await actions.enqueue('/api/v1/admin/instances/1/settings', { confirm_restart: true }, controller);
|
|
assert.equal(calls.length, 1);
|
|
assert.equal(calls[0].options.method, 'PATCH');
|
|
assert.equal(calls[0].options.headers.get('X-Admin-CSRF-Token'), 'a'.repeat(64));
|
|
assert.ok(calls[0].options.headers.get('Idempotency-Key'));
|
|
|
|
let result;
|
|
actions.watch(receipt, { title: '修改设置', onResult(value) { result = value; } });
|
|
assert.equal(announced, 1);
|
|
observation.onTerminal(tasks.normalizeTask({ id: '42', completed: true, status: 'succeeded' }));
|
|
assert.equal(result.succeeded, true);
|
|
observation.onTerminal(tasks.normalizeTask({ id: '42', completed: true, status: 'failed',
|
|
effect_may_have_applied: true, safe_to_resubmit: false, error: '网络中断' }));
|
|
assert.equal(result.succeeded, false);
|
|
assert.match(result.message, /请勿再次提交/);
|
|
observation.onTerminal(tasks.normalizeTask({ id: '42', completed: true, status: 'succeeded',
|
|
recovery: { pending: true, message: '本地账本收尾中',
|
|
last_checked_at: 1600000000, next_check_at: 1600000300 } }));
|
|
assert.equal(result.succeeded, false, 'A successful host receipt with pending recovery must not unlock the UI.');
|
|
|
|
response = { status: 200, ok: true, json: async () => ({ task_id: '42', status_url: '/api/v1/tasks/42' }) };
|
|
await assert.rejects(actions.enqueue('/api/task', {}, controller, 'POST'), error =>
|
|
error.code === 'invalid_task_receipt' && error.mutationMayHaveApplied && !error.safeToRetry);
|
|
assert.equal(calls.length, 2, 'A malformed receipt must not replay the mutation.');
|
|
response = { status: 200, ok: true, json: async () => ({ deleted: true }) };
|
|
assert.equal((await actions.commit('/api/backup/1', undefined, controller, 'DELETE')).deleted, true);
|
|
assert.equal(calls[2].options.method, 'DELETE');
|
|
await actions.read('/api/backup', controller);
|
|
assert.equal(calls[3].options.method, 'GET');
|
|
assert.equal(calls[3].options.headers.get('Idempotency-Key'), null);
|
|
assert.equal(actions.blockMessage({ blocks_mutation: false }, '恢复'), '');
|
|
assert.match(actions.blockMessage({ id: '42', blocks_mutation: true, completed: true,
|
|
recovery: { pending: true }, dispatch_state: 'not_dispatched' }, '恢复'), /PHP 本地账本收尾尚未完成/);
|
|
assert.match(actions.blockMessage({ id: '42', blocks_mutation: true, completed: false, status: 'active' }, '恢复'), /执行中/);
|
|
console.log('Administrator instance action submission, observation and fence behavior passed.');
|