Add amber phosphor Telnet terminal with isolated BPQ sessions

This commit is contained in:
alberto committed 2026-10-16 18:55:15 +02:00
1 parent b5ee05f257
commit 3f6d35690b
8 files changed
+96 -5

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
import {test} from 'node:test';import assert from 'node:assert/strict';import net from 'node:net';import {once} from 'node:events';import {Terminal,TelnetText} from './terminal.mjs';
test('Telnet handles fragmented negotiation, escaped IAC and subnegotiations',()=>{const replies=[],t=new TelnetText(b=>replies.push([...b]));assert.deepEqual([...t.read(Buffer.from([65,255,251]))],[65]);assert.deepEqual([...t.read(Buffer.from([1,66,255,250,24,4,255]))],[66]);assert.deepEqual([...t.read(Buffer.from([240,67,255,255]))],[67,255]);t.read(Buffer.from([255,253,3]));assert.deepEqual(replies,[[255,254,1],[255,252,3]]);});
test('terminal authenticates privately, preserves fragmented output, sends lines and disconnects',async()=>{
const sockets=new Set(),received=[];const server=net.createServer(socket=>{sockets.add(socket);socket.on('close',()=>sockets.delete(socket));socket.write('User');setImmediate(()=>socket.write('name:'));let stage=0,buffer='';socket.on('data',chunk=>{buffer+=chunk;let pos;while((pos=buffer.indexOf('\r'))>=0){const line=buffer.slice(0,pos);buffer=buffer.slice(pos+1);received.push(line);if(stage++===0)socket.write('Password:');else if(stage===2){socket.write('secret-example\r\nWelcome to TEST\r');setImmediate(()=>socket.write('\nTEST:N0TEST>'));}else socket.write('Response: '+line+'\r\n');}});});server.listen(0,'127.0.0.1');await once(server,'listening');const t=new Terminal(async()=>({user:'fixture',password:'secret-example'}),{port:server.address().port});
const until=async fn=>{for(let i=0;i<100&&!fn();i++)await new Promise(r=>setTimeout(r,10));assert.ok(fn());};
try{await Promise.all([t.connect(),t.connect()]);await until(()=>t.state==='online');await until(()=>t.events.some(e=>e.text.includes('TEST:N0TEST>')));const text=t.snapshot().events.map(e=>e.text).join('');assert.equal(text,'Welcome to TEST\nTEST:N0TEST>');assert.ok(!text.includes('secret-example'));assert.ok(!text.includes('fixture'));assert.equal(received.filter(x=>x==='fixture').length,1);t.send('INFO');await until(()=>received.includes('INFO'));assert.throws(()=>t.send('INFO\rC EVIL'),/un solo comando/);assert.throws(()=>t.send('x'.repeat(1001)),/1000/);await until(()=>t.events.some(e=>e.text.includes('Response: INFO')));const last=t.seq;assert.equal(t.snapshot(last).events.length,0);t.output('\x1b[31mred\x1b[0m\r\0next');assert.equal(t.events.at(-1).text,'red\nnext');t.disconnect();assert.throws(()=>t.send('INFO'),/Collega/);assert.equal(t.state,'offline');}finally{t.close();for(const socket of sockets)socket.destroy();server.close();await once(server,'close');}
});
test('terminal transcript stays bounded and reconnect history does not grow indefinitely',()=>{const t=new Terminal(async()=>({}),{port:1});try{for(let i=0;i<800;i++)t.event('x'.repeat(500));assert.ok(t.events.length<=500);assert.ok(t.events.reduce((n,e)=>n+e.text.length,0)<=200000);}finally{t.close();}});