|
1
|
import { describe, it, expect } from "vitest" |
|
2
|
import type { OpenAPI } from "types" |
|
3
|
import operationToMarkdown from "../operation-to-markdown" |
|
4
|
|
|
5
|
const baseSpecs = { |
|
6
|
components: { |
|
7
|
securitySchemes: { |
|
8
|
api_token: { type: "http", "x-displayName": "API Token" }, |
|
9
|
}, |
|
10
|
}, |
|
11
|
} as unknown as OpenAPI.ExpandedDocument |
|
12
|
|
|
13
|
const buildOptions = () => ({ |
|
14
|
area: "admin" as const, |
|
15
|
baseUrl: "https://docs.medusajs.com", |
|
16
|
basePath: "/api", |
|
17
|
endpointPath: "/admin/products/{id}", |
|
18
|
method: "post", |
|
19
|
baseSpecs, |
|
20
|
}) |
|
21
|
|
|
22
|
describe("operationToMarkdown", () => { |
|
23
|
it("renders heading, method+path, description and sections", () => { |
|
24
|
const operation = { |
|
25
|
operationId: "postProductsId", |
|
26
|
summary: "Update Product", |
|
27
|
description: "Updates a product.", |
|
28
|
security: [{ api_token: [] }], |
|
29
|
parameters: [ |
|
30
|
{ |
|
31
|
name: "id", |
|
32
|
in: "path", |
|
33
|
required: true, |
|
34
|
schema: { type: "string" }, |
|
35
|
description: "The product ID.", |
|
36
|
}, |
|
37
|
], |
|
38
|
requestBody: { |
|
39
|
content: { |
|
40
|
"application/json": { |
|
41
|
schema: { |
|
42
|
type: "object", |
|
43
|
required: ["title"], |
|
44
|
properties: { title: { type: "string" } }, |
|
45
|
}, |
|
46
|
}, |
|
47
|
}, |
|
48
|
}, |
|
49
|
responses: { |
|
50
|
"200": { |
|
51
|
description: "OK", |
|
52
|
content: { |
|
53
|
"application/json": { |
|
54
|
schema: { |
|
55
|
type: "object", |
|
56
|
properties: { id: { type: "string" } }, |
|
57
|
}, |
|
58
|
}, |
|
59
|
}, |
|
60
|
}, |
|
61
|
}, |
|
62
|
"x-codeSamples": [{ lang: "bash", label: "cURL", source: "curl ..." }], |
|
63
|
"x-workflow": "updateProductsWorkflow", |
|
64
|
} as unknown as OpenAPI.Operation |
|
65
|
|
|
66
|
const md = operationToMarkdown(operation, buildOptions()) |
|
67
|
|
|
68
|
expect(md).toContain("## Update Product") |
|
69
|
expect(md).toContain("`POST /admin/products/{id}`") |
|
70
|
expect(md).toContain("Updates a product.") |
|
71
|
expect(md).toContain("**Authorization:** API Token") |
|
72
|
expect(md).toContain( |
|
73
|
"Workflow: [updateProductsWorkflow](https://docs.medusajs.com/resources/references/medusa-workflows/updateProductsWorkflow)" |
|
74
|
) |
|
75
|
expect(md).toContain("### Path Parameters") |
|
76
|
expect(md).toContain("### Request Body") |
|
77
|
expect(md).toContain("### Responses") |
|
78
|
expect(md).toContain("#### 200 OK") |
|
79
|
expect(md).toContain("### Code Samples") |
|
80
|
expect(md).toContain("```bash") |
|
81
|
// example generated from the 200 response schema |
|
82
|
expect(md).toContain("### Example") |
|
83
|
}) |
|
84
|
|
|
85
|
it("marks deprecated/since/badges in the heading", () => { |
|
86
|
const operation = { |
|
87
|
operationId: "getThing", |
|
88
|
summary: "Get Thing", |
|
89
|
deprecated: true, |
|
90
|
"x-since": "2.5.0", |
|
91
|
"x-badges": [{ text: "beta", description: "Beta feature" }], |
|
92
|
responses: {}, |
|
93
|
} as unknown as OpenAPI.Operation |
|
94
|
|
|
95
|
const md = operationToMarkdown(operation, { |
|
96
|
...buildOptions(), |
|
97
|
method: "get", |
|
98
|
}) |
|
99
|
expect(md).toContain("## Get Thing (deprecated, since v2.5.0, beta)") |
|
100
|
// badge description is preserved as a note (not dropped) |
|
101
|
expect(md).toContain("> **beta:** Beta feature") |
|
102
|
}) |
|
103
|
|
|
104
|
it("normalizes code-sample fence languages", () => { |
|
105
|
const operation = { |
|
106
|
operationId: "op", |
|
107
|
summary: "Op", |
|
108
|
responses: {}, |
|
109
|
"x-codeSamples": [ |
|
110
|
{ lang: "JavaScript", label: "JS SDK", source: "const a = 1" }, |
|
111
|
{ lang: "Shell", label: "cURL", source: "curl x" }, |
|
112
|
], |
|
113
|
} as unknown as OpenAPI.Operation |
|
114
|
|
|
115
|
const md = operationToMarkdown(operation, { |
|
116
|
...buildOptions(), |
|
117
|
method: "get", |
|
118
|
}) |
|
119
|
expect(md).toContain("```js\nconst a = 1") |
|
120
|
expect(md).toContain("```bash\ncurl x") |
|
121
|
expect(md).not.toContain("```JavaScript") |
|
122
|
expect(md).not.toContain("```Shell") |
|
123
|
}) |
|
124
|
|
|
125
|
it("does not double-wrap an already-fenced event payload", () => { |
|
126
|
const operation = { |
|
127
|
operationId: "op", |
|
128
|
summary: "Op", |
|
129
|
responses: {}, |
|
130
|
"x-events": [ |
|
131
|
{ |
|
132
|
name: "cart.created", |
|
133
|
description: "Emitted when a cart is created.", |
|
134
|
payload: "```ts\n{\n id,\n}\n```", |
|
135
|
}, |
|
136
|
], |
|
137
|
} as unknown as OpenAPI.Operation |
|
138
|
|
|
139
|
const md = operationToMarkdown(operation, { |
|
140
|
...buildOptions(), |
|
141
|
method: "get", |
|
142
|
}) |
|
143
|
expect(md).toContain("```ts\n{\n id,\n}\n```") |
|
144
|
// no ```json wrapper around the already-fenced payload |
|
145
|
expect(md).not.toContain("```json\n```ts") |
|
146
|
}) |
|
147
|
|
|
148
|
it("shows Empty response for responses without content", () => { |
|
149
|
const operation = { |
|
150
|
operationId: "del", |
|
151
|
summary: "Delete", |
|
152
|
responses: { "200": { description: "OK" } }, |
|
153
|
} as unknown as OpenAPI.Operation |
|
154
|
|
|
155
|
const md = operationToMarkdown(operation, { |
|
156
|
...buildOptions(), |
|
157
|
method: "delete", |
|
158
|
}) |
|
159
|
expect(md).toContain("#### 200 OK") |
|
160
|
expect(md).toContain("Empty response") |
|
161
|
}) |
|
162
|
}) |