Debian Codegraph @

pkg/twwidth/ea_test.go

raw
1 package twwidth
2
3 import (
4 "os"
5 "testing"
6 )
7
8 func TestDetectEastAsian_Logic(t *testing.T) {
9 // We cannot run this in parallel (t.Parallel()) because it modifies
10 // process-level environment variables and global state.
11
12 // Helper struct to define the environment state for a test case
13 type envConfig struct {
14 lcAll string
15 lcCtype string
16 lang string
17 runeWidth string // RUNEWIDTH_EASTASIAN
18 termProg string // TERM_PROGRAM
19 term string // TERM
20 forceLegacy bool // Global legacy switch
21 }
22
23 tests := []struct {
24 name string
25 env envConfig
26 expected bool
27 }{
28
29 // LOCALE ONLY (Legacy Behavior / Non-Modern Terminal)
30 {
31 name: "Locale: Chinese (Simplified)",
32 env: envConfig{lang: "zh_CN.UTF-8"},
33 expected: true,
34 },
35 {
36 name: "Locale: Japanese",
37 env: envConfig{lang: "ja_JP.UTF-8"},
38 expected: true,
39 },
40 {
41 name: "Locale: English",
42 env: envConfig{lang: "en_US.UTF-8"},
43 expected: false,
44 },
45 {
46 name: "Locale Priority: LC_ALL overrides LANG",
47 env: envConfig{lcAll: "ja_JP", lang: "en_US"},
48 expected: true,
49 },
50 {
51 name: "Locale Region: English in Hong Kong (en_HK)",
52 env: envConfig{lang: "en_HK"},
53 expected: true,
54 },
55
56 // MODERN ENVIRONMENT (Should force Narrow/False)
57 {
58 name: "Modern: VSCode with Chinese Locale",
59 env: envConfig{lang: "zh_CN.UTF-8", termProg: "vscode"},
60 expected: false, // Modern env implies single-width font
61 },
62 {
63 name: "Modern: iTerm2 with Japanese Locale",
64 env: envConfig{lang: "ja_JP.UTF-8", termProg: "iTerm.app"},
65 expected: false,
66 },
67 {
68 name: "Modern: Windows Terminal via WT_PROFILE_ID (Simulated by TERM checks in this test structure)",
69 // Note: We can't easily mock runtime.GOOS, so we stick to env vars
70 // that work cross-platform in the heuristic function.
71 // Let's test Alacritty which is checked via env var.
72 env: envConfig{lang: "zh_CN.UTF-8", termProg: "Alacritty"},
73 expected: false,
74 },
75 {
76 name: "Modern: TERM=xterm-kitty with Chinese Locale",
77 env: envConfig{lang: "zh_CN.UTF-8", term: "xterm-kitty"},
78 expected: false,
79 },
80
81 // USER OVERRIDE (Highest Priority)
82 {
83 name: "Override: Force ON (1) in English Env",
84 env: envConfig{lang: "en_US.UTF-8", runeWidth: "1"},
85 expected: true,
86 },
87 {
88 name: "Override: Force ON (true) overrides Modern Env",
89 env: envConfig{lang: "en_US.UTF-8", termProg: "vscode", runeWidth: "true"},
90 expected: true,
91 },
92 {
93 name: "Override: Force OFF (0) in Chinese Env",
94 env: envConfig{lang: "zh_CN.UTF-8", runeWidth: "0"},
95 expected: false,
96 },
97 {
98 name: "Override: Force OFF (false) overrides Legacy Detection",
99 env: envConfig{lang: "zh_CN.UTF-8", runeWidth: "false"},
100 expected: false,
101 },
102
103 // LEGACY FORCE SWITCH (Programmatic Override)
104 {
105 name: "Legacy Force: Ignores Modern Env",
106 env: envConfig{lang: "zh_CN.UTF-8", termProg: "vscode", forceLegacy: true},
107 expected: true, // Should use Locale (True) despite VSCode
108 },
109 {
110 name: "Legacy Force: Still respects User Override (RW=0)",
111 env: envConfig{lang: "zh_CN.UTF-8", runeWidth: "0", forceLegacy: true},
112 expected: false, // User env var is supreme
113 },
114 }
115
116 for _, tt := range tests {
117 t.Run(tt.name, func(t *testing.T) {
118 // Save current state
119 saveEnv := func(key string) string { return os.Getenv(key) }
120 oldLCAll := saveEnv(EnvLCAll)
121 oldLCCtype := saveEnv(EnvLCCtype)
122 oldLang := saveEnv(EnvLang)
123 oldRuneWidth := saveEnv(EnvRuneWidthEastAsian)
124 oldTermProg := saveEnv(EnvTermProgram)
125 oldTerm := saveEnv(EnvTerm)
126
127 // Helper to set/unset
128 setEnv := func(key, val string) {
129 if val == "" {
130 os.Unsetenv(key)
131 } else {
132 os.Setenv(key, val)
133 }
134 }
135
136 // Restore after test
137 defer func() {
138 setEnv(EnvLCAll, oldLCAll)
139 setEnv(EnvLCCtype, oldLCCtype)
140 setEnv(EnvLang, oldLang)
141 setEnv(EnvRuneWidthEastAsian, oldRuneWidth)
142 setEnv(EnvTermProgram, oldTermProg)
143 setEnv(EnvTerm, oldTerm)
144 EastAsianForceLegacy(false) // Reset global flag
145 }()
146
147 // Apply test configuration
148 setEnv(EnvLCAll, tt.env.lcAll)
149 setEnv(EnvLCCtype, tt.env.lcCtype)
150 setEnv(EnvLang, tt.env.lang)
151 setEnv(EnvRuneWidthEastAsian, tt.env.runeWidth)
152 setEnv(EnvTermProgram, tt.env.termProg)
153 setEnv(EnvTerm, tt.env.term)
154 EastAsianForceLegacy(tt.env.forceLegacy)
155
156 // Call internal logic directly to bypass sync.Once
157 if got := detectEastAsian(); got != tt.expected {
158 t.Errorf("detectEastAsian() = %v, want %v\nEnv: %+v", got, tt.expected, tt.env)
159 }
160 })
161 }
162 }
163
164 func TestAutoUseEastAsian_Cache(t *testing.T) {
165 // This test verifies that the result is cached (sync.Once).
166 // Since 'eastAsianOnce' is private and global, we assume this is the
167 // first call in this process execution, OR we implicitly accept checking
168 // the behavior of the *existing* singleton state if unrelated tests ran before.
169
170 // IMPORTANT: Because other tests might have run, we can't guarantee `eastAsianOnce`
171 // is fresh. This test is best effort to ensure stability.
172
173 // Snapshot Result
174 firstResult := EastAsianDetect()
175
176 // Change Environmental factors to the OPPOSITE of what triggered firstResult
177 if firstResult {
178 // If currently True (e.g. CJK), try to force False
179 os.Setenv(EnvLang, "en_US.UTF-8")
180 os.Setenv(EnvRuneWidthEastAsian, "0")
181 } else {
182 // If currently False, try to force True
183 os.Setenv(EnvLang, "zh_CN.UTF-8")
184 os.Setenv(EnvRuneWidthEastAsian, "1")
185 }
186 // Ensure we clean up
187 defer func() {
188 os.Unsetenv(EnvLang)
189 os.Unsetenv(EnvRuneWidthEastAsian)
190 }()
191
192 // Second call
193 secondResult := EastAsianDetect()
194
195 if firstResult != secondResult {
196 t.Errorf("EastAsianDetect() did not cache result. First=%v, Second=%v", firstResult, secondResult)
197 }
198 }