|
| 1 | +import { AttributeParser } from '../../parsers/AttributeParser'; |
| 2 | +import { TypeParser } from '../../generators/TypeParser'; |
| 3 | +import { UtilityHelpers } from '../../generators/UtilityHelpers'; |
| 4 | + |
| 5 | +describe('AttributeParser - Quote Types Issue', () => { |
| 6 | + let typeParser: TypeParser; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + const utilityHelpers = new UtilityHelpers(); |
| 10 | + typeParser = new TypeParser(utilityHelpers); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should parse quote attribute with multiple types (Quote, ShallowQuote, null)', () => { |
| 14 | + // Mock content representing the Status entity quote attribute |
| 15 | + // This reproduces the exact content from Status.md |
| 16 | + const content = ` |
| 17 | +### \`quote\` {#quote} |
| 18 | +
|
| 19 | +**Description:** Information about the status being quoted, if any\\ |
| 20 | +**Type:** {{<nullable>}} [Quote]({{< relref "entities/quote" >}}), [ShallowQuote]({{< relref "entities/ShallowQuote" >}}) or null\\ |
| 21 | +**Version history:**\\ |
| 22 | +4.4.0 - added |
| 23 | +`; |
| 24 | + |
| 25 | + const attributes = AttributeParser.parseAttributesFromSection( |
| 26 | + content, |
| 27 | + 'Status' |
| 28 | + ); |
| 29 | + |
| 30 | + expect(attributes).toHaveLength(1); |
| 31 | + const quoteAttribute = attributes[0]; |
| 32 | + |
| 33 | + expect(quoteAttribute.name).toBe('quote'); |
| 34 | + expect(quoteAttribute.nullable).toBe(true); |
| 35 | + |
| 36 | + // The key issue: the type should capture both Quote and ShallowQuote |
| 37 | + // Currently it likely only captures Quote |
| 38 | + console.log('Parsed quote attribute type:', quoteAttribute.type); |
| 39 | + console.log( |
| 40 | + 'Full quote attribute:', |
| 41 | + JSON.stringify(quoteAttribute, null, 2) |
| 42 | + ); |
| 43 | + |
| 44 | + // This test will initially fail, showing us the current behavior |
| 45 | + // The type should ideally be something that includes both Quote and ShallowQuote |
| 46 | + expect(quoteAttribute.type).toContain('Quote'); |
| 47 | + expect(quoteAttribute.type).toContain('ShallowQuote'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should parse quote attribute from actual Status entity file', () => { |
| 51 | + // Parse actual Status entity to check the real behavior |
| 52 | + const { EntityParser } = require('../../parsers/EntityParser'); |
| 53 | + const parser = new EntityParser(); |
| 54 | + const entities = parser.parseAllEntities(); |
| 55 | + const statusEntity = entities.find((e: any) => e.name === 'Status'); |
| 56 | + |
| 57 | + expect(statusEntity).toBeDefined(); |
| 58 | + |
| 59 | + if (statusEntity) { |
| 60 | + const quoteAttribute = statusEntity.attributes.find( |
| 61 | + (a: any) => a.name === 'quote' |
| 62 | + ); |
| 63 | + |
| 64 | + expect(quoteAttribute).toBeDefined(); |
| 65 | + |
| 66 | + if (quoteAttribute) { |
| 67 | + console.log('Real Status quote attribute type:', quoteAttribute.type); |
| 68 | + console.log( |
| 69 | + 'Real Status quote attribute:', |
| 70 | + JSON.stringify(quoteAttribute, null, 2) |
| 71 | + ); |
| 72 | + |
| 73 | + // This should include both Quote and ShallowQuote types |
| 74 | + expect(quoteAttribute.type).toContain('Quote'); |
| 75 | + // This will likely fail, showing the current issue |
| 76 | + expect(quoteAttribute.type).toContain('ShallowQuote'); |
| 77 | + } |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + it('should parse TypeParser.parseType with multiple entity references', () => { |
| 82 | + // Test the TypeParser directly to verify our fix |
| 83 | + const typeString = '[Quote](), [ShallowQuote]() or null'; |
| 84 | + const parsedType = typeParser.parseType(typeString); |
| 85 | + |
| 86 | + console.log( |
| 87 | + 'TypeParser result for multiple entities:', |
| 88 | + JSON.stringify(parsedType, null, 2) |
| 89 | + ); |
| 90 | + |
| 91 | + // After our fix, this should return a oneOf structure with both Quote and ShallowQuote |
| 92 | + expect(parsedType.oneOf).toBeDefined(); |
| 93 | + expect(parsedType.oneOf).toHaveLength(2); |
| 94 | + |
| 95 | + // Check that both entities are referenced |
| 96 | + const refs = parsedType.oneOf?.map((item: any) => item.$ref) || []; |
| 97 | + expect(refs).toContain('#/components/schemas/Quote'); |
| 98 | + expect(refs).toContain('#/components/schemas/ShallowQuote'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should handle single entity reference correctly', () => { |
| 102 | + // Test that single entity references still work as before |
| 103 | + const typeString = '[Quote]() or null'; |
| 104 | + const parsedType = typeParser.parseType(typeString); |
| 105 | + |
| 106 | + console.log( |
| 107 | + 'TypeParser result for single entity:', |
| 108 | + JSON.stringify(parsedType, null, 2) |
| 109 | + ); |
| 110 | + |
| 111 | + // Should return a direct reference for single entity |
| 112 | + expect(parsedType.$ref).toBe('#/components/schemas/Quote'); |
| 113 | + expect(parsedType.oneOf).toBeUndefined(); |
| 114 | + }); |
| 115 | +}); |
0 commit comments