|
| 1 | +import { describe, it, expect, beforeEach } from '@jest/globals'; |
| 2 | +import { z } from 'zod'; |
| 3 | +import { MCPPrompt } from '../../src/prompts/BasePrompt.js'; |
| 4 | +import { MCPResource } from '../../src/resources/BaseResource.js'; |
| 5 | + |
| 6 | +describe('Completions', () => { |
| 7 | + describe('Prompt Completions', () => { |
| 8 | + class TestPrompt extends MCPPrompt<{ language: string }> { |
| 9 | + name = 'test_prompt'; |
| 10 | + description = 'Test prompt with completion'; |
| 11 | + schema = { |
| 12 | + language: { |
| 13 | + type: z.string(), |
| 14 | + description: 'Programming language', |
| 15 | + required: true as const, |
| 16 | + }, |
| 17 | + }; |
| 18 | + |
| 19 | + async complete(argumentName: string, value: string) { |
| 20 | + if (argumentName === 'language') { |
| 21 | + const languages = ['python', 'typescript', 'rust', 'go']; |
| 22 | + const matches = languages.filter((l) => l.startsWith(value.toLowerCase())); |
| 23 | + return { values: matches, total: matches.length, hasMore: false }; |
| 24 | + } |
| 25 | + return { values: [] }; |
| 26 | + } |
| 27 | + |
| 28 | + async generateMessages(args: { language: string }) { |
| 29 | + return [ |
| 30 | + { |
| 31 | + role: 'user' as const, |
| 32 | + content: { type: 'text' as const, text: `Review ${args.language} code` }, |
| 33 | + }, |
| 34 | + ]; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + let prompt: TestPrompt; |
| 39 | + beforeEach(() => { |
| 40 | + prompt = new TestPrompt(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return matching completions', async () => { |
| 44 | + const result = await prompt.complete('language', 'py'); |
| 45 | + expect(result.values).toEqual(['python']); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return empty for no matches', async () => { |
| 49 | + const result = await prompt.complete('language', 'xyz'); |
| 50 | + expect(result.values).toEqual([]); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return all values for empty input', async () => { |
| 54 | + const result = await prompt.complete('language', ''); |
| 55 | + expect(result.values).toHaveLength(4); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should return empty for unknown argument', async () => { |
| 59 | + const result = await prompt.complete('unknown', 'test'); |
| 60 | + expect(result.values).toEqual([]); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should include total and hasMore', async () => { |
| 64 | + const result = await prompt.complete('language', 't'); |
| 65 | + expect(result).toHaveProperty('total'); |
| 66 | + expect(result).toHaveProperty('hasMore'); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('Default Prompt Completion', () => { |
| 71 | + class BasicPrompt extends MCPPrompt<{ name: string }> { |
| 72 | + name = 'basic'; |
| 73 | + description = 'Basic prompt without custom completion'; |
| 74 | + schema = { |
| 75 | + name: { |
| 76 | + type: z.string(), |
| 77 | + description: 'Name', |
| 78 | + required: true as const, |
| 79 | + }, |
| 80 | + }; |
| 81 | + async generateMessages(args: { name: string }) { |
| 82 | + return [ |
| 83 | + { |
| 84 | + role: 'user' as const, |
| 85 | + content: { type: 'text' as const, text: args.name }, |
| 86 | + }, |
| 87 | + ]; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + it('should return empty values by default', async () => { |
| 92 | + const prompt = new BasicPrompt(); |
| 93 | + const result = await prompt.complete('name', 'test'); |
| 94 | + expect(result.values).toEqual([]); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('Resource Templates', () => { |
| 99 | + class TemplateResource extends MCPResource { |
| 100 | + uri = 'config://app/theme'; |
| 101 | + name = 'App Config'; |
| 102 | + description = 'Application configuration'; |
| 103 | + mimeType = 'application/json'; |
| 104 | + |
| 105 | + protected template = { |
| 106 | + uriTemplate: 'config://app/{section}', |
| 107 | + description: 'Access config by section', |
| 108 | + }; |
| 109 | + |
| 110 | + async complete(argumentName: string, value: string) { |
| 111 | + if (argumentName === 'section') { |
| 112 | + const sections = ['theme', 'network', 'auth']; |
| 113 | + return { |
| 114 | + values: sections.filter((s) => s.startsWith(value)), |
| 115 | + total: sections.length, |
| 116 | + }; |
| 117 | + } |
| 118 | + return { values: [] }; |
| 119 | + } |
| 120 | + |
| 121 | + async read() { |
| 122 | + return [{ uri: this.uri, text: '{}', mimeType: this.mimeType }]; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + let resource: TemplateResource; |
| 127 | + beforeEach(() => { |
| 128 | + resource = new TemplateResource(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should expose template definition', () => { |
| 132 | + const tmpl = resource.templateDefinition; |
| 133 | + expect(tmpl).toBeDefined(); |
| 134 | + expect(tmpl!.uriTemplate).toBe('config://app/{section}'); |
| 135 | + expect(tmpl!.name).toBe('App Config'); |
| 136 | + expect(tmpl!.mimeType).toBe('application/json'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should use template description over resource description', () => { |
| 140 | + const tmpl = resource.templateDefinition; |
| 141 | + expect(tmpl!.description).toBe('Access config by section'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should fall back to resource description when template has none', () => { |
| 145 | + class FallbackResource extends MCPResource { |
| 146 | + uri = 'fallback://test'; |
| 147 | + name = 'Fallback'; |
| 148 | + description = 'Resource description'; |
| 149 | + protected template = { uriTemplate: 'fallback://{id}' }; |
| 150 | + async read() { |
| 151 | + return [{ uri: this.uri, text: 'data' }]; |
| 152 | + } |
| 153 | + } |
| 154 | + const r = new FallbackResource(); |
| 155 | + expect(r.templateDefinition!.description).toBe('Resource description'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should return undefined templateDefinition when no template', () => { |
| 159 | + class PlainResource extends MCPResource { |
| 160 | + uri = 'plain://test'; |
| 161 | + name = 'Plain'; |
| 162 | + async read() { |
| 163 | + return [{ uri: this.uri, text: 'data' }]; |
| 164 | + } |
| 165 | + } |
| 166 | + const plain = new PlainResource(); |
| 167 | + expect(plain.templateDefinition).toBeUndefined(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should provide completions for template arguments', async () => { |
| 171 | + const result = await resource.complete('section', 'th'); |
| 172 | + expect(result.values).toEqual(['theme']); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should return empty for unknown template argument', async () => { |
| 176 | + const result = await resource.complete('unknown', 'test'); |
| 177 | + expect(result.values).toEqual([]); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + describe('Default Resource Completion', () => { |
| 182 | + class BasicResource extends MCPResource { |
| 183 | + uri = 'basic://test'; |
| 184 | + name = 'Basic'; |
| 185 | + async read() { |
| 186 | + return [{ uri: this.uri, text: 'data' }]; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + it('should return empty values by default', async () => { |
| 191 | + const resource = new BasicResource(); |
| 192 | + const result = await resource.complete('arg', 'val'); |
| 193 | + expect(result.values).toEqual([]); |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments