|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { assert } from 'chai'; |
| 4 | +import { ParseResultElement, isParseResultElement } from '@speclynx/apidom-datamodel'; |
| 5 | +import { parse } from '@speclynx/apidom-parser-adapter-arazzo-json-1'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | + |
| 8 | +import { dereferenceSourceDescriptions } from '../../../../../src/dereference/strategies/arazzo-1/index.ts'; |
| 9 | +import { options, mergeOptions } from '../../../../../src/configuration/saturated.ts'; |
| 10 | + |
| 11 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 12 | + |
| 13 | +describe('dereference', function () { |
| 14 | + context('dereferenceSourceDescriptions', function () { |
| 15 | + context('given naked parser adapter usage', function () { |
| 16 | + specify('should dereference source descriptions from ParseResult', async function () { |
| 17 | + const uri = path.join(__dirname, 'fixtures', 'root.json'); |
| 18 | + const data = fs.readFileSync(uri).toString(); |
| 19 | + |
| 20 | + // use naked parser adapter directly |
| 21 | + const parseResult = await parse(data); |
| 22 | + |
| 23 | + // use exported dereferenceSourceDescriptions function |
| 24 | + const sourceDescriptions = await dereferenceSourceDescriptions( |
| 25 | + parseResult, |
| 26 | + uri, |
| 27 | + mergeOptions(options, { |
| 28 | + dereference: { strategyOpts: { sourceDescriptions: true } }, |
| 29 | + }), |
| 30 | + ); |
| 31 | + |
| 32 | + assert.strictEqual(sourceDescriptions.length, 1); |
| 33 | + |
| 34 | + const sdParseResult = sourceDescriptions[0]!; |
| 35 | + assert.isTrue(isParseResultElement(sdParseResult)); |
| 36 | + assert.isTrue(sdParseResult.classes.includes('source-description')); |
| 37 | + assert.strictEqual(sdParseResult.meta.get('name')!.toValue(), 'petStore'); |
| 38 | + assert.strictEqual(sdParseResult.meta.get('type')!.toValue(), 'openapi'); |
| 39 | + }); |
| 40 | + |
| 41 | + specify('should filter source descriptions by name', async function () { |
| 42 | + const uri = path.join(__dirname, 'fixtures', 'root-multi.json'); |
| 43 | + const data = fs.readFileSync(uri).toString(); |
| 44 | + const parseResult = await parse(data); |
| 45 | + |
| 46 | + const sourceDescriptions = await dereferenceSourceDescriptions( |
| 47 | + parseResult, |
| 48 | + uri, |
| 49 | + mergeOptions(options, { |
| 50 | + dereference: { strategyOpts: { sourceDescriptions: ['petStore'] } }, |
| 51 | + }), |
| 52 | + ); |
| 53 | + |
| 54 | + assert.strictEqual(sourceDescriptions.length, 1); |
| 55 | + |
| 56 | + const sdParseResult = sourceDescriptions[0]! as ParseResultElement; |
| 57 | + assert.isTrue(isParseResultElement(sdParseResult)); |
| 58 | + assert.strictEqual(sdParseResult.meta.get('name')!.toValue(), 'petStore'); |
| 59 | + }); |
| 60 | + |
| 61 | + specify('should respect sourceDescriptionsMaxDepth option', async function () { |
| 62 | + const uri = path.join(__dirname, 'fixtures', 'root-recursive.json'); |
| 63 | + const data = fs.readFileSync(uri).toString(); |
| 64 | + const parseResult = await parse(data); |
| 65 | + |
| 66 | + const sourceDescriptions = await dereferenceSourceDescriptions( |
| 67 | + parseResult, |
| 68 | + uri, |
| 69 | + mergeOptions(options, { |
| 70 | + dereference: { |
| 71 | + strategyOpts: { sourceDescriptions: true, sourceDescriptionsMaxDepth: 1 }, |
| 72 | + }, |
| 73 | + }), |
| 74 | + ); |
| 75 | + |
| 76 | + assert.strictEqual(sourceDescriptions.length, 1); |
| 77 | + |
| 78 | + const nestedArazzo = sourceDescriptions[0]! as ParseResultElement; |
| 79 | + assert.isTrue(isParseResultElement(nestedArazzo)); |
| 80 | + assert.isTrue(nestedArazzo.classes.includes('source-description')); |
| 81 | + // nested arazzo has its API + error annotation for max depth exceeded |
| 82 | + assert.strictEqual(nestedArazzo.length, 2); |
| 83 | + |
| 84 | + const annotationResult = nestedArazzo.get(1)! as ParseResultElement; |
| 85 | + assert.isTrue(isParseResultElement(annotationResult)); |
| 86 | + const annotation = annotationResult.get(0); |
| 87 | + assert.strictEqual(annotation?.element, 'annotation'); |
| 88 | + assert.isTrue(annotation?.classes.includes('error')); |
| 89 | + assert.include(annotation?.toValue(), 'Maximum dereference depth of 1 has been exceeded'); |
| 90 | + }); |
| 91 | + |
| 92 | + specify('should return empty array when sourceDescriptions is false', async function () { |
| 93 | + const uri = path.join(__dirname, 'fixtures', 'root.json'); |
| 94 | + const data = fs.readFileSync(uri).toString(); |
| 95 | + const parseResult = await parse(data); |
| 96 | + |
| 97 | + const sourceDescriptions = await dereferenceSourceDescriptions( |
| 98 | + parseResult, |
| 99 | + uri, |
| 100 | + mergeOptions(options, { |
| 101 | + dereference: { strategyOpts: { sourceDescriptions: false } }, |
| 102 | + }), |
| 103 | + ); |
| 104 | + |
| 105 | + assert.strictEqual(sourceDescriptions.length, 0); |
| 106 | + }); |
| 107 | + |
| 108 | + specify('should allow overriding strategyName parameter', async function () { |
| 109 | + const uri = path.join(__dirname, 'fixtures', 'root.json'); |
| 110 | + const data = fs.readFileSync(uri).toString(); |
| 111 | + const parseResult = await parse(data); |
| 112 | + |
| 113 | + // use custom strategy name for options lookup |
| 114 | + const sourceDescriptions = await dereferenceSourceDescriptions( |
| 115 | + parseResult, |
| 116 | + uri, |
| 117 | + mergeOptions(options, { |
| 118 | + dereference: { strategyOpts: { 'custom-strategy': { sourceDescriptions: true } } }, |
| 119 | + }), |
| 120 | + 'custom-strategy', |
| 121 | + ); |
| 122 | + |
| 123 | + assert.strictEqual(sourceDescriptions.length, 1); |
| 124 | + assert.isTrue(isParseResultElement(sourceDescriptions[0])); |
| 125 | + }); |
| 126 | + |
| 127 | + specify('should default to arazzo-1 strategyName for options lookup', async function () { |
| 128 | + const uri = path.join(__dirname, 'fixtures', 'root.json'); |
| 129 | + const data = fs.readFileSync(uri).toString(); |
| 130 | + const parseResult = await parse(data); |
| 131 | + |
| 132 | + // only set strategy-specific option, no global sourceDescriptions |
| 133 | + const sourceDescriptions = await dereferenceSourceDescriptions( |
| 134 | + parseResult, |
| 135 | + uri, |
| 136 | + mergeOptions(options, { |
| 137 | + dereference: { strategyOpts: { 'arazzo-1': { sourceDescriptions: true } } }, |
| 138 | + }), |
| 139 | + ); |
| 140 | + |
| 141 | + assert.strictEqual(sourceDescriptions.length, 1); |
| 142 | + |
| 143 | + const sdParseResult = sourceDescriptions[0]!; |
| 144 | + assert.isTrue(isParseResultElement(sdParseResult)); |
| 145 | + assert.isTrue(sdParseResult.classes.includes('source-description')); |
| 146 | + assert.strictEqual(sdParseResult.meta.get('name')!.toValue(), 'petStore'); |
| 147 | + }); |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
0 commit comments