package zddc

import "testing"

func bptr(b bool) *bool { return &b }

func TestEffectiveHistory(t *testing.T) {
	tests := []struct {
		name  string
		chain PolicyChain
		want  bool
	}{
		{
			name:  "no levels, no embedded default",
			chain: PolicyChain{},
			want:  false,
		},
		{
			name:  "embedded default true, no explicit level",
			chain: PolicyChain{Levels: []ZddcFile{{}, {}}, Embedded: ZddcFile{History: bptr(true)}},
			want:  true,
		},
		{
			name:  "ancestor true inherits down to leaf (nil)",
			chain: PolicyChain{Levels: []ZddcFile{{History: bptr(true)}, {}, {}}},
			want:  true,
		},
		{
			name:  "leaf false overrides ancestor true",
			chain: PolicyChain{Levels: []ZddcFile{{History: bptr(true)}, {History: bptr(false)}}},
			want:  false,
		},
		{
			name:  "leaf true overrides embedded false",
			chain: PolicyChain{Levels: []ZddcFile{{History: bptr(true)}}, Embedded: ZddcFile{History: bptr(false)}},
			want:  true,
		},
		{
			// History must inherit THROUGH an inherit:false ACL fence —
			// versioning is a write behavior, not a permission. The fenced
			// middle level sets no History, so the ancestor's true wins.
			name: "inherits through an inherit:false fence",
			chain: PolicyChain{Levels: []ZddcFile{
				{History: bptr(true)},
				{ACL: ACLRules{Inherit: bptr(false)}},
				{},
			}},
			want: true,
		},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			if got := tc.chain.EffectiveHistory(); got != tc.want {
				t.Errorf("EffectiveHistory() = %v, want %v", got, tc.want)
			}
		})
	}
}
