import test from 'node:test'; import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import {developmentServer,serverTokenKey,connectionClient,checkDevelopmentServer} from '../src/core/server-address.ts'; const response=value=>new Response(JSON.stringify(value),{headers:{'Content-Type':'application/json'}}); test('development address accepts private IP, port and API prefix with optional scheme',()=>{ assert.equal(developmentServer(' 192.168.28.20:8080 '),'http://192.168.28.20:8080'); assert.equal(developmentServer('192.168.28.20:8088/dev-api/'),'http://192.168.28.20:8088/dev-api'); assert.equal(developmentServer('https://10.0.0.2/api/'),'https://10.0.0.2/api'); assert.equal(developmentServer('127.0.0.1:8080'),'http://127.0.0.1:8080'); for(const value of ['', 'example.com','http://8.8.8.8','192.168.28.1.evil.test','http://user:secret@10.0.0.2','10.0.0.2?token=x','10.0.0.2/#a','file:///tmp','10.0.0.2/%2e%2e/private','10.0.0.2/'+ 'a'.repeat(240)])assert.throws(()=>developmentServer(value)); }); test('saved native credentials are isolated by exact server address and environment',()=>{ assert.notEqual(serverTokenKey('dev','http://10.0.0.1:8080'),serverTokenKey('dev','http://10.0.0.2:8080')); assert.notEqual(serverTokenKey('dev','http://10.0.0.1/a'),serverTokenKey('dev','http://10.0.0.1/b')); assert.equal(serverTokenKey('prod','http://192.168.28.10/prod-api'),'cmvr.prod.token.internal'); assert.notEqual(serverTokenKey('prod','http://192.168.28.10/prod-api'),serverTokenKey('prod','https://wms.svc.cmvr-iot.icu/prod-api')); assert.throws(()=>serverTokenKey('prod','https://evil.example/prod-api')); assert.match(serverTokenKey('dev','http://10.0.0.1:8080'),/^[a-z\d._-]+$/); }); test('connection handshake is anonymous, read-only, validates system response and never retries writes',async()=>{ const calls=[];const transport=async(url,options)=>{calls.push({url,options});return response({code:200,captchaEnabled:false});}; assert.equal(await checkDevelopmentServer('10.0.0.2:8080',undefined,transport),'http://10.0.0.2:8080'); assert.equal(calls[0].url,'http://10.0.0.2:8080/captchaImage');assert.equal(calls[0].options.method,'GET'); assert.equal(calls[0].options.headers.Authorization,undefined);assert.equal(calls[0].options.redirect,'error'); await assert.rejects(checkDevelopmentServer('10.0.0.2',undefined,async()=>response({code:200})),/系统登录信息/); await assert.rejects(checkDevelopmentServer('10.0.0.2',undefined,async()=>{throw Error('offline');}),error=>error.code===503); }); test('preview explicitly routes to selected service; native never uses a preview routing header',async()=>{ const calls=[];const transport=async(url,options)=>{calls.push({url,options});return response({code:200,captchaEnabled:false});}; await checkDevelopmentServer('192.168.28.20:8080','http://localhost:8091',transport); assert.equal(calls[0].url,'http://localhost:8091/native-api/captchaImage'); assert.equal(calls[0].options.headers['X-CMVR-Dev-Server'],'http://192.168.28.20:8080'); const api=connectionClient('http://10.0.0.2',()=>{},undefined,transport);await api.request('/getInfo'); assert.equal(calls[1].options.headers['X-CMVR-Dev-Server'],undefined); }); test('both login and settings expose the editor only for development; old in-flight sessions invalidated',()=>{ const app=readFileSync(new URL('../src/App.tsx',import.meta.url),'utf8'); assert.match(app,/variant==='dev'\?]*connection=\{connection\}/);assert.match(app,/]*connection=\{connection\}/); assert.match(app,/epoch\.current\+\+;api\.clear\(\)/);assert.match(app,/await checkDevelopmentServer/); assert.match(app,/await SecureStore\.deleteItemAsync\(serverTokenKey\(variant,checked\)\)/); const plugin=readFileSync(new URL('../plugins/with-internal-network.cjs',import.meta.url),'utf8'); assert.match(plugin,/env\.variant==='dev'\?'true':'false'/); const inputs=readFileSync(new URL('../plugins/with-private-build-inputs.cjs',import.meta.url),'utf8'); assert.match(inputs,/sources\.exclude\("keys", "keys\/\*\*"/); });