(t *testing.T)
| 211 | } |
| 212 | |
| 213 | func TestAddDebugFileSink_RedactionOnceInvariant(t *testing.T) { |
| 214 | r := &countingRedactor{} |
| 215 | SetRedactor(r) |
| 216 | t.Cleanup(func() { SetRedactor(nil) }) |
| 217 | |
| 218 | console := &syncBuffer{} |
| 219 | logger := newConsoleLogger(console, zap.InfoLevel) |
| 220 | |
| 221 | tmp, err := os.CreateTemp(t.TempDir(), "redact-*.log") |
| 222 | if err != nil { |
| 223 | t.Fatalf("create temp: %v", err) |
| 224 | } |
| 225 | defer tmp.Close() |
| 226 | wrapped, sink := AddDebugFileSink(logger, tmp, 0) |
| 227 | |
| 228 | const N = 50 |
| 229 | for i := 0; i < N; i++ { |
| 230 | wrapped.Debug("test", zap.String("k", "v")) |
| 231 | } |
| 232 | if err := sink.Flush(); err != nil { |
| 233 | t.Fatalf("flush: %v", err) |
| 234 | } |
| 235 | |
| 236 | // RedactEntry runs once per Write call to the OUTER redactingCore. |
| 237 | // For Debug entries, the outer core writes once, then the inner Tee |
| 238 | // fans out — so we expect N entry redactions, not 2N. |
| 239 | if got := atomic.LoadInt64(&r.entries); got != N { |
| 240 | t.Errorf("RedactEntry: expected %d, got %d (suggests redaction is double-wrapped)", N, got) |
| 241 | } |
| 242 | // One field per call → N field redactions. |
| 243 | if got := atomic.LoadInt64(&r.fields); got != N { |
| 244 | t.Errorf("RedactField: expected %d, got %d", N, got) |
| 245 | } |
| 246 | // RedactEncoded fires per writer.Write — and we have two writers |
| 247 | // (console + buffered debug file). Buffered may not fire for every |
| 248 | // entry. So the lower bound is N (console fires every time at debug |
| 249 | // level... no, console is at Info — debug entries don't reach console). |
| 250 | // Console core's Enabled returns false for Debug, so the entry does |
| 251 | // not flow through its writer. Only the debug-file writer fires. |
| 252 | // That's at most ceil(payload/buffer-size) calls. Just assert it's |
| 253 | // >0 to confirm the post-encode pass runs at all. |
| 254 | if got := atomic.LoadInt64(&r.encoded); got == 0 { |
| 255 | t.Errorf("RedactEncoded never fired; expected at least one call") |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | func TestDebugFileSink_RotateForScope(t *testing.T) { |
| 260 | SetRedactor(nil) |
nothing calls this directly
no test coverage detected