package webhooks

import (
	"strings"
	"testing"
	"time"
)

func TestWebhookRetryDelay(t *testing.T) {
	tests := []struct {
		attempt int
		want    time.Duration
	}{
		{attempt: 1, want: 500 * time.Millisecond},
		{attempt: 2, want: time.Second},
		{attempt: 3, want: 2 * time.Second},
		{attempt: 6, want: 8 * time.Second},
		{attempt: 9, want: 8 * time.Second},
	}

	for _, tt := range tests {
		if got := webhookRetryDelay(tt.attempt); got != tt.want {
			t.Fatalf("webhookRetryDelay(%d) = %v, want %v", tt.attempt, got, tt.want)
		}
	}
}

func TestSignPayload(t *testing.T) {
	got := signPayload("secret", "1716372000", `{"ok":true}`)
	if !strings.HasPrefix(got, "sha256=") {
		t.Fatalf("signature prefix missing: %q", got)
	}
	if len(got) != len("sha256=")+64 {
		t.Fatalf("signature length = %d, want %d", len(got), len("sha256=")+64)
	}
}

func TestTruncateForLog(t *testing.T) {
	preview, truncated := truncateForLog("abcdef", 4)
	if !truncated {
		t.Fatal("truncated = false, want true")
	}
	if preview != "abcd" {
		t.Fatalf("preview = %q, want %q", preview, "abcd")
	}
}
