xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / tui / unicode_test.go

10.7 KB
1package tui
2
3import (
4	"fmt"
5	"testing"
6	"time"
7
8	"github.com/charmbracelet/bubbles/spinner"
9	"github.com/charmbracelet/lipgloss"
10	"github.com/mattn/go-runewidth"
11)
12
13// TestUnicodeCharacterInventory documents all Unicode characters used in the TUI.
14// This test serves as a comprehensive reference for all decorative characters.
15func TestUnicodeCharacterInventory(t *testing.T) {
16	inventory := map[string]struct {
17		char      string
18		codepoint string
19		name      string
20		usage     []string
21	}{
22		"black_medium_diamond": {
23			char:      "⬥",
24			codepoint: "U+2B25",
25			name:      "BLACK MEDIUM DIAMOND",
26			usage: []string{
27				"Header corners",
28				"Status bar LLM indicator",
29				"Focus view header decoration",
30				"Message mode prompt",
31				"Spinner frame (running/loading)",
32			},
33		},
34		"black_medium_lozenge": {
35			char:      "⬧",
36			codepoint: "U+2B27",
37			name:      "BLACK MEDIUM LOZENGE",
38			usage: []string{
39				"Section title borders (SWARM BROADCAST, MYSIS SWARM)",
40				"Conversation log header",
41				"Broadcast mode prompt",
42			},
43		},
44		"white_hexagon": {
45			char:      "⬡",
46			codepoint: "U+2B21",
47			name:      "WHITE HEXAGON",
48			usage: []string{
49				"Header title decoration",
50				"Focus view mysis name decoration",
51				"New mysis mode prompt",
52				"Spinner frame 0, 2 (running/loading)",
53			},
54		},
55		"black_hexagon": {
56			char:      "⬢",
57			codepoint: "U+2B22",
58			name:      "BLACK HEXAGON",
59			usage: []string{
60				"Spinner frame 1, 3 (running/loading)",
61			},
62		},
63		"white_medium_diamond": {
64			char:      "⬦",
65			codepoint: "U+2B26",
66			name:      "WHITE MEDIUM DIAMOND",
67			usage: []string{
68				"Spinner frame 4, 6 (running/loading)",
69				"Idle status bar indicator (no activity)",
70			},
71		},
72		"white_bullet": {
73			char:      "◦",
74			codepoint: "U+25E6",
75			name:      "WHITE BULLET",
76			usage: []string{
77				"Idle state indicator in mysis list",
78			},
79		},
80		"dotted_circle": {
81			char:      "◌",
82			codepoint: "U+25CC",
83			name:      "DOTTED CIRCLE",
84			usage: []string{
85				"Stopped state indicator in mysis list",
86			},
87		},
88		"heavy_multiplication_x": {
89			char:      "✖",
90			codepoint: "U+2716",
91			name:      "HEAVY MULTIPLICATION X",
92			usage: []string{
93				"Errored state indicator in mysis list",
94			},
95		},
96		"gear": {
97			char:      "⚙",
98			codepoint: "U+2699",
99			name:      "GEAR",
100			usage: []string{
101				"Config provider mode prompt",
102			},
103		},
104	}
105
106	// Verify all characters are documented
107	for name, info := range inventory {
108		t.Run(name, func(t *testing.T) {
109			if info.char == "" {
110				t.Errorf("Character %s has empty char field", name)
111			}
112			if info.codepoint == "" {
113				t.Errorf("Character %s has empty codepoint field", name)
114			}
115			if info.name == "" {
116				t.Errorf("Character %s has empty name field", name)
117			}
118			if len(info.usage) == 0 {
119				t.Errorf("Character %s has no usage documented", name)
120			}
121
122			// Output character for visual inspection
123			t.Logf("%s (%s): %s - %s", name, info.char, info.codepoint, info.name)
124			for _, usage := range info.usage {
125				t.Logf("  - %s", usage)
126			}
127		})
128	}
129}
130
131// TestSpinnerFrameRendering verifies all 8 spinner frames render correctly.
132func TestSpinnerFrameRendering(t *testing.T) {
133	frames := []struct {
134		index     int
135		char      string
136		codepoint string
137	}{
138		{0, "⬡", "U+2B21"},
139		{1, "⬢", "U+2B22"},
140		{2, "⬡", "U+2B21"},
141		{3, "⬢", "U+2B22"},
142		{4, "⬦", "U+2B26"},
143		{5, "⬥", "U+2B25"},
144		{6, "⬦", "U+2B26"},
145		{7, "⬥", "U+2B25"},
146	}
147
148	// Create spinner with actual frames
149	sp := spinner.New()
150	sp.Spinner = spinner.Spinner{
151		Frames: []string{"⬡", "⬢", "⬡", "⬢", "⬦", "⬥", "⬦", "⬥"},
152		FPS:    time.Second / 8, // 125ms per frame
153	}
154
155	for _, frame := range frames {
156		t.Run(fmt.Sprintf("frame_%d", frame.index), func(t *testing.T) {
157			// Verify frame matches expected character
158			actualFrame := sp.Spinner.Frames[frame.index]
159			if actualFrame != frame.char {
160				t.Errorf("Frame %d: expected %q (%s), got %q",
161					frame.index, frame.char, frame.codepoint, actualFrame)
162			}
163
164			// Verify character width is 1
165			width := lipgloss.Width(frame.char)
166			if width != 1 {
167				t.Errorf("Frame %d character %q has width %d, expected 1",
168					frame.index, frame.char, width)
169			}
170
171			t.Logf("Frame %d: %s (%s) - width: %d", frame.index, frame.char, frame.codepoint, width)
172		})
173	}
174
175	// Verify FPS
176	expectedFPS := time.Second / 8
177	if sp.Spinner.FPS != expectedFPS {
178		t.Errorf("Spinner FPS: expected %v, got %v", expectedFPS, sp.Spinner.FPS)
179	}
180	t.Logf("Spinner FPS: %v (125ms per frame)", sp.Spinner.FPS)
181}
182
183// TestUnicodeWidthConsistency verifies all characters have consistent width.
184func TestUnicodeWidthConsistency(t *testing.T) {
185	chars := map[string]string{
186		"black_medium_diamond": "⬥",
187		"black_medium_lozenge": "⬧",
188		"white_hexagon":        "⬡",
189		"black_hexagon":        "⬢",
190		"white_medium_diamond": "⬦",
191		"white_bullet":         "◦",
192		"dotted_circle":        "◌",
193		"heavy_x":              "✖",
194		"gear":                 "⚙",
195	}
196
197	for name, char := range chars {
198		t.Run(name, func(t *testing.T) {
199			// Test with lipgloss.Width
200			width := lipgloss.Width(char)
201			if width != 1 {
202				t.Errorf("Character %q (%s) has width %d via lipgloss, expected 1",
203					char, name, width)
204			}
205
206			// Test with runewidth (East Asian Width safety)
207			r := []rune(char)[0]
208
209			// Test in narrow mode
210			runewidth.DefaultCondition.EastAsianWidth = false
211			narrowWidth := runewidth.RuneWidth(r)
212
213			// Test in wide mode
214			runewidth.DefaultCondition.EastAsianWidth = true
215			wideWidth := runewidth.RuneWidth(r)
216
217			if narrowWidth != wideWidth {
218				t.Errorf("Character %q (%s) is ambiguous width: narrow=%d, wide=%d",
219					char, name, narrowWidth, wideWidth)
220			}
221
222			if narrowWidth != 1 {
223				t.Errorf("Character %q (%s) has width %d via runewidth, expected 1",
224					char, name, narrowWidth)
225			}
226
227			t.Logf("%s: %q - lipgloss width: %d, runewidth narrow: %d, runewidth wide: %d",
228				name, char, width, narrowWidth, wideWidth)
229		})
230	}
231}
232
233// TestInputModePromptIndicators verifies all input mode prompt indicators.
234func TestInputModePromptIndicators(t *testing.T) {
235	indicators := map[InputMode]struct {
236		char      string
237		codepoint string
238		name      string
239	}{
240		InputModeBroadcast: {
241			char:      "⬧",
242			codepoint: "U+2B27",
243			name:      "BLACK MEDIUM LOZENGE",
244		},
245		InputModeMessage: {
246			char:      "⬥",
247			codepoint: "U+2B25",
248			name:      "BLACK MEDIUM DIAMOND",
249		},
250		InputModeNewMysis: {
251			char:      "⬡",
252			codepoint: "U+2B21",
253			name:      "WHITE HEXAGON",
254		},
255		InputModeConfigProvider: {
256			char:      "⚙",
257			codepoint: "U+2699",
258			name:      "GEAR",
259		},
260		// Note: InputModeConfigModel uses "cfg" text, not a Unicode character
261	}
262
263	modeNames := map[InputMode]string{
264		InputModeBroadcast:      "broadcast",
265		InputModeMessage:        "message",
266		InputModeNewMysis:       "new_mysis",
267		InputModeConfigProvider: "config_provider",
268	}
269
270	for mode, info := range indicators {
271		modeName := modeNames[mode]
272		t.Run(modeName, func(t *testing.T) {
273			// Create input model
274			m := NewInputModel()
275			m.SetMode(mode, "")
276
277			// Get prompt (strip styles)
278			prompt := m.textInput.Prompt
279			if prompt == "" {
280				t.Errorf("Mode %s has empty prompt", modeName)
281			}
282
283			// Verify character is in prompt
284			// (Prompt is styled, so we just check for presence)
285			if len(prompt) < len(info.char) {
286				t.Errorf("Mode %s prompt too short: %q", modeName, prompt)
287			}
288
289			t.Logf("Mode %s: indicator %s (%s) - %s",
290				modeName, info.char, info.codepoint, info.name)
291		})
292	}
293}
294
295// TestStateIndicatorCharacters verifies all state indicator characters.
296func TestStateIndicatorCharacters(t *testing.T) {
297	states := map[string]struct {
298		char      string
299		codepoint string
300		name      string
301	}{
302		"running": {
303			char:      "⬡", // First spinner frame (changes with animation)
304			codepoint: "U+2B21",
305			name:      "WHITE HEXAGON (animated)",
306		},
307		"idle": {
308			char:      "◦",
309			codepoint: "U+25E6",
310			name:      "WHITE BULLET",
311		},
312		"stopped": {
313			char:      "◌",
314			codepoint: "U+25CC",
315			name:      "DOTTED CIRCLE",
316		},
317		"errored": {
318			char:      "✖",
319			codepoint: "U+2716",
320			name:      "HEAVY MULTIPLICATION X",
321		},
322	}
323
324	for state, info := range states {
325		t.Run(state, func(t *testing.T) {
326			// Verify character width
327			width := lipgloss.Width(info.char)
328			if width != 1 {
329				t.Errorf("State %s indicator %q has width %d, expected 1",
330					state, info.char, width)
331			}
332
333			t.Logf("State %s: %s (%s) - %s",
334				state, info.char, info.codepoint, info.name)
335		})
336	}
337}
338
339// TestBorderCharacters documents border characters used in panels.
340func TestBorderCharacters(t *testing.T) {
341	borders := map[string]struct {
342		chars      []string
343		codepoints []string
344		usage      string
345	}{
346		"double_line_box": {
347			chars:      []string{"╔", "═", "╗", "║", "╚", "╝"},
348			codepoints: []string{"U+2554", "U+2550", "U+2557", "U+2551", "U+255A", "U+255D"},
349			usage:      "Mysis list panel border",
350		},
351		"horizontal_line": {
352			chars:      []string{"─"},
353			codepoints: []string{"U+2500"},
354			usage:      "Section title decorations, focus header",
355		},
356		"rounded_corner": {
357			chars:      []string{"╭", "╮", "╰", "╯"},
358			codepoints: []string{"U+256D", "U+256E", "U+2570", "U+256F"},
359			usage:      "Input prompt border",
360		},
361	}
362
363	for name, info := range borders {
364		t.Run(name, func(t *testing.T) {
365			if len(info.chars) != len(info.codepoints) {
366				t.Errorf("Border %s: char count (%d) != codepoint count (%d)",
367					name, len(info.chars), len(info.codepoints))
368			}
369
370			t.Logf("Border %s: %s", name, info.usage)
371			for i, char := range info.chars {
372				width := lipgloss.Width(char)
373				t.Logf("  %s (%s) - width: %d", char, info.codepoints[i], width)
374			}
375		})
376	}
377}
378
379// TestCharacterRenderingMatrix provides a visual reference chart.
380func TestCharacterRenderingMatrix(t *testing.T) {
381	// Skip in short mode since this is for documentation
382	if testing.Short() {
383		t.Skip("Skipping visual reference chart in short mode")
384	}
385
386	categories := map[string][]struct {
387		name string
388		char string
389		code string
390	}{
391		"State Indicators": {
392			{"Running/Loading", "⬡⬢⬦⬥", "animated"},
393			{"Idle", "◦", "U+25E6"},
394			{"Stopped", "◌", "U+25CC"},
395			{"Errored", "✖", "U+2716"},
396		},
397		"Decorative Elements": {
398			{"Diamond", "⬥", "U+2B25"},
399			{"Lozenge", "⬧", "U+2B27"},
400			{"Hexagon White", "⬡", "U+2B21"},
401			{"Hexagon Black", "⬢", "U+2B22"},
402			{"Diamond White", "⬦", "U+2B26"},
403		},
404		"Input Prompts": {
405			{"Broadcast", "⬧", "U+2B27"},
406			{"Message", "⬥", "U+2B25"},
407			{"New Mysis", "⬡", "U+2B21"},
408			{"Config", "⚙", "U+2699"},
409		},
410	}
411
412	t.Log("\n=== Unicode Character Rendering Reference ===")
413	for category, chars := range categories {
414		t.Logf("\n%s:", category)
415		for _, item := range chars {
416			t.Logf("  %-20s %s  (%s)", item.name, item.char, item.code)
417		}
418	}
419}