package streaming

import (
	"testing"
	"time"
)

func TestPrincipalSessionOperationsRequireTheImmutableOwner(t *testing.T) {
	manager := NewSessionManager(time.Minute)
	defer manager.Stop()

	clientID := manager.CreatePrincipalSession("user-1")
	if _, exists := manager.GetPrincipalSession(clientID, "user-2"); exists {
		t.Fatal("another principal retrieved the bound session")
	}
	if manager.SubscribePrincipal(clientID, "user-2", "/topic/records") {
		t.Fatal("another principal changed the bound session")
	}
	if manager.DestroyPrincipalSession(clientID, "user-2") {
		t.Fatal("another principal destroyed the bound session")
	}
	if _, exists := manager.GetPrincipalSession(clientID, "user-1"); !exists {
		t.Fatal("owner cannot retrieve the bound session")
	}
	if !manager.SubscribePrincipal(clientID, "user-1", "/topic/records") {
		t.Fatal("owner cannot subscribe the bound session")
	}
	if !manager.DestroyPrincipalSession(clientID, "user-1") {
		t.Fatal("owner cannot destroy the bound session")
	}
}

func TestLegacyUnboundSessionDoesNotSatisfyPrincipalOperations(t *testing.T) {
	manager := NewSessionManager(time.Minute)
	defer manager.Stop()

	clientID := manager.CreateSession()
	if !manager.ValidateSession(clientID) {
		t.Fatal("legacy session is unavailable through the compatible API")
	}
	if _, exists := manager.GetPrincipalSession(clientID, "user-1"); exists {
		t.Fatal("unbound session satisfied principal retrieval")
	}
	if manager.SubscribePrincipal(clientID, "user-1", "/topic/records") {
		t.Fatal("unbound session satisfied principal subscription")
	}
	if manager.DestroyPrincipalSession(clientID, "user-1") {
		t.Fatal("unbound session satisfied principal destruction")
	}
	if !manager.Subscribe(clientID, "/topic/legacy") {
		t.Fatal("legacy session no longer supports the compatible subscription API")
	}
}
