|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { formatMcpError, stringifyForMcp } from '../stringify' |
| 3 | + |
| 4 | +describe('stringifyForMcp', () => { |
| 5 | + it('returns "undefined" sentinel for undefined', () => { |
| 6 | + expect(stringifyForMcp(undefined)).toBe('undefined') |
| 7 | + }) |
| 8 | + |
| 9 | + it('passes strings through unchanged', () => { |
| 10 | + expect(stringifyForMcp('hello')).toBe('hello') |
| 11 | + }) |
| 12 | + |
| 13 | + it('serializes plain JSON-safe objects with indentation', () => { |
| 14 | + expect(stringifyForMcp({ a: 1, b: 'two' })).toBe('{\n "a": 1,\n "b": "two"\n}') |
| 15 | + }) |
| 16 | + |
| 17 | + it('coerces BigInt to a trailing-n string', () => { |
| 18 | + expect(JSON.parse(stringifyForMcp({ count: 42n }))).toEqual({ count: '42n' }) |
| 19 | + }) |
| 20 | + |
| 21 | + it('coerces Date to ISO string via toJSON', () => { |
| 22 | + expect(JSON.parse(stringifyForMcp({ when: new Date(0) }))).toEqual({ |
| 23 | + when: '1970-01-01T00:00:00.000Z', |
| 24 | + }) |
| 25 | + }) |
| 26 | + |
| 27 | + it('coerces Map to a tagged entries object', () => { |
| 28 | + const value = new Map<string, number>([['a', 1], ['b', 2]]) |
| 29 | + expect(JSON.parse(stringifyForMcp(value))).toEqual({ |
| 30 | + __type: 'Map', |
| 31 | + entries: [['a', 1], ['b', 2]], |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + it('coerces Set to a tagged entries object', () => { |
| 36 | + const value = new Set(['x', 'y']) |
| 37 | + expect(JSON.parse(stringifyForMcp(value))).toEqual({ |
| 38 | + __type: 'Set', |
| 39 | + entries: ['x', 'y'], |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + it('serializes Error with name, message, stack, and cause', () => { |
| 44 | + const inner = new Error('inner') |
| 45 | + const outer = new TypeError('boom', { cause: inner }) |
| 46 | + const parsed = JSON.parse(stringifyForMcp(outer)) |
| 47 | + expect(parsed.name).toBe('TypeError') |
| 48 | + expect(parsed.message).toBe('boom') |
| 49 | + expect(typeof parsed.stack).toBe('string') |
| 50 | + expect(parsed.cause.name).toBe('Error') |
| 51 | + expect(parsed.cause.message).toBe('inner') |
| 52 | + }) |
| 53 | + |
| 54 | + it('coerces Function to a readable token', () => { |
| 55 | + function namedFn() {} |
| 56 | + expect(JSON.parse(stringifyForMcp({ fn: namedFn }))).toEqual({ |
| 57 | + fn: '[Function: namedFn]', |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + it('coerces anonymous functions', () => { |
| 62 | + expect(JSON.parse(stringifyForMcp({ fn: () => {} }))).toEqual({ |
| 63 | + fn: '[Function: fn]', |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + it('coerces Symbol to its description', () => { |
| 68 | + expect(JSON.parse(stringifyForMcp({ s: Symbol('hi') }))).toEqual({ |
| 69 | + s: 'Symbol(hi)', |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + it('replaces circular refs with [Circular]', () => { |
| 74 | + const obj: Record<string, unknown> = { name: 'root' } |
| 75 | + obj.self = obj |
| 76 | + const parsed = JSON.parse(stringifyForMcp(obj)) |
| 77 | + expect(parsed.name).toBe('root') |
| 78 | + expect(parsed.self).toBe('[Circular]') |
| 79 | + }) |
| 80 | + |
| 81 | + it('handles a mixed payload end-to-end', () => { |
| 82 | + const value = { |
| 83 | + count: 42n, |
| 84 | + when: new Date(0), |
| 85 | + tags: new Set(['a', 'b']), |
| 86 | + } |
| 87 | + const text = stringifyForMcp(value) |
| 88 | + expect(text).toContain('"42n"') |
| 89 | + expect(text).toContain('1970-01-01T00:00:00.000Z') |
| 90 | + expect(text).toContain('"__type": "Set"') |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +describe('formatMcpError', () => { |
| 95 | + it('returns String(value) for non-Error throws', () => { |
| 96 | + expect(formatMcpError('boom')).toBe('boom') |
| 97 | + expect(formatMcpError(42)).toBe('42') |
| 98 | + }) |
| 99 | + |
| 100 | + it('formats an Error as "name: message"', () => { |
| 101 | + expect(formatMcpError(new TypeError('bad'))).toBe('TypeError: bad') |
| 102 | + }) |
| 103 | + |
| 104 | + it('appends cause.message for Error causes', () => { |
| 105 | + const err = new Error('outer', { cause: new Error('inner') }) |
| 106 | + expect(formatMcpError(err)).toBe('Error: outer (cause: inner)') |
| 107 | + }) |
| 108 | + |
| 109 | + it('appends String(cause) for non-Error causes', () => { |
| 110 | + const err = new Error('outer', { cause: 'bad input' }) |
| 111 | + expect(formatMcpError(err)).toBe('Error: outer (cause: bad input)') |
| 112 | + }) |
| 113 | +}) |
0 commit comments