import test from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { readSolanaEndpointSettings, validateHttpEndpoint, verifyCustomSolanaCluster } from '../js/services/solana-endpoint-settings.js'; const read = p => readFileSync(new URL('../' + p, import.meta.url), 'utf8'); test('legacy settings and gateway mode derive the endpoint from the selected SHiNE server', () => { assert.deepEqual(readSolanaEndpointSettings({solanaServer:'https://old.example/rpc'}, 'https://t1.example'), {solanaRpcMode:'gateway',customSolanaServer:'',solanaServer:'https://t1.example/solana-rpc'}); assert.equal(readSolanaEndpointSettings({solanaRpcMode:'gateway',customSolanaServer:'https://custom.example/rpc'}, 'https://t2.example/').solanaServer, 'https://t2.example/solana-rpc'); }); test('custom RPC survives persistence, including path and query; switching to gateway preserves it', () => { const customSolanaServer = 'https://rpc.example/path/?api-key=abc'; const custom = readSolanaEndpointSettings({solanaRpcMode:'custom',customSolanaServer}, 'https://shine.example'); assert.equal(custom.solanaServer,customSolanaServer); const stored = JSON.parse(JSON.stringify(custom)); assert.deepEqual(readSolanaEndpointSettings(stored,'https://shine.example'),custom); const gateway = readSolanaEndpointSettings({...stored,solanaRpcMode:'gateway'},'https://shine.example'); assert.equal(gateway.customSolanaServer,customSolanaServer); assert.equal(validateHttpEndpoint(customSolanaServer),customSolanaServer); assert.throws(()=>validateHttpEndpoint('javascript:alert(1)')); assert.throws(()=>validateHttpEndpoint('https://user:pass@rpc.example')); }); test('custom RPC must report the correct cluster', async () => { const fake = hash => async (_url, init) => {assert.equal(JSON.parse(init.body).method,'getGenesisHash'); return {ok:true,json:async()=>({result:hash})};}; assert.equal(await verifyCustomSolanaCluster('https://rpc.example','devnet',fake('EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG')),true); assert.equal(await verifyCustomSolanaCluster('https://rpc.example','mainnet-beta',fake('EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG')),false); assert.equal(await verifyCustomSolanaCluster('https://rpc.example','devnet',async()=>{throw Error('offline');}),false); }); test('both settings screens share the selector; server PDA tools do not expose test top-ups', () => { for(const name of ['entry','server']) { const source=read(`js/pages/${name}-settings-view.js`); assert.match(source,/createSolanaRpcMode\(/); assert.doesNotMatch(source,/key: 'shineServerLogin'/); } for(const name of ['create','update']) { assert.doesNotMatch(read(`server-ui/${name}-server-pda.html`),/btnTopupDevnet/); const source=read(`server-ui/js/${name}-server-pda-page.js`); assert.doesNotMatch(source,/openDevnetTopup|btnTopupDevnet/); assert.match(source,/btnRefreshBalance/); } });