Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | import { describe, expect, test } from 'bun:test'; |
| 2 | import { renderToStaticMarkup } from 'react-dom/server'; |
| 3 | |
| 4 | import { CustomSchemaUI } from '../../components/custom-schema-ui'; |
| 5 | import { generateSchemaData } from '../../components/schema-generator'; |
| 6 | |
| 7 | type TestSchema = Parameters<typeof generateSchemaData>[0]['root']; |
| 8 | type TestSchemaObject = Exclude<TestSchema, boolean>; |
| 9 | |
| 10 | // Reference Objects must be read through the document resolver while retaining |
| 11 | // their raw pointer as the stable schema identity. |
| 12 | |
| 13 | const components: Record<string, TestSchemaObject> = { |
| 14 | '#/components/schemas/User': { |
| 15 | type: 'object', |
| 16 | required: ['id'], |
| 17 | properties: { |
| 18 | id: { type: 'string', description: 'Unique user id' }, |
| 19 | nickname: { type: 'string', description: 'Display name' }, |
| 20 | secret: { type: 'string', writeOnly: true }, |
| 21 | }, |
| 22 | }, |
| 23 | '#/components/schemas/Address': { |
| 24 | type: 'object', |
| 25 | properties: { |
| 26 | city: { type: 'string', description: 'City name' }, |
| 27 | }, |
| 28 | }, |
| 29 | }; |
| 30 | |
| 31 | // Mirrors ctx.schema.resolve from fumadocs: shallowly resolves a Reference |
| 32 | // Object and merges sibling keywords; non-references pass through unchanged. |
| 33 | const resolve = (node: TestSchema): TestSchema => { |
| 34 | if (typeof node !== 'object' || !node.$ref) return node; |
| 35 | const { $ref, ...siblings } = node; |
| 36 | if (typeof $ref !== 'string') return node; |
| 37 | const target = components[$ref]; |
| 38 | return { ...target, ...siblings }; |
| 39 | }; |
| 40 | |
| 41 | const getRawRef = (value: object): string | undefined => |
| 42 | '$ref' in value && typeof value.$ref === 'string' ? value.$ref : undefined; |
| 43 | |
| 44 | const ctx = { renderMarkdown: (text: string) => text, schema: { getRawRef, resolve } }; |
| 45 | |
| 46 | describe('$ref resolution in generated API schemas', () => { |
| 47 | test('renders properties for a root that is a Reference Object', () => { |
| 48 | const generated = generateSchemaData( |
| 49 | { root: { $ref: '#/components/schemas/User' }, readOnly: true }, |
| 50 | ctx |
| 51 | ); |
| 52 | |
| 53 | const html = renderToStaticMarkup( |
| 54 | <CustomSchemaUI name="body" as="body" generated={generated} /> |
| 55 | ); |
| 56 | |
| 57 | expect(html).toContain('id'); |
| 58 | expect(html).toContain('Unique user id'); |
| 59 | expect(html).toContain('nickname'); |
| 60 | expect(html).toContain('Display name'); |
| 61 | }); |
| 62 | |
| 63 | test('resolves Reference Objects nested in properties', () => { |
| 64 | const generated = generateSchemaData( |
| 65 | { |
| 66 | root: { |
| 67 | type: 'object', |
| 68 | properties: { address: { $ref: '#/components/schemas/Address' } }, |
| 69 | }, |
| 70 | readOnly: true, |
| 71 | }, |
| 72 | ctx |
| 73 | ); |
| 74 | |
| 75 | const html = renderToStaticMarkup( |
| 76 | <CustomSchemaUI name="body" as="body" generated={generated} /> |
| 77 | ); |
| 78 | |
| 79 | expect(html).toContain('address'); |
| 80 | // The referenced object's own fields render inside a collapsed accordion, |
| 81 | // so assert on the resolved shape rather than the nested description text: |
| 82 | // an unresolved `$ref` would fall through to a primitive with no children. |
| 83 | expect(html).toContain('Address'); |
| 84 | expect(html).toContain('Show 1 child attributes'); |
| 85 | }); |
| 86 | |
| 87 | test('keeps the schema name as the display type for a Reference Object', () => { |
| 88 | const generated = generateSchemaData( |
| 89 | { root: { $ref: '#/components/schemas/User' }, readOnly: true }, |
| 90 | ctx |
| 91 | ); |
| 92 | |
| 93 | expect(generated.refs[generated.$root]?.typeName).toBe('User'); |
| 94 | }); |
| 95 | |
| 96 | test('applies readOnly/writeOnly visibility through a Reference Object', () => { |
| 97 | const generated = generateSchemaData( |
| 98 | { root: { $ref: '#/components/schemas/User' }, readOnly: true }, |
| 99 | ctx |
| 100 | ); |
| 101 | |
| 102 | const html = renderToStaticMarkup( |
| 103 | <CustomSchemaUI name="body" as="body" generated={generated} isResponse /> |
| 104 | ); |
| 105 | |
| 106 | // `secret` is writeOnly, so it must not appear on a read-only (response) view. |
| 107 | expect(html).not.toContain('secret'); |
| 108 | }); |
| 109 | |
| 110 | test('dedupes repeated Reference Objects onto one entry', () => { |
| 111 | const generated = generateSchemaData( |
| 112 | { |
| 113 | root: { |
| 114 | type: 'object', |
| 115 | properties: { |
| 116 | primary: { $ref: '#/components/schemas/Address' }, |
| 117 | billing: { $ref: '#/components/schemas/Address' }, |
| 118 | }, |
| 119 | }, |
| 120 | readOnly: true, |
| 121 | }, |
| 122 | ctx |
| 123 | ); |
| 124 | |
| 125 | expect(generated.refs['#/components/schemas/Address']).toBeDefined(); |
| 126 | expect( |
| 127 | Object.keys(generated.refs).filter(key => key === '#/components/schemas/Address') |
| 128 | ).toHaveLength(1); |
| 129 | }); |
| 130 | }); |