import { describe, expect, it } from 'vitest' import { renderRequestBodyMarkdown } from './api-request-body-markdown' describe('renderRequestBodyMarkdown', () => { it('should return null without a request body schema', () => { expect(renderRequestBodyMarkdown(undefined)).toBeNull() expect(renderRequestBodyMarkdown({ content: {} })).toBeNull() }) it('should render nested array items and objects', () => { expect( renderRequestBodyMarkdown({ required: true, content: { 'application/json': { schema: { type: 'object', required: ['data'], properties: { data: { type: 'array', minItems: 1, items: { type: 'object', required: ['attributes'], properties: { attributes: { type: 'object', required: ['role'], properties: { role: { type: 'string', description: 'Role name.', enum: ['owner', 'developer'], }, projects: { type: 'array', items: { type: 'object', required: ['ref'], properties: { ref: { type: 'string' } }, }, }, }, }, }, }, }, }, }, }, }, }) ).toMatchInlineSnapshot(` "### Request body Content type \`application/json\` (object, required) - \`data\` (array of object, required): Constraints: min items 1. - \`attributes\` (object, required) - \`role\` (string, required): Role name. Accepted values: \`owner\`, \`developer\`. - \`projects\` (array of object, optional) - \`ref\` (string, required)" `) }) it('should render each option of a union with its accepted values', () => { expect( renderRequestBodyMarkdown({ content: { 'application/json': { schema: { type: 'object', properties: { variant: { anyOf: [ { type: 'string', enum: ['a', 'b'] }, { type: 'object', properties: { size: { type: 'integer', minimum: 1, description: 'Size in GB' }, }, }, ], }, }, }, }, }, }) ).toMatchInlineSnapshot(` "### Request body Content type \`application/json\` (object) - \`variant\` (string or object, optional) - Any of the following: - Option 1 (string): Accepted values: \`a\`, \`b\`. - Option 2 (object) - \`size\` (integer, optional): Size in GB. Constraints: minimum 1." `) }) it('should render const values, option titles, maps, defaults, and nullable unions', () => { expect( renderRequestBodyMarkdown({ content: { 'application/json': { schema: { type: 'object', properties: { type: { type: 'string', const: 'endpoint' }, config: { anyOf: [ { title: 'webhook', type: 'object', properties: { url: { type: 'string' } } }, { title: 'datadog', type: 'object', properties: { region: { type: 'string' } }, }, ], }, headers: { type: 'object', additionalProperties: { type: 'string' } }, extra: { type: 'object', additionalProperties: {} }, label: { anyOf: [{ type: 'string', maxLength: 512 }, { type: 'null' }], default: 'none', }, files: { type: 'array', items: { type: 'string', format: 'binary' } }, }, }, }, }, }) ).toMatchInlineSnapshot(` "### Request body Content type \`application/json\` (object) - \`type\` (string, optional): Accepted values: \`endpoint\`. - \`config\` (object, optional) - Any of the following: - Option 1: webhook (object) - \`url\` (string, optional) - Option 2: datadog (object) - \`region\` (string, optional) - \`headers\` (object of string, optional) - Any other key (string) - \`extra\` (object, optional) - \`label\` (string, optional, nullable): Default: \`none\`. Constraints: max length 512. - \`files\` (array of string, optional): Constraints: each item: format \`binary\`." `) }) it('should render body notes, boolean bounds, closed objects, refs, and backtick values', () => { expect( renderRequestBodyMarkdown({ required: true, content: { 'application/json': { schema: { type: 'array', maxItems: 100, items: { type: 'object', additionalProperties: false, properties: { size: { type: 'integer', minimum: 0, exclusiveMinimum: true, maximum: Number.MAX_SAFE_INTEGER, }, characters: { type: 'string', enum: ['a`b', ''] }, value: { $ref: '#/components/schemas/JsonValue' }, }, }, }, }, 'application/octet-stream': { schema: { type: 'string', format: 'binary', description: 'Compressed file.\n\n```js\nawait upload(file)\n```', }, }, }, }) ).toMatchInlineSnapshot(` "### Request body Content type \`application/json\` (array of object, required): Constraints: max items 100, each item: no other keys. - \`size\` (integer, optional): Constraints: greater than 0. - \`characters\` (string, optional): Accepted values: \`\` a\`b \`\`, \`""\`. - \`value\` (JsonValue, optional) Content type \`application/octet-stream\` (string, required): Compressed file. \`\`\`js await upload(file) \`\`\` Constraints: format \`binary\`." `) }) })