Files
SHiNE-server/shine-UI/tests/registration-namespace-options.test.mjs
T

53 lines
4.0 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { registrationNamespaceOptions, chooseRegistrationNamespace, formatRegistrationFee, registrationFee } from '../js/services/registration-namespace-options.js';
const ns = (name, fee, type = 1, currencyCode = 1) => ({ name, type, minLocalLength: 3, registrationFeeLamports: fee, currencyCode });
const all = [ns('shine', '70000000'), ns('people', '20000000'), ns('human', '20000000'), ns('free', '0', 2)];
test('paid options use actual prices, sort by fee then name, and preserve selected namespace', () => {
const options = registrationNamespaceOptions(all, 'paid');
assert.deepEqual(options.map(x => x.name), ['human', 'people', 'shine']);
assert.equal(chooseRegistrationNamespace(options, '').name, 'human');
assert.equal(chooseRegistrationNamespace(options, 'shine').name, 'shine');
assert.equal(chooseRegistrationNamespace(options, 'removed').name, 'human');
assert.equal(all[0].name, 'shine');
assert.equal(chooseRegistrationNamespace([], 'shine'), undefined);
});
test('free remains server-scoped, one option; empty and invalid types are safe', () => {
assert.deepEqual(registrationNamespaceOptions(all, 'free').map(x => x.name), ['free']);
assert.deepEqual(registrationNamespaceOptions(undefined, 'paid'), []);
assert.deepEqual(registrationNamespaceOptions(all, ''), []);
assert.deepEqual(registrationNamespaceOptions([ns('new', '3')], 'paid').map(x => x.name), ['new']);
});
test('SOL formatting preserves exact lamports and locale, rejects unsupported currencies', () => {
assert.equal(formatRegistrationFee(all[0], 'ru-RU'), '0,07 SOL');
assert.equal(formatRegistrationFee(all[1], 'en-US'), '0.02 SOL');
assert.equal(formatRegistrationFee(ns('one', '1'), 'en-US'), '0.000000001 SOL');
assert.equal(formatRegistrationFee(ns('free', '0'), 'en-US'), '0 SOL');
assert.equal(formatRegistrationFee(ns('large', '9007199254740993'), 'en-US'), '9,007,199.254740993 SOL');
assert.equal(formatRegistrationFee(ns('usdt', '3000000', 1, 2)), null);
assert.equal(registrationFee(ns('bad', '-1')), null);
assert.equal(registrationFee(ns('bad', undefined)), null);
assert.equal(registrationFee(ns('bad', Number.MAX_SAFE_INTEGER + 1)), null);
assert.deepEqual(registrationNamespaceOptions([ns('usd', '1', 1, 2), ...all], 'paid').map(x => x.name), ['human', 'people', 'shine', 'usd']);
});
test('login step displays the same access host as start page and distinguishes free migration', async () => {
const { readFileSync } = await import('node:fs');
const read = path => readFileSync(new URL(path, import.meta.url), 'utf8');
const login = read('../js/pages/register-view.js');
const start = read('../js/pages/start-view.js');
const hostExpression = /window\.location\.host \|\| window\.location\.hostname \|\| 'shineup\.me'/;
assert.match(login, hostExpression);
assert.match(start, hostExpression);
assert.match(login, /flowType === 'free'\s*\? 'screens\.register_view\.access_server_free_migration'\s*:\s*'screens\.register_view\.access_server_migration'/);
assert.match(login, /body\.append\(heading, description, label, namespaceLabel, preview, fee, limits, caseInfo, accessServer, status, next\)/);
for (const language of ['ru', 'en']) {
const strings = JSON.parse(read(`../js/i18n/locales/${language}.json`)).screens.register_view;
for (const key of ['access_server_title', 'access_server_intro', 'access_server_migration', 'access_server_free_migration']) assert.ok(strings[key]);
assert.match(strings.access_server_free_migration, /Solana/);
assert.notEqual(strings.access_server_migration, strings.access_server_free_migration);
}
const ru = JSON.parse(read('../js/i18n/locales/ru.json')).screens.register_view;
assert.equal(ru.access_server_migration, 'Ваш аккаунт не привязан навсегда к серверу доступа. В будущем вы можете сменить его, сохранив свой логин, социальные связи и доступность для других пользователей.');
});