@grafana/openapi-to-k6 0.2.6 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/README.md +5 -1
  2. package/dist/constants.js +7 -3
  3. package/dist/generator/index.js +13 -6
  4. package/dist/generator/k6Client.js +8 -56
  5. package/dist/generator/k6ScriptBuilder.js +256 -0
  6. package/dist/helper.js +16 -0
  7. package/examples/basic_schema/schema.json +2 -1
  8. package/examples/basic_schema/single/k6-script.sample.ts +3 -2
  9. package/examples/basic_schema/single/simpleAPI.ts +4 -1
  10. package/examples/basic_schema/split/k6-script.sample.ts +3 -2
  11. package/examples/basic_schema/split/simpleAPI.schemas.ts +2 -1
  12. package/examples/basic_schema/split/simpleAPI.ts +3 -1
  13. package/examples/basic_schema/tags/default.ts +3 -1
  14. package/examples/basic_schema/tags/k6-script.sample.ts +4 -3
  15. package/examples/basic_schema/tags/simpleAPI.schemas.ts +2 -1
  16. package/examples/form_data_schema/schema.json +6 -3
  17. package/examples/form_data_schema/single/formDataAPI.ts +8 -6
  18. package/examples/form_data_schema/single/k6-script.sample.ts +10 -2
  19. package/examples/form_data_schema/split/formDataAPI.schemas.ts +5 -5
  20. package/examples/form_data_schema/split/formDataAPI.ts +4 -2
  21. package/examples/form_data_schema/split/k6-script.sample.ts +10 -2
  22. package/examples/form_data_schema/tags/default.ts +4 -2
  23. package/examples/form_data_schema/tags/formDataAPI.schemas.ts +5 -5
  24. package/examples/form_data_schema/tags/k6-script.sample.ts +11 -3
  25. package/examples/form_url_encoded_data_schema/schema.json +6 -3
  26. package/examples/form_url_encoded_data_schema/single/formURLEncodedAPI.ts +8 -6
  27. package/examples/form_url_encoded_data_schema/single/k6-script.sample.ts +11 -2
  28. package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.schemas.ts +5 -5
  29. package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.ts +4 -2
  30. package/examples/form_url_encoded_data_schema/split/k6-script.sample.ts +11 -2
  31. package/examples/form_url_encoded_data_schema/tags/default.ts +4 -2
  32. package/examples/form_url_encoded_data_schema/tags/formURLEncodedAPI.schemas.ts +5 -5
  33. package/examples/form_url_encoded_data_schema/tags/k6-script.sample.ts +12 -3
  34. package/examples/form_url_encoded_data_with_query_params_schema/schema.json +8 -4
  35. package/examples/form_url_encoded_data_with_query_params_schema/single/formURLEncodedAPIWithQueryParameters.ts +8 -6
  36. package/examples/form_url_encoded_data_with_query_params_schema/single/k6-script.sample.ts +19 -6
  37. package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.schemas.ts +5 -5
  38. package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.ts +4 -2
  39. package/examples/form_url_encoded_data_with_query_params_schema/split/k6-script.sample.ts +19 -6
  40. package/examples/form_url_encoded_data_with_query_params_schema/tags/default.ts +4 -2
  41. package/examples/form_url_encoded_data_with_query_params_schema/tags/formURLEncodedAPIWithQueryParameters.schemas.ts +5 -5
  42. package/examples/form_url_encoded_data_with_query_params_schema/tags/k6-script.sample.ts +14 -3
  43. package/examples/get_request_with_path_parameters_schema/schema.json +2 -1
  44. package/examples/get_request_with_path_parameters_schema/single/k6-script.sample.ts +6 -2
  45. package/examples/get_request_with_path_parameters_schema/single/simpleAPI.ts +4 -2
  46. package/examples/get_request_with_path_parameters_schema/split/k6-script.sample.ts +6 -2
  47. package/examples/get_request_with_path_parameters_schema/split/simpleAPI.schemas.ts +2 -2
  48. package/examples/get_request_with_path_parameters_schema/split/simpleAPI.ts +3 -1
  49. package/examples/get_request_with_path_parameters_schema/tags/default.ts +3 -1
  50. package/examples/get_request_with_path_parameters_schema/tags/k6-script.sample.ts +7 -3
  51. package/examples/get_request_with_path_parameters_schema/tags/simpleAPI.schemas.ts +2 -2
  52. package/examples/headers_schema/schema.json +2 -1
  53. package/examples/headers_schema/single/headerDemoAPI.ts +5 -3
  54. package/examples/headers_schema/single/k6-script.sample.ts +15 -4
  55. package/examples/headers_schema/split/headerDemoAPI.schemas.ts +1 -1
  56. package/examples/headers_schema/split/headerDemoAPI.ts +5 -3
  57. package/examples/headers_schema/split/k6-script.sample.ts +15 -4
  58. package/examples/headers_schema/tags/default.ts +5 -3
  59. package/examples/headers_schema/tags/headerDemoAPI.schemas.ts +1 -1
  60. package/examples/headers_schema/tags/k6-script.sample.ts +16 -5
  61. package/examples/no_title_schema/single/k6-script.sample.ts +3 -2
  62. package/examples/no_title_schema/single/k6Client.ts +3 -1
  63. package/examples/no_title_schema/split/k6-script.sample.ts +3 -2
  64. package/examples/no_title_schema/split/k6Client.schemas.ts +1 -1
  65. package/examples/no_title_schema/split/k6Client.ts +3 -1
  66. package/examples/no_title_schema/tags/default.ts +3 -1
  67. package/examples/no_title_schema/tags/k6-script.sample.ts +4 -3
  68. package/examples/no_title_schema/tags/k6Client.schemas.ts +1 -1
  69. package/examples/post_request_with_query_params/schema.json +14 -7
  70. package/examples/post_request_with_query_params/single/exampleAPI.ts +8 -6
  71. package/examples/post_request_with_query_params/single/k6-script.sample.ts +11 -2
  72. package/examples/post_request_with_query_params/split/exampleAPI.schemas.ts +5 -5
  73. package/examples/post_request_with_query_params/split/exampleAPI.ts +4 -2
  74. package/examples/post_request_with_query_params/split/k6-script.sample.ts +11 -2
  75. package/examples/post_request_with_query_params/tags/default.ts +4 -2
  76. package/examples/post_request_with_query_params/tags/exampleAPI.schemas.ts +5 -5
  77. package/examples/post_request_with_query_params/tags/k6-script.sample.ts +12 -3
  78. package/examples/query_params_schema/schema.json +20 -10
  79. package/examples/query_params_schema/single/exampleAPI.ts +5 -3
  80. package/examples/query_params_schema/single/k6-script.sample.ts +8 -2
  81. package/examples/query_params_schema/split/exampleAPI.schemas.ts +3 -3
  82. package/examples/query_params_schema/split/exampleAPI.ts +3 -1
  83. package/examples/query_params_schema/split/k6-script.sample.ts +8 -2
  84. package/examples/query_params_schema/tags/default.ts +3 -1
  85. package/examples/query_params_schema/tags/exampleAPI.schemas.ts +3 -3
  86. package/examples/query_params_schema/tags/k6-script.sample.ts +9 -3
  87. package/examples/simple_post_request_schema/schema.json +30 -15
  88. package/examples/simple_post_request_schema/single/exampleAPI.ts +14 -12
  89. package/examples/simple_post_request_schema/single/k6-script.sample.ts +16 -2
  90. package/examples/simple_post_request_schema/split/exampleAPI.schemas.ts +11 -11
  91. package/examples/simple_post_request_schema/split/exampleAPI.ts +4 -2
  92. package/examples/simple_post_request_schema/split/k6-script.sample.ts +16 -2
  93. package/examples/simple_post_request_schema/tags/default.ts +4 -2
  94. package/examples/simple_post_request_schema/tags/exampleAPI.schemas.ts +11 -11
  95. package/examples/simple_post_request_schema/tags/k6-script.sample.ts +17 -3
  96. package/package.json +5 -2
  97. package/src/constants.ts +7 -3
  98. package/src/generator/index.ts +20 -7
  99. package/src/generator/k6Client.ts +6 -73
  100. package/src/generator/k6ScriptBuilder.ts +328 -0
  101. package/src/helper.ts +17 -0
  102. package/tests/e2e/schema.json +135 -18
  103. package/tests/e2e/single/k6Script.ts +54 -1
  104. package/tests/functional-tests/helper.ts +16 -0
  105. package/tests/functional-tests/test-generator/fixtures/basic_parameter_in_ref.json +59 -0
  106. package/tests/functional-tests/{fixtures/schemas → test-generator/fixtures}/form_data_schema.json +1 -1
  107. package/tests/functional-tests/{fixtures/schemas → test-generator/fixtures}/form_url_encoded_data_schema.json +1 -1
  108. package/tests/functional-tests/{fixtures/schemas → test-generator/fixtures}/headers_schema.json +2 -2
  109. package/tests/functional-tests/{fixtures/schemas → test-generator/fixtures}/simple_post_request_schema.json +1 -1
  110. package/tests/functional-tests/test-generator/generator.test.ts +154 -0
  111. package/tests/functional-tests/test-sample-k6-scripts/fixtures/schema_using_ref_models.json +394 -0
  112. package/tests/functional-tests/test-sample-k6-scripts/fixtures/schema_with_examples.json +416 -0
  113. package/tests/functional-tests/test-sample-k6-scripts/fixtures/schema_with_no_variables.json +32 -0
  114. package/tests/functional-tests/test-sample-k6-scripts/sampleK6Scripts.test.ts +248 -0
  115. package/tests/functional-tests/test-tags-filtering/tagsFiltering.test.ts +166 -0
  116. package/{vite.config.js → vite.config.mjs} +4 -0
  117. package/tests/functional-tests/generator.test.ts +0 -319
  118. /package/tests/functional-tests/{fixtures/schemas → test-generator/fixtures}/basic_schema.json +0 -0
  119. /package/tests/functional-tests/{fixtures/schemas → test-generator/fixtures}/form_url_encoded_data_with_query_params_schema.json +0 -0
  120. /package/tests/functional-tests/{fixtures/schemas → test-generator/fixtures}/get_request_with_path_parameters_schema.json +0 -0
  121. /package/tests/functional-tests/{fixtures/schemas → test-generator/fixtures}/no_title_schema.json +0 -0
  122. /package/tests/functional-tests/{fixtures/schemas → test-generator/fixtures}/post_request_with_query_params.json +0 -0
  123. /package/tests/functional-tests/{fixtures/schemas → test-generator/fixtures}/query_params_schema.json +0 -0
  124. /package/tests/functional-tests/{fixtures → test-tags-filtering/fixtures}/tags_filtering.json +0 -0
@@ -0,0 +1,166 @@
1
+ import fs from 'fs'
2
+ import os from 'os'
3
+ import path from 'path'
4
+ import { afterAll, beforeAll, describe, expect, it } from 'vitest'
5
+ import { Mode } from '../../../src/constants'
6
+ import { NoFilesGeneratedError } from '../../../src/errors'
7
+ import generator from '../../../src/generator'
8
+
9
+ import { loadFixture, mkdtemp, readFile, rmdir, writeFile } from '../helper'
10
+
11
+ describe('validate tags filtering', () => {
12
+ let tempDir: string, openApiPath: string
13
+ const tagsFilteringSchema = loadFixture(
14
+ path.join(__dirname, 'fixtures', 'tags_filtering.json')
15
+ )
16
+
17
+ beforeAll(async () => {
18
+ tempDir = await mkdtemp(path.join(os.tmpdir(), 'tags-filtering-test-'))
19
+ openApiPath = path.join(tempDir, 'openapi-schema.json')
20
+ await writeFile(openApiPath, JSON.stringify(tagsFilteringSchema))
21
+ })
22
+
23
+ afterAll(async () => {
24
+ await rmdir(tempDir, { recursive: true })
25
+ })
26
+
27
+ const generateAndTest = async (
28
+ mode: Mode,
29
+ tags: string[],
30
+ expectedFiles: string[],
31
+ expectedContents: string[],
32
+ unexpectedContents: string[]
33
+ ) => {
34
+ const generatedSchemaPath = path.join(
35
+ tempDir,
36
+ `generated-schema-${mode}-${tags.join('-')}`
37
+ )
38
+ await generator({
39
+ openApiPath,
40
+ outputDir: generatedSchemaPath,
41
+ tags,
42
+ mode,
43
+ })
44
+
45
+ const generatedFiles = fs.readdirSync(generatedSchemaPath)
46
+ expect(generatedFiles).to.have.members(expectedFiles)
47
+
48
+ for (const file of expectedFiles) {
49
+ if (file.includes('.schemas.ts')) {
50
+ // Skip schemas file
51
+ continue
52
+ }
53
+ const generatedFilePath = path.join(generatedSchemaPath, file)
54
+ const generatedContent = await readFile(generatedFilePath, 'utf-8')
55
+
56
+ for (const content of expectedContents) {
57
+ expect(generatedContent).to.contain(content)
58
+ }
59
+
60
+ for (const content of unexpectedContents) {
61
+ expect(generatedContent).not.to.contain(content)
62
+ }
63
+ }
64
+ }
65
+
66
+ it('should generate only endpoints with specified tags in single mode', async () => {
67
+ await generateAndTest(
68
+ Mode.SINGLE,
69
+ ['users'],
70
+ ['sampleAPI.ts'],
71
+ ['/users'],
72
+ ['/user-profiles', '/pets', '/auth']
73
+ )
74
+
75
+ await generateAndTest(
76
+ Mode.SINGLE,
77
+ ['users', 'userProfiles'],
78
+ ['sampleAPI.ts'],
79
+ ['/users', '/user-profiles'],
80
+ ['/pets', '/auth']
81
+ )
82
+
83
+ // Invalid tag should not generate any endpoints
84
+ await generateAndTest(
85
+ Mode.SINGLE,
86
+ ['invalid-tag'],
87
+ ['sampleAPI.ts'],
88
+ [],
89
+ ['/users', '/user-profiles', '/pets', '/auth']
90
+ )
91
+
92
+ // Empty tags should generate all endpoints
93
+ await generateAndTest(
94
+ Mode.SINGLE,
95
+ [],
96
+ ['sampleAPI.ts'],
97
+ ['/users', '/user-profiles', '/pets', '/auth'],
98
+ []
99
+ )
100
+ })
101
+
102
+ it('should generate only endpoints with specified tags in split mode', async () => {
103
+ await generateAndTest(
104
+ Mode.SPLIT,
105
+ ['users'],
106
+ ['sampleAPI.ts', 'sampleAPI.schemas.ts'],
107
+ ['/users'],
108
+ ['/user-profiles', '/pets', '/auth']
109
+ )
110
+
111
+ // Test empty tags
112
+ await generateAndTest(
113
+ Mode.SPLIT,
114
+ [],
115
+ ['sampleAPI.ts', 'sampleAPI.schemas.ts'],
116
+ ['/users', '/user-profiles', '/pets', '/auth'],
117
+ []
118
+ )
119
+
120
+ // Invalid tag should not generate any endpoints
121
+ // In the single mode it will create the client file which will have only the schemas
122
+ await expect(
123
+ generateAndTest(
124
+ Mode.SPLIT,
125
+ ['invalid-tag'],
126
+ [],
127
+ [],
128
+ ['/users', '/user-profiles', '/pets', '/auth']
129
+ )
130
+ ).rejects.toThrow(NoFilesGeneratedError)
131
+
132
+ // Empty tags should generate all endpoints
133
+ await generateAndTest(
134
+ Mode.SPLIT,
135
+ [],
136
+ ['sampleAPI.ts', 'sampleAPI.schemas.ts'],
137
+ ['/users', '/user-profiles', '/pets', '/auth'],
138
+ []
139
+ )
140
+ })
141
+
142
+ it('should generate only endpoints with specified tags in tags mode', async () => {
143
+ await generateAndTest(
144
+ Mode.TAGS,
145
+ ['users'],
146
+ ['users.ts', 'sampleAPI.schemas.ts'],
147
+ ['/users'],
148
+ ['/user-profiles', '/pets', '/auth']
149
+ )
150
+
151
+ // Empty tags should generate all endpoints
152
+ await generateAndTest(
153
+ Mode.TAGS,
154
+ [],
155
+ [
156
+ 'users.ts',
157
+ 'sampleAPI.schemas.ts',
158
+ 'auth.ts',
159
+ 'pets.ts',
160
+ 'user-profiles.ts',
161
+ ],
162
+ [], // Skipping test for content
163
+ []
164
+ )
165
+ })
166
+ })
@@ -1,8 +1,12 @@
1
1
  import { defineConfig } from 'vite'
2
+ import tsconfigPaths from 'vite-tsconfig-paths'
2
3
 
3
4
  export default defineConfig({
5
+ plugins: [tsconfigPaths()],
4
6
  test: {
5
7
  environment: 'node',
8
+ globals: true,
9
+ include: ['tests/**/*.test.ts'],
6
10
  coverage: {
7
11
  reporter: ['json', 'text', 'lcov'],
8
12
  reportsDirectory: 'coverage',
@@ -1,319 +0,0 @@
1
- import fs from 'fs'
2
- import os from 'os'
3
- import path from 'path'
4
- import { promisify } from 'util'
5
- import {
6
- afterAll,
7
- afterEach,
8
- beforeAll,
9
- beforeEach,
10
- describe,
11
- expect,
12
- it,
13
- } from 'vitest'
14
- import { Mode } from '../../src/constants'
15
- import { NoFilesGeneratedError } from '../../src/errors'
16
- import generator from '../../src/generator'
17
-
18
- const writeFile = promisify(fs.writeFile)
19
- const readFile = promisify(fs.readFile)
20
- const mkdtemp = promisify(fs.mkdtemp)
21
- const rmdir = promisify(fs.rmdir)
22
-
23
- const fixturesDir = path.join(__dirname, 'fixtures')
24
-
25
- const loadFixture = (filename: string) => {
26
- const filePath = path.join(fixturesDir, filename)
27
- const data = fs.readFileSync(filePath, 'utf-8')
28
- return JSON.parse(data)
29
- }
30
-
31
- const commonSubstringsForAllSDK = [
32
- 'Automatically generated by',
33
- 'Do not edit manually',
34
- 'Service version',
35
- 'const mergedRequestParameters = this._mergeRequestParameters( requestParameters || {}, this.commonRequestParameters, );',
36
- 'try { data = response.json(); } catch { data = response.body; }',
37
- 'return { response, data, };',
38
- ]
39
-
40
- const commonSubstringsForK6SampleScript = [`const baseUrl = "<BASE_URL>";`]
41
-
42
- function replaceSpacesAndNewLineToSingleSpace(input: string): string {
43
- return input.replace(/\s+/g, ' ')
44
- }
45
-
46
- describe('generator', () => {
47
- let tempDir: string, schemaDirectory: string
48
- const allFixtures = fs.readdirSync(path.join(fixturesDir, 'schemas'))
49
-
50
- beforeAll(async () => {
51
- tempDir = await mkdtemp(path.join(os.tmpdir(), 'sdk-generator-'))
52
- })
53
-
54
- afterAll(async () => {
55
- await rmdir(tempDir, { recursive: true })
56
- })
57
-
58
- for (const fixtureName of allFixtures) {
59
- describe(`test ${fixtureName} OpenAPI schema`, () => {
60
- let openApiPath: string, generatedSchemaPath: string
61
- const fixture = loadFixture(path.join('schemas', fixtureName))
62
-
63
- beforeEach(async () => {
64
- schemaDirectory = await mkdtemp(
65
- path.join(tempDir, fixtureName.replace('.', '-'))
66
- )
67
- // Write the OpenAPI schema to a file
68
-
69
- openApiPath = path.join(schemaDirectory, 'openapi-schema.json')
70
-
71
- await writeFile(openApiPath, JSON.stringify(fixture['openapi_schema']))
72
-
73
- generatedSchemaPath = path.join(schemaDirectory, 'generated-schema')
74
- })
75
-
76
- afterEach(async () => {
77
- await rmdir(schemaDirectory, { recursive: true })
78
- })
79
-
80
- it(`should generate SDK client from the OpenAPI schema`, async () => {
81
- const expectedGeneratedCode = fixture['expected_sdk']
82
-
83
- await generator({
84
- openApiPath,
85
- outputDir: generatedSchemaPath,
86
- mode: Mode.SINGLE,
87
- })
88
-
89
- const generatedFiles = fs.readdirSync(generatedSchemaPath)
90
- expect(generatedFiles.length).toBe(1)
91
- expect(generatedFiles[0]).toBeDefined()
92
- const fileName = path.basename(generatedFiles[0]!)
93
- expect(fileName).toBe(expectedGeneratedCode.fileName)
94
- const generatedFilePath = path.join(
95
- generatedSchemaPath,
96
- generatedFiles[0]!
97
- )
98
- const generatedContent = await readFile(generatedFilePath, 'utf-8')
99
-
100
- for (const expectedString of [
101
- ...expectedGeneratedCode['expectedSubstrings'],
102
- ...commonSubstringsForAllSDK,
103
- ]) {
104
- expect(
105
- replaceSpacesAndNewLineToSingleSpace(generatedContent)
106
- ).toContain(expectedString)
107
- }
108
- })
109
-
110
- it('should generate a sample K6 script', async () => {
111
- await generator({
112
- openApiPath,
113
- outputDir: generatedSchemaPath,
114
- shouldGenerateSampleK6Script: true,
115
- mode: Mode.SINGLE,
116
- })
117
-
118
- const generatedFiles = fs.readdirSync(generatedSchemaPath)
119
- expect(generatedFiles.length).toBe(2)
120
- const k6ScriptFile = generatedFiles.find((file) =>
121
- file.includes('k6-script')
122
- )
123
- expect(k6ScriptFile).toBeDefined()
124
- expect(k6ScriptFile).toBe('k6-script.sample.ts')
125
-
126
- const generatedFilePath = path.join(generatedSchemaPath, k6ScriptFile!)
127
- const generatedContent = await readFile(generatedFilePath, 'utf-8')
128
-
129
- for (const expectedString of [...commonSubstringsForK6SampleScript]) {
130
- expect(generatedContent).toContain(expectedString)
131
- }
132
- })
133
-
134
- it('should not contain types in main file when using split mode', async () => {
135
- await generator({
136
- openApiPath,
137
- outputDir: generatedSchemaPath,
138
- shouldGenerateSampleK6Script: true,
139
- mode: Mode.SPLIT,
140
- })
141
- const expectedGeneratedCode = fixture['expected_sdk']
142
- const generatedFiles = fs.readdirSync(generatedSchemaPath)
143
-
144
- expect(generatedFiles.length).toBe(3)
145
-
146
- const clientFile = generatedFiles.find((file) =>
147
- file.includes(expectedGeneratedCode.fileName)
148
- )
149
- const schemaFile = generatedFiles.find((file) =>
150
- file.includes('.schemas.ts')
151
- )
152
-
153
- expect(clientFile).toBeDefined()
154
- expect(schemaFile).toBeDefined()
155
-
156
- const generatedFilePath = path.join(generatedSchemaPath, clientFile!)
157
- const generatedContent = await readFile(generatedFilePath, 'utf-8')
158
-
159
- expect(generatedContent).not.toContain('export type')
160
- expect(generatedContent).not.toContain('export interface')
161
- })
162
- })
163
- }
164
-
165
- describe('validate tags filtering', () => {
166
- let tempDir: string, openApiPath: string
167
- const tagsFilteringSchema = loadFixture('tags_filtering.json')
168
-
169
- beforeAll(async () => {
170
- tempDir = await mkdtemp(path.join(os.tmpdir(), 'tags-filtering-test-'))
171
- openApiPath = path.join(tempDir, 'openapi-schema.json')
172
- await writeFile(openApiPath, JSON.stringify(tagsFilteringSchema))
173
- })
174
-
175
- afterAll(async () => {
176
- await rmdir(tempDir, { recursive: true })
177
- })
178
-
179
- const generateAndTest = async (
180
- mode: Mode,
181
- tags: string[],
182
- expectedFiles: string[],
183
- expectedContents: string[],
184
- unexpectedContents: string[]
185
- ) => {
186
- const generatedSchemaPath = path.join(
187
- tempDir,
188
- `generated-schema-${mode}-${tags.join('-')}`
189
- )
190
- await generator({
191
- openApiPath,
192
- outputDir: generatedSchemaPath,
193
- tags,
194
- mode,
195
- })
196
-
197
- const generatedFiles = fs.readdirSync(generatedSchemaPath)
198
- expect(generatedFiles).to.have.members(expectedFiles)
199
-
200
- for (const file of expectedFiles) {
201
- if (file.includes('.schemas.ts')) {
202
- // Skip schemas file
203
- continue
204
- }
205
- const generatedFilePath = path.join(generatedSchemaPath, file)
206
- const generatedContent = await readFile(generatedFilePath, 'utf-8')
207
-
208
- for (const content of expectedContents) {
209
- expect(generatedContent).to.contain(content)
210
- }
211
-
212
- for (const content of unexpectedContents) {
213
- expect(generatedContent).not.to.contain(content)
214
- }
215
- }
216
- }
217
-
218
- it('should generate only endpoints with specified tags in single mode', async () => {
219
- await generateAndTest(
220
- Mode.SINGLE,
221
- ['users'],
222
- ['sampleAPI.ts'],
223
- ['/users'],
224
- ['/user-profiles', '/pets', '/auth']
225
- )
226
-
227
- await generateAndTest(
228
- Mode.SINGLE,
229
- ['users', 'userProfiles'],
230
- ['sampleAPI.ts'],
231
- ['/users', '/user-profiles'],
232
- ['/pets', '/auth']
233
- )
234
-
235
- // Invalid tag should not generate any endpoints
236
- await generateAndTest(
237
- Mode.SINGLE,
238
- ['invalid-tag'],
239
- ['sampleAPI.ts'],
240
- [],
241
- ['/users', '/user-profiles', '/pets', '/auth']
242
- )
243
-
244
- // Empty tags should generate all endpoints
245
- await generateAndTest(
246
- Mode.SINGLE,
247
- [],
248
- ['sampleAPI.ts'],
249
- ['/users', '/user-profiles', '/pets', '/auth'],
250
- []
251
- )
252
- })
253
-
254
- it('should generate only endpoints with specified tags in split mode', async () => {
255
- await generateAndTest(
256
- Mode.SPLIT,
257
- ['users'],
258
- ['sampleAPI.ts', 'sampleAPI.schemas.ts'],
259
- ['/users'],
260
- ['/user-profiles', '/pets', '/auth']
261
- )
262
-
263
- // Test empty tags
264
- await generateAndTest(
265
- Mode.SPLIT,
266
- [],
267
- ['sampleAPI.ts', 'sampleAPI.schemas.ts'],
268
- ['/users', '/user-profiles', '/pets', '/auth'],
269
- []
270
- )
271
-
272
- // Invalid tag should not generate any endpoints
273
- // In the single mode it will create the client file which will have only the schemas
274
- await expect(
275
- generateAndTest(
276
- Mode.SPLIT,
277
- ['invalid-tag'],
278
- [],
279
- [],
280
- ['/users', '/user-profiles', '/pets', '/auth']
281
- )
282
- ).rejects.toThrow(NoFilesGeneratedError)
283
-
284
- // Empty tags should generate all endpoints
285
- await generateAndTest(
286
- Mode.SPLIT,
287
- [],
288
- ['sampleAPI.ts', 'sampleAPI.schemas.ts'],
289
- ['/users', '/user-profiles', '/pets', '/auth'],
290
- []
291
- )
292
- })
293
-
294
- it('should generate only endpoints with specified tags in tags mode', async () => {
295
- await generateAndTest(
296
- Mode.TAGS,
297
- ['users'],
298
- ['users.ts', 'sampleAPI.schemas.ts'],
299
- ['/users'],
300
- ['/user-profiles', '/pets', '/auth']
301
- )
302
-
303
- // Empty tags should generate all endpoints
304
- await generateAndTest(
305
- Mode.TAGS,
306
- [],
307
- [
308
- 'users.ts',
309
- 'sampleAPI.schemas.ts',
310
- 'auth.ts',
311
- 'pets.ts',
312
- 'user-profiles.ts',
313
- ],
314
- [], // Skipping test for content
315
- []
316
- )
317
- })
318
- })
319
- })