package limits

import (
	"errors"
	"testing"
)

func TestValidateAttachmentSize(t *testing.T) {
	t.Parallel()
	if err := ValidateAttachmentSize(MaxAttachmentBytes); err != nil {
		t.Fatalf("at limit: %v", err)
	}
	if err := ValidateAttachmentSize(MaxAttachmentBytes + 1); !errors.Is(err, ErrAttachmentTooLarge) {
		t.Fatalf("over limit: %v", err)
	}
}

func TestValidateAttachmentQuota(t *testing.T) {
	t.Parallel()
	cases := []struct {
		name    string
		count   int
		total   int64
		newSize int64
		want    error
	}{
		{"ok", 0, 0, 1, nil},
		{"at count", MaxAttachmentsPerMessage, 0, 1, ErrTooManyAttachments},
		{"at total", 0, MaxTotalAttachmentsPerMessageBytes, 1, ErrAttachmentsTotalTooLarge},
		{"would exceed total", 1, MaxTotalAttachmentsPerMessageBytes - 10, 11, ErrAttachmentsTotalTooLarge},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			err := ValidateAttachmentQuota(tc.count, tc.total, tc.newSize)
			if !errors.Is(err, tc.want) {
				t.Fatalf("got %v want %v", err, tc.want)
			}
		})
	}
}
