Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { expect, test } from 'vitest'; |
| 2 | |
| 3 | import { docComment } from './DocComment'; |
| 4 | import { functionType } from './FunctionType'; |
| 5 | import { namedType } from './NamedType'; |
| 6 | import { objectType } from './ObjectType'; |
| 7 | import { property } from './Property'; |
| 8 | import { stringify } from './stringify'; |
| 9 | import { unionType } from './UnionType'; |
| 10 | import { toStringTag } from './WellKnownSymbol'; |
| 11 | |
| 12 | const A = namedType('A'); |
| 13 | const stringType = namedType('string'); |
| 14 | const numberType = namedType('number'); |
| 15 | |
| 16 | test('name and type', () => { |
| 17 | const prop = property('foo', A); |
| 18 | |
| 19 | expect(stringify(prop)).toMatchInlineSnapshot(`"foo: A"`); |
| 20 | }); |
| 21 | |
| 22 | test('invalid identifier', () => { |
| 23 | const prop = property('this is not a valid JS identifier', A); |
| 24 | |
| 25 | expect(stringify(prop)).toMatchInlineSnapshot(`"["this is not a valid JS identifier"]: A"`); |
| 26 | }); |
| 27 | |
| 28 | test('well-known symbol', () => { |
| 29 | const prop = property(toStringTag, A); |
| 30 | |
| 31 | expect(stringify(prop)).toMatchInlineSnapshot(`"[Symbol.toStringTag]: A"`); |
| 32 | }); |
| 33 | |
| 34 | test('optional', () => { |
| 35 | const prop = property('foo', A).optional(); |
| 36 | |
| 37 | expect(stringify(prop)).toMatchInlineSnapshot(`"foo?: A"`); |
| 38 | }); |
| 39 | |
| 40 | test('readonly', () => { |
| 41 | const prop = property('foo', A).readonly(); |
| 42 | |
| 43 | expect(stringify(prop)).toMatchInlineSnapshot(`"readonly foo: A"`); |
| 44 | }); |
| 45 | |
| 46 | test('with doc comment', () => { |
| 47 | const prop = property('foo', A).setDocComment(docComment('This is foo')); |
| 48 | |
| 49 | expect(stringify(prop)).toMatchInlineSnapshot(` |
| 50 | "/** |
| 51 | * This is foo |
| 52 | */ |
| 53 | foo: A" |
| 54 | `); |
| 55 | }); |
| 56 | |
| 57 | test('property with union type does not add parentheses (ObjectProperty context)', () => { |
| 58 | // Union has precedence 1, ObjectProperty context requires precedence 5 |
| 59 | // However, for object properties, union types don't need parentheses syntactically |
| 60 | // because the property boundary is clear: { prop: string | number } |
| 61 | // But according to precedence rules, 1 < 5 means parentheses would be added |
| 62 | const unionProp = unionType([stringType, numberType]); |
| 63 | const obj = objectType().add(property('value', unionProp)).formatInline(); |
| 64 | const result = stringify(obj); |
| 65 | |
| 66 | // ObjectProperty context has precedence 5, Union has precedence 1 |
| 67 | // Since 1 < 5, parentheses are added |
| 68 | expect(result).toBe('{ value: (string | number) }'); |
| 69 | }); |
| 70 | |
| 71 | test('property with function type does not add parentheses (ObjectProperty context)', () => { |
| 72 | // Function type has precedence 4, ObjectProperty context requires precedence 5 |
| 73 | // Since 4 < 5, parentheses would be added |
| 74 | const funcProp = functionType().setReturnType(stringType); |
| 75 | const obj = objectType().add(property('callback', funcProp)).formatInline(); |
| 76 | const result = stringify(obj); |
| 77 | |
| 78 | // Function type (precedence 4) < ObjectProperty (precedence 5), so parentheses added |
| 79 | expect(result).toBe('{ callback: (() => string) }'); |
| 80 | }); |
| 81 | |
| 82 | test('optional property with union type adds parentheses', () => { |
| 83 | const unionProp = unionType([stringType, numberType]); |
| 84 | const obj = objectType().add(property('value', unionProp).optional()).formatInline(); |
| 85 | const result = stringify(obj); |
| 86 | |
| 87 | expect(result).toBe('{ value?: (string | number) }'); |
| 88 | }); |
| 89 | |
| 90 | test('readonly property with union type adds parentheses', () => { |
| 91 | const unionProp = unionType([stringType, numberType]); |
| 92 | const obj = objectType().add(property('value', unionProp).readonly()).formatInline(); |
| 93 | const result = stringify(obj); |
| 94 | |
| 95 | expect(result).toBe('{ readonly value: (string | number) }'); |
| 96 | }); |