package protosocket

import "testing"

func TestEventSubscriptionAllowsMatchingBranchEvent(t *testing.T) {
	subscription := EventSubscription{
		Events: []string{"branch.updated"},
		RepoID: "nomadcode",
		Branch: "develop",
	}
	event := NewEventEnvelope("branch.updated", map[string]any{
		"type":    "branch.updated",
		"repo_id": "nomadcode",
		"branch":  "develop",
	})

	if !subscription.Allows(event) {
		t.Fatal("expected matching branch event to be allowed")
	}
}

func TestEventSubscriptionRejectsDifferentBranch(t *testing.T) {
	subscription := EventSubscription{
		Events: []string{"branch.updated"},
		RepoID: "nomadcode",
		Branch: "develop",
	}
	event := NewEventEnvelope("branch.updated", map[string]any{
		"type":    "branch.updated",
		"repo_id": "nomadcode",
		"branch":  "feature/a",
	})

	if subscription.Allows(event) {
		t.Fatal("expected different branch event to be rejected")
	}
}

func TestEventSubscriptionRejectsDifferentEvent(t *testing.T) {
	subscription := EventSubscription{
		Events: []string{"branch.updated"},
		RepoID: "nomadcode",
	}
	event := NewEventEnvelope("operation.completed", map[string]any{
		"type":    "operation.completed",
		"repo_id": "nomadcode",
	})

	if subscription.Allows(event) {
		t.Fatal("expected different event action to be rejected")
	}
}

func TestEventSubscriptionWildcardAllowsAnyEventForRepo(t *testing.T) {
	subscription := EventSubscription{
		Events: []string{"*"},
		RepoID: "nomadcode",
	}
	event := NewEventEnvelope("operation.completed", map[string]any{
		"type":    "operation.completed",
		"repo_id": "nomadcode",
	})

	if !subscription.Allows(event) {
		t.Fatal("expected wildcard event subscription to allow matching repo")
	}
}
