package gnet

import (
	"fmt"
	"testing"
	"time"

	"golib/v2/pkg/telnet-go/telnet"
)

func TestDialTelnet(t *testing.T) {
	conn, err := DialTelnet("192.168.111.126:23")
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		_ = conn.Close()
	}()
	data := make(Bytes, 0, 4096)
	go func() {
		for {
			b := make(Bytes, 4096)
			n, err := conn.Read(b)
			if err != nil {
				data = append(data, Bytes(err.Error())...)
				return
			}
			data = append(data, b[:n]...)
		}
	}()
	last := len(data)
	for {
		time.Sleep(1 * time.Second)
		if len(data) > last {
			a := string(data.TrimEnter().TrimNUL())
			fmt.Println(a)
			last = len(data)
		}
	}
}

func TestDialTelnetSH(t *testing.T) {
	var caller = telnet.StandardCaller
	if err := telnet.DialToAndCall("192.168.111.126:23", caller); err != nil {
		t.Error(err)
		return
	}
	time.Sleep(1 * time.Hour)
}
