summaryrefslogtreecommitdiff
path: root/internal/tmux/status_more_test.go
blob: 39cbe45151aaa5a91a214528f3f7f116ff619c37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package tmux

import (
	"os"
	"testing"
	"time"
)

func TestFormatLLMStatsStatus_Basic(t *testing.T) {
	s := FormatLLMStatsStatus("gpt-4.1", 5, 0.8, 1234, 5678)
	if s == "" || !containsAll(s, []string{"LLM:gpt-4.1", "5r", "0.8rpm"}) {
		t.Fatalf("unexpected status: %q", s)
	}
}

func TestFormatLLMStatsStatusColored_Basic(t *testing.T) {
	s := FormatLLMStatsStatusColored("openai", "gpt-4.1", 2, 1.2, 100, 200)
	if s == "" || !containsAll(s, []string{"LLM:openai:gpt-4.1", "rpm", "2r"}) {
		t.Fatalf("colored status missing parts: %q", s)
	}
}

func TestFormatGlobalStatusColored_NarrowAndMaxLen(t *testing.T) {
	// Narrow mode should elide the tail
	_ = os.Setenv("HEXAI_TMUX_STATUS_NARROW", "1")
	defer func() { _ = os.Unsetenv("HEXAI_TMUX_STATUS_NARROW") }()
	s := FormatGlobalStatusColored(10, 3.3, 1000, 2000, "prov", "model", 1.1, 4, 30*time.Minute)
	if containsAll(s, []string{"|", "prov:model"}) {
		t.Fatalf("narrow mode should not include tail: %q", s)
	}
	// Max length should also drop the tail when it would overflow
	_ = os.Unsetenv("HEXAI_TMUX_STATUS_NARROW")
	_ = os.Setenv("HEXAI_TMUX_STATUS_MAXLEN", "10")
	defer func() { _ = os.Unsetenv("HEXAI_TMUX_STATUS_MAXLEN") }()
	s2 := FormatGlobalStatusColored(10, 3.3, 1000, 2000, "prov", "model", 1.1, 4, 30*time.Minute)
	if containsAll(s2, []string{"|", "prov:model"}) {
		t.Fatalf("maxlen should drop tail when overflowing: %q", s2)
	}
}

func containsAll(s string, parts []string) bool {
	for _, p := range parts {
		if !contains(s, p) {
			return false
		}
	}
	return true
}

func contains(s, sub string) bool {
	return len(s) >= len(sub) && (len(sub) == 0 || (len(s) > 0 && indexOf(s, sub) >= 0))
}

func indexOf(s, sub string) int {
	// tiny wrapper to avoid importing strings
	for i := 0; i+len(sub) <= len(s); i++ {
		if s[i:i+len(sub)] == sub {
			return i
		}
	}
	return -1
}