import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { extractRoutePatterns, findRouteTreeFile, makeRoutePatternPlugin } from '../../src/vite/routePatterns'; describe('makeRoutePatternPlugin', () => { const configEnvironment = makeRoutePatternPlugin().configEnvironment as ( name: string, config: { consumer?: 'client' | 'server' }, env: { command: 'build' | 'serve'; mode: string }, ) => { resolve: { noExternal: string[] } } | null; it('bundles the SDK into server environments of a build', () => { const build = { command: 'build', mode: 'production' } as const; expect(configEnvironment('ssr', {}, build)).toEqual({ resolve: { noExternal: ['@sentry/tanstackstart-react'] } }); expect(configEnvironment('nitro', { consumer: 'server' }, build)).toEqual({ resolve: { noExternal: ['@sentry/tanstackstart-react'] }, }); }); it('does not change client environments or the dev server', () => { expect(configEnvironment('client', {}, { command: 'build', mode: 'production' })).toBeNull(); expect(configEnvironment('ssr', { consumer: 'client' }, { command: 'build', mode: 'production' })).toBeNull(); expect(configEnvironment('ssr', {}, { command: 'serve', mode: 'development' })).toBeNull(); }); }); describe('extractRoutePatterns', () => { it('extracts full path patterns from single-line fullPaths union', () => { const content = ` export interface FileRouteTypes { fullPaths: '/' | '/page-a' | '/page-b/$id' fileRoutesByTo: FileRoutesByTo } `; const patterns = extractRoutePatterns(content); expect(patterns).toContain('/'); expect(patterns).toContain('/page-a'); expect(patterns).toContain('/page-b/$id'); expect(patterns).toHaveLength(3); }); it('extracts full path patterns from multi-line fullPaths union', () => { const content = ` export interface FileRouteTypes { fullPaths: | '/' | '/page-a' | '/page-b/$id' | '/api/error' fileRoutesByTo: FileRoutesByTo } `; const patterns = extractRoutePatterns(content); expect(patterns).toContain('/'); expect(patterns).toContain('/page-a'); expect(patterns).toContain('/page-b/$id'); expect(patterns).toContain('/api/error'); expect(patterns).toHaveLength(4); }); it('returns empty array when fullPaths is not found', () => { const patterns = extractRoutePatterns(''); expect(patterns).toEqual([]); }); it('extracts nested route full paths correctly', () => { const content = ` export interface FileRouteTypes { fullPaths: | '/' | '/users' | '/users/$userId' | '/users/$userId/posts/$postId' fileRoutesByTo: FileRoutesByTo } `; const patterns = extractRoutePatterns(content); expect(patterns).toContain('/users'); expect(patterns).toContain('/users/$userId'); expect(patterns).toContain('/users/$userId/posts/$postId'); }); it('handles double-quoted paths (quoteStyle: "double")', () => { const content = ` export interface FileRouteTypes { fullPaths: "/" | "/page-a" | "/page-b/$id" fileRoutesByTo: FileRoutesByTo } `; const patterns = extractRoutePatterns(content); expect(patterns).toContain('/'); expect(patterns).toContain('/page-a'); expect(patterns).toContain('/page-b/$id'); }); it('deduplicates patterns', () => { const content = ` export interface FileRouteTypes { fullPaths: '/' | '/page-a' | '/page-a' fileRoutesByTo: FileRoutesByTo } `; const patterns = extractRoutePatterns(content); expect(patterns.filter(p => p === '/page-a')).toHaveLength(1); }); }); describe('findRouteTreeFile', () => { let root: string; beforeEach(() => { root = fs.mkdtempSync(path.join(os.tmpdir(), 'sentry-tanstackstart-route-tree-')); }); afterEach(() => { fs.rmSync(root, { recursive: true, force: true }); }); function writeRouteTree(dir: string): string { const filePath = path.join(root, dir, 'routeTree.gen.ts'); fs.mkdirSync(path.dirname(filePath), { recursive: true }); fs.writeFileSync(filePath, ''); return filePath; } it('finds the route tree in the default `src` directory', () => { const expected = writeRouteTree('src'); writeRouteTree('app'); expect(findRouteTreeFile(root)).toBe(expected); }); it('finds the route tree in a custom `srcDirectory`', () => { fs.mkdirSync(path.join(root, 'src')); const expected = writeRouteTree('app'); expect(findRouteTreeFile(root)).toBe(expected); }); it('finds the route tree in the project root', () => { const expected = writeRouteTree('.'); expect(findRouteTreeFile(root)).toBe(expected); }); it('ignores node_modules and dot directories', () => { writeRouteTree('node_modules'); writeRouteTree('.output'); expect(findRouteTreeFile(root)).toBeUndefined(); }); });