|
| 1 | +import { describe, it } from 'node:test'; |
| 2 | +import { deepStrictEqual } from 'assert'; |
| 3 | +import { duplicateStabilityNodes } from '../../rules/duplicate-stability-nodes.mjs'; |
| 4 | +import { LINT_MESSAGES } from '../../../constants.mjs'; |
| 5 | + |
| 6 | +// Mock data structure for creating test entries |
| 7 | +const createEntry = ( |
| 8 | + depth, |
| 9 | + stabilityIndex, |
| 10 | + source = 'file.yaml', |
| 11 | + position = { line: 1, column: 1 } |
| 12 | +) => ({ |
| 13 | + heading: { data: { depth } }, |
| 14 | + stability: { children: [{ data: { index: stabilityIndex } }] }, |
| 15 | + api_doc_source: source, |
| 16 | + yaml_position: position, |
| 17 | +}); |
| 18 | + |
| 19 | +describe('duplicateStabilityNodes', () => { |
| 20 | + it('returns empty array when there are no entries', () => { |
| 21 | + deepStrictEqual(duplicateStabilityNodes([]), []); |
| 22 | + }); |
| 23 | + |
| 24 | + it('returns empty array when there are no duplicate stability nodes', () => { |
| 25 | + const entries = [createEntry(1, 0), createEntry(2, 1), createEntry(3, 2)]; |
| 26 | + deepStrictEqual(duplicateStabilityNodes(entries), []); |
| 27 | + }); |
| 28 | + |
| 29 | + it('detects duplicate stability nodes within a chain', () => { |
| 30 | + const entries = [ |
| 31 | + createEntry(1, 0), |
| 32 | + createEntry(2, 0), // Duplicate stability node |
| 33 | + ]; |
| 34 | + |
| 35 | + deepStrictEqual(duplicateStabilityNodes(entries), [ |
| 36 | + { |
| 37 | + level: 'warn', |
| 38 | + message: LINT_MESSAGES.duplicateStabilityNode, |
| 39 | + location: { |
| 40 | + path: 'file.yaml', |
| 41 | + position: { line: 1, column: 1 }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + it('resets stability tracking when depth decreases', () => { |
| 48 | + const entries = [ |
| 49 | + createEntry(1, 0), |
| 50 | + createEntry(2, 0), // This should trigger an issue |
| 51 | + createEntry(1, 1), |
| 52 | + createEntry(2, 1), // This should trigger another issue |
| 53 | + ]; |
| 54 | + |
| 55 | + deepStrictEqual(duplicateStabilityNodes(entries), [ |
| 56 | + { |
| 57 | + level: 'warn', |
| 58 | + message: LINT_MESSAGES.duplicateStabilityNode, |
| 59 | + location: { |
| 60 | + path: 'file.yaml', |
| 61 | + position: { line: 1, column: 1 }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + level: 'warn', |
| 66 | + message: LINT_MESSAGES.duplicateStabilityNode, |
| 67 | + location: { |
| 68 | + path: 'file.yaml', |
| 69 | + position: { line: 1, column: 1 }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + ]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('handles missing stability nodes gracefully', () => { |
| 76 | + const entries = [ |
| 77 | + createEntry(1, -1), |
| 78 | + createEntry(2, -1), |
| 79 | + createEntry(3, 0), |
| 80 | + createEntry(4, 0), // This should trigger an issue |
| 81 | + ]; |
| 82 | + |
| 83 | + deepStrictEqual(duplicateStabilityNodes(entries), [ |
| 84 | + { |
| 85 | + level: 'warn', |
| 86 | + message: LINT_MESSAGES.duplicateStabilityNode, |
| 87 | + location: { |
| 88 | + path: 'file.yaml', |
| 89 | + position: { line: 1, column: 1 }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + ]); |
| 93 | + }); |
| 94 | + |
| 95 | + it('handles entries with no stability property gracefully', () => { |
| 96 | + const entries = [ |
| 97 | + { |
| 98 | + heading: { data: { depth: 1 } }, |
| 99 | + stability: { children: [] }, |
| 100 | + api_doc_source: 'file.yaml', |
| 101 | + yaml_position: { line: 2, column: 5 }, |
| 102 | + }, |
| 103 | + createEntry(2, 0), |
| 104 | + ]; |
| 105 | + deepStrictEqual(duplicateStabilityNodes(entries), []); |
| 106 | + }); |
| 107 | + |
| 108 | + it('handles entries with undefined stability index', () => { |
| 109 | + const entries = [ |
| 110 | + createEntry(1, undefined), |
| 111 | + createEntry(2, undefined), |
| 112 | + createEntry(3, 1), |
| 113 | + createEntry(4, 1), // This should trigger an issue |
| 114 | + ]; |
| 115 | + deepStrictEqual(duplicateStabilityNodes(entries), [ |
| 116 | + { |
| 117 | + level: 'warn', |
| 118 | + message: LINT_MESSAGES.duplicateStabilityNode, |
| 119 | + location: { |
| 120 | + path: 'file.yaml', |
| 121 | + position: { line: 1, column: 1 }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + ]); |
| 125 | + }); |
| 126 | + |
| 127 | + it('handles mixed depths and stability nodes correctly', () => { |
| 128 | + const entries = [ |
| 129 | + createEntry(1, 0), |
| 130 | + createEntry(2, 1), |
| 131 | + createEntry(3, 1), // This should trigger an issue |
| 132 | + createEntry(2, 2), |
| 133 | + createEntry(3, 2), // This should trigger another issue |
| 134 | + ]; |
| 135 | + |
| 136 | + deepStrictEqual(duplicateStabilityNodes(entries), [ |
| 137 | + { |
| 138 | + level: 'warn', |
| 139 | + message: LINT_MESSAGES.duplicateStabilityNode, |
| 140 | + location: { |
| 141 | + path: 'file.yaml', |
| 142 | + position: { line: 1, column: 1 }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + { |
| 146 | + level: 'warn', |
| 147 | + message: LINT_MESSAGES.duplicateStabilityNode, |
| 148 | + location: { |
| 149 | + path: 'file.yaml', |
| 150 | + position: { line: 1, column: 1 }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + ]); |
| 154 | + }); |
| 155 | +}); |
0 commit comments