|
| 1 | +import { OpenAPIV3 } from 'openapi-types'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { codegen } from './codegen'; |
| 5 | + |
| 6 | +const document = { |
| 7 | + openapi: '3.0.0', |
| 8 | + info: { title: 'Tiny API', version: '1.0.0' }, |
| 9 | + paths: { |
| 10 | + '/things': { |
| 11 | + get: { operationId: 'thingList', description: 'List things', responses: {} }, |
| 12 | + post: { operationId: 'thingCreate', description: 'Create a thing', responses: {} }, |
| 13 | + }, |
| 14 | + '/things/{thingId}': { |
| 15 | + put: { operationId: 'thingUpdate', description: 'Update a thing', responses: {} }, |
| 16 | + delete: { operationId: 'thingDelete', description: 'Delete a thing', responses: {} }, |
| 17 | + }, |
| 18 | + }, |
| 19 | +} as OpenAPIV3.Document; |
| 20 | + |
| 21 | +/** |
| 22 | + * Extracts the annotation hints for a named tool out of the generated |
| 23 | + * (prettier-formatted) source. |
| 24 | + */ |
| 25 | +const hintsFor = (output: string, toolName: string) => { |
| 26 | + const chunk = output |
| 27 | + .split('server.tool(') |
| 28 | + .find((part) => part.trimStart().startsWith(`"${toolName}"`)); |
| 29 | + expect(chunk, `generated output contains tool ${toolName}`).toBeDefined(); |
| 30 | + |
| 31 | + const hints: Record<string, boolean> = {}; |
| 32 | + for (const key of ['readOnlyHint', 'destructiveHint', 'idempotentHint', 'openWorldHint']) { |
| 33 | + const match = chunk?.match(new RegExp(`${key}:\\s*(true|false)`)); |
| 34 | + expect(match, `${toolName} carries ${key}`).toBeTruthy(); |
| 35 | + hints[key] = match?.[1] === 'true'; |
| 36 | + } |
| 37 | + return hints; |
| 38 | +}; |
| 39 | + |
| 40 | +describe('codegen derived annotations', () => { |
| 41 | + it('derives the full MCP hint set from the HTTP method', async () => { |
| 42 | + const output = await codegen({ document }); |
| 43 | + |
| 44 | + expect(hintsFor(output, 'thingList')).toEqual({ |
| 45 | + readOnlyHint: true, |
| 46 | + destructiveHint: false, |
| 47 | + idempotentHint: true, |
| 48 | + openWorldHint: false, |
| 49 | + }); |
| 50 | + expect(hintsFor(output, 'thingCreate')).toEqual({ |
| 51 | + readOnlyHint: false, |
| 52 | + destructiveHint: false, |
| 53 | + idempotentHint: false, |
| 54 | + openWorldHint: false, |
| 55 | + }); |
| 56 | + expect(hintsFor(output, 'thingUpdate')).toEqual({ |
| 57 | + readOnlyHint: false, |
| 58 | + destructiveHint: false, |
| 59 | + idempotentHint: true, |
| 60 | + openWorldHint: false, |
| 61 | + }); |
| 62 | + expect(hintsFor(output, 'thingDelete')).toEqual({ |
| 63 | + readOnlyHint: false, |
| 64 | + destructiveHint: true, |
| 65 | + idempotentHint: true, |
| 66 | + openWorldHint: false, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('lets a per-action config override any derived hint, including to false', async () => { |
| 71 | + const output = await codegen({ |
| 72 | + document, |
| 73 | + actions: { |
| 74 | + // A POST that is actually idempotent (e.g. taskComplete-style actions). |
| 75 | + thingCreate: { idempotentHint: true }, |
| 76 | + // An explicit `false` override must beat a derived `true`. |
| 77 | + thingDelete: { destructiveHint: false }, |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + expect(hintsFor(output, 'thingCreate')).toEqual({ |
| 82 | + readOnlyHint: false, |
| 83 | + destructiveHint: false, |
| 84 | + idempotentHint: true, |
| 85 | + openWorldHint: false, |
| 86 | + }); |
| 87 | + expect(hintsFor(output, 'thingDelete')).toEqual({ |
| 88 | + readOnlyHint: false, |
| 89 | + destructiveHint: false, |
| 90 | + idempotentHint: true, |
| 91 | + openWorldHint: false, |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments