package app

import "testing"

func TestValidateStoreRuntimeMode(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name          string
		mode          string
		allowInMemory bool
		expectError   bool
	}{
		{
			name:          "development allows in-memory store",
			mode:          "development",
			allowInMemory: false,
			expectError:   false,
		},
		{
			name:          "production rejects in-memory store by default",
			mode:          "production",
			allowInMemory: false,
			expectError:   true,
		},
		{
			name:          "non-development can be explicitly overridden",
			mode:          "staging",
			allowInMemory: true,
			expectError:   false,
		},
	}

	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()
			err := validateStoreRuntimeMode(test.mode, test.allowInMemory)
			if test.expectError && err == nil {
				t.Fatalf("expected error for mode=%q allowInMemory=%v", test.mode, test.allowInMemory)
			}
			if !test.expectError && err != nil {
				t.Fatalf("did not expect error for mode=%q allowInMemory=%v: %v", test.mode, test.allowInMemory, err)
			}
		})
	}
}

func TestInMemoryStoreAllowed(t *testing.T) {
	tests := []struct {
		name    string
		value   string
		allowed bool
	}{
		{name: "empty", value: "", allowed: false},
		{name: "true", value: "true", allowed: true},
		{name: "uppercase true", value: "TRUE", allowed: true},
		{name: "one", value: "1", allowed: true},
		{name: "yes", value: "yes", allowed: true},
		{name: "off", value: "off", allowed: false},
	}

	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
			t.Setenv("ALLOW_INMEMORY_STORE", test.value)
			if got := inMemoryStoreAllowed(); got != test.allowed {
				t.Fatalf("inMemoryStoreAllowed() = %v, want %v for %q", got, test.allowed, test.value)
			}
		})
	}
}
