|
| 1 | +package tui |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "runtime" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/charmbracelet/colorprofile" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// TestResolveColorProfile_WindowsWithoutVT_ForcesASCII is the regression guard |
| 16 | +// for classic cmd.exe garbage (←[38;2;...m). Detect alone returns TrueColor on |
| 17 | +// Win10+ by OS build; without VT we MUST force ASCII so nothing emits ANSI. |
| 18 | +func TestResolveColorProfile_WindowsWithoutVT_ForcesASCII(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + if runtime.GOOS != "windows" { |
| 21 | + t.Skip("Windows-only regression: VT-disabled console") |
| 22 | + } |
| 23 | + |
| 24 | + // Simulate a TTY-ish env that Detect upgrades to TrueColor on Win10+. |
| 25 | + env := []string{ |
| 26 | + "TERM=", |
| 27 | + "COLORTERM=truecolor", |
| 28 | + } |
| 29 | + // Use os.Stderr so Detect sees a real file handle; VT flag forced false. |
| 30 | + got := ResolveColorProfile(os.Stderr, env, false) |
| 31 | + assert.LessOrEqual(t, got, colorprofile.ASCII, |
| 32 | + "Windows console without VT must not emit color ANSI (got %v)", got) |
| 33 | +} |
| 34 | + |
| 35 | +// TestResolveColorProfile_WindowsWithVT_AllowsColor ensures we do not |
| 36 | +// over-downgrade modern hosts (Windows Terminal, cmd with VT on). |
| 37 | +// TTY_FORCE makes Detect treat the writer as a TTY even under go test pipes. |
| 38 | +func TestResolveColorProfile_WindowsWithVT_AllowsColor(t *testing.T) { |
| 39 | + t.Parallel() |
| 40 | + if runtime.GOOS != "windows" { |
| 41 | + t.Skip("Windows-only") |
| 42 | + } |
| 43 | + |
| 44 | + env := []string{ |
| 45 | + "TTY_FORCE=1", |
| 46 | + "WT_SESSION=test-session", |
| 47 | + "COLORTERM=truecolor", |
| 48 | + } |
| 49 | + got := ResolveColorProfile(os.Stderr, env, true) |
| 50 | + assert.Greater(t, got, colorprofile.ASCII, |
| 51 | + "VT-enabled Windows console should keep color (got %v)", got) |
| 52 | + |
| 53 | + // Same env without VT must still force ASCII (the cmd.exe regression). |
| 54 | + downgraded := ResolveColorProfile(os.Stderr, env, false) |
| 55 | + assert.LessOrEqual(t, downgraded, colorprofile.ASCII) |
| 56 | +} |
| 57 | + |
| 58 | +// TestResolveColorProfile_NoColorEnv_StaysPlain covers NO_COLOR on any OS. |
| 59 | +func TestResolveColorProfile_NoColorEnv_StaysPlain(t *testing.T) { |
| 60 | + t.Parallel() |
| 61 | + env := []string{"NO_COLOR=1", "TERM=xterm-256color", "COLORTERM=truecolor"} |
| 62 | + got := ResolveColorProfile(os.Stderr, env, true) |
| 63 | + assert.LessOrEqual(t, got, colorprofile.ASCII) |
| 64 | +} |
| 65 | + |
| 66 | +// TestResolveColorProfile_NonWindows_IgnoresVTFlag documents that Unix |
| 67 | +// hosts always trust Detect (vtEnabled is irrelevant). |
| 68 | +func TestResolveColorProfile_NonWindows_IgnoresVTFlag(t *testing.T) { |
| 69 | + t.Parallel() |
| 70 | + if runtime.GOOS == "windows" { |
| 71 | + t.Skip("Unix-only branch") |
| 72 | + } |
| 73 | + env := []string{"TERM=xterm-256color", "COLORTERM=truecolor"} |
| 74 | + // Even with vtEnabled=false, non-Windows must not force ASCII solely for that. |
| 75 | + with := ResolveColorProfile(os.Stderr, env, true) |
| 76 | + without := ResolveColorProfile(os.Stderr, env, false) |
| 77 | + assert.Equal(t, with, without) |
| 78 | +} |
| 79 | + |
| 80 | +// TestResolveColorProfile_PipeWriter_NoTTY ensures redirected writers stay plain. |
| 81 | +func TestResolveColorProfile_PipeWriter_NoTTY(t *testing.T) { |
| 82 | + t.Parallel() |
| 83 | + var buf bytes.Buffer |
| 84 | + got := ResolveColorProfile(&buf, []string{"TERM=xterm-256color"}, false) |
| 85 | + assert.LessOrEqual(t, got, colorprofile.ASCII, |
| 86 | + "non-TTY writer must not use color profiles (got %v)", got) |
| 87 | +} |
| 88 | + |
| 89 | +func TestEnableVirtualTerminal_DoesNotPanic(t *testing.T) { |
| 90 | + t.Parallel() |
| 91 | + _ = EnableVirtualTerminal() |
| 92 | + _ = EnableVirtualTerminal() |
| 93 | +} |
| 94 | + |
| 95 | +func TestHasVirtualTerminal_NilFile(t *testing.T) { |
| 96 | + t.Parallel() |
| 97 | + assert.False(t, HasVirtualTerminal(nil)) |
| 98 | +} |
| 99 | + |
| 100 | +func TestSupportsANSI_NilFile(t *testing.T) { |
| 101 | + t.Parallel() |
| 102 | + assert.False(t, SupportsANSI(nil)) |
| 103 | +} |
| 104 | + |
| 105 | +// TestEnableVirtualTerminal_EnablesFlagWhenConsole is the live console check. |
| 106 | +// Skips when stdout is not a console (CI pipes, go test capture). |
| 107 | +func TestEnableVirtualTerminal_EnablesFlagWhenConsole(t *testing.T) { |
| 108 | + if runtime.GOOS != "windows" { |
| 109 | + t.Skip("Windows console mode flag only") |
| 110 | + } |
| 111 | + ok := EnableVirtualTerminal() |
| 112 | + if !HasVirtualTerminal(os.Stdout) && !HasVirtualTerminal(os.Stderr) { |
| 113 | + t.Skip("no console attached (piped CI) — cannot assert VT flag") |
| 114 | + } |
| 115 | + require.True(t, ok, "EnableVirtualTerminal should succeed on a real console") |
| 116 | + assert.True(t, HasVirtualTerminal(os.Stdout) || HasVirtualTerminal(os.Stderr)) |
| 117 | +} |
| 118 | + |
| 119 | +// TestConsoleColorProfile_NeverTrueColorWithoutVT hard-guards the user-facing |
| 120 | +// bug: profile used by logger/TUI must never be TrueColor/ANSI256 when VT is off. |
| 121 | +func TestConsoleColorProfile_NeverTrueColorWithoutVT(t *testing.T) { |
| 122 | + t.Parallel() |
| 123 | + if runtime.GOOS != "windows" { |
| 124 | + t.Skip("Windows-only contract") |
| 125 | + } |
| 126 | + // Force the pure path with vtEnabled=false regardless of host state. |
| 127 | + p := ResolveColorProfile(os.Stderr, os.Environ(), false) |
| 128 | + assert.LessOrEqual(t, p, colorprofile.ASCII) |
| 129 | +} |
| 130 | + |
| 131 | +// TestRestoreTerminalState_NoANSIWhenUnsupported ensures exit cleanup does not |
| 132 | +// dump TerminalResetSequence into classic cmd.exe. |
| 133 | +func TestRestoreTerminalState_NoANSIWhenUnsupported(t *testing.T) { |
| 134 | + t.Parallel() |
| 135 | + // bytes.Buffer is not *os.File → SupportsANSI path not taken; sequence writes. |
| 136 | + // Test the *os.File branch with a temp file (not a console → no VT). |
| 137 | + f, err := os.CreateTemp(t.TempDir(), "restore-*.txt") |
| 138 | + require.NoError(t, err) |
| 139 | + t.Cleanup(func() { _ = f.Close() }) |
| 140 | + |
| 141 | + if runtime.GOOS == "windows" { |
| 142 | + // Temp file is not a console → SupportsANSI false → no write. |
| 143 | + RestoreTerminalState(f) |
| 144 | + _, _ = f.Seek(0, 0) |
| 145 | + data, err := os.ReadFile(f.Name()) |
| 146 | + require.NoError(t, err) |
| 147 | + assert.Empty(t, data, "must not write ANSI reset to non-console Windows handle") |
| 148 | + assert.NotContains(t, string(data), "\x1b") |
| 149 | + return |
| 150 | + } |
| 151 | + // Non-Windows: sequence is written (SupportsANSI true for any non-nil file). |
| 152 | + RestoreTerminalState(f) |
| 153 | + _, _ = f.Seek(0, 0) |
| 154 | + data, err := os.ReadFile(f.Name()) |
| 155 | + require.NoError(t, err) |
| 156 | + assert.Contains(t, string(data), "\x1b[?25h") |
| 157 | +} |
| 158 | + |
| 159 | +// TestTerminalResetSequence_ContainsNoRIS documents we never hard-reset the |
| 160 | +// screen (would wipe scrollback) — keeps restore safe. |
| 161 | +func TestTerminalResetSequence_ContainsNoRIS(t *testing.T) { |
| 162 | + t.Parallel() |
| 163 | + assert.NotContains(t, TerminalResetSequence, "\x1bc") |
| 164 | + assert.NotContains(t, TerminalResetSequence, "\x1b[2J") |
| 165 | + assert.True(t, strings.Contains(TerminalResetSequence, "\x1b[?25h")) |
| 166 | +} |
0 commit comments