okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bcf88b738
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { EventEmitter } from 'node:events' ; 4import { createLocalShare , NO_KEY_WARNING } from '../server/local-share.mjs' ; 5import { createCloudflare , NO_KEY_TUNNEL_WARNING } from '../server/cloudflare.mjs' ; 6 7test ( 'sharing on the network works without an API key and says so; with a key there is no warning' , async () => { 8let key = false ; 9const share = createLocalShare ({ handle :async ( req , res ) => res . end ( 'ok' ), hasKey :() => key , host :'127.0.0.1' , port :0 }); 10try { 11const started = await share . start (); 12assert . equal ( started . active , true ); 13assert . equal ( started . warning , NO_KEY_WARNING ); 14key = true ; 15assert . equal ( share . state (). warning , null ); 16} finally { await share . stop (); } 17}); 18 19test ( 'a Cloudflare tunnel connects without an API key and carries the stronger warning' , async () => { 20let key = false ; 21const origin = { start :async () => {}, stop :async () => {} }; 22const launch = () => { 23const child = new EventEmitter (); 24child . stdout = new EventEmitter (); child . stderr = new EventEmitter (); 25child . kill = () => { setImmediate (() => child . emit ( 'exit' )); }; 26setImmediate (() => child . stderr . emit ( 'data' , 'https://demo-abc.trycloudflare.com\nRegistered tunnel connection\n' )); 27return child ; 28}; 29const tunnel = createCloudflare ({ origin, originUrl :'http://127.0.0.1:1' , hasKey :() => key , launch, locate :async () => '/bin/cloudflared' , timeoutMs :5000 }); 30try { 31const first = await tunnel . start ({ mode :'quick' }); 32assert . ok ([ 'starting' , 'connected' ]. includes ( first . status )); 33await new Promise ( resolve => setTimeout ( resolve , 50 )); 34const now = tunnel . state (); 35assert . equal ( now . status , 'connected' ); 36assert . equal ( now . warning , NO_KEY_TUNNEL_WARNING ); 37assert . match ( NO_KEY_TUNNEL_WARNING , / internet / ); 38key = true ; 39assert . equal ( tunnel . state (). warning , null ); 40} finally { await tunnel . stop (); } 41});