|
1
|
import { afterEach, describe, expect, it, vi } from "vitest" |
|
2
|
|
|
3
|
const archivedVersions = Array.from( |
|
4
|
{ length: 20 }, |
|
5
|
(_, index) => `2.${18 - index}.0` |
|
6
|
) |
|
7
|
|
|
8
|
vi.mock("@/utils/get-spec-versions", () => ({ |
|
9
|
getCachedSpecVersions: vi.fn(async () => archivedVersions), |
|
10
|
specVersionExists: vi.fn(async (version: string) => |
|
11
|
archivedVersions.includes(version) |
|
12
|
), |
|
13
|
})) |
|
14
|
|
|
15
|
vi.mock("docs-utils/global-config", () => ({ |
|
16
|
globalConfig: { |
|
17
|
version: { |
|
18
|
number: "2.19", |
|
19
|
releaseUrl: "https://github.com/medusajs/medusa/releases/tag/v2.19.0", |
|
20
|
releaseDate: "2026-08-13T13:09:55Z", |
|
21
|
}, |
|
22
|
}, |
|
23
|
})) |
|
24
|
|
|
25
|
import { GET } from "../route" |
|
26
|
import { |
|
27
|
getCachedSpecVersions, |
|
28
|
specVersionExists, |
|
29
|
} from "@/utils/get-spec-versions" |
|
30
|
|
|
31
|
const getResponse = (query?: string) => |
|
32
|
GET( |
|
33
|
new Request( |
|
34
|
`https://docs.medusajs.com/api/versions${query ? `?${query}` : ""}` |
|
35
|
) |
|
36
|
) |
|
37
|
|
|
38
|
const getVersions = async (query?: string) => { |
|
39
|
const response = await GET( |
|
40
|
new Request( |
|
41
|
`https://docs.medusajs.com/api/versions${query ? `?${query}` : ""}` |
|
42
|
) |
|
43
|
) |
|
44
|
|
|
45
|
return (await response.json()) as { |
|
46
|
versions: { version: string; admin_url: string; store_url: string }[] |
|
47
|
count: number |
|
48
|
limit: number |
|
49
|
offset: number |
|
50
|
} |
|
51
|
} |
|
52
|
|
|
53
|
describe("GET /versions", () => { |
|
54
|
afterEach(() => { |
|
55
|
delete process.env.SPECS_R2_BASE_URL |
|
56
|
vi.clearAllMocks() |
|
57
|
}) |
|
58
|
|
|
59
|
it("returns the first 15 versions, led by the latest one", async () => { |
|
60
|
const { versions, count, limit, offset } = await getVersions() |
|
61
|
|
|
62
|
expect(versions).toHaveLength(15) |
|
63
|
expect(versions.map(({ version }) => version)).toEqual([ |
|
64
|
"2.19.0", |
|
65
|
...archivedVersions.slice(0, 14), |
|
66
|
]) |
|
67
|
expect({ count, limit, offset }).toEqual({ |
|
68
|
count: 21, |
|
69
|
limit: 15, |
|
70
|
offset: 0, |
|
71
|
}) |
|
72
|
}) |
|
73
|
|
|
74
|
it("returns the versions at the requested offset and limit", async () => { |
|
75
|
const { versions, offset, limit } = await getVersions("offset=15&limit=5") |
|
76
|
|
|
77
|
expect({ offset, limit }).toEqual({ offset: 15, limit: 5 }) |
|
78
|
expect(versions.map(({ version }) => version)).toEqual( |
|
79
|
archivedVersions.slice(14, 19) |
|
80
|
) |
|
81
|
}) |
|
82
|
|
|
83
|
it("includes the latest version only at an offset of 0", async () => { |
|
84
|
const { versions } = await getVersions("offset=1&limit=2") |
|
85
|
|
|
86
|
expect(versions.map(({ version }) => version)).toEqual( |
|
87
|
archivedVersions.slice(0, 2) |
|
88
|
) |
|
89
|
}) |
|
90
|
|
|
91
|
it("clamps invalid and out-of-range limits and offsets", async () => { |
|
92
|
expect(await getVersions("limit=0")).toMatchObject({ limit: 1 }) |
|
93
|
expect(await getVersions("limit=500")).toMatchObject({ limit: 100 }) |
|
94
|
expect(await getVersions("limit=nope")).toMatchObject({ limit: 15 }) |
|
95
|
expect(await getVersions("offset=-5")).toMatchObject({ offset: 0 }) |
|
96
|
expect(await getVersions("offset=999")).toMatchObject({ |
|
97
|
offset: 21, |
|
98
|
versions: [], |
|
99
|
}) |
|
100
|
}) |
|
101
|
|
|
102
|
it("returns R2 JSON URLs when R2 is configured", async () => { |
|
103
|
process.env.SPECS_R2_BASE_URL = "https://assets.medusajs.com/api-reference" |
|
104
|
|
|
105
|
const { versions } = await getVersions() |
|
106
|
|
|
107
|
expect(versions[0]).toEqual({ |
|
108
|
version: "2.19.0", |
|
109
|
admin_url: |
|
110
|
"https://assets.medusajs.com/api-reference/specs/admin/openapi.full.json", |
|
111
|
store_url: |
|
112
|
"https://assets.medusajs.com/api-reference/specs/store/openapi.full.json", |
|
113
|
}) |
|
114
|
expect(versions[1]).toEqual({ |
|
115
|
version: "2.18.0", |
|
116
|
admin_url: |
|
117
|
"https://assets.medusajs.com/api-reference/specs/versions/2.18.0/admin/openapi.full.json", |
|
118
|
store_url: |
|
119
|
"https://assets.medusajs.com/api-reference/specs/versions/2.18.0/store/openapi.full.json", |
|
120
|
}) |
|
121
|
}) |
|
122
|
|
|
123
|
it("falls back to the download route when R2 isn't configured", async () => { |
|
124
|
const { versions } = await getVersions() |
|
125
|
|
|
126
|
expect(versions[0].admin_url).toBe( |
|
127
|
"https://docs.medusajs.com/api/download/admin" |
|
128
|
) |
|
129
|
expect(versions[1].store_url).toBe( |
|
130
|
"https://docs.medusajs.com/api/download/store?version=2.18.0" |
|
131
|
) |
|
132
|
}) |
|
133
|
|
|
134
|
describe("version filter", () => { |
|
135
|
it("returns only the requested archived version", async () => { |
|
136
|
const { versions, count, limit, offset } = |
|
137
|
await getVersions("version=2.15.0") |
|
138
|
|
|
139
|
expect(versions.map(({ version }) => version)).toEqual(["2.15.0"]) |
|
140
|
expect({ count, limit, offset }).toEqual({ |
|
141
|
count: 1, |
|
142
|
limit: 1, |
|
143
|
offset: 0, |
|
144
|
}) |
|
145
|
}) |
|
146
|
|
|
147
|
it("returns the latest version without looking it up", async () => { |
|
148
|
const { versions } = await getVersions("version=2.19.0") |
|
149
|
|
|
150
|
expect(versions[0].admin_url).toBe( |
|
151
|
"https://docs.medusajs.com/api/download/admin" |
|
152
|
) |
|
153
|
expect(specVersionExists).not.toHaveBeenCalled() |
|
154
|
}) |
|
155
|
|
|
156
|
it("never retrieves the full listing", async () => { |
|
157
|
await getVersions("version=2.15.0") |
|
158
|
|
|
159
|
expect(getCachedSpecVersions).not.toHaveBeenCalled() |
|
160
|
}) |
|
161
|
|
|
162
|
it("responds with a 404 for an unknown version", async () => { |
|
163
|
const response = await getResponse("version=9.9.9") |
|
164
|
|
|
165
|
expect(response.status).toBe(404) |
|
166
|
expect(await response.json()).toEqual({ |
|
167
|
error: { |
|
168
|
status: 404, |
|
169
|
name: "NotFound", |
|
170
|
message: 'No OpenAPI specs found for version "9.9.9"', |
|
171
|
}, |
|
172
|
}) |
|
173
|
}) |
|
174
|
}) |
|
175
|
}) |