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

61 lines
2.7 KiB
JavaScript

import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
globalThis.document = { querySelector: () => null };
const {
serviceCompensationInstanceQuery,
serviceCompensationScopeId,
serviceCompensationScopeLabel,
} = await import(new URL(
'../public/cloud/admin-service-compensations.js?scope-behavior-test',
import.meta.url,
));
assert.equal(serviceCompensationScopeId('17'), 17,
'A selected positive decimal identifier must remain numeric at the API boundary.');
for (const invalid of ['', '0', '-1', '01', '1e2', '0x10', '1.5',
String(Number.MAX_SAFE_INTEGER + 1)]) {
assert.equal(serviceCompensationScopeId(invalid), null,
`The selector accepted a non-canonical or unsafe identifier: ${invalid}`);
}
assert.equal(serviceCompensationScopeLabel('host', {
id: 7, name: 'HKG-A', region: 'Hong Kong', enabled: true,
}), 'HKG-A · Hong Kong');
assert.equal(serviceCompensationScopeLabel('public_ip_pool', {
id: 8, host_name: 'HKG-A', address_family: 6,
name: 'IPv6 Primary', cidr: '2404:c140:156:38::/64', state: 'active',
}), 'HKG-A · IPv6 · IPv6 Primary · 2404:c140:156:38::/64');
assert.equal(serviceCompensationScopeLabel('instance', {
id: '19', name: 'vm-web', public_ipv4: '203.0.113.19',
host_name: 'HKG-B', user_name: 'alice',
}), 'vm-web · #19 · 203.0.113.19 · HKG-B · alice');
const search = new URL(
serviceCompensationInstanceQuery(' vm-web '),
'https://panel.example',
);
assert.equal(search.pathname, '/api/v1/admin/instances');
assert.equal(search.searchParams.get('q'), 'vm-web');
assert.equal(search.searchParams.get('page'), '1');
assert.equal(search.searchParams.get('per_page'), '20');
const source = await readFile(new URL(
'../public/cloud/admin-service-compensations.js', import.meta.url,
), 'utf8');
assert.match(source,
/function queueInstanceSearch\(\)[\s\S]*state\.instanceSearchSequence \+= 1;[\s\S]*setTimeout/,
'Typing a new query must invalidate an older in-flight instance response immediately.');
assert.match(source,
/if \(state\.instanceSearchTimer !== null\) \{[\s\S]*clearTimeout\(state\.instanceSearchTimer\);[\s\S]*state\.instanceSearchTimer = null;/,
'Switching away from instance search must cancel its pending debounce timer.');
assert.match(source,
/function invalidatePreview\(\) \{[\s\S]*state\.previewRequestSequence \+= 1;/,
'Changing the selected scope must invalidate an in-flight audience preview.');
assert.match(source,
/const requestSequence = \+\+state\.previewRequestSequence;[\s\S]*requestSequence !== state\.previewRequestSequence/,
'A stale audience response must not restore the preview for an older scope.');
console.log('Service compensation readable-scope selector behavior passed.');