package tests

import (
	"errors"
	"strings"
	"testing"

	fetchErrors "github.com/jfraeys/fetch_ml/internal/errtypes"
)

func TestDataFetchErrorFormattingAndUnwrap(t *testing.T) {
	underlying := errors.New("disk failure")
	dfErr := &fetchErrors.DataFetchError{
		Dataset: "imagenet",
		JobName: "resnet",
		Err:     underlying,
	}

	msg := dfErr.Error()
	if !strings.Contains(msg, "imagenet") || !strings.Contains(msg, "resnet") {
		t.Fatalf("error message missing context: %s", msg)
	}

	if !errors.Is(dfErr, underlying) {
		t.Fatalf("expected DataFetchError to unwrap to underlying error")
	}
}

func TestTaskExecutionErrorFormattingAndUnwrap(t *testing.T) {
	underlying := errors.New("segfault")
	taskErr := &fetchErrors.TaskExecutionError{
		TaskID:  "1234567890",
		JobName: "bert",
		Phase:   "execution",
		Err:     underlying,
	}

	msg := taskErr.Error()
	if !strings.Contains(msg, "12345678") || !strings.Contains(msg, "bert") || !strings.Contains(msg, "execution") {
		t.Fatalf("error message missing context: %s", msg)
	}

	if !errors.Is(taskErr, underlying) {
		t.Fatalf("expected TaskExecutionError to unwrap to underlying error")
	}
}
