import test from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { formatChannelAddress, makeShineChannelShortRoute } from '../js/services/shine-routes.js'; import { validateChannelDisplayName, channelNameErrorText } from '../js/services/channel-name-rules.js'; const source = (path) => readFileSync(new URL(path, import.meta.url), 'utf8'); test('новый канал: 1–3 символа запрещены, 4 и 32 разрешены, 33 запрещены', () => { for (const value of ['a', 'ab', 'abc', ' АБВ ']) { assert.equal(validateChannelDisplayName(value).ok, false, value); } for (const value of ['abcd', 'tantra', 'ABC_', 'x'.repeat(32)]) { assert.equal(validateChannelDisplayName(value).ok, true, value); } assert.equal(validateChannelDisplayName('x'.repeat(33)).code, 'too_long'); assert.equal(validateChannelDisplayName('1234').code, 'digits_only'); assert.equal(validateChannelDisplayName('a.bc').code, 'bad_chars'); assert.equal(channelNameErrorText('too_short'), 'Название слишком короткое: минимум 4 символа.'); }); test('адрес канала не получает лишнюю @, даже если логин начинается с @', () => { for (const ownerLogin of ['Alice@free', '@Alice@free', '@@Alice@free']) { assert.equal(formatChannelAddress({ ownerLogin, channelName: 'tantra' }), 'Alice@free/tantra'); } assert.equal(formatChannelAddress({ ownerLogin: 'Alice@shine', channelName: 'course' }), 'Alice@shine/course'); assert.equal(formatChannelAddress({ ownerLogin: 'Alice@free' }), 'Alice@free'); assert.equal(makeShineChannelShortRoute({ ownerLogin: '@Alice@free', channelName: 'tantra' }), 'SHiNE/Alice@free/tantra'); }); test('адрес используется в шапке, списке, о канале, обсуждении, поиске, донате', () => { for (const path of [ '../js/pages/channel-view.js', '../js/pages/channels-list.js', '../js/pages/channel-about-view.js', '../js/pages/channel-thread-view.js', '../js/pages/search-view.js', '../js/pages/channel-donate-view.js', ]) { assert.match(source(path), /formatChannelAddress\(/, path); } const list = source('../js/pages/channels-list.js'); assert.match(list, /technical\.textContent = channel\.technicalLabel/); assert.doesNotMatch(list, /technical\.hidden = !!channel\.isOwnChannel/); }); test('сервер проверяет 4–32 при AddBlock, но декодер исторических блоков не ограничивает минимум', () => { const rules = source('../../SHiNE-server/shine-server-db/src/main/java/shine/db/channels/ChannelNameRules.java'); const handler = source('../../SHiNE-server/shine-server-net-protocol/src/main/java/server/logic/ws_protocol/JSON/handlers/blockchain/Net_AddBlock_Handler.java'); const body = source('../../SHiNE-server/shine-server-blockchain/src/main/java/blockchain/body/CreateChannelBody.java'); assert.match(rules, /MIN_DISPLAY_NAME_LENGTH = 4;/); assert.match(rules, /channelName length must be 4\.\.32/); assert.match(handler, /ChannelNameRules\.requireValidPublicDisplayNameForCreate\(createChannelBody\.channelName\)/); assert.match(handler, /"bad_channel_name"/); assert.match(body, /MIN_NAME_LENGTH_FOR_CREATE = 4/); assert.match(body, /checkForCreate\(\)/); assert.match(body, /public byte\[\] toBytes\(\) \{[\s\S]*?checkForCreate\(\)/); assert.match(handler, /createChannelBody\.checkForCreate\(\)/); assert.match(handler, /legacyReplayName = replayMode/); assert.match(handler, /if \(legacyReplayName\)/); assert.doesNotMatch(body.slice(body.indexOf('public CreateChannelBody check()'), body.indexOf('public CreateChannelBody checkForCreate()')), /length < MIN_NAME_LENGTH_FOR_CREATE/); const auth = source('../js/services/auth-service.js'); assert.match(auth, /length < 4 \|\| length > 20/); });