Files
dnswatcher/internal/logger/logger_test.go
T
sneak 218abb4daf
check / check (push) Failing after 1s
Add tests for globals, healthcheck, and logger packages (closes #110)
These three packages had no tests. Add behavioural, external
(package foo_test) tests that can each fail on a plausible break:

- globals: setters and read-back through New, plus that New returns
  an independent snapshot. One sequential function with a disclosed
  paralleltest suppression, because it mutates shared package globals.
- healthcheck: Check returns status "ok", the exact documented JSON
  field set, an RFC3339Nano timestamp, maintenanceMode mirroring the
  config in both states, and the version and appname from globals.
- logger: New yields a usable *slog.Logger, debug is suppressed at the
  default level, and EnableDebugLogging turns it on.

No production code changed. TTY-branch output format is not asserted.

Model: opus-4-8
2026-09-21 07:48:43 +00:00

67 lines
1.7 KiB
Go

package logger_test
import (
"context"
"log/slog"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sneak.berlin/go/dnswatcher/internal/globals"
"sneak.berlin/go/dnswatcher/internal/logger"
)
func newTestLogger(t *testing.T) *logger.Logger {
t.Helper()
g := &globals.Globals{Appname: "dnswatcher", Version: "test"}
l, err := logger.New(nil, logger.Params{Globals: g})
require.NoError(t, err)
return l
}
// TestNewReturnsUsableLogger checks that the constructor yields a
// working *slog.Logger.
func TestNewReturnsUsableLogger(t *testing.T) {
t.Parallel()
l := newTestLogger(t)
require.NotNil(t, l.Get(), "Get must return a non-nil logger")
}
// TestDefaultLevelExcludesDebug verifies the default configuration
// logs at info: debug records are suppressed, info records pass.
func TestDefaultLevelExcludesDebug(t *testing.T) {
t.Parallel()
log := newTestLogger(t).Get()
ctx := context.Background()
assert.False(t, log.Enabled(ctx, slog.LevelDebug),
"debug must be suppressed at the default level")
assert.True(t, log.Enabled(ctx, slog.LevelInfo),
"info must be enabled at the default level")
}
// TestEnableDebugLoggingChangesLevel verifies the debug and non-debug
// configurations differ as intended: enabling debug makes debug
// records pass where they previously did not.
func TestEnableDebugLoggingChangesLevel(t *testing.T) {
t.Parallel()
l := newTestLogger(t)
log := l.Get()
ctx := context.Background()
require.False(t, log.Enabled(ctx, slog.LevelDebug),
"debug must start disabled")
l.EnableDebugLogging()
assert.True(t, log.Enabled(ctx, slog.LevelDebug),
"debug must be enabled after EnableDebugLogging")
}