|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'bun:test' |
| 2 | +import * as fs from 'node:fs' |
| 3 | +import * as os from 'node:os' |
| 4 | +import * as path from 'node:path' |
| 5 | +import { Router } from '../src/file-router' |
| 6 | + |
| 7 | +/** |
| 8 | + * Layered page roots: the Router accepts an ordered stack of page |
| 9 | + * directories. Earlier roots shadow later ones for the same pattern, |
| 10 | + * which lets a framework ship default views (cart, checkout, orders) |
| 11 | + * and let apps override them just by dropping a file with the same |
| 12 | + * relative path into their own views directory. |
| 13 | + */ |
| 14 | +describe('Router with layered pagesDirs', () => { |
| 15 | + let baseDir: string |
| 16 | + let userDir: string |
| 17 | + let defaultsDir: string |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + baseDir = fs.mkdtempSync(path.join(os.tmpdir(), 'stx-router-roots-')) |
| 21 | + userDir = path.join(baseDir, 'resources', 'views') |
| 22 | + defaultsDir = path.join(baseDir, 'storage', 'framework', 'defaults', 'resources', 'views') |
| 23 | + fs.mkdirSync(userDir, { recursive: true }) |
| 24 | + fs.mkdirSync(path.join(defaultsDir, 'orders'), { recursive: true }) |
| 25 | + }) |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + fs.rmSync(baseDir, { recursive: true, force: true }) |
| 29 | + }) |
| 30 | + |
| 31 | + function writeView(dir: string, relPath: string, body = '<html></html>'): string { |
| 32 | + const full = path.join(dir, relPath) |
| 33 | + fs.mkdirSync(path.dirname(full), { recursive: true }) |
| 34 | + fs.writeFileSync(full, body) |
| 35 | + return full |
| 36 | + } |
| 37 | + |
| 38 | + it('serves a default-only page from the second root', () => { |
| 39 | + writeView(userDir, 'index.stx') |
| 40 | + writeView(defaultsDir, 'cart.stx') |
| 41 | + writeView(defaultsDir, 'orders/[id].stx') |
| 42 | + |
| 43 | + const router = new Router(baseDir, { |
| 44 | + pagesDirs: [ |
| 45 | + path.relative(baseDir, userDir), |
| 46 | + path.relative(baseDir, defaultsDir), |
| 47 | + ], |
| 48 | + }) |
| 49 | + |
| 50 | + const patterns = router.routes.map(r => r.pattern).sort() |
| 51 | + expect(patterns).toContain('/cart') |
| 52 | + expect(patterns).toContain('/orders/:id') |
| 53 | + expect(patterns).toContain('/') |
| 54 | + }) |
| 55 | + |
| 56 | + it('first root shadows the second for the same pattern', () => { |
| 57 | + const userIndex = writeView(userDir, 'index.stx', '<!-- user -->') |
| 58 | + writeView(defaultsDir, 'index.stx', '<!-- defaults -->') |
| 59 | + |
| 60 | + const router = new Router(baseDir, { |
| 61 | + pagesDirs: [ |
| 62 | + path.relative(baseDir, userDir), |
| 63 | + path.relative(baseDir, defaultsDir), |
| 64 | + ], |
| 65 | + }) |
| 66 | + |
| 67 | + const indexRoute = router.routes.find(r => r.pattern === '/') |
| 68 | + expect(indexRoute).toBeDefined() |
| 69 | + expect(indexRoute!.filePath).toBe(userIndex) |
| 70 | + |
| 71 | + // The shadowed default must NOT also be in the route list — that |
| 72 | + // would duplicate `/` and the wrong one might match first. |
| 73 | + const indexes = router.routes.filter(r => r.pattern === '/') |
| 74 | + expect(indexes.length).toBe(1) |
| 75 | + }) |
| 76 | + |
| 77 | + it('falls back to second root for patterns not present in the first', () => { |
| 78 | + writeView(userDir, 'index.stx') |
| 79 | + const defaultCart = writeView(defaultsDir, 'cart.stx', '<!-- defaults -->') |
| 80 | + |
| 81 | + const router = new Router(baseDir, { |
| 82 | + pagesDirs: [ |
| 83 | + path.relative(baseDir, userDir), |
| 84 | + path.relative(baseDir, defaultsDir), |
| 85 | + ], |
| 86 | + }) |
| 87 | + |
| 88 | + const cartRoute = router.routes.find(r => r.pattern === '/cart') |
| 89 | + expect(cartRoute).toBeDefined() |
| 90 | + expect(cartRoute!.filePath).toBe(defaultCart) |
| 91 | + }) |
| 92 | + |
| 93 | + it('preserves backwards-compat single-root pagesDir', () => { |
| 94 | + writeView(userDir, 'about.stx') |
| 95 | + |
| 96 | + const router = new Router(baseDir, { |
| 97 | + pagesDir: path.relative(baseDir, userDir), |
| 98 | + }) |
| 99 | + |
| 100 | + expect(router.routes.map(r => r.pattern)).toEqual(['/about']) |
| 101 | + }) |
| 102 | + |
| 103 | + it('matches /cart against a defaults-only view', () => { |
| 104 | + writeView(userDir, 'index.stx') |
| 105 | + writeView(defaultsDir, 'cart.stx') |
| 106 | + |
| 107 | + const router = new Router(baseDir, { |
| 108 | + pagesDirs: [ |
| 109 | + path.relative(baseDir, userDir), |
| 110 | + path.relative(baseDir, defaultsDir), |
| 111 | + ], |
| 112 | + }) |
| 113 | + |
| 114 | + const match = router.match('/cart') |
| 115 | + expect(match).not.toBeNull() |
| 116 | + expect(match!.route.pattern).toBe('/cart') |
| 117 | + }) |
| 118 | + |
| 119 | + it('handles missing first root gracefully', () => { |
| 120 | + writeView(defaultsDir, 'cart.stx') |
| 121 | + |
| 122 | + const router = new Router(baseDir, { |
| 123 | + pagesDirs: [ |
| 124 | + 'does/not/exist', |
| 125 | + path.relative(baseDir, defaultsDir), |
| 126 | + ], |
| 127 | + }) |
| 128 | + |
| 129 | + expect(router.routes.map(r => r.pattern)).toContain('/cart') |
| 130 | + }) |
| 131 | +}) |
0 commit comments