|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { generateIdFromFsPath } from '../../../src/utils/collection' |
| 3 | +import type { CollectionInfo } from '@nuxt/content' |
| 4 | +import { collections } from '../../mocks/collection' |
| 5 | + |
| 6 | +describe('generateIdFromFsPath', () => { |
| 7 | + it('should generate id for single file with no prefix', () => { |
| 8 | + const path = 'index.md' |
| 9 | + const result = generateIdFromFsPath(path, collections.landing!) |
| 10 | + expect(result).toBe('landing/index.md') |
| 11 | + }) |
| 12 | + |
| 13 | + it('should generate id for nested file with global pattern', () => { |
| 14 | + const path = '1.getting-started/2.introduction.md' |
| 15 | + const result = generateIdFromFsPath(path, collections.docs!) |
| 16 | + expect(result).toBe('docs/1.getting-started/2.introduction.md') |
| 17 | + }) |
| 18 | + |
| 19 | + it('should handle deeply nested paths', () => { |
| 20 | + const path = '2.essentials/1.nested/3.components.md' |
| 21 | + const result = generateIdFromFsPath(path, collections.docs!) |
| 22 | + expect(result).toBe('docs/2.essentials/1.nested/3.components.md') |
| 23 | + }) |
| 24 | + |
| 25 | + it('should handle collection with custom prefix', () => { |
| 26 | + const customCollection: CollectionInfo = { |
| 27 | + name: 'docs_en', |
| 28 | + pascalName: 'DocsEn', |
| 29 | + tableName: '_content_docs_en', |
| 30 | + source: [ |
| 31 | + { |
| 32 | + _resolved: true, |
| 33 | + prefix: '/en', |
| 34 | + cwd: '', |
| 35 | + include: 'en/**/*', |
| 36 | + exclude: ['en/index.md'], |
| 37 | + }, |
| 38 | + ], |
| 39 | + type: 'page', |
| 40 | + fields: {}, |
| 41 | + schema: { |
| 42 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 43 | + $ref: '#/definitions/docs_en', |
| 44 | + definitions: {}, |
| 45 | + }, |
| 46 | + tableDefinition: '', |
| 47 | + } |
| 48 | + |
| 49 | + const path = 'en/1.getting-started/2.introduction.md' |
| 50 | + const result = generateIdFromFsPath(path, customCollection) |
| 51 | + expect(result).toBe('docs_en/en/1.getting-started/2.introduction.md') |
| 52 | + }) |
| 53 | + |
| 54 | + it('should handle empty prefix correctly', () => { |
| 55 | + const customCollection: CollectionInfo = { |
| 56 | + name: 'pages', |
| 57 | + pascalName: 'Pages', |
| 58 | + tableName: '_content_pages', |
| 59 | + source: [ |
| 60 | + { |
| 61 | + _resolved: true, |
| 62 | + prefix: '', |
| 63 | + cwd: '', |
| 64 | + include: 'content/**/*.md', |
| 65 | + }, |
| 66 | + ], |
| 67 | + type: 'page', |
| 68 | + fields: {}, |
| 69 | + schema: { |
| 70 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 71 | + $ref: '#/definitions/pages', |
| 72 | + definitions: {}, |
| 73 | + }, |
| 74 | + tableDefinition: '', |
| 75 | + } |
| 76 | + |
| 77 | + const path = 'content/about.md' |
| 78 | + const result = generateIdFromFsPath(path, customCollection) |
| 79 | + expect(result).toBe('pages/about.md') |
| 80 | + }) |
| 81 | +}) |
0 commit comments