import test from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { AuthService } from '../js/services/auth-service.js'; import { formatChannelAddress, makeShineChannelShortRoute, makeProfileRoute } from '../js/services/shine-routes.js'; import { initI18n, setLanguage } from '../js/i18n/index.js'; import { validateChannelDisplayName } from '../js/services/channel-name-rules.js'; const hash = 'a'.repeat(64); const zero = '0'.repeat(64); test('канал 0: адрес локализуется, существующий маршрут профиля сохраняется', async () => { const originalFetch = globalThis.fetch; globalThis.fetch = async url => ({ ok: true, json: async () => JSON.parse(readFileSync(new URL(url), 'utf8')) }); try { await initI18n(); await setLanguage('ru'); assert.equal(formatChannelAddress({ ownerLogin: 'Alice@free', channelName: 'stories', channelRootBlockNumber: 0 }), 'Alice@free (Публикации)'); assert.equal(formatChannelAddress({ ownerLogin: 'Alice@free', channelName: 'news', channelRootBlockNumber: 8 }), 'Alice@free/news'); assert.equal(makeShineChannelShortRoute({ ownerLogin: 'Alice@free', channelName: 'stories' }), 'SHiNE/Alice@free/stories'); assert.equal(makeProfileRoute('Alice@free'), 'SHiNE/Alice@free'); await setLanguage('en'); assert.equal(formatChannelAddress({ ownerLogin: 'Alice@free', channelRootBlockNumber: 0 }), 'Alice@free (Publications)'); } finally { globalThis.fetch = originalFetch; } assert.equal(validateChannelDisplayName('Stories').code, 'reserved_stories'); }); test('первая публикация использует Header, нулевой lineCode и sequence 0', async () => { const mock = { resolveHeaderHashForBlockchain: async name => { assert.equal(name, 'alice@free-001'); return hash; }, lineTailCache: new Map(), findLatestOwnLineBlock: async options => { assert.equal(options.lineCode, 0); return null; }, listOwnChannelsForBlockchain: async () => assert.fail('CreateChannel is not required'), }; const args = { login: 'alice@free', blockchainName: 'alice@free-001', channel: { ownerBlockchainName: 'alice@free-001', channelRootBlockNumber: 0, channelRootBlockHash: zero } }; const result = await AuthService.prototype.resolveChannelLineTail.call(mock, args); assert.equal(result.lineCode, 0); assert.equal(result.channelSlug, 'stories'); assert.equal(result.prevLineNumber, 0); assert.equal(result.prevLineHashHex, hash); assert.equal(result.thisLineNumber, 0); }); test('FOLLOW на публикации использует тот же корень; UNFOLLOW снимает и старые хеши', async () => { const calls = []; const mock = { resolveHeaderHashForBlockchain: async () => hash, listSubscriptionsFeed: async () => ({ followedUsersChannels: [zero, hash].map(blockHash => ({ channel: { ownerLogin: 'Bob@free', channelRoot: { blockNumber: 0, blockHash } } })) }), addBlockConnectionTarget: async value => { calls.push(value); return {}; }, }; const args = { login: 'Alice@free', targetBlockchainName: 'bob@free-001', targetBlockNumber: 0, targetBlockHashHex: zero }; await AuthService.prototype.addBlockFollowChannel.call(mock, args); assert.equal(calls[0].toBlockNumber, 0); assert.equal(calls[0].toBlockHashHex, hash); await AuthService.prototype.addBlockFollowChannel.call(mock, { ...args, unfollow: true }); assert.deepEqual(calls.slice(1).map(call => call.toBlockHashHex), [zero, hash]); assert.ok(calls.slice(1).every(call => call.toBlockNumber === 0)); });