1package tui
2
3import (
4 "reflect"
5 "strings"
6 "testing"
7 "time"
8
9 "github.com/patriceckhart/zot/packages/provider"
10)
11
12func timestampTestMessage(at time.Time) provider.Message {
13 return provider.Message{Role: provider.RoleAssistant, Time: at, Content: []provider.Content{provider.TextBlock{Text: "same text"}}}
14}
15
16func TestChatTimestampDateDisplayAndMargin(t *testing.T) {
17 start := time.Date(2026, 9, 22, 23, 58, 0, 0, time.Local)
18 v := View{Theme: Dark, ChatTimestamps: true, Messages: []provider.Message{
19 timestampTestMessage(start), timestampTestMessage(start.Add(time.Minute)), timestampTestMessage(start.Add(2 * time.Minute)),
20 }}
21 for _, mode := range []string{"", "day_start", "every_message", "invalid"} {
22 v.ChatTimestampDate = mode
23 want := []string{"2026-09-22 23:58", "23:59", "2026-09-23 00:00"}
24 if mode == "every_message" {
25 want[1] = "2026-09-22 23:59"
26 }
27 if got := v.messageTimestamps(); !reflect.DeepEqual(got, want) {
28 t.Fatalf("mode %q: got %v, want %v", mode, got, want)
29 }
30 for _, width := range []int{20, 40, 80} {
31 row := stripANSI(v.timestampRow(want[0], width))
32 box := stripANSI(toolBoxBottom(Dark, width))
33 if len(strings.TrimRight(row, " ")) != visibleWidth(strings.TrimRight(box, " ")) {
34 t.Fatalf("width %d: timestamp does not align with box right edge", width)
35 }
36 }
37 }
38}
39
40func TestChatTimestampIntervals(t *testing.T) {
41 start := time.Date(2026, 9, 22, 12, 0, 0, 0, time.Local)
42 five := 5
43 v := View{ChatTimestamps: true, ChatTimestampIntervalMinutes: &five}
44 for _, delta := range []time.Duration{0, 2 * time.Minute, 5*time.Minute - time.Second, 5 * time.Minute, 6 * time.Minute, 10 * time.Minute} {
45 v.Messages = append(v.Messages, timestampTestMessage(start.Add(delta)))
46 }
47 want := []string{"2026-09-22 12:00", "", "", "12:05", "", "12:10"}
48 if got := v.messageTimestamps(); !reflect.DeepEqual(got, want) {
49 t.Fatalf("labels = %v, want %v", got, want)
50 }
51 zero := 0
52 v.ChatTimestampIntervalMinutes = nil
53 for idx, label := range v.messageTimestamps() {
54 if label == "" {
55 t.Fatalf("every-message mode omitted message %d", idx)
56 }
57 }
58 negative := -1
59 if ChatTimestampInterval(nil) != 0 || ChatTimestampInterval(&negative) != 0 || ChatTimestampInterval(&zero) != 0 {
60 t.Fatal("incorrect interval defaults")
61 }
62}
63
64func TestChatTimestampMissingHiddenAndDayChange(t *testing.T) {
65 start := time.Date(2026, 9, 22, 23, 59, 0, 0, time.Local)
66 hidden := timestampTestMessage(start.Add(time.Minute))
67 hidden.Meta = map[string]string{"compaction": "true"}
68 toolOnly := provider.Message{Role: provider.RoleAssistant, Time: start.Add(time.Minute), Content: []provider.Content{provider.ToolCallBlock{ID: "call"}}}
69 v := View{ChatTimestamps: true, Messages: []provider.Message{
70 timestampTestMessage(time.Time{}), timestampTestMessage(start), hidden, toolOnly,
71 timestampTestMessage(start.Add(time.Minute)),
72 }}
73 want := []string{"", "2026-09-22 23:59", "", "", "2026-09-23 00:00"}
74 if got := v.messageTimestamps(); !reflect.DeepEqual(got, want) {
75 t.Fatalf("labels = %v, want %v", got, want)
76 }
77}
78
79func TestChatTimestampsPreserveRowsAndCaches(t *testing.T) {
80 start := time.Date(2026, 9, 22, 12, 0, 0, 0, time.Local)
81 for _, compact := range []bool{false, true} {
82 v := View{Theme: Dark, CompactMode: compact, Messages: []provider.Message{
83 timestampTestMessage(start), timestampTestMessage(start.Add(5 * time.Minute)),
84 }}
85 v.Messages[0].Role = provider.RoleUser
86 plain, anchors := v.BuildWithAnchors(40)
87 v.ChatTimestamps = true
88 stamped, stampedAnchors := v.BuildWithAnchors(40)
89 if len(plain) != len(stamped) || !reflect.DeepEqual(anchors, stampedAnchors) {
90 t.Fatal("timestamps changed transcript height or anchors")
91 }
92 for idx, line := range stamped {
93 if line != plain[idx] && (plain[idx] != "" || visibleWidth(line) != 40) {
94 t.Fatalf("timestamp overwrote content or was not right aligned: %q", line)
95 }
96 }
97 if !strings.Contains(strings.Join(stamped, "\n"), v.Theme.FG256(v.Theme.Muted, "12:05")) {
98 t.Fatal("missing muted timestamp")
99 }
100 v.Messages[1].Time = start.Add(6 * time.Minute)
101 if !strings.Contains(strings.Join(v.Build(40), "\n"), "12:06") {
102 t.Fatal("content cache reused the wrong timestamp")
103 }
104 v.ChatTimestamps = false
105 if got := v.Build(40); !reflect.DeepEqual(got, plain) {
106 t.Fatal("disabling timestamps did not restore original rows")
107 }
108 }
109}
110
111func TestChatTimestampTailAndNarrowWidths(t *testing.T) {
112 start := time.Date(2026, 9, 22, 12, 0, 0, 0, time.Local)
113 v := View{Theme: Dark, ChatTimestamps: true, Messages: []provider.Message{
114 timestampTestMessage(start), timestampTestMessage(start.Add(time.Minute)), timestampTestMessage(start.Add(5 * time.Minute)),
115 }}
116 all, anchors := v.BuildWithAnchors(40)
117 v.TailLimit = 2
118 if got := v.Build(40); !reflect.DeepEqual(got, all[anchors[1].Row:]) {
119 t.Fatal("tail rendering changed timestamp selection")
120 }
121 for _, width := range []int{0, 1, 4, 5, 15, 16, 40} {
122 for _, label := range []string{"12:00", "2026-09-22 12:00"} {
123 line := v.timestampRow(label, width)
124 if visibleWidth(line) > width || (width-2*toolBoxOuterMargin < len(label) && line != "") {
125 t.Fatalf("width %d: label overflow: %q", width, line)
126 }
127 }
128 }
129}
130
131func TestChatTimestampsToolLayoutsAndClockRegression(t *testing.T) {
132 start := time.Date(2026, 9, 22, 12, 0, 0, 0, time.Local)
133 zero := 0
134 for _, layout := range []string{"box", "flat", "compact"} {
135 v := View{Theme: Dark, FlatTools: layout == "flat", CompactMode: layout == "compact", CollapseToolCall: true,
136 ChatTimestampIntervalMinutes: &zero, Messages: []provider.Message{
137 timestampTestMessage(start),
138 {Role: provider.RoleTool, Time: start.Add(-time.Minute), Content: []provider.Content{
139 provider.ToolResultBlock{CallID: "call", Content: []provider.Content{provider.TextBlock{Text: "tool output"}}},
140 }},
141 },
142 }
143 plain := v.Build(80)
144 v.ChatTimestamps = true
145 stamped := v.Build(80)
146 if len(plain) != len(stamped) || !strings.Contains(strings.Join(stamped, "\n"), "11:59") {
147 t.Fatalf("%s: tool timestamp missing or added rows", layout)
148 }
149 five := 5
150 v.ChatTimestampIntervalMinutes = &five
151 if v.messageTimestamps()[1] != "" {
152 t.Fatal("clock regression bypassed positive minimum interval")
153 }
154 }
155}
156
157func TestChatTimestampsWaitForCompletedMessages(t *testing.T) {
158 v := View{Theme: Dark, StreamingActive: true, Streaming: "streaming text"}
159 before := v.Build(80)
160 liveBefore := v.BuildLive(80)
161 v.ChatTimestamps = true
162 if !reflect.DeepEqual(before, v.Build(80)) || !reflect.DeepEqual(liveBefore, v.BuildLive(80)) {
163 t.Fatal("timestamps modified in-progress streaming output")
164 }
165 v.StreamingActive = false
166 v.Streaming = ""
167 v.Messages = []provider.Message{timestampTestMessage(time.Date(2026, 9, 22, 12, 0, 0, 0, time.Local))}
168 if !strings.Contains(strings.Join(v.Build(80), "\n"), "2026-09-22 12:00") {
169 t.Fatal("completed message missing timestamp")
170 }
171}