fix(diary): skip HideOnTTY records only on a terminal console, also with forced color (IN-01, quick-261028-rqd)

This commit is contained in:
Jakub Zych
2026-10-28 20:41:07 +01:00
parent f91d70d05e
commit 3520d5d480
8 changed files with 75 additions and 23 deletions

View File

@@ -0,0 +1,36 @@
package diary
import (
"bytes"
"context"
"log/slog"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/charmbracelet/colorprofile"
)
// TestConsoleHideOnTTYTerminal (IN-01): the styled console handler skips a
// HideOnTTY record when its writer is a terminal, and writes ordinary
// records.
func TestConsoleHideOnTTYTerminal(t *testing.T) {
var buf bytes.Buffer
h := newConsoleHandler(&buf, ConsoleOptions{Profile: colorprofile.ANSI}, new(atomic.Bool))
h.terminal = true
hidden := slog.NewRecord(time.Time{}, slog.LevelInfo, "hidden summary", 0)
if err := h.Handle(HideOnTTY(context.Background()), hidden); err != nil {
t.Fatal(err)
}
shown := slog.NewRecord(time.Time{}, slog.LevelInfo, "shown line", 0)
if err := h.Handle(context.Background(), shown); err != nil {
t.Fatal(err)
}
if strings.Contains(buf.String(), "hidden summary") {
t.Errorf("terminal console shows a HideOnTTY record:\n%s", buf.String())
}
if !strings.Contains(buf.String(), "shown line") {
t.Errorf("terminal console lacks the ordinary record:\n%s", buf.String())
}
}