|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { buildToolsNameFormatCheck, validateToolNameFormat } from './tools.js'; |
| 3 | + |
| 4 | +describe('validateToolNameFormat', () => { |
| 5 | + it('accepts a typical snake_case name', () => { |
| 6 | + expect(validateToolNameFormat('test_simple_text')).toBeNull(); |
| 7 | + }); |
| 8 | + |
| 9 | + it('accepts all allowed character classes', () => { |
| 10 | + expect(validateToolNameFormat('Aa0_.-/')).toBeNull(); |
| 11 | + }); |
| 12 | + |
| 13 | + it('accepts a single-character name (lower length boundary)', () => { |
| 14 | + expect(validateToolNameFormat('a')).toBeNull(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('accepts a 64-character name (upper length boundary)', () => { |
| 18 | + expect(validateToolNameFormat('a'.repeat(64))).toBeNull(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('rejects an empty name', () => { |
| 22 | + expect(validateToolNameFormat('')).toMatch(/length 0 is outside/); |
| 23 | + }); |
| 24 | + |
| 25 | + it('rejects a 65-character name', () => { |
| 26 | + expect(validateToolNameFormat('a'.repeat(65))).toMatch( |
| 27 | + /length 65 is outside/ |
| 28 | + ); |
| 29 | + }); |
| 30 | + |
| 31 | + it.each([ |
| 32 | + ['space', 'bad name'], |
| 33 | + ['colon', 'bad:name'], |
| 34 | + ['at sign', 'bad@name'], |
| 35 | + ['unicode', 'bad\u00e9name'], |
| 36 | + ['backslash', 'bad\\name'], |
| 37 | + ['plus', 'bad+name'] |
| 38 | + ])('rejects a name with a disallowed character (%s)', (_label, name) => { |
| 39 | + expect(validateToolNameFormat(name)).toMatch(/contains characters outside/); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('buildToolsNameFormatCheck', () => { |
| 44 | + it('returns INFO when tools is undefined', () => { |
| 45 | + const check = buildToolsNameFormatCheck(undefined); |
| 46 | + expect(check.status).toBe('INFO'); |
| 47 | + expect(check.id).toBe('tools-name-format'); |
| 48 | + expect(check.details).toEqual({ toolCount: 0 }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns INFO when tools is an empty array', () => { |
| 52 | + const check = buildToolsNameFormatCheck([]); |
| 53 | + expect(check.status).toBe('INFO'); |
| 54 | + expect(check.errorMessage).toBe('No tools advertised; nothing to validate'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns SUCCESS when all tool names are valid', () => { |
| 58 | + const check = buildToolsNameFormatCheck([ |
| 59 | + { name: 'test_simple_text' }, |
| 60 | + { name: 'namespace/tool-v1.2' } |
| 61 | + ]); |
| 62 | + expect(check.status).toBe('SUCCESS'); |
| 63 | + expect(check.errorMessage).toBeUndefined(); |
| 64 | + expect(check.details).toMatchObject({ |
| 65 | + toolCount: 2, |
| 66 | + results: { |
| 67 | + test_simple_text: 'valid', |
| 68 | + 'namespace/tool-v1.2': 'valid' |
| 69 | + } |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns FAILURE with per-tool details when some names are invalid', () => { |
| 74 | + const check = buildToolsNameFormatCheck([ |
| 75 | + { name: 'good_tool' }, |
| 76 | + { name: 'bad name with spaces' }, |
| 77 | + { name: 'a'.repeat(65) } |
| 78 | + ]); |
| 79 | + |
| 80 | + expect(check.status).toBe('FAILURE'); |
| 81 | + expect(check.errorMessage).toContain( |
| 82 | + '2 tool name(s) violate SEP-986 format' |
| 83 | + ); |
| 84 | + |
| 85 | + const results = (check.details as { results: Record<string, string> }) |
| 86 | + .results; |
| 87 | + expect(results['good_tool']).toBe('valid'); |
| 88 | + expect(results['bad name with spaces']).toMatch(/^invalid: /); |
| 89 | + expect(results['a'.repeat(65)]).toMatch(/^invalid: length 65/); |
| 90 | + }); |
| 91 | + |
| 92 | + it('flags a tool whose name is not a string', () => { |
| 93 | + const check = buildToolsNameFormatCheck([{ name: 123 as unknown }]); |
| 94 | + expect(check.status).toBe('FAILURE'); |
| 95 | + const results = (check.details as { results: Record<string, string> }) |
| 96 | + .results; |
| 97 | + expect(results['<tool[0] missing name>']).toBe( |
| 98 | + 'invalid: name is not a string' |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it('includes both MCP-Tools-List and SEP-986 spec references', () => { |
| 103 | + const check = buildToolsNameFormatCheck([{ name: 'ok' }]); |
| 104 | + const ids = check.specReferences?.map((r) => r.id); |
| 105 | + expect(ids).toEqual(['MCP-Tools-List', 'SEP-986']); |
| 106 | + }); |
| 107 | +}); |
0 commit comments