package domain

import "testing"

func TestClassifyAgentError(t *testing.T) {
	tests := []struct {
		name     string
		errMsg   string
		stderr   string
		expected WorkErrorCode
	}{
		{
			name:     "rate limit in stderr",
			errMsg:   "command failed",
			stderr:   "You've hit your limit · resets 7am (UTC)",
			expected: WorkErrorCodeRateLimited,
		},
		{
			name:     "rate limit in error message",
			errMsg:   "rate limit exceeded, try again later",
			stderr:   "",
			expected: WorkErrorCodeRateLimited,
		},
		{
			name:     "quota exceeded",
			errMsg:   "Quota exceeded for today",
			stderr:   "",
			expected: WorkErrorCodeRateLimited,
		},
		{
			name:     "auth failed - not authenticated",
			errMsg:   "not authenticated, please log in",
			stderr:   "",
			expected: WorkErrorCodeAuthFailed,
		},
		{
			name:     "auth failed - invalid api key",
			errMsg:   "Invalid API key provided",
			stderr:   "",
			expected: WorkErrorCodeAuthFailed,
		},
		{
			name:     "auth failed - claude login hint",
			errMsg:   "",
			stderr:   "Run claude login to authenticate",
			expected: WorkErrorCodeAuthFailed,
		},
		{
			name:     "context timeout",
			errMsg:   "context deadline exceeded",
			stderr:   "",
			expected: WorkErrorCodeTimeout,
		},
		{
			name:     "operation timed out",
			errMsg:   "operation timed out after 10 minutes",
			stderr:   "",
			expected: WorkErrorCodeTimeout,
		},
		{
			name:     "generic error",
			errMsg:   "something went wrong",
			stderr:   "error: file not found",
			expected: WorkErrorCodeAgentError,
		},
		{
			name:     "empty error",
			errMsg:   "",
			stderr:   "",
			expected: WorkErrorCodeAgentError,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := ClassifyAgentError(tt.errMsg, tt.stderr)
			if got != tt.expected {
				t.Errorf("ClassifyAgentError(%q, %q) = %q, want %q",
					tt.errMsg, tt.stderr, got, tt.expected)
			}
		})
	}
}

func TestWorkErrorCode_Constants(t *testing.T) {
	// Ensure constants are defined with expected values
	codes := map[WorkErrorCode]string{
		WorkErrorCodeNone:        "",
		WorkErrorCodeRateLimited: "RATE_LIMITED",
		WorkErrorCodeAuthFailed:  "AUTH_FAILED",
		WorkErrorCodeTimeout:     "TIMEOUT",
		WorkErrorCodeStaleWorker: "STALE_WORKER",
		WorkErrorCodeAgentError:  "AGENT_ERROR",
		WorkErrorCodeInvalidSpec: "INVALID_SPEC",
	}

	for code, expected := range codes {
		if string(code) != expected {
			t.Errorf("WorkErrorCode constant %q has value %q, want %q",
				expected, string(code), expected)
		}
	}
}
