internal/control/term_test.go
81 lines · 2049 bytes
1package control
2
3import (
4 "testing"
5 "time"
6)
7
8func TestParseTerm(t *testing.T) {
9 cases := map[string]Term{
10 "120": {Cols: 120},
11 "120,color": {Cols: 120, Color: true},
12 "40": {Cols: 40},
13 "39": {},
14 "": {},
15 "abc": {},
16 "80,blink": {},
17 "80,": {},
18 "5000": {},
19 }
20 for in, want := range cases {
21 if got := ParseTerm(in); got != want {
22 t.Errorf("ParseTerm(%q) = %+v, want %+v", in, got, want)
23 }
24 }
25}
26
27func TestCells(t *testing.T) {
28 cases := map[string]int{
29 "abc": 3,
30 "日本": 4,
31 "é": 1,
32 "é": 1,
33 "\x1b[32mopen\x1b[0m": 4,
34 "": 0,
35 }
36 for in, want := range cases {
37 if got := cells(in); got != want {
38 t.Errorf("cells(%q) = %d, want %d", in, got, want)
39 }
40 }
41}
42
43func TestClip(t *testing.T) {
44 if got := clip("Dependency updates available", 14); got != "Dependency up…" {
45 t.Errorf("clip = %q", got)
46 }
47 if got := clip("short", 14); got != "short" {
48 t.Errorf("clip = %q", got)
49 }
50 if got := clip("日本語のタイトル", 7); got != "日本語…" {
51 t.Errorf("clip wide = %q", got)
52 }
53}
54
55func TestStampAndRelAge(t *testing.T) {
56 if got := stamp("2026-09-23T23:26:00.570Z"); got != "2026-09-23T23:26:00Z" {
57 t.Errorf("stamp = %q", got)
58 }
59 if got := stamp("2026-09-23 23:26:00"); got != "2026-09-23T23:26:00Z" {
60 t.Errorf("stamp sqlite = %q", got)
61 }
62 if got := stamp("garbage"); got != "garbage" {
63 t.Errorf("stamp garbage = %q", got)
64 }
65 now := time.Date(2026, 9, 23, 12, 0, 0, 0, time.UTC)
66 cases := map[string]string{
67 "2026-09-23T11:59:30Z": "just now",
68 "2026-09-23T11:55:00Z": "5m ago",
69 "2026-09-23T10:00:00Z": "2h ago",
70 "2026-09-20T12:00:00Z": "3d ago",
71 "2026-09-02T12:00:00Z": "3w ago",
72 "2026-06-01T12:00:00Z": "2026-06-01",
73 "2026-09-24T12:00:00Z": "just now",
74 "not a time": "not a time",
75 }
76 for in, want := range cases {
77 if got := relAge(in, now); got != want {
78 t.Errorf("relAge(%q) = %q, want %q", in, got, want)
79 }
80 }
81}