|
| 1 | +import { describe, expect, expectTypeOf, it } from 'vitest'; |
| 2 | + |
| 3 | +import type { SpecTypeName, SpecTypes } from '../../src/types/specTypeSchema.js'; |
| 4 | +import { isSpecType, specTypeSchema } from '../../src/types/specTypeSchema.js'; |
| 5 | +import type { CallToolResult, ContentBlock, Implementation, JSONRPCRequest, Tool } from '../../src/types/types.js'; |
| 6 | + |
| 7 | +describe('specTypeSchema()', () => { |
| 8 | + it('returns a StandardSchemaV1 validator that accepts valid values', () => { |
| 9 | + const schema = specTypeSchema('Implementation'); |
| 10 | + const result = schema['~standard'].validate({ name: 'x', version: '1.0.0' }); |
| 11 | + expect((result as { issues?: unknown }).issues).toBeUndefined(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('returns a validator that rejects invalid values with issues', () => { |
| 15 | + const schema = specTypeSchema('Implementation'); |
| 16 | + const result = schema['~standard'].validate({ name: 'x' }); |
| 17 | + expect((result as { issues?: readonly unknown[] }).issues?.length).toBeGreaterThan(0); |
| 18 | + }); |
| 19 | + |
| 20 | + it('throws TypeError for an unknown name', () => { |
| 21 | + expect(() => specTypeSchema('NotASpecType' as SpecTypeName)).toThrow(TypeError); |
| 22 | + }); |
| 23 | + |
| 24 | + it('covers JSON-RPC envelope types', () => { |
| 25 | + const ok = specTypeSchema('JSONRPCRequest')['~standard'].validate({ jsonrpc: '2.0', id: 1, method: 'ping' }); |
| 26 | + expect((ok as { issues?: unknown }).issues).toBeUndefined(); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe('isSpecType()', () => { |
| 31 | + it('CallToolResult — accepts valid, rejects invalid/null/primitive', () => { |
| 32 | + expect(isSpecType('CallToolResult', { content: [{ type: 'text', text: 'hi' }] })).toBe(true); |
| 33 | + expect(isSpecType('CallToolResult', { content: 'not-an-array' })).toBe(false); |
| 34 | + expect(isSpecType('CallToolResult', null)).toBe(false); |
| 35 | + expect(isSpecType('CallToolResult', 'string')).toBe(false); |
| 36 | + }); |
| 37 | + |
| 38 | + it('ContentBlock — accepts text block, rejects wrong shape', () => { |
| 39 | + expect(isSpecType('ContentBlock', { type: 'text', text: 'hi' })).toBe(true); |
| 40 | + expect(isSpecType('ContentBlock', { type: 'text' })).toBe(false); |
| 41 | + expect(isSpecType('ContentBlock', {})).toBe(false); |
| 42 | + }); |
| 43 | + |
| 44 | + it('Tool — accepts valid, rejects missing inputSchema', () => { |
| 45 | + expect(isSpecType('Tool', { name: 'echo', inputSchema: { type: 'object' } })).toBe(true); |
| 46 | + expect(isSpecType('Tool', { name: 'echo' })).toBe(false); |
| 47 | + }); |
| 48 | + |
| 49 | + it('returns false (not throw) for unknown name', () => { |
| 50 | + expect(isSpecType('NotASpecType' as SpecTypeName, {})).toBe(false); |
| 51 | + }); |
| 52 | + |
| 53 | + it('narrows the value type', () => { |
| 54 | + const v: unknown = { name: 'x', version: '1.0.0' }; |
| 55 | + if (isSpecType('Implementation', v)) { |
| 56 | + expectTypeOf(v).toEqualTypeOf<SpecTypes['Implementation']>(); |
| 57 | + } |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe('SpecTypeName / SpecTypes (type-level)', () => { |
| 62 | + it('SpecTypeName includes representative names', () => { |
| 63 | + expectTypeOf<'CallToolResult'>().toMatchTypeOf<SpecTypeName>(); |
| 64 | + expectTypeOf<'ContentBlock'>().toMatchTypeOf<SpecTypeName>(); |
| 65 | + expectTypeOf<'Tool'>().toMatchTypeOf<SpecTypeName>(); |
| 66 | + expectTypeOf<'Implementation'>().toMatchTypeOf<SpecTypeName>(); |
| 67 | + expectTypeOf<'JSONRPCRequest'>().toMatchTypeOf<SpecTypeName>(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('SpecTypes[K] matches the named export type', () => { |
| 71 | + expectTypeOf<SpecTypes['CallToolResult']>().toEqualTypeOf<CallToolResult>(); |
| 72 | + expectTypeOf<SpecTypes['ContentBlock']>().toEqualTypeOf<ContentBlock>(); |
| 73 | + expectTypeOf<SpecTypes['Tool']>().toEqualTypeOf<Tool>(); |
| 74 | + expectTypeOf<SpecTypes['Implementation']>().toEqualTypeOf<Implementation>(); |
| 75 | + expectTypeOf<SpecTypes['JSONRPCRequest']>().toEqualTypeOf<JSONRPCRequest>(); |
| 76 | + }); |
| 77 | +}); |
0 commit comments