package core

import (
	"strings"
	"testing"
)

func TestValidOwner(t *testing.T) {
	tests := []struct {
		in   string
		want bool
	}{
		{"bigbes", true},
		{"user_name", true},
		{"user-name", true},
		{"user.name", true},
		{"u123", true},
		{"a", true},

		{"", false},
		{"-leading", false},
		{"has/slash", false},
		{"has..dots", false},
		{"Upper", false},
		{"has space", false},
		{"tilde~", false},
		{"emoji😀", false},
	}
	for _, tc := range tests {
		if got := ValidOwner(tc.in); got != tc.want {
			t.Errorf("ValidOwner(%q) = %v, want %v", tc.in, got, tc.want)
		}
	}
}

func TestValidRepoName(t *testing.T) {
	tests := []struct {
		in   string
		want bool
	}{
		{"core-go", true},
		{"my.repo", true},
		{"repo_1", true},
		{strings.Repeat("a", 100), true},

		{"", false},
		{strings.Repeat("a", 101), false},
		{"-dashfirst", false},
		{"a/b", false},
		{"a..b", false},
		{"CamelCase", false},
	}
	for _, tc := range tests {
		if got := ValidRepoName(tc.in); got != tc.want {
			t.Errorf("ValidRepoName(%q) = %v, want %v", tc.in, got, tc.want)
		}
	}
}

func TestValidRef(t *testing.T) {
	tests := []struct {
		in   string
		want bool
	}{
		// Accepted.
		{"main", true},
		{"feature/foo", true},
		{"feature/with-slash", true},
		{"release/v1.0", true},
		{"v1.0.0", true},
		{"a/b/c/d", true},
		{"0123456789abcdef0123456789abcdef01234567", true},                         // SHA-1
		{"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", true}, // SHA-256
		{"DEADBEEFdeadbeef0000111122223333abcdabcd", true},                         // mixed-case SHA-1
		{"foo.lockfile", true}, // ".lock" only rejected as a suffix

		// Rejected — structural.
		{"", false},
		{"@", false},
		{"-oops", false},
		{".hidden", false},
		{"/leading", false},
		{"trailing/", false},
		{"trailing.", false},
		{"foo.lock", false},
		{"feature/bar.lock", false}, // per-component .lock
		{"feature/.hidden", false},  // per-component leading dot
		{"a..b", false},
		{"a//b", false},
		{"main@{u}", false},

		// Rejected — forbidden bytes.
		{"has space", false},
		{"tilde~1", false},
		{"caret^", false},
		{"colon:x", false},
		{"star*", false},
		{"quest?", false},
		{"brack[et", false},
		{"back\\slash", false},
		{"ctrl\x01char", false},
		{"del\x7fchar", false},

		// Not a full-length SHA -> falls through to name rules (valid here).
		{"abcdef", true},
	}
	for _, tc := range tests {
		if got := ValidRef(tc.in); got != tc.want {
			t.Errorf("ValidRef(%q) = %v, want %v", tc.in, got, tc.want)
		}
	}
}
