package internal

import (
	"regexp"
	"strings"
	"testing"
)

func TestSanitizeDesc(t *testing.T) {
	cases := []struct {
		in   string
		want string
	}{
		{"", "upload"},
		{"  hello world  ", "hello_world"},
		{"a/b*c", "a_b_c"},
	}
	for _, tc := range cases {
		if got := SanitizeDesc(tc.in); got != tc.want {
			t.Fatalf("SanitizeDesc(%q)=%q want=%q", tc.in, got, tc.want)
		}
	}

	long := strings.Repeat("x", 200)
	if got := SanitizeDesc(long); len(got) != descMax {
		t.Fatalf("expected max len %d, got %d", descMax, len(got))
	}
}

func TestComposeFinalName(t *testing.T) {
	got, err := ComposeFinalName("2026-04-09", "My desc", "Movie.MP4")
	if err != nil {
		t.Fatalf("ComposeFinalName returned error: %v", err)
	}
	if got != "2026.04.09.My_desc.mp4" {
		t.Fatalf("unexpected final name %q", got)
	}

	if _, err := ComposeFinalName("2026-99-01", "desc", "x.mov"); err == nil {
		t.Fatalf("expected invalid date error")
	}

	fallback, err := ComposeFinalName("not-a-date", "desc", "file")
	if err != nil {
		t.Fatalf("unexpected fallback error: %v", err)
	}
	if !regexp.MustCompile(`^\d{4}\.\d{2}\.\d{2}\.desc\.bin$`).MatchString(fallback) {
		t.Fatalf("unexpected fallback name %q", fallback)
	}
}

func TestComposeFinalNameRejectsLongExtension(t *testing.T) {
	if _, err := ComposeFinalName("2026-04-09", "desc", "movie.toolongext"); err == nil {
		t.Fatalf("expected invalid final-name error")
	}
}
