package services

// Unit tests for BuildingBlockService helpers — pure functions, no DB
// dependency (t-paliad-315 Slice C).

import "testing"

func TestValidVisibility(t *testing.T) {
	cases := []struct {
		in    string
		valid bool
	}{
		{"private", true},
		{"team", true},
		{"firm", true},
		{"global", true},
		{"PRIVATE", false}, // case-sensitive
		{"", false},
		{"public", false},
		{"all", false},
	}
	for _, tc := range cases {
		t.Run(tc.in, func(t *testing.T) {
			if got := validVisibility(tc.in); got != tc.valid {
				t.Errorf("validVisibility(%q) = %v; want %v", tc.in, got, tc.valid)
			}
		})
	}
}

func TestAppendBlockContent(t *testing.T) {
	cases := []struct {
		existing string
		addition string
		want     string
	}{
		{"", "hello", "hello"},
		{"existing", "", "existing"},
		{"", "", ""},
		{"existing", "addition", "existing\n\naddition"},
		{"existing\n", "addition", "existing\n\naddition"},
		{"existing\n\n\n", "addition", "existing\n\naddition"},
		{"   ", "addition", "addition"}, // whitespace-only existing counts as empty
		{"existing", "   ", "existing"}, // whitespace-only addition counts as empty
	}
	for _, tc := range cases {
		if got := appendBlockContent(tc.existing, tc.addition); got != tc.want {
			t.Errorf("appendBlockContent(%q,%q) = %q; want %q", tc.existing, tc.addition, got, tc.want)
		}
	}
}

func TestBuildingBlockVisibilityConstants(t *testing.T) {
	// Pin the constants so a typo somewhere doesn't silently flip a
	// tier name. The DB CHECK constraint and the RLS predicate both
	// hard-code these literals.
	if VisPrivate != "private" || VisTeam != "team" || VisFirm != "firm" || VisGlobal != "global" {
		t.Errorf("visibility constants drifted: %q/%q/%q/%q", VisPrivate, VisTeam, VisFirm, VisGlobal)
	}
}
