package utils

import (
	"bytes"
	"crypto/sha256"
	"encoding/base64"
	"os/exec"
	"testing"
	"time"
)

func TestFindCmd(t *testing.T) {
	target := "ypcat"
	p := FindCmd(target)
	if p == nil {
		t.Logf("not find %s", target)
		return
	}
	t.Logf("find : %s", *p)
}

func TestKeySha256(t *testing.T) {
	// AAAAC3NzaC1lZDI1NTE5AAAAIJ//DeJIwpNQ3bboddfladiUs/xMbsbRikgvJ/m+30Uj
	// 0o84iZ8MJUCRzTIipR8eLzX2g+Rx96MKMVq/RakG/GA

	b, err := base64.StdEncoding.DecodeString("AAAAC3NzaC1lZDI1NTE5AAAAIJ//DeJIwpNQ3bboddfladiUs/xMbsbRikgvJ/m+30Uj")
	if err != nil {
		t.Error(err)
	}
	sh := sha256.Sum256(b)
	t.Log(base64.StdEncoding.EncodeToString(sh[:]))
}

func TestGetSysUser(t *testing.T) {
	u, err := GetSysUser()
	if err != nil {
		t.Error(err)
	}
	for k, v := range u {
		t.Logf("%d: %s, home: %s, shell: %s", k, v.Name, v.Home, v.Shell)
		if len(v.SSHkeyInfo) > 0 {
			for _, s := range v.SSHkeyInfo {
				t.Logf("    %s: %s", s.UserInfo, s.Type)
			}
		}
	}
}

func TestGetNisUser(t *testing.T) {
	u, err := GetNisUser()
	if err != nil {
		t.Error(err)
	}
	for k, v := range u {
		t.Logf("%d: %s, home: %s, shell: %s", k, v.Name, v.Home, v.Shell)
		if len(v.SSHkeyInfo) > 0 {
			for _, s := range v.SSHkeyInfo {
				t.Logf("    %s: %s", s.UserInfo, s.Type)
			}
		}
	}
}

func TestReOnLineUser(t *testing.T) {
	target := "gaojm    pts/2        2025-12-22 14:13 00:08       12010 (10.16.4.4)"
	m := ReOnLineUser.MatchString(target)
	if m {
		f := ReOnLineUser.FindStringSubmatch(target)
		for _, v := range f {
			t.Log(v)
		}
	} else {
		t.Error("not match")
	}
}

func TestReOnLineUserX(t *testing.T) {
	target := "ops      :1           2025-12-22 19:45   ?        430638 (:1)"
	m := ReOnLineUserX.MatchString(target)
	if m {
		f := ReOnLineUserX.FindStringSubmatch(target)
		for _, v := range f {
			t.Log(v)
		}
	} else {
		t.Error("not match")
	}
}

func TestReOnLineUserTTY(t *testing.T) {
	target := "root     tty1         2025-12-22 19:38   .          1603"
	m := ReOnLineUserTTY.MatchString(target)
	if m {
		f := ReOnLineUserTTY.FindStringSubmatch(target)
		for _, v := range f {
			t.Log(v)
		}
	} else {
		t.Error("not match")
	}
}

func TestGetOnlineUser(t *testing.T) {
	u, err := GetOnlineUser()
	if err != nil {
		t.Error(err)
	}
	for _, v := range u {
		t.Logf("%+v\n", v)
	}
}

func TestGetOneSysUser(t *testing.T) {
	u, err := GetOneSysUser("root")
	if err != nil {
		t.Error(err)
	}
	t.Logf("%d: %s, home: %s, shell: %s", u.Uid, u.Name, u.Home, u.Shell)
	if len(u.SSHkeyInfo) > 0 {
		for _, s := range u.SSHkeyInfo {
			t.Logf("    %s: %s", s.UserInfo, s.Type)
		}
	}
}

func TestGetOneNisUser(t *testing.T) {
	u, err := GetOneNisUser("liming6")
	if err != nil {
		t.Error(err)
	}
	t.Logf("%d: %s, home: %s, shell: %s", u.Uid, u.Name, u.Home, u.Shell)
	if len(u.SSHkeyInfo) > 0 {
		for _, s := range u.SSHkeyInfo {
			t.Logf("    %s: %s", s.UserInfo, s.Type)
		}
	}
}

func TestGetPidChild(t *testing.T) {
	start := time.Now()
	pids, err := GetPidChild(1)
	d := time.Since(start)
	t.Logf("%d ms", d.Milliseconds())
	if err != nil {
		t.Error(err)
	}
	t.Logf("%v", pids)
	t.Logf("num: %d", len(pids))
}

func TestGetPidChild2(t *testing.T) {
	start := time.Now()
	pids, err := GetPidChild2(1)
	d := time.Since(start)
	t.Logf("%d ms", d.Milliseconds())
	if err != nil {
		t.Error(err)
	}
	t.Logf("%v", pids)
	t.Logf("num: %d", len(pids))
}

func TestRunCmd(t *testing.T) {
	cmd1 := exec.Command("echo", "hello world")
	cmd2 := exec.Command("grep", "-o", "h.*o")
	so, err := cmd1.StdoutPipe()
	if err != nil {
		t.Fatal(err)
	}
	cmd2.Stdin = so
	var out bytes.Buffer
	cmd2.Stdout = &out
	_ = cmd1.Start()
	_ = cmd2.Start()
	cmd1.Wait()
	cmd2.Wait()
	t.Logf("%s", out.String())
}
