package infra

import (
	"testing"
)

func TestE2ERunChecks(t *testing.T) {
	t.Run("todos los checks pasan con exit 0", func(t *testing.T) {
		zero := 0
		checks := []E2ECheck{
			{ID: "check-echo", Cmd: "echo hello", ExpectExit: &zero, ExpectStdoutContains: "hello"},
			{ID: "check-true", Cmd: "true"},
		}
		results, err := E2ERunChecks(checks, "")
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		if len(results) != 2 {
			t.Fatalf("expected 2 results, got %d", len(results))
		}
		for _, r := range results {
			if r.Status != "pass" {
				t.Errorf("check %q: expected pass, got %q (error: %s)", r.ID, r.Status, r.Error)
			}
		}
	})

	t.Run("check falla por exit code incorrecto", func(t *testing.T) {
		expectedExit := 0
		checks := []E2ECheck{
			{ID: "check-fail-exit", Cmd: "exit 1", ExpectExit: &expectedExit},
		}
		results, err := E2ERunChecks(checks, "")
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		if len(results) != 1 {
			t.Fatalf("expected 1 result, got %d", len(results))
		}
		if results[0].Status != "fail" {
			t.Errorf("expected fail, got %q", results[0].Status)
		}
		if results[0].ExitCode == 0 {
			t.Errorf("expected non-zero exit code")
		}
	})

	t.Run("check falla por stdout_contains no encontrado", func(t *testing.T) {
		checks := []E2ECheck{
			{ID: "check-stdout", Cmd: "echo hello", ExpectStdoutContains: "world"},
		}
		results, err := E2ERunChecks(checks, "")
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		if results[0].Status != "fail" {
			t.Errorf("expected fail when stdout does not contain expected string, got %q", results[0].Status)
		}
	})

	t.Run("check falla por timeout de comando", func(t *testing.T) {
		checks := []E2ECheck{
			{ID: "check-timeout", Cmd: "sleep 60", TimeoutS: 1},
		}
		results, err := E2ERunChecks(checks, "")
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		if results[0].Status != "fail" {
			t.Errorf("expected fail on timeout, got %q (error: %s)", results[0].Status, results[0].Error)
		}
	})
}
