FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sentry-docs/scripts/lint-redirect-chains.spec.ts at master · getsentry/sentry-docs · GitHub
getsentry
sentry-docs
Repository navigation
Code
Issues
414
(414)
Pull requests
250
(250)
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sentry-docs
/
scripts
/
lint-redirect-chains.spec.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
370 lines (317 loc) · 13.4 KB
Breadcrumbs
sentry-docs
/
scripts
/
lint-redirect-chains.spec.ts
Copy path
File metadata and controls
370 lines (317 loc) · 13.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import
fs
from
'fs'
;
import
path
from
'path'
;
import
{
afterEach
,
beforeEach
,
describe
,
expect
,
it
}
from
'vitest'
;
import
{
parseMiddlewareTs
,
parseRedirectsJs
}
from
'./check-redirects-on-rename'
;
import
{
detectRedirectChains
,
resolveToFinal
,
walkChain
}
from
'./lint-redirect-chains'
;
describe
(
'walkChain'
,
(
)
=>
{
it
(
'should return just the source when no chain exists'
,
(
)
=>
{
const
map
=
new
Map
(
[
[
'/a/'
,
'/b/'
]
,
[
'/c/'
,
'/d/'
]
,
]
)
;
expect
(
walkChain
(
'/a/'
,
map
)
)
.
toEqual
(
[
'/a/'
,
'/b/'
]
)
;
}
)
;
it
(
'should walk a 2-hop chain'
,
(
)
=>
{
const
map
=
new
Map
(
[
[
'/a/'
,
'/b/'
]
,
[
'/b/'
,
'/c/'
]
,
]
)
;
expect
(
walkChain
(
'/a/'
,
map
)
)
.
toEqual
(
[
'/a/'
,
'/b/'
,
'/c/'
]
)
;
}
)
;
it
(
'should walk a 3-hop chain'
,
(
)
=>
{
const
map
=
new
Map
(
[
[
'/a/'
,
'/b/'
]
,
[
'/b/'
,
'/c/'
]
,
[
'/c/'
,
'/d/'
]
,
]
)
;
expect
(
walkChain
(
'/a/'
,
map
)
)
.
toEqual
(
[
'/a/'
,
'/b/'
,
'/c/'
,
'/d/'
]
)
;
}
)
;
it
(
'should detect cycles'
,
(
)
=>
{
const
map
=
new
Map
(
[
[
'/a/'
,
'/b/'
]
,
[
'/b/'
,
'/a/'
]
,
]
)
;
const
result
=
walkChain
(
'/a/'
,
map
)
;
expect
(
result
)
.
toEqual
(
[
'/a/'
,
'/b/'
,
'/a/ (CYCLE)'
]
)
;
}
)
;
it
(
'should detect self-referencing cycles'
,
(
)
=>
{
const
map
=
new
Map
(
[
[
'/a/'
,
'/a/'
]
]
)
;
const
result
=
walkChain
(
'/a/'
,
map
)
;
expect
(
result
)
.
toEqual
(
[
'/a/'
,
'/a/ (CYCLE)'
]
)
;
}
)
;
it
(
'should cap at maxDepth'
,
(
)
=>
{
const
map
=
new
Map
<
string
,
string
>
(
)
;
for
(
let
i
=
0
;
i
<
20
;
i
++
)
{
map
.
set
(
`/
${
i
}
/`
,
`/
${
i
+
1
}
/`
)
;
}
const
result
=
walkChain
(
'/0/'
,
map
,
5
)
;
expect
(
result
)
.
toHaveLength
(
6
)
;
// source + 5 hops
}
)
;
}
)
;
describe
(
'resolveToFinal'
,
(
)
=>
{
it
(
'should resolve a single redirect'
,
(
)
=>
{
const
map
=
new
Map
(
[
[
'/a/'
,
'/b/'
]
]
)
;
expect
(
resolveToFinal
(
'/a/'
,
map
)
)
.
toBe
(
'/b/'
)
;
}
)
;
it
(
'should resolve a chain to the final destination'
,
(
)
=>
{
const
map
=
new
Map
(
[
[
'/a/'
,
'/b/'
]
,
[
'/b/'
,
'/c/'
]
,
[
'/c/'
,
'/d/'
]
,
]
)
;
expect
(
resolveToFinal
(
'/a/'
,
map
)
)
.
toBe
(
'/d/'
)
;
}
)
;
it
(
'should handle self-referencing cycles without infinite loop'
,
(
)
=>
{
const
map
=
new
Map
(
[
[
'/a/'
,
'/a/'
]
]
)
;
expect
(
resolveToFinal
(
'/a/'
,
map
)
)
.
toBe
(
'/a/'
)
;
}
)
;
it
(
'should return the source when not in the map'
,
(
)
=>
{
const
map
=
new
Map
(
[
[
'/a/'
,
'/b/'
]
]
)
;
expect
(
resolveToFinal
(
'/x/'
,
map
)
)
.
toBe
(
'/x/'
)
;
}
)
;
}
)
;
describe
(
'detectRedirectChains'
,
(
)
=>
{
it
(
'should detect chains within a single redirect source'
,
(
)
=>
{
const
jsRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
{
source
:
'/old/'
,
destination
:
'/middle/'
}
,
{
source
:
'/middle/'
,
destination
:
'/new/'
}
,
]
,
}
;
const
mwRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
}
;
const
chains
=
detectRedirectChains
(
jsRedirects
,
mwRedirects
)
;
expect
(
chains
)
.
toHaveLength
(
1
)
;
expect
(
chains
[
0
]
.
source
)
.
toBe
(
'/old/'
)
;
expect
(
chains
[
0
]
.
currentDest
)
.
toBe
(
'/middle/'
)
;
expect
(
chains
[
0
]
.
finalDest
)
.
toBe
(
'/new/'
)
;
expect
(
chains
[
0
]
.
file
)
.
toBe
(
'redirects.js'
)
;
expect
(
chains
[
0
]
.
isDeveloperDocs
)
.
toBe
(
false
)
;
}
)
;
it
(
'should detect cross-system chains (middleware -> redirects.js)'
,
(
)
=>
{
const
jsRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
{
source
:
'/middle/'
,
destination
:
'/final/'
}
]
,
}
;
const
mwRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
{
source
:
'/old/'
,
destination
:
'/middle/'
}
]
,
}
;
const
chains
=
detectRedirectChains
(
jsRedirects
,
mwRedirects
)
;
expect
(
chains
)
.
toHaveLength
(
1
)
;
expect
(
chains
[
0
]
.
source
)
.
toBe
(
'/old/'
)
;
expect
(
chains
[
0
]
.
finalDest
)
.
toBe
(
'/final/'
)
;
expect
(
chains
[
0
]
.
file
)
.
toBe
(
'middleware.ts'
)
;
}
)
;
it
(
'should report duplicate sources from both files separately'
,
(
)
=>
{
const
jsRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
{
source
:
'/dup/'
,
destination
:
'/middle/'
}
]
,
}
;
const
mwRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
{
source
:
'/dup/'
,
destination
:
'/other-middle/'
}
,
{
source
:
'/middle/'
,
destination
:
'/final/'
}
,
{
source
:
'/other-middle/'
,
destination
:
'/other-final/'
}
,
]
,
}
;
const
chains
=
detectRedirectChains
(
jsRedirects
,
mwRedirects
)
;
// Both entries for /dup/ should be reported with correct file attribution
const
dupChains
=
chains
.
filter
(
c
=>
c
.
source
===
'/dup/'
)
;
expect
(
dupChains
)
.
toHaveLength
(
2
)
;
const
jsChain
=
dupChains
.
find
(
c
=>
c
.
file
===
'redirects.js'
)
;
const
mwChain
=
dupChains
.
find
(
c
=>
c
.
file
===
'middleware.ts'
)
;
expect
(
jsChain
)
.
toBeDefined
(
)
;
expect
(
mwChain
)
.
toBeDefined
(
)
;
// Each entry's chain should be consistent with its own currentDest
expect
(
jsChain
!
.
currentDest
)
.
toBe
(
'/middle/'
)
;
expect
(
jsChain
!
.
chain
[
1
]
)
.
toBe
(
'/middle/'
)
;
// chain starts source -> currentDest
expect
(
jsChain
!
.
finalDest
)
.
toBe
(
'/final/'
)
;
expect
(
mwChain
!
.
currentDest
)
.
toBe
(
'/other-middle/'
)
;
expect
(
mwChain
!
.
chain
[
1
]
)
.
toBe
(
'/other-middle/'
)
;
expect
(
mwChain
!
.
finalDest
)
.
toBe
(
'/other-final/'
)
;
}
)
;
it
(
'should return empty array when no chains exist'
,
(
)
=>
{
const
jsRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
{
source
:
'/a/'
,
destination
:
'/b/'
}
,
{
source
:
'/c/'
,
destination
:
'/d/'
}
,
]
,
}
;
const
mwRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
}
;
const
chains
=
detectRedirectChains
(
jsRedirects
,
mwRedirects
)
;
expect
(
chains
)
.
toHaveLength
(
0
)
;
}
)
;
it
(
'should handle self-redirect cycles'
,
(
)
=>
{
const
jsRedirects
=
{
developerDocsRedirects
:
[
{
source
:
'/loop/'
,
destination
:
'/loop/'
}
]
,
userDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
}
;
const
mwRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
}
;
const
chains
=
detectRedirectChains
(
jsRedirects
,
mwRedirects
)
;
expect
(
chains
)
.
toHaveLength
(
1
)
;
expect
(
chains
[
0
]
.
finalDest
)
.
toBe
(
'(CYCLE DETECTED)'
)
;
}
)
;
it
(
'should handle parameterized redirect chains'
,
(
)
=>
{
const
jsRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
{
source
:
'/old/:path*'
,
destination
:
'/middle/:path*'
}
,
{
source
:
'/middle/:path*'
,
destination
:
'/new/:path*'
}
,
]
,
}
;
const
mwRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
}
;
const
chains
=
detectRedirectChains
(
jsRedirects
,
mwRedirects
)
;
expect
(
chains
)
.
toHaveLength
(
1
)
;
expect
(
chains
[
0
]
.
source
)
.
toBe
(
'/old/:path*'
)
;
expect
(
chains
[
0
]
.
finalDest
)
.
toBe
(
'/new/:path*'
)
;
}
)
;
it
(
'should separate user docs and developer docs'
,
(
)
=>
{
const
jsRedirects
=
{
developerDocsRedirects
:
[
{
source
:
'/sdk/old/'
,
destination
:
'/sdk/middle/'
}
,
{
source
:
'/sdk/middle/'
,
destination
:
'/sdk/new/'
}
,
]
,
userDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
}
;
const
mwRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
}
;
const
chains
=
detectRedirectChains
(
jsRedirects
,
mwRedirects
)
;
expect
(
chains
)
.
toHaveLength
(
1
)
;
expect
(
chains
[
0
]
.
isDeveloperDocs
)
.
toBe
(
true
)
;
}
)
;
it
(
'should detect chains with trailing slash mismatches'
,
(
)
=>
{
const
jsRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
{
source
:
'/old/'
,
destination
:
'/middle'
}
,
// no trailing slash on dest
{
source
:
'/middle/'
,
destination
:
'/new/'
}
,
// source has trailing slash
]
,
}
;
const
mwRedirects
=
{
developerDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
userDocsRedirects
:
[
]
as
Array
<
{
destination
:
string
;
source
:
string
}
>
,
}
;
const
chains
=
detectRedirectChains
(
jsRedirects
,
mwRedirects
)
;
expect
(
chains
)
.
toHaveLength
(
1
)
;
expect
(
chains
[
0
]
.
source
)
.
toBe
(
'/old/'
)
;
expect
(
chains
[
0
]
.
finalDest
)
.
toBe
(
'/new/'
)
;
}
)
;
}
)
;
describe
(
'detectContentLinkIssues'
,
(
)
=>
{
let
tempDir
:
string
;
beforeEach
(
(
)
=>
{
tempDir
=
path
.
join
(
process
.
cwd
(
)
,
'__test_mdx_temp__'
)
;
fs
.
mkdirSync
(
path
.
join
(
tempDir
,
'docs'
)
,
{
recursive
:
true
}
)
;
}
)
;
afterEach
(
(
)
=>
{
fs
.
rmSync
(
tempDir
,
{
recursive
:
true
,
force
:
true
}
)
;
}
)
;
it
(
'should detect markdown links pointing to redirect sources'
,
(
)
=>
{
const
mdxContent
=
`---
title: Test
---
Check out the [old page](/old/path/) for more info.
`
;
fs
.
writeFileSync
(
path
.
join
(
tempDir
,
'docs'
,
'test.mdx'
)
,
mdxContent
)
;
// Test the link detection logic by checking the regex patterns directly
const
markdownLinkRegex
=
/
\]
\(
(
\/
[
^
)
#
\s
]
+
?
)
(
#
[
^
)
]
+
)
?
\)
/
g
;
const
matches
=
[
...
mdxContent
.
matchAll
(
markdownLinkRegex
)
]
;
expect
(
matches
)
.
toHaveLength
(
1
)
;
expect
(
matches
[
0
]
[
1
]
)
.
toBe
(
'/old/path/'
)
;
}
)
;
it
(
'should detect JSX links pointing to redirect sources'
,
(
)
=>
{
const
mdxContent
=
`<LinkCard href="/old/path/" title="Test" />`
;
const
jsxLinkRegex
=
/
(
(?:
h
r
e
f
|
t
o
|
u
r
l
)
)
=
"
(
\/
[
^
"
#
]
+
?
)
(
#
[
^
"
]
+
)
?
"
/
g
;
const
matches
=
[
...
mdxContent
.
matchAll
(
jsxLinkRegex
)
]
;
expect
(
matches
)
.
toHaveLength
(
1
)
;
expect
(
matches
[
0
]
[
2
]
)
.
toBe
(
'/old/path/'
)
;
}
)
;
it
(
'should extract path and fragment separately from anchored links'
,
(
)
=>
{
const
mdxContent
=
`[link](/old/path/#section)`
;
const
markdownLinkRegex
=
/
\]
\(
(
\/
[
^
)
#
\s
]
+
?
)
(
#
[
^
)
]
+
)
?
\)
/
g
;
const
matches
=
[
...
mdxContent
.
matchAll
(
markdownLinkRegex
)
]
;
expect
(
matches
)
.
toHaveLength
(
1
)
;
expect
(
matches
[
0
]
[
1
]
)
.
toBe
(
'/old/path/'
)
;
expect
(
matches
[
0
]
[
2
]
)
.
toBe
(
'#section'
)
;
}
)
;
it
(
'should skip external links'
,
(
)
=>
{
const
mdxContent
=
`[link](https://example.com/path/)`
;
const
markdownLinkRegex
=
/
\]
\(
(
\/
[
^
)
#
\s
]
+
?
)
(
#
[
^
)
]
+
)
?
\)
/
g
;
const
matches
=
[
...
mdxContent
.
matchAll
(
markdownLinkRegex
)
]
;
expect
(
matches
)
.
toHaveLength
(
0
)
;
}
)
;
it
(
'should skip hash-only links'
,
(
)
=>
{
const
mdxContent
=
`[link](#section)`
;
const
markdownLinkRegex
=
/
\]
\(
(
\/
[
^
)
#
\s
]
+
?
)
(
#
[
^
)
]
+
)
?
\)
/
g
;
const
matches
=
[
...
mdxContent
.
matchAll
(
markdownLinkRegex
)
]
;
expect
(
matches
)
.
toHaveLength
(
0
)
;
}
)
;
it
(
'should handle links without trailing slashes'
,
(
)
=>
{
const
mdxContent
=
`[link](/old/path)`
;
const
markdownLinkRegex
=
/
\]
\(
(
\/
[
^
)
#
\s
]
+
?
)
(
#
[
^
)
]
+
)
?
\)
/
g
;
const
matches
=
[
...
mdxContent
.
matchAll
(
markdownLinkRegex
)
]
;
expect
(
matches
)
.
toHaveLength
(
1
)
;
expect
(
matches
[
0
]
[
1
]
)
.
toBe
(
'/old/path'
)
;
// Normalization adds trailing slash: /old/path -> /old/path/
}
)
;
it
(
'should skip PlatformLink to= attributes (platform-relative paths)'
,
(
)
=>
{
// PlatformLink to= should be skipped since PlatformLink prepends
// the platform base URL, making these platform-relative paths.
// The linter checks for <PlatformLink on the line level.
const
line
=
`<PlatformLink to="/old/feature/">text</PlatformLink>`
;
const
isPlatformLinkLine
=
line
.
includes
(
'<PlatformLink'
)
;
expect
(
isPlatformLinkLine
)
.
toBe
(
true
)
;
// The to= attribute IS matched by the regex...
const
jsxLinkRegex
=
/
(
(?:
h
r
e
f
|
t
o
|
u
r
l
)
)
=
"
(
\/
[
^
"
#
]
+
?
)
(
#
[
^
"
]
+
)
?
"
/
g
;
const
matches
=
[
...
line
.
matchAll
(
jsxLinkRegex
)
]
;
expect
(
matches
)
.
toHaveLength
(
1
)
;
expect
(
matches
[
0
]
[
1
]
)
.
toBe
(
'to'
)
;
// attribute name
// ...but the linter skips it because isPlatformLinkLine is true and attr is 'to'
}
)
;
it
(
'should skip PlatformLink with props before to= attribute'
,
(
)
=>
{
// Handles case where PlatformLink has other props first
const
line
=
`<PlatformLink platform="android" to="/old/feature/">text</PlatformLink>`
;
const
isPlatformLinkLine
=
line
.
includes
(
'<PlatformLink'
)
;
expect
(
isPlatformLinkLine
)
.
toBe
(
true
)
;
// Skipped because the line contains <PlatformLink and attr is 'to'
}
)
;
it
(
'should detect non-PlatformLink to= attributes'
,
(
)
=>
{
const
line
=
`<Link to="/old/feature/">text</Link>`
;
const
isPlatformLinkLine
=
line
.
includes
(
'<PlatformLink'
)
;
expect
(
isPlatformLinkLine
)
.
toBe
(
false
)
;
const
jsxLinkRegex
=
/
(
(?:
h
r
e
f
|
t
o
|
u
r
l
)
)
=
"
(
\/
[
^
"
#
]
+
?
)
(
#
[
^
"
]
+
)
?
"
/
g
;
const
matches
=
[
...
line
.
matchAll
(
jsxLinkRegex
)
]
;
expect
(
matches
)
.
toHaveLength
(
1
)
;
expect
(
matches
[
0
]
[
2
]
)
.
toBe
(
'/old/feature/'
)
;
// Not skipped because isPlatformLinkLine is false
}
)
;
}
)
;
describe
(
'integration: real redirect files'
,
(
)
=>
{
it
(
'should parse real redirects.js without errors'
,
(
)
=>
{
const
result
=
parseRedirectsJs
(
'redirects.js'
)
;
expect
(
result
.
userDocsRedirects
.
length
)
.
toBeGreaterThan
(
0
)
;
expect
(
result
.
developerDocsRedirects
.
length
)
.
toBeGreaterThan
(
0
)
;
}
)
;
it
(
'should parse real middleware.ts without errors'
,
(
)
=>
{
const
result
=
parseMiddlewareTs
(
'middleware.ts'
)
;
expect
(
result
.
userDocsRedirects
.
length
)
.
toBeGreaterThan
(
0
)
;
expect
(
result
.
developerDocsRedirects
.
length
)
.
toBeGreaterThan
(
0
)
;
}
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL