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();}}); test('terminal stays connected without browser polling or user input and still closes explicitly',context=>{ context.mock.timers.enable({apis:['Date','setInterval']}); const t=new Terminal(async()=>({}),{port:1});let destroyed=false; t.socket={destroyed:false,destroy(){destroyed=true;this.destroyed=true;}};t.state='online'; try{ context.mock.timers.tick(20*60*1000); assert.equal(t.state,'online');assert.equal(destroyed,false);assert.equal(t.events.length,0); t.disconnect();assert.equal(t.state,'offline');assert.equal(destroyed,true); }finally{t.close();context.mock.timers.reset();} });