|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import {afterEach, beforeEach, describe, expect, it} from 'vitest'; |
| 5 | + |
| 6 | +import { |
| 7 | + filePathToUrls, |
| 8 | + parseRedirectsJs, |
| 9 | + redirectMatches, |
| 10 | +} from './check-redirects-on-rename'; |
| 11 | + |
| 12 | +// Mock redirects fixture |
| 13 | +const mockRedirectsJs = ` |
| 14 | +const isDeveloperDocs = !!process.env.NEXT_PUBLIC_DEVELOPER_DOCS; |
| 15 | +
|
| 16 | +const developerDocsRedirects = [ |
| 17 | + { |
| 18 | + source: '/sdk/old-path/', |
| 19 | + destination: '/sdk/new-path/', |
| 20 | + }, |
| 21 | +]; |
| 22 | +
|
| 23 | +const userDocsRedirects = [ |
| 24 | + { |
| 25 | + source: '/platforms/javascript/old-guide/', |
| 26 | + destination: '/platforms/javascript/new-guide/', |
| 27 | + }, |
| 28 | + { |
| 29 | + source: '/platforms/python/old-tutorial', |
| 30 | + destination: '/platforms/python/new-tutorial', |
| 31 | + }, |
| 32 | +]; |
| 33 | +`; |
| 34 | + |
| 35 | +describe('filePathToUrls', () => { |
| 36 | + it('should convert docs file path to URLs', () => { |
| 37 | + const result = filePathToUrls('docs/platforms/javascript/index.mdx'); |
| 38 | + expect(result.isDeveloperDocs).toBe(false); |
| 39 | + expect(result.urls).toContain('/platforms/javascript/'); |
| 40 | + expect(result.urls).toContain('/platforms/javascript'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should convert develop-docs file path to URLs', () => { |
| 44 | + const result = filePathToUrls('develop-docs/backend/api/index.mdx'); |
| 45 | + expect(result.isDeveloperDocs).toBe(true); |
| 46 | + expect(result.urls).toContain('/backend/api/'); |
| 47 | + expect(result.urls).toContain('/backend/api'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should handle non-index files', () => { |
| 51 | + const result = filePathToUrls('docs/platforms/javascript/guide.mdx'); |
| 52 | + expect(result.isDeveloperDocs).toBe(false); |
| 53 | + expect(result.urls).toContain('/platforms/javascript/guide'); |
| 54 | + expect(result.urls).toContain('/platforms/javascript/guide/'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should return empty for paths outside docs/develop-docs', () => { |
| 58 | + const result = filePathToUrls('scripts/something.mdx'); |
| 59 | + expect(result.isDeveloperDocs).toBe(false); |
| 60 | + expect(result.urls).toEqual([]); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe('parseRedirectsJs', () => { |
| 65 | + let tempFile: string; |
| 66 | + |
| 67 | + beforeEach(() => { |
| 68 | + tempFile = path.join(process.cwd(), 'redirects-test-temp.js'); |
| 69 | + }); |
| 70 | + |
| 71 | + afterEach(() => { |
| 72 | + if (fs.existsSync(tempFile)) { |
| 73 | + fs.unlinkSync(tempFile); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it('should parse developer docs redirects', () => { |
| 78 | + fs.writeFileSync(tempFile, mockRedirectsJs); |
| 79 | + const result = parseRedirectsJs(tempFile); |
| 80 | + expect(result.developerDocsRedirects).toHaveLength(1); |
| 81 | + expect(result.developerDocsRedirects[0].source).toBe('/sdk/old-path/'); |
| 82 | + expect(result.developerDocsRedirects[0].destination).toBe('/sdk/new-path/'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should parse user docs redirects', () => { |
| 86 | + fs.writeFileSync(tempFile, mockRedirectsJs); |
| 87 | + const result = parseRedirectsJs(tempFile); |
| 88 | + expect(result.userDocsRedirects).toHaveLength(2); |
| 89 | + expect(result.userDocsRedirects[0].source).toBe('/platforms/javascript/old-guide/'); |
| 90 | + expect(result.userDocsRedirects[0].destination).toBe( |
| 91 | + '/platforms/javascript/new-guide/' |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should return empty arrays for non-existent file', () => { |
| 96 | + const result = parseRedirectsJs('/nonexistent/file.js'); |
| 97 | + expect(result.developerDocsRedirects).toEqual([]); |
| 98 | + expect(result.userDocsRedirects).toEqual([]); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should parse real redirects.js file', () => { |
| 102 | + const result = parseRedirectsJs('redirects.js'); |
| 103 | + // Should have some redirects |
| 104 | + expect(result.developerDocsRedirects.length).toBeGreaterThan(0); |
| 105 | + expect(result.userDocsRedirects.length).toBeGreaterThan(0); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('redirectMatches', () => { |
| 110 | + it('should match exact redirects', () => { |
| 111 | + const redirect = { |
| 112 | + source: '/old/path/', |
| 113 | + destination: '/new/path/', |
| 114 | + }; |
| 115 | + expect(redirectMatches(redirect, '/old/path/', '/new/path/')).toBe(true); |
| 116 | + expect(redirectMatches(redirect, '/different/path/', '/new/path/')).toBe(false); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should match redirects with path parameters', () => { |
| 120 | + const redirect = { |
| 121 | + source: '/platforms/:platform/old/:path*', |
| 122 | + destination: '/platforms/:platform/new/:path*', |
| 123 | + }; |
| 124 | + expect( |
| 125 | + redirectMatches( |
| 126 | + redirect, |
| 127 | + '/platforms/javascript/old/guide', |
| 128 | + '/platforms/javascript/new/guide' |
| 129 | + ) |
| 130 | + ).toBe(true); |
| 131 | + expect( |
| 132 | + redirectMatches( |
| 133 | + redirect, |
| 134 | + '/platforms/python/old/tutorial/', |
| 135 | + '/platforms/python/new/tutorial/' |
| 136 | + ) |
| 137 | + ).toBe(true); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should handle redirects with single path parameter', () => { |
| 141 | + const redirect = { |
| 142 | + source: '/platforms/:platform/old', |
| 143 | + destination: '/platforms/:platform/new', |
| 144 | + }; |
| 145 | + expect( |
| 146 | + redirectMatches(redirect, '/platforms/javascript/old', '/platforms/javascript/new') |
| 147 | + ).toBe(true); |
| 148 | + expect( |
| 149 | + redirectMatches(redirect, '/platforms/python/old', '/platforms/python/new') |
| 150 | + ).toBe(true); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should not match when source pattern does not match', () => { |
| 154 | + const redirect = { |
| 155 | + source: '/platforms/:platform/old', |
| 156 | + destination: '/platforms/:platform/new', |
| 157 | + }; |
| 158 | + expect( |
| 159 | + redirectMatches(redirect, '/different/path', '/platforms/javascript/new') |
| 160 | + ).toBe(false); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should match destination with exact path when no params', () => { |
| 164 | + const redirect = { |
| 165 | + source: '/old/path', |
| 166 | + destination: '/new/exact/path', |
| 167 | + }; |
| 168 | + expect(redirectMatches(redirect, '/old/path', '/new/exact/path')).toBe(true); |
| 169 | + expect(redirectMatches(redirect, '/old/path', '/different/path')).toBe(false); |
| 170 | + }); |
| 171 | +}); |
0 commit comments