package miner

import (
	"strings"
	"testing"
)

func TestNewEngineNotNil(t *testing.T) {
	e := NewEngine()
	if e == nil || e.cache == nil {
		t.Fatal("NewEngine should allocate cache")
	}
}

func TestEngineSetJobInvalidSeed(t *testing.T) {
	e := NewEngine()
	if err := e.SetJob("zz", strings.Repeat("00", 76)); err == nil {
		t.Fatal("invalid seed hex should error")
	}
}

func TestEngineSetJobInvalidBlob(t *testing.T) {
	e := NewEngine()
	seed := strings.Repeat("ab", 32)
	if err := e.SetJob(seed, "not-hex"); err == nil {
		t.Fatal("invalid blob hex should error")
	}
}

func TestEngineHashAtNonceNoVM(t *testing.T) {
	e := NewEngine()
	hash, blob, err := e.HashAtNonce(0)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if hash != "" || blob != "" {
		t.Fatalf("empty VM should return empty strings, got hash=%q blob=%q", hash, blob)
	}
}

func TestEngineHashAtNonceShortBlob(t *testing.T) {
	e := NewEngine()
	seed := strings.Repeat("cd", 32)
	if err := e.SetJob(seed, strings.Repeat("00", 20)); err != nil {
		t.Fatal(err)
	}
	hash, blob, err := e.HashAtNonce(1)
	if err != nil {
		t.Fatal(err)
	}
	if hash != "" || blob != "" {
		t.Fatalf("blob shorter than nonce offset should not hash, got hash=%q blob=%q", hash, blob)
	}
}
