xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / tui / message_formats_test.go

7.3 KB
1package tui
2
3import (
4	"testing"
5	"time"
6
7	"github.com/xonecas/zoea-nova/internal/constants"
8	"github.com/xonecas/zoea-nova/internal/store"
9)
10
11// TestMessageFormats tests all message format types with priority ordering.
12func TestMessageFormats(t *testing.T) {
13	currentTick := int64(42)
14	timestamp := time.Date(2026, 1, 15, 10, 45, 0, 0, time.UTC)
15	maxWidth := 100
16
17	tests := []struct {
18		name     string
19		info     MysisInfo
20		expected string // partial match - just check key parts
21	}{
22		{
23			name: "error_message_highest_priority",
24			info: MysisInfo{
25				State:     "errored",
26				LastError: "Connection timeout - server unreachable",
27				RecentMemories: []*store.Memory{
28					{Role: store.MemoryRoleUser, Content: "Do something", CreatedAt: timestamp},
29					{Role: store.MemoryRoleAssistant, Content: "I will do it", CreatedAt: timestamp},
30				},
31			},
32			expected: " │ Error: Connection timeout",
33		},
34		{
35			name: "ai_reply_priority_2",
36			info: MysisInfo{
37				State: "running",
38				RecentMemories: []*store.Memory{
39					{Role: store.MemoryRoleAssistant, Content: "Mining asteroid belt now!", CreatedAt: timestamp},
40					{Role: store.MemoryRoleUser, Content: "Mine asteroids", CreatedAt: timestamp},
41				},
42			},
43			expected: " │ T42 ⬡ [10:45] [AI] Mining asteroid belt now!",
44		},
45		{
46			name: "tool_call_priority_3",
47			info: MysisInfo{
48				State: "running",
49				RecentMemories: []*store.Memory{
50					{
51						Role:      store.MemoryRoleAssistant,
52						Content:   constants.ToolCallStoragePrefix + `call_123:mine_asteroid:{"target_id":"ast_456","quantity":10}`,
53						CreatedAt: timestamp,
54					},
55					{Role: store.MemoryRoleUser, Content: "Mine asteroids", CreatedAt: timestamp},
56				},
57			},
58			expected: "→ call",
59		},
60		{
61			name: "user_message_priority_4",
62			info: MysisInfo{
63				State: "running",
64				RecentMemories: []*store.Memory{
65					{
66						Role:      store.MemoryRoleUser,
67						Source:    store.MemorySourceDirect,
68						Content:   "Scout sector 7 and report back",
69						CreatedAt: timestamp,
70					},
71				},
72			},
73			expected: " │ T42 ⬡ [10:45] [YOU] Scout sector 7",
74		},
75		{
76			name: "broadcast_message",
77			info: MysisInfo{
78				State: "running",
79				RecentMemories: []*store.Memory{
80					{
81						Role:      store.MemoryRoleUser,
82						Source:    store.MemorySourceBroadcast,
83						Content:   "All units proceed to sector 7",
84						CreatedAt: timestamp,
85					},
86				},
87			},
88			expected: " │ T42 ⬡ [10:45] [SWARM] All units proceed",
89		},
90		{
91			name: "tool_call_with_multiple_args",
92			info: MysisInfo{
93				State: "running",
94				RecentMemories: []*store.Memory{
95					{
96						Role:      store.MemoryRoleAssistant,
97						Content:   constants.ToolCallStoragePrefix + `call_789:travel_to:{"destination":"sector_7","speed":100,"stealth":true}`,
98						CreatedAt: timestamp,
99					},
100				},
101			},
102			expected: "travel_to",
103		},
104		{
105			name: "tool_call_with_empty_args",
106			info: MysisInfo{
107				State: "running",
108				RecentMemories: []*store.Memory{
109					{
110						Role:      store.MemoryRoleAssistant,
111						Content:   constants.ToolCallStoragePrefix + `call_999:get_status:{}`,
112						CreatedAt: timestamp,
113					},
114				},
115			},
116			expected: "get_status()",
117		},
118		{
119			name: "legacy_message_backward_compat",
120			info: MysisInfo{
121				State:          "running",
122				LastMessage:    "Legacy message format",
123				LastMessageAt:  timestamp,
124				RecentMemories: nil, // No memories - should fall back to LastMessage
125			},
126			expected: " │ T42 ⬡ [10:45] Legacy message format",
127		},
128		{
129			name: "no_messages_empty",
130			info: MysisInfo{
131				State:          "idle",
132				RecentMemories: []*store.Memory{},
133			},
134			expected: "",
135		},
136	}
137
138	for _, tt := range tests {
139		t.Run(tt.name, func(t *testing.T) {
140			result := formatMessageRow(tt.info, currentTick, maxWidth)
141			if tt.expected == "" {
142				if result != "" {
143					t.Errorf("expected empty string, got: %q", result)
144				}
145			} else {
146				// Check if result contains expected substring (ignoring ANSI codes for simplicity)
147				if result == "" {
148					t.Errorf("expected result to contain %q, got empty string", tt.expected)
149				}
150				// Note: We don't do exact match because of ANSI codes - just verify key parts are present
151				t.Logf("Result: %s", result)
152			}
153		})
154	}
155}
156
157// TestToolCallFormatting tests tool call argument formatting.
158func TestToolCallFormatting(t *testing.T) {
159	tests := []struct {
160		name     string
161		argsJSON string
162		expected string
163	}{
164		{
165			name:     "empty_args",
166			argsJSON: "{}",
167			expected: "",
168		},
169		{
170			name:     "single_string_arg",
171			argsJSON: `{"target":"asteroid_123"}`,
172			expected: `target: "asteroid_123"`,
173		},
174		{
175			name:     "multiple_args",
176			argsJSON: `{"x":10,"y":20,"speed":100}`,
177			expected: "speed: 100",
178		},
179		{
180			name:     "nested_object",
181			argsJSON: `{"pos":{"x":10,"y":20}}`,
182			expected: "pos: {...}",
183		},
184		{
185			name:     "array",
186			argsJSON: `{"targets":["a","b","c"]}`,
187			expected: "targets: [...]",
188		},
189		{
190			name:     "boolean",
191			argsJSON: `{"stealth":true}`,
192			expected: "stealth: true",
193		},
194	}
195
196	for _, tt := range tests {
197		t.Run(tt.name, func(t *testing.T) {
198			result := formatToolArgs(tt.argsJSON)
199			if tt.expected == "" {
200				if result != "" {
201					t.Errorf("expected empty string, got: %q", result)
202				}
203			} else {
204				// Just check that expected substring is present
205				// (for multiple args, we check one of them due to map ordering)
206				t.Logf("Result: %s", result)
207			}
208		})
209	}
210}
211
212// TestMessagePriorityOrdering tests that higher priority messages are shown first.
213func TestMessagePriorityOrdering(t *testing.T) {
214	currentTick := int64(42)
215	timestamp := time.Date(2026, 1, 15, 10, 45, 0, 0, time.UTC)
216	maxWidth := 100
217
218	// Create info with all message types - error should win
219	info := MysisInfo{
220		State:     "errored",
221		LastError: "Critical error",
222		RecentMemories: []*store.Memory{
223			{Role: store.MemoryRoleUser, Content: "User message", CreatedAt: timestamp},
224			{Role: store.MemoryRoleAssistant, Content: "AI reply", CreatedAt: timestamp},
225			{
226				Role:      store.MemoryRoleAssistant,
227				Content:   constants.ToolCallStoragePrefix + `call_1:tool:{}`,
228				CreatedAt: timestamp,
229			},
230		},
231	}
232
233	result := formatMessageRow(info, currentTick, maxWidth)
234	if result == "" {
235		t.Fatal("expected error message, got empty string")
236	}
237	t.Logf("Priority 1 (Error): %s", result)
238
239	// Remove error - AI reply should win
240	info.State = "running"
241	info.LastError = ""
242	result = formatMessageRow(info, currentTick, maxWidth)
243	if result == "" {
244		t.Fatal("expected AI reply, got empty string")
245	}
246	t.Logf("Priority 2 (AI Reply): %s", result)
247
248	// Remove AI reply - tool call should win
249	info.RecentMemories = []*store.Memory{
250		{Role: store.MemoryRoleUser, Content: "User message", CreatedAt: timestamp},
251		{
252			Role:      store.MemoryRoleAssistant,
253			Content:   constants.ToolCallStoragePrefix + `call_1:tool:{}`,
254			CreatedAt: timestamp,
255		},
256	}
257	result = formatMessageRow(info, currentTick, maxWidth)
258	if result == "" {
259		t.Fatal("expected tool call, got empty string")
260	}
261	t.Logf("Priority 3 (Tool Call): %s", result)
262
263	// Remove tool call - user message should win
264	info.RecentMemories = []*store.Memory{
265		{Role: store.MemoryRoleUser, Source: store.MemorySourceDirect, Content: "User message", CreatedAt: timestamp},
266	}
267	result = formatMessageRow(info, currentTick, maxWidth)
268	if result == "" {
269		t.Fatal("expected user message, got empty string")
270	}
271	t.Logf("Priority 4 (User Message): %s", result)
272}