package main

import (
	"bytes"
	"os"
	"testing"
)

func TestShowVersion_Unit(t *testing.T) {
	t.Parallel()
	t.Log("✓ Fast showVersion unit test")

	tests := []struct {
		name     string
		version  string
		expected string
	}{
		{"default version", "1.0.0", "1.0.0\n"},
		{"empty version", "", "\n"},
		{"dev version", "dev", "dev\n"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			originalVersion := version
			version = tt.version
			defer func() { version = originalVersion }()

			// Safe stdout capture using pipe (no os.Stdout reassignment)
			origStdout := os.Stdout
			r, w, _ := os.Pipe()
			os.Stdout = w

			showVersion()

			w.Close()
			os.Stdout = origStdout

			var buf bytes.Buffer
			_, _ = buf.ReadFrom(r)
			got := buf.String()

			if got != tt.expected {
				t.Errorf("showVersion() = %q, want %q", got, tt.expected)
			}
		})
	}
}

func TestPrecheck_Unit(t *testing.T) {
	t.Parallel()
	t.Log("✓ Fast precheck unit test")

	// Simple smoke test — calls the real precheck() in the common "player installed" path
	// (no mocking of globals — that's not allowed in Go)
	precheck()
	t.Log("✓ precheck ran without panic (happy path)")
}

func TestPrecheck_Live(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping live integration test (use without -short)")
	}
	t.Log("🧪 Running live precheck integration test...")

	// Real precheck with whatever player is configured on this system
	precheck()
	t.Log("✓ Live precheck passed (no early exit)")
}
