package operation

import (
	"testing"
)

func TestParseAuthToken(t *testing.T) {
	tests := []struct {
		name      string
		header    string
		wantToken string
		wantOK    bool
	}{
		{
			name:      "valid Bearer token",
			header:    "Bearer validtoken",
			wantToken: "validtoken",
			wantOK:    true,
		},
		{
			name:      "lowercase bearer",
			header:    "bearer lowercase",
			wantToken: "lowercase",
			wantOK:    true,
		},
		{
			name:      "uppercase BEARER",
			header:    "BEARER uppercase",
			wantToken: "uppercase",
			wantOK:    true,
		},
		{
			name:      "token with spaces trimmed",
			header:    "Bearer   spacedToken ",
			wantToken: "spacedToken",
			wantOK:    true,
		},
		{
			name:      "bearer with no token",
			header:    "Bearer ",
			wantToken: "",
			wantOK:    false,
		},
		{
			name:      "bearer with only spaces",
			header:    "Bearer     ",
			wantToken: "",
			wantOK:    false,
		},
		{
			name:      "missing space after Bearer",
			header:    "Bearertoken",
			wantToken: "",
			wantOK:    false,
		},
		{
			name:      "Gitea token format",
			header:    "token giteaapitoken",
			wantToken: "giteaapitoken",
			wantOK:    true,
		},
		{
			name:      "Gitea Token format capitalized",
			header:    "Token giteaapitoken",
			wantToken: "giteaapitoken",
			wantOK:    true,
		},
		{
			name:      "token with no value",
			header:    "token ",
			wantToken: "",
			wantOK:    false,
		},
		{
			name:      "different auth type",
			header:    "Basic dXNlcjpwYXNz",
			wantToken: "",
			wantOK:    false,
		},
		{
			name:      "empty header",
			header:    "",
			wantToken: "",
			wantOK:    false,
		},
		{
			name:      "bearer token with internal spaces",
			header:    "Bearer token with spaces",
			wantToken: "token with spaces",
			wantOK:    true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gotToken, gotOK := parseAuthToken(tt.header)
			if gotToken != tt.wantToken {
				t.Errorf("parseAuthToken() token = %q, want %q", gotToken, tt.wantToken)
			}
			if gotOK != tt.wantOK {
				t.Errorf("parseAuthToken() ok = %v, want %v", gotOK, tt.wantOK)
			}
		})
	}
}
