File size: 10,132 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect } from 'vitest';
import {
  A2AAgentError,
  AgentCardNotFoundError,
  AgentCardAuthError,
  AgentAuthConfigMissingError,
  AgentConnectionError,
  classifyAgentError,
} from './a2a-errors.js';

describe('A2A Error Types', () => {
  describe('A2AAgentError', () => {
    it('should set name, agentName, and userMessage', () => {
      const error = new A2AAgentError('my-agent', 'internal msg', 'user msg');
      expect(error.name).toBe('A2AAgentError');
      expect(error.agentName).toBe('my-agent');
      expect(error.message).toBe('internal msg');
      expect(error.userMessage).toBe('user msg');
    });
  });

  describe('AgentCardNotFoundError', () => {
    it('should produce a user-friendly 404 message', () => {
      const error = new AgentCardNotFoundError(
        'my-agent',
        'https://example.com/card',
      );
      expect(error.name).toBe('AgentCardNotFoundError');
      expect(error.agentName).toBe('my-agent');
      expect(error.userMessage).toContain('404');
      expect(error.userMessage).toContain('https://example.com/card');
      expect(error.userMessage).toContain('agent_card_url');
    });
  });

  describe('AgentCardAuthError', () => {
    it('should produce a user-friendly 401 message', () => {
      const error = new AgentCardAuthError(
        'secure-agent',
        'https://example.com/card',
        401,
      );
      expect(error.name).toBe('AgentCardAuthError');
      expect(error.statusCode).toBe(401);
      expect(error.userMessage).toContain('401');
      expect(error.userMessage).toContain('Unauthorized');
      expect(error.userMessage).toContain('"auth" configuration');
    });

    it('should produce a user-friendly 403 message', () => {
      const error = new AgentCardAuthError(
        'secure-agent',
        'https://example.com/card',
        403,
      );
      expect(error.statusCode).toBe(403);
      expect(error.userMessage).toContain('403');
      expect(error.userMessage).toContain('Forbidden');
    });
  });

  describe('AgentAuthConfigMissingError', () => {
    it('should list missing config fields', () => {
      const error = new AgentAuthConfigMissingError(
        'api-agent',
        'API Key (x-api-key): Send x-api-key in header',
        [
          'Authentication is required but not configured',
          "Scheme 'api_key' requires apiKey authentication",
        ],
      );
      expect(error.name).toBe('AgentAuthConfigMissingError');
      expect(error.requiredAuth).toContain('API Key');
      expect(error.missingFields).toHaveLength(2);
      expect(error.userMessage).toContain('API Key');
      expect(error.userMessage).toContain('no auth is configured');
      expect(error.userMessage).toContain('Missing:');
    });
  });

  describe('AgentConnectionError', () => {
    it('should wrap the original error cause', () => {
      const cause = new Error('ECONNREFUSED');
      const error = new AgentConnectionError(
        'my-agent',
        'https://example.com/card',
        cause,
      );
      expect(error.name).toBe('AgentConnectionError');
      expect(error.userMessage).toContain('ECONNREFUSED');
      expect(error.userMessage).toContain('https://example.com/card');
    });

    it('should handle non-Error causes', () => {
      const error = new AgentConnectionError(
        'my-agent',
        'https://example.com/card',
        'raw string error',
      );
      expect(error.userMessage).toContain('raw string error');
    });
  });

  describe('classifyAgentError', () => {
    it('should classify a 404 error message', () => {
      const raw = new Error('HTTP 404: Not Found');
      const result = classifyAgentError(
        'agent-a',
        'https://example.com/card',
        raw,
      );
      expect(result).toBeInstanceOf(AgentCardNotFoundError);
      expect(result.agentName).toBe('agent-a');
    });

    it('should classify a "not found" error message (case-insensitive)', () => {
      const raw = new Error('Agent card not found at the given URL');
      const result = classifyAgentError(
        'agent-a',
        'https://example.com/card',
        raw,
      );
      expect(result).toBeInstanceOf(AgentCardNotFoundError);
    });

    it('should classify a 401 error message', () => {
      const raw = new Error('Request failed with status 401');
      const result = classifyAgentError(
        'agent-b',
        'https://example.com/card',
        raw,
      );
      expect(result).toBeInstanceOf(AgentCardAuthError);
      expect((result as AgentCardAuthError).statusCode).toBe(401);
    });

    it('should classify an "unauthorized" error message', () => {
      const raw = new Error('Unauthorized access to agent card');
      const result = classifyAgentError(
        'agent-b',
        'https://example.com/card',
        raw,
      );
      expect(result).toBeInstanceOf(AgentCardAuthError);
    });

    it('should classify a 403 error message', () => {
      const raw = new Error('HTTP 403 Forbidden');
      const result = classifyAgentError(
        'agent-c',
        'https://example.com/card',
        raw,
      );
      expect(result).toBeInstanceOf(AgentCardAuthError);
      expect((result as AgentCardAuthError).statusCode).toBe(403);
    });

    it('should fall back to AgentConnectionError for unknown errors', () => {
      const raw = new Error('Something completely unexpected');
      const result = classifyAgentError(
        'agent-d',
        'https://example.com/card',
        raw,
      );
      expect(result).toBeInstanceOf(AgentConnectionError);
    });

    it('should classify ECONNREFUSED as AgentConnectionError', () => {
      const raw = new Error('ECONNREFUSED 127.0.0.1:8080');
      const result = classifyAgentError(
        'agent-d',
        'https://example.com/card',
        raw,
      );
      expect(result).toBeInstanceOf(AgentConnectionError);
    });

    it('should handle non-Error values', () => {
      const result = classifyAgentError(
        'agent-e',
        'https://example.com/card',
        'some string error',
      );
      expect(result).toBeInstanceOf(AgentConnectionError);
    });

    describe('cause chain inspection', () => {
      it('should detect 404 in a nested cause', () => {
        const inner = new Error('HTTP 404 Not Found');
        const outer = new Error('fetch failed', { cause: inner });
        const result = classifyAgentError(
          'agent-nested',
          'https://example.com/card',
          outer,
        );
        expect(result).toBeInstanceOf(AgentCardNotFoundError);
      });

      it('should detect 401 in a deeply nested cause', () => {
        const innermost = new Error('Server returned 401');
        const middle = new Error('Request error', { cause: innermost });
        const outer = new Error('fetch failed', { cause: middle });
        const result = classifyAgentError(
          'agent-deep',
          'https://example.com/card',
          outer,
        );
        expect(result).toBeInstanceOf(AgentCardAuthError);
        expect((result as AgentCardAuthError).statusCode).toBe(401);
      });

      it('should detect ECONNREFUSED error code in cause chain', () => {
        const inner = Object.assign(new Error('connect failed'), {
          code: 'ECONNREFUSED',
        });
        const outer = new Error('fetch failed', { cause: inner });
        const result = classifyAgentError(
          'agent-conn',
          'https://example.com/card',
          outer,
        );
        expect(result).toBeInstanceOf(AgentConnectionError);
      });

      it('should detect status property on error objects in cause chain', () => {
        const inner = Object.assign(new Error('Bad response'), {
          status: 403,
        });
        const outer = new Error('agent card resolution failed', {
          cause: inner,
        });
        const result = classifyAgentError(
          'agent-status',
          'https://example.com/card',
          outer,
        );
        expect(result).toBeInstanceOf(AgentCardAuthError);
        expect((result as AgentCardAuthError).statusCode).toBe(403);
      });

      it('should detect status on a plain-object cause (non-Error)', () => {
        const outer = new Error('fetch failed');
        // Some HTTP libs set cause to a plain object, not an Error instance
        (outer as unknown as { cause: unknown }).cause = {
          message: 'Unauthorized',
          status: 401,
        };
        const result = classifyAgentError(
          'agent-plain-cause',
          'https://example.com/card',
          outer,
        );
        expect(result).toBeInstanceOf(AgentCardAuthError);
        expect((result as AgentCardAuthError).statusCode).toBe(401);
      });

      it('should detect statusCode on a plain-object cause (non-Error)', () => {
        const outer = new Error('fetch failed');
        (outer as unknown as { cause: unknown }).cause = {
          message: 'Forbidden',
          statusCode: 403,
        };
        const result = classifyAgentError(
          'agent-plain-cause-403',
          'https://example.com/card',
          outer,
        );
        expect(result).toBeInstanceOf(AgentCardAuthError);
        expect((result as AgentCardAuthError).statusCode).toBe(403);
      });

      it('should classify ENOTFOUND as AgentConnectionError, not 404', () => {
        // ENOTFOUND (DNS resolution failure) should NOT be misclassified
        // as a 404 despite containing "NOTFOUND" in the error code.
        const inner = Object.assign(
          new Error('getaddrinfo ENOTFOUND example.invalid'),
          {
            code: 'ENOTFOUND',
          },
        );
        const outer = new Error('fetch failed', { cause: inner });
        const result = classifyAgentError(
          'agent-dns',
          'https://example.invalid/card',
          outer,
        );
        expect(result).toBeInstanceOf(AgentConnectionError);
        expect(result).not.toBeInstanceOf(AgentCardNotFoundError);
      });
    });
  });
});