package main

import (
	"os"
	"testing"
)

// TestBuildLogger verifies that buildLogger returns a non-nil logger for each
// supported log level string and for unknown values.
func TestBuildLogger(t *testing.T) {
	for _, level := range []string{"debug", "info", "warn", "error", "unknown", ""} {
		l := buildLogger(level)
		if l == nil {
			t.Errorf("buildLogger(%q) returned nil", level)
		}
	}
}

// TestNoopKokoro verifies that the no-op Kokoro stub returns the expected
// sentinel error from GenerateAudio and nil, nil from ListVoices.
func TestNoopKokoro(t *testing.T) {
	noop := &noopKokoro{}

	_, err := noop.GenerateAudio(t.Context(), "text", "af_bella")
	if err == nil {
		t.Fatal("noopKokoro.GenerateAudio: expected error, got nil")
	}

	voices, err := noop.ListVoices(t.Context())
	if err != nil {
		t.Fatalf("noopKokoro.ListVoices: unexpected error: %v", err)
	}
	if voices != nil {
		t.Fatalf("noopKokoro.ListVoices: expected nil slice, got %v", voices)
	}
}

// TestRunStorageUnreachable verifies that run() fails fast and returns a
// descriptive error when PocketBase is unreachable.
func TestRunStorageUnreachable(t *testing.T) {
	// Point at an address nothing is listening on.
	t.Setenv("POCKETBASE_URL", "http://127.0.0.1:19999")
	// Use a fast listen address so we don't accidentally start a real server.
	t.Setenv("BACKEND_HTTP_ADDR", "127.0.0.1:0")

	err := run()
	if err == nil {
		t.Fatal("run() should have returned an error when storage is unreachable")
	}

	t.Logf("got expected error: %v", err)
}

// TestMain runs the test suite. No special setup required.
func TestMain(m *testing.M) {
	os.Exit(m.Run())
}
