package logging

import (
	"os"
	"path/filepath"
	"testing"
	"time"
)

func TestDailyWriterCleansExpiredDirectoriesOnRotation(t *testing.T) {
	root := t.TempDir()
	writer := NewDailyWriter(root, "application.log", 1)
	oldDate := time.Now().AddDate(0, 0, -3).Format("2006-01-02")
	oldDir := filepath.Join(root, oldDate)
	if err := os.MkdirAll(oldDir, 0o755); err != nil {
		t.Fatal(err)
	}
	writer.date = "2000-01-01"
	if _, err := writer.Write([]byte("rotated\n")); err != nil {
		t.Fatal(err)
	}
	_ = writer.Close()
	if _, err := os.Stat(oldDir); !os.IsNotExist(err) {
		t.Fatalf("expired directory still exists: err=%v", err)
	}
}
