package analyzererrors_test

import (
	"errors"
	"fmt"
	"testing"

	contractevents "bit.admedia.com/scm/ad/adpilot-indexing-code-parser.com/internal/contracts/events"
	"bit.admedia.com/scm/ad/adpilot-indexing-code-parser.com/internal/service/parser/analyzererrors"
)

func TestDiagnosticsFromError(t *testing.T) {
	parseErr := &analyzererrors.ParseError{
		Diagnostics: []contractevents.ParserDiagnostic{
			{
				Code:     "syntax_error",
				Message:  "expected package",
				Severity: contractevents.DiagnosticSeverityError,
				FilePath: "main.go",
				Line:     1,
			},
		},
		Cause: fmt.Errorf("expected package"),
	}

	diagnostics := analyzererrors.DiagnosticsFromError(parseErr)
	if len(diagnostics) != 1 {
		t.Fatalf("expected 1 diagnostic, got %d", len(diagnostics))
	}

	wrapped := fmt.Errorf("parse file: %w", parseErr)
	diagnostics = analyzererrors.DiagnosticsFromError(wrapped)
	if len(diagnostics) != 1 {
		t.Fatalf("expected wrapped diagnostic, got %d", len(diagnostics))
	}

	if analyzererrors.DiagnosticsFromError(errors.New("other")) != nil {
		t.Fatal("expected nil diagnostics for unrelated error")
	}
}
