package ugh_test

import (
	"testing"
	"time"

	"charm.land/huh/v2"
	"git.sr.ht/~timofurrer/ugh"
)

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

	c := ugh.New(t)

	if c == nil {
		t.Fatal("New returned nil")
	}
}

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

	c := ugh.New(t,
		ugh.WithSize(testWidth, testHeight),
		ugh.WithTimeout(5*time.Second),
	)

	if c == nil {
		t.Fatal("New returned nil")
	}
}

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

	c := ugh.New(t)

	if s := c.Screen(); s != "" {
		t.Fatalf("expected empty screen before Run, got %q", s)
	}
}

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

	var confirmed bool

	c := ugh.New(t, ugh.WithTimeout(5*time.Second))
	c.Expect(ugh.Confirm("OK?")).
		Do(ugh.Affirm)

	runTestForm(t, c,
		huh.NewGroup(
			huh.NewConfirm().Title("OK?").Value(&confirmed),
		),
	)

	if !confirmed {
		t.Fatal("expected confirmed")
	}
}

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

	_ = ugh.New(t)
}
