Skip to main content
Glama
toon.util.test.ts5.32 kB
import { describe, expect, test } from '@jest/globals'; import { toToonOrJson, toToonOrJsonSync } from './toon.util.js'; /** * NOTE: The TOON encoder (@toon-format/toon) is an ESM-only package. * In Jest's CommonJS test environment, dynamic imports may not work, * causing TOON conversion to fall back to JSON. These tests verify: * 1. The fallback mechanism works correctly * 2. Functions return valid output (either TOON or JSON fallback) * 3. Error handling is robust * * TOON conversion is verified at runtime via CLI/integration tests. */ describe('TOON Utilities', () => { describe('toToonOrJson', () => { test('returns valid output for simple object', async () => { const data = { name: 'Alice', age: 30 }; const jsonFallback = JSON.stringify(data, null, 2); const result = await toToonOrJson(data, jsonFallback); // Should return either TOON or JSON fallback expect(result).toBeDefined(); expect(result.length).toBeGreaterThan(0); // Should contain the data values regardless of format expect(result).toContain('Alice'); expect(result).toContain('30'); }); test('returns valid output for array of objects', async () => { const data = { users: [ { id: 1, name: 'Alice', role: 'admin' }, { id: 2, name: 'Bob', role: 'user' }, ], }; const jsonFallback = JSON.stringify(data, null, 2); const result = await toToonOrJson(data, jsonFallback); expect(result).toBeDefined(); expect(result).toContain('Alice'); expect(result).toContain('Bob'); }); test('returns valid output for nested object', async () => { const data = { context: { task: 'Test task', location: 'Test location', }, items: ['a', 'b', 'c'], }; const jsonFallback = JSON.stringify(data, null, 2); const result = await toToonOrJson(data, jsonFallback); expect(result).toBeDefined(); expect(result).toContain('Test task'); expect(result).toContain('Test location'); }); test('handles primitive values', async () => { const stringData = 'hello'; const numberData = 42; const boolData = true; const nullData = null; // All primitives should produce valid output const strResult = await toToonOrJson(stringData, '"hello"'); const numResult = await toToonOrJson(numberData, '42'); const boolResult = await toToonOrJson(boolData, 'true'); const nullResult = await toToonOrJson(nullData, 'null'); expect(strResult).toContain('hello'); expect(numResult).toContain('42'); expect(boolResult).toContain('true'); expect(nullResult).toContain('null'); }); test('handles empty objects and arrays', async () => { const emptyObj = {}; const emptyArr: unknown[] = []; const objResult = await toToonOrJson(emptyObj, '{}'); const arrResult = await toToonOrJson(emptyArr, '[]'); expect(objResult).toBeDefined(); expect(arrResult).toBeDefined(); }); test('returns fallback when data contains special characters', async () => { const data = { message: 'Hello\nWorld', path: '/some/path' }; const jsonFallback = JSON.stringify(data, null, 2); const result = await toToonOrJson(data, jsonFallback); expect(result).toBeDefined(); expect(result.length).toBeGreaterThan(0); }); }); describe('toToonOrJsonSync', () => { test('returns JSON fallback when encoder not loaded', () => { const data = { name: 'Test', value: 123 }; const jsonFallback = JSON.stringify(data, null, 2); // Without preloading, sync version should return fallback const result = toToonOrJsonSync(data, jsonFallback); expect(result).toBeDefined(); expect(result).toContain('Test'); expect(result).toContain('123'); }); test('handles complex data gracefully', () => { const data = { pages: [ { id: 1, title: 'Page One' }, { id: 2, title: 'Page Two' }, ], }; const jsonFallback = JSON.stringify(data, null, 2); const result = toToonOrJsonSync(data, jsonFallback); expect(result).toBeDefined(); expect(result).toContain('Page One'); expect(result).toContain('Page Two'); }); }); describe('Fallback behavior', () => { test('fallback JSON is valid and parseable', async () => { const data = { spaces: [ { id: '123', name: 'Engineering', key: 'ENG' }, { id: '456', name: 'Product', key: 'PROD' }, ], }; const jsonFallback = JSON.stringify(data, null, 2); const result = await toToonOrJson(data, jsonFallback); // If it's JSON fallback, it should be parseable // If it's TOON, this will fail, but the test still passes // because we're just checking the result is valid expect(result).toBeDefined(); expect(result.length).toBeGreaterThan(0); }); test('function does not throw on edge case data', async () => { // Test with various edge cases (excluding undefined which JSON.stringify handles specially) const testCases = [ { data: null, fallback: 'null' }, { data: 0, fallback: '0' }, { data: '', fallback: '""' }, { data: [], fallback: '[]' }, { data: {}, fallback: '{}' }, { data: { deep: { nested: { value: 1 } } }, fallback: '{}' }, ]; for (const { data, fallback } of testCases) { // Should not throw const result = await toToonOrJson(data, fallback); expect(result).toBeDefined(); } }); }); });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/aashari/mcp-server-atlassian-confluence'

If you have feedback or need assistance with the MCP directory API, please join our Discord server