import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import vm from 'node:vm'; const read = file => fs.readFileSync(new URL(file, import.meta.url), 'utf8'); const auth = read('../js/services/auth-service.js'); const register = read('../js/pages/register-view.js'); const termsPath = new URL('../js/pages/registration-terms-view.js', import.meta.url); const hasRegistrationTerms = fs.existsSync(termsPath); const terms = hasRegistrationTerms ? read('../js/pages/registration-terms-view.js') : ''; const router = read('../js/router.js'); const email = read('../js/pages/registration-email-view.js'); const paid = read('../js/pages/registration-payment-view.js'); // Исполняем именно production-сериализатор, а не дублируем алгоритм тестовым кодом. const makeBytesFunction = auth.split('function makeHeaderBodyBytes(')[1]?.split('\nfunction makeUserParamBodyBytes(')[0]; assert.ok(makeBytesFunction); const context = { utf8Bytes: value => new TextEncoder().encode(value), int8Byte: value => Uint8Array.of(value), concatBytes: (...parts) => Uint8Array.from(parts.flatMap(part => Array.from(part))), Uint8Array, HEADER_LICENSE_FIELD_CC0: 'license:CC0-1.0', }; const makeHeaderBodyBytes = vm.runInNewContext(`function makeHeaderBodyBytes(${makeBytesFunction}\nmakeHeaderBodyBytes`, context); test('HEADER v2 places literal license:CC0-1.0 before the initial key', () => { const key = Uint8Array.from({ length: 32 }, (_, n) => n + 1); const result = makeHeaderBodyBytes({ login: 'Alice@shine', initialBlockchainKey32: key }); const login = new TextEncoder().encode('Alice@shine'); assert.equal(new TextDecoder().decode(result.slice(0,5)), 'SHiNE'); assert.equal(result[5], login.length); assert.deepEqual(Array.from(result.slice(6, 6+login.length)), Array.from(login)); assert.equal(new TextDecoder().decode(result.slice(6+login.length, 6+login.length+15)), 'license:CC0-1.0'); assert.deepEqual(Array.from(result.slice(6+login.length+15)), Array.from(key)); assert.equal(result.length, 5 + 1 + login.length + 15 + 32); assert.match(auth, /msgSubType: MSG_SUBTYPE_TECH_HEADER,\s*msgVersion: HEADER_VERSION_CC0/); assert.match(auth, /export const HEADER_VERSION_CC0 = 2/); }); test('free and paid registration both require accepted documents before any registration operation', { skip: !hasRegistrationTerms }, () => { assert.match(register, /navigate\('registration-terms-view'\)/); assert.match(terms, /draft\.termsAccepted = true/); assert.match(terms, /draft\.flowType === 'free' \? 'registration-email-view' : 'registration-payment-view'/); assert.match(email, /if \(!state\.registrationDraft\.termsAccepted\)/); assert.match(paid, /if \(!state\.registrationDraft\.termsAccepted\)/); assert.match(router, /\['registration-terms-view', 'register\/terms'\]/); assert.match(terms, /CC0_LEGAL_CODE_URL/); assert.match(terms, /DOCUMENT_COUNT = 4/); });