package transport

import (
	"os"
	"path/filepath"
	"testing"
)

// TestSelect covers the branch-by-abstraction toggle: unibus only when the flag
// is on AND the bot opted in; Matrix in every other combination (the default,
// so unmigrated bots keep working).
func TestSelect(t *testing.T) {
	cases := []struct {
		flag, optIn bool
		want        Kind
	}{
		{true, true, KindUnibus},
		{true, false, KindMatrix},
		{false, true, KindMatrix},
		{false, false, KindMatrix},
	}
	for _, c := range cases {
		if got := Select(c.flag, c.optIn); got != c.want {
			t.Errorf("Select(flag=%v, optIn=%v) = %q, want %q", c.flag, c.optIn, got, c.want)
		}
	}
}

func TestFlagEnabled(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "feature_flags.json")
	const content = `{"flags":{"unibus-transport":{"enabled":true,"issue":"x"},"off":{"enabled":false}}}`
	if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
		t.Fatalf("write flags: %v", err)
	}

	on, err := FlagEnabled(path, "unibus-transport")
	if err != nil {
		t.Fatalf("FlagEnabled: %v", err)
	}
	if !on {
		t.Errorf("expected unibus-transport enabled")
	}

	off, err := FlagEnabled(path, "off")
	if err != nil {
		t.Fatalf("FlagEnabled off: %v", err)
	}
	if off {
		t.Errorf("expected off flag disabled")
	}

	// Missing flag and missing file both read as false (fail-safe to Matrix).
	if missing, err := FlagEnabled(path, "does-not-exist"); err != nil || missing {
		t.Errorf("missing flag should be (false, nil), got (%v, %v)", missing, err)
	}
	if absent, err := FlagEnabled(filepath.Join(dir, "nope.json"), "x"); err != nil || absent {
		t.Errorf("absent file should be (false, nil), got (%v, %v)", absent, err)
	}
}
