tui_test.go 2.88 KB
Newer Older
liming6's avatar
liming6 committed
1
2
3
4
package tui

import (
	"fmt"
liming6's avatar
liming6 committed
5
	"get-container/cmd/dcutop/tchart"
liming6's avatar
liming6 committed
6
	"testing"
liming6's avatar
liming6 committed
7
	"time"
liming6's avatar
liming6 committed
8
9

	"github.com/charmbracelet/lipgloss"
liming6's avatar
liming6 committed
10
11
	"github.com/emirpasic/gods/v2/maps/linkedhashmap"
	"github.com/emirpasic/gods/v2/trees/binaryheap"
liming6's avatar
liming6 committed
12
13
)

liming6's avatar
liming6 committed
14
15
16
17
18
19
const S = `├───────────────────────────────┼──────────────────────┼──────────────────────┼
│ 2    BW200,            manual │ 0000:5e:00.0     Off │ Off               On │ DCU: ░░░░░░░░░░░░░░░░░░░░░░░░░   0% │`

func TestLine(t *testing.T) {
	t.Logf("%d", lipgloss.Width(S))
}
liming6's avatar
liming6 committed
20

liming6's avatar
liming6 committed
21
22
23
24
25
26
func TestHeader(t *testing.T) {
	m := ModelHeader{}
	cmd := m.Init()
	m.Update(cmd)
	fmt.Println(m.View())
}
liming6's avatar
liming6 committed
27
28
29
30
31
32
33
34
35
36

func TestAis(t *testing.T) {
	for i := 10; i < 180; i++ {
		str := genXAxis(i)
		if lipgloss.Width(str) != i {
			t.Error("error length")
		}
		fmt.Println(str)
	}
}
liming6's avatar
liming6 committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

func TestFormatStr(t *testing.T) {
	str := lipgloss.NewStyle().Foreground(lipgloss.Color("#2b95ffff")).SetString("hello world!").String()
	t.Logf("|%s|", FormatStr(str, 5, lipgloss.Left))
	t.Logf("|%s|", FormatStr(str, 5, lipgloss.Right))
	t.Logf("|%s|", FormatStr(str, 20, lipgloss.Left))
	t.Logf("|%s|", FormatStr(str, 20, lipgloss.Right))
}

func TestModel(t *testing.T) {
	m := NewModelMain(200, 100)
	m.Init()
	m.DCUInfo.Update(m.modelMsg)
	str := m.View()
	t.Log(str)
}
liming6's avatar
liming6 committed
53
54

func TestMyTimeChart(t *testing.T) {
liming6's avatar
liming6 committed
55
	chart := New(100, 5, 0.0, 100.0, map[string]lipgloss.Color{"default": lipgloss.Color("#ff2222ff"), "other": lipgloss.Color("#0037ffff")})
liming6's avatar
liming6 committed
56
57
	chart.Init()
	s := chart.View()
liming6's avatar
liming6 committed
58
	t.Logf("\n%s", s)
liming6's avatar
liming6 committed
59
60
61
	time.Sleep(time.Second)
	now := time.Now()
	points := make(map[string][]tchart.TimePoint)
liming6's avatar
liming6 committed
62
63
	points["default"] = []tchart.TimePoint{{Time: now, Value: 94.0}}
	points["other"] = []tchart.TimePoint{{Time: now, Value: 94.0}}
liming6's avatar
liming6 committed
64
	chart.Update(MyTimeChartMsg{Points: points})
liming6's avatar
liming6 committed
65
66
67
68
69
70
71
72
	time.Sleep(time.Second)
	now = time.Now()
	points = make(map[string][]tchart.TimePoint)
	points["default"] = []tchart.TimePoint{{Time: now, Value: 0.0}}
	points["other"] = []tchart.TimePoint{{Time: now, Value: 0.0}}
	chart.Update(MyTimeChartMsg{Points: points})
	t.Logf("\n%s", chart.View())
	t.Logf("ok")
liming6's avatar
liming6 committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
}

func TestBinaryHeap(t *testing.T) {

	heap := binaryheap.NewWith(func(a, b time.Time) int {
		if a.After(b) {
			return 1
		}
		if a.Before(b) {
			return -1
		}
		return 0
	})
	now := time.Now()
	for i := range 5 {
		heap.Push(now.Add(time.Duration(i) * time.Second))
	}
	for {
		tt, ok := heap.Pop()
		if ok {
			t.Logf("%s", tt)
		} else {
			break
		}
	}
}

func TestLinkedHashMap(t *testing.T) {
	m := linkedhashmap.New[time.Time, int]()
	now := time.Now()
	for i := range 5 {
		m.Put(now.Add(time.Duration(i)*time.Second), 5-i)
	}
	mi := m.Iterator()
	for {
		if mi.Next() {
			t.Logf("%v: %d", mi.Key(), mi.Value())
		} else {
			break
		}
	}
	t.Log(m.Size())
}