1
- import fs from 'fs/promises '
1
+ import fs from 'fs'
2
2
import path from 'path'
3
3
4
4
import { describe } from '@jest/globals'
@@ -8,6 +8,8 @@ import { isPlainObject, difference } from 'lodash-es'
8
8
import { isApiVersioned , allVersions } from '#src/versions/lib/all-versions.js'
9
9
import getRest from '../lib/index.js'
10
10
import readFrontmatter from '../../../lib/read-frontmatter.js'
11
+ import frontmatter from '../../../lib/frontmatter.js'
12
+ import getApplicableVersions from '../../versions/lib/get-applicable-versions.js'
11
13
12
14
const schemasPath = 'src/rest/data'
13
15
@@ -42,34 +44,63 @@ function createCategoryList(operations) {
42
44
}
43
45
44
46
describe ( 'markdown for each rest version' , ( ) => {
45
- test ( 'markdown file exists for every operationId prefix in all versions of the OpenAPI schema' , async ( ) => {
46
- // list of REST markdown files that do not correspond to REST API resources
47
- // TODO could we get this list dynamically, say via page frontmatter?
48
- const excludeFromResourceNameCheck = [
49
- 'README.md' ,
50
- 'index.md' ,
51
- 'guides' ,
52
- 'overview' ,
53
- 'quickstart.md' ,
54
- ]
47
+ // Unique set of all categories across all versions of the OpenAPI schema
48
+ const allCategories = new Set ( )
49
+ // Entire schema including categories and subcategories
50
+ const openApiSchema = { }
51
+ // All applicable version of categories based on frontmatter in the categories index.md file
52
+ const categoryApplicableVersions = { }
53
+ const referenceDir = path . join ( process . cwd ( ) , 'content/rest' )
54
+ const excludeFromResourceNameCheck = [
55
+ 'README.md' ,
56
+ 'index.md' ,
57
+ 'guides' ,
58
+ 'overview' ,
59
+ 'quickstart.md' ,
60
+ ]
61
+
62
+ function getApplicableVersionFromFile ( file ) {
63
+ const currentFile = fs . readFileSync ( file , 'utf8' )
64
+ const { data } = frontmatter ( currentFile )
65
+ return getApplicableVersions ( data . versions , file )
66
+ }
55
67
56
- // Unique set of all categories across all versions of the OpenAPI schema
57
- const allCategories = new Set ( )
68
+ function getCategorySubcategory ( file ) {
69
+ const fileSplit = file . split ( '/' )
70
+ const cat = fileSplit [ fileSplit . length - 2 ]
71
+ const subCat = fileSplit [ fileSplit . length - 1 ] . replace ( '.md' , '' )
72
+ return { category : cat , subCategory : subCat }
73
+ }
58
74
75
+ beforeAll ( async ( ) => {
59
76
for ( const version in allVersions ) {
60
77
if ( isApiVersioned ( version ) ) {
61
78
for ( const apiVersion of allVersions [ version ] . apiVersions ) {
62
79
const apiOperations = await getRest ( version , apiVersion )
63
80
Object . keys ( apiOperations ) . forEach ( ( category ) => allCategories . add ( category ) )
81
+ openApiSchema [ version ] = apiOperations
64
82
}
65
83
} else {
66
84
const apiOperations = await getRest ( version )
67
85
Object . keys ( apiOperations ) . forEach ( ( category ) => allCategories . add ( category ) )
86
+ openApiSchema [ version ] = apiOperations
68
87
}
69
88
}
70
89
71
- const referenceDir = path . join ( 'content/rest' )
72
- const filenames = ( await fs . readdir ( referenceDir ) )
90
+ walk ( referenceDir , { includeBasePath : true , directories : false } )
91
+ . filter ( ( filename ) => filename . includes ( 'index.md' ) )
92
+ . forEach ( ( file ) => {
93
+ const applicableVersions = getApplicableVersionFromFile ( file )
94
+ const { category } = getCategorySubcategory ( file )
95
+ categoryApplicableVersions [ category ] = applicableVersions
96
+ } )
97
+ } )
98
+
99
+ test ( 'markdown file exists for every operationId prefix in all versions of the OpenAPI schema' , async ( ) => {
100
+ // list of REST markdown files that do not correspond to REST API resources
101
+ // TODO could we get this list dynamically, say via page frontmatter?
102
+ const filenames = fs
103
+ . readdirSync ( referenceDir )
73
104
. filter (
74
105
( filename ) =>
75
106
! excludeFromResourceNameCheck . find ( ( excludedFile ) => filename . endsWith ( excludedFile ) ) ,
@@ -84,11 +115,30 @@ describe('markdown for each rest version', () => {
84
115
'Found an OpenAPI REST operation category that is not represented by a markdown file in content/rest.'
85
116
expect ( difference ( [ ...allCategories ] , filenames ) , missingFile ) . toEqual ( [ ] )
86
117
} )
118
+
119
+ test ( 'category and subcategory exist in OpenAPI schema for every applicable version in markdown frontmatter' , async ( ) => {
120
+ walk ( referenceDir , { includeBasePath : true , directories : false } )
121
+ . filter (
122
+ ( filename ) => ! excludeFromResourceNameCheck . some ( ( pattern ) => filename . includes ( pattern ) ) ,
123
+ )
124
+ . forEach ( ( file ) => {
125
+ const applicableVersions = getApplicableVersionFromFile ( file )
126
+ const { category, subCategory } = getCategorySubcategory ( file )
127
+
128
+ for ( const version of applicableVersions ) {
129
+ expect (
130
+ Object . keys ( openApiSchema [ version ] [ category ] ) ,
131
+ `The REST version: ${ version } 's category: ${ category } does not include the subcategory: ${ subCategory } . Please check file: ${ file } ` ,
132
+ ) . toContain ( subCategory )
133
+ expect ( categoryApplicableVersions [ category ] ) . toContain ( version )
134
+ }
135
+ } )
136
+ } )
87
137
} )
88
138
89
139
describe ( 'rest file structure' , ( ) => {
90
140
test ( 'children of content/rest/index.md are in alphabetical order' , async ( ) => {
91
- const indexContent = await fs . readFile ( 'content/rest/index.md' , 'utf8' )
141
+ const indexContent = fs . readFileSync ( 'content/rest/index.md' , 'utf8' )
92
142
const { data } = readFrontmatter ( indexContent )
93
143
const sortableChildren = data . children . filter (
94
144
( child ) => child !== '/quickstart' && child !== '/overview' && child !== '/guides' ,
0 commit comments