|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { getGHESVersionFromFilepath } from '../scripts/utils/update-markdown.js' |
| 3 | + |
| 4 | +describe('GHES version extraction for update-markdown', () => { |
| 5 | + test('extracts GHES version from file path with date suffix', () => { |
| 6 | + const filePath = 'src/rest/data/ghes-3.10-2022-11-28/schema.json' |
| 7 | + expect(getGHESVersionFromFilepath(filePath)).toBe('3.10') |
| 8 | + }) |
| 9 | + |
| 10 | + test('extracts GHES version from file path without date suffix', () => { |
| 11 | + const filePath = 'src/rest/data/ghes-3.6/schema.json' |
| 12 | + expect(getGHESVersionFromFilepath(filePath)).toBe('3.6') |
| 13 | + }) |
| 14 | + |
| 15 | + test('returns null for non-GHES file paths', () => { |
| 16 | + expect(getGHESVersionFromFilepath('src/rest/data/ghae/schema.json')).toBeNull() |
| 17 | + expect(getGHESVersionFromFilepath('src/rest/data/fpt-2022-11-28/schema.json')).toBeNull() |
| 18 | + expect(getGHESVersionFromFilepath('src/rest/data/ghec-2022-11-28/schema.json')).toBeNull() |
| 19 | + }) |
| 20 | + |
| 21 | + test('handles various GHES version formats', () => { |
| 22 | + expect(getGHESVersionFromFilepath('src/rest/data/ghes-2.22/schema.json')).toBe('2.22') |
| 23 | + expect(getGHESVersionFromFilepath('src/rest/data/ghes-3.0-2022-01-01/schema.json')).toBe('3.0') |
| 24 | + expect(getGHESVersionFromFilepath('src/rest/data/ghes-3.15-2023-05-15/schema.json')).toBe( |
| 25 | + '3.15', |
| 26 | + ) |
| 27 | + }) |
| 28 | + |
| 29 | + test('returns null for malformed GHES paths', () => { |
| 30 | + expect(getGHESVersionFromFilepath('src/rest/data/ghes-/schema.json')).toBeNull() |
| 31 | + expect(getGHESVersionFromFilepath('src/rest/data/ghes-abc/schema.json')).toBeNull() |
| 32 | + expect(getGHESVersionFromFilepath('src/rest/data/ghes/schema.json')).toBeNull() |
| 33 | + }) |
| 34 | + |
| 35 | + test('works with different path separators and nested paths', () => { |
| 36 | + const windowsPath = 'src\\rest\\data\\ghes-3.10-2022-11-28\\schema.json' |
| 37 | + expect(getGHESVersionFromFilepath(windowsPath)).toBe('3.10') |
| 38 | + |
| 39 | + const nestedPath = 'some/deep/path/src/rest/data/ghes-3.5-2021-12-31/nested/schema.json' |
| 40 | + expect(getGHESVersionFromFilepath(nestedPath)).toBe('3.5') |
| 41 | + }) |
| 42 | + |
| 43 | + test('demonstrates the original bug scenario', () => { |
| 44 | + // This test demonstrates the bug described in the issue |
| 45 | + // where ghes-3.10 would incorrectly match deprecated version 3.1 |
| 46 | + const filePath = 'src/rest/data/ghes-3.10-2022-11-28/schema.json' |
| 47 | + const extractedVersion = getGHESVersionFromFilepath(filePath) |
| 48 | + |
| 49 | + // Mock deprecated versions array like in the actual code |
| 50 | + const deprecated = ['3.0', '3.1', '3.2', '2.22', '2.21'] |
| 51 | + |
| 52 | + expect(extractedVersion).toBe('3.10') |
| 53 | + // This should be false - 3.10 is NOT in the deprecated list |
| 54 | + expect(deprecated.includes(extractedVersion)).toBe(false) |
| 55 | + |
| 56 | + // The old buggy logic would have incorrectly flagged this as deprecated |
| 57 | + // because it would find '3.1' as a substring in the path |
| 58 | + }) |
| 59 | +}) |
0 commit comments