CORE/CONTENT test5593 и адаптация UI

This commit is contained in:
AidarKC
2026-11-27 00:47:13 +03:00
parent 205b7ad971
commit 873535654e
1271 changed files with 219621 additions and 1900 deletions
+60
View File
@@ -0,0 +1,60 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { KeyRotationClient } from '../js/services/key-rotation-service.js';
import { coreHex, coreHashBytes, signCoreContentPair, verifyCoreContentPair } from '../js/services/core-content.js';
import { parseAns104DataItem } from '../js/services/ans104-data-item.js';
import { SHINE_CORE_LINE_TECH } from '../js/services/protocol-tags.js';
const b64=b=>Buffer.from(b).toString('base64');
const unb64=s=>Uint8Array.from(Buffer.from(s,'base64'));
const id=b=>Buffer.from(b).toString('base64url');
const sha=async b=>new Uint8Array(await crypto.subtle.digest('SHA-256',b));
const old=await crypto.subtle.generateKey('Ed25519',true,['sign','verify']);
const next=await crypto.subtle.generateKey('Ed25519',true,['sign','verify']);
const owner= new Uint8Array(await crypto.subtle.exportKey('raw',old.publicKey));
const nextOwner= new Uint8Array(await crypto.subtle.exportKey('raw',next.publicKey));
const bundle={blockchainPair:{publicKeyB64:b64(nextOwner),privateKey:next.privateKey}};
const name='Alice-001', start=1760000000;
const rows=[];
async function append(type,subType,lineCode=null,prevLine=0){
const n=rows.length;
const pair=await signCoreContentPair({owner32:owner,privateKey:old.privateKey,content:{type,subType,version:1,body:new Uint8Array()},prevCoreHash32:n?coreHashBytes(rows[n-1].coreHash):new Uint8Array(32),blockNumber:n,timestamp:start+n,line:lineCode===null?null:{lineCode,prevLineBlockNumber:prevLine,prevLineCoreHash32:coreHashBytes(rows[prevLine].coreHash)}});
rows.push({blockchainName:name,blockNumber:n,coreHash:coreHex(pair.coreHash32),coreDataItemId:id(await sha(parseAns104DataItem(pair.coreDataItem).signature)),contentHash:coreHex(pair.contentHash32),contentDataItemId:id(pair.contentDataItemId32),coreDataItemB64:b64(pair.coreDataItem),contentDataItemB64:b64(pair.contentDataItem),contentStatus:'VALID'});
}
await append(0,0);await append(0,1,SHINE_CORE_LINE_TECH);await append(1,10,1,1);await append(0,1,SHINE_CORE_LINE_TECH,1);
const rotation={sourceBlockchainName:name,forkFromBlock:2,forkFromHash:rows[2].coreHash,sourceTipBlock:3,sourceTipHash:rows[3].coreHash,oldBlockchainKey:b64(owner),newBlockchainKey:b64(nextOwner),createdAtMs:(start+10)*1000,reasonCode:3,comment:' test\r\nrollback ',progressCurrent:0};
function fixture(change={}){
const writes=[],reads=[];
const client=new KeyRotationClient({ws:{request:async(op,payload)=>{
if(op==='GetBlockchainBlock'){reads.push(payload.blockNumber);assert.equal(payload.blockchainName,name);return {status:200,payload:{...rows[payload.blockNumber],...change[payload.blockNumber]}};}
if(op==='GetMyBlockchain'){assert.equal(payload.includeBlockBytes,false);return {status:200,payload:{blocks:rows.map(r=>({...r,contentDataItemB64:null})),tipBlockNumber:3,nextBeforeBlock:null}};}
assert.equal(op,'KeyRotationAddBlock');writes.push(payload);return {status:200,payload:{rotationStatus:'COPYING_CHAIN'}};
}}});
return {client,writes,reads};
}
test('original cutoff picker derives hash and time from signed CORE-only page',async()=>{
const {client}=fixture();const page=await client.getMyBlockchain();assert.equal(page.blocks[2].blockHash,rows[2].coreHash);assert.equal(page.blocks[2].timestampMs,(start+2)*1000);
});
test('rollback FORK preserves body layout and TECH predecessor before cutoff; deterministic retry',async()=>{
const {client,writes,reads}=fixture({1:{contentDataItemB64:null,contentStatus:'MISSING'}});const progress=[];
await client.copyCandidateChain({rotation,newBundle:bundle,onProgress:p=>progress.push(p)});
await client.copyCandidateChain({rotation,newBundle:bundle});assert.deepEqual(writes[0],writes[1]);assert.deepEqual(reads,[2,3,1,2,3,1]);assert.deepEqual(progress,[{phase:'copy',current:1,total:1}]);
const w=writes[0];assert.deepEqual(Object.keys(w).sort(),['blockNumber','contentDataItemB64','coreDataItemB64','prevBlockHash']);
const {core,content}=await verifyCoreContentPair(unb64(w.coreDataItemB64),unb64(w.contentDataItemB64),nextOwner);
assert.equal(core.blockNumber,3);assert.equal(core.timestamp,BigInt(start+10));assert.equal(coreHex(core.prevCoreHash32),rows[2].coreHash);assert.equal(core.line.lineCode,SHINE_CORE_LINE_TECH);assert.equal(core.line.prevLineBlockNumber,1);assert.equal(coreHex(core.line.prevLineCoreHash32),rows[1].coreHash);
assert.deepEqual([content.type,content.subType,content.version],[0,2,1]);
// Independent layout matching ForkContentBody.java, no structural prefix.
const b=Buffer.from(content.body);assert.equal(b.subarray(0,32).toString('base64'),b64(owner));assert.equal(b.readInt32BE(32),2);assert.equal(b.subarray(36,68).toString('hex'),rows[2].coreHash);assert.equal(b.readBigInt64BE(68),BigInt((start+2)*1000));assert.equal(b.readInt32BE(76),3);assert.equal(b.subarray(80,112).toString('hex'),rows[3].coreHash);assert.equal(b.readBigInt64BE(112),BigInt((start+3)*1000));assert.equal(b.readInt32BE(120),1);assert.equal(b[124],3);assert.equal(b.readUInt16BE(125),13);assert.equal(b.subarray(127).toString(),'test\nrollback');
});
test('genesis cutoff uses HEADER as TECH predecessor and preserves old source tip',async()=>{
const {client,writes}=fixture();await client.copyCandidateChain({rotation:{...rotation,forkFromBlock:0,forkFromHash:rows[0].coreHash},newBundle:bundle});const {core}=await verifyCoreContentPair(unb64(writes[0].coreDataItemB64),unb64(writes[0].contentDataItemB64),nextOwner);assert.equal(core.line.prevLineBlockNumber,0);assert.equal(coreHex(core.line.prevLineCoreHash32),rows[0].coreHash);
});
test('wrong source metadata or session hash refuses candidate before write',async()=>{
for(const [overrides,change] of [[{forkFromHash:'ff'.repeat(32)},{}],[{}, {2:{coreHash:'ff'.repeat(32)}}],[{}, {1:{blockchainName:'Bob-001'}}]]){
const {client,writes}=fixture(change);await assert.rejects(client.copyCandidateChain({rotation:{...rotation,...overrides},newBundle:bundle}));assert.equal(writes.length,0);
}
});
test('resume of published candidate does not submit again; invalid key and comment rejected',async()=>{
const {client,writes,reads}=fixture();await client.copyCandidateChain({rotation:{...rotation,progressCurrent:1},newBundle:bundle});assert.equal(writes.length,0);assert.equal(reads.length,0);
await assert.rejects(client.copyCandidateChain({rotation:{...rotation,newBlockchainKey:b64(owner)},newBundle:bundle}));
await assert.rejects(client.copyCandidateChain({rotation:{...rotation,comment:'я'.repeat(513)},newBundle:bundle}));assert.equal(writes.length,0);
});