|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect } from 'vitest'; |
| 8 | +import { getAcpErrorMessage } from './acpErrors.js'; |
| 9 | + |
| 10 | +describe('getAcpErrorMessage', () => { |
| 11 | + it('should return plain error message', () => { |
| 12 | + expect(getAcpErrorMessage(new Error('plain error'))).toBe('plain error'); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should parse simple JSON error response', () => { |
| 16 | + const json = JSON.stringify({ error: { message: 'json error' } }); |
| 17 | + expect(getAcpErrorMessage(new Error(json))).toBe('json error'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should parse double-encoded JSON error response', () => { |
| 21 | + const innerJson = JSON.stringify({ error: { message: 'nested error' } }); |
| 22 | + const outerJson = JSON.stringify({ error: { message: innerJson } }); |
| 23 | + expect(getAcpErrorMessage(new Error(outerJson))).toBe('nested error'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should parse array-style JSON error response', () => { |
| 27 | + const json = JSON.stringify([{ error: { message: 'array error' } }]); |
| 28 | + expect(getAcpErrorMessage(new Error(json))).toBe('array error'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should parse JSON with top-level message field', () => { |
| 32 | + const json = JSON.stringify({ message: 'top-level message' }); |
| 33 | + expect(getAcpErrorMessage(new Error(json))).toBe('top-level message'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should handle JSON with trailing newline', () => { |
| 37 | + const json = JSON.stringify({ error: { message: 'newline error' } }) + '\n'; |
| 38 | + expect(getAcpErrorMessage(new Error(json))).toBe('newline error'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should return original message if JSON parsing fails', () => { |
| 42 | + const invalidJson = '{ not-json }'; |
| 43 | + expect(getAcpErrorMessage(new Error(invalidJson))).toBe(invalidJson); |
| 44 | + }); |
| 45 | +}); |
0 commit comments