|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { readScalarField } from '../src/utils.js' |
| 3 | + |
| 4 | +describe('readScalarField', () => { |
| 5 | + it('reads a top-level scalar (old shape)', () => { |
| 6 | + expect(readScalarField({ type: 'core' }, 'type')).toBe('core') |
| 7 | + }) |
| 8 | + |
| 9 | + it('reads a scalar nested under metadata (new shape)', () => { |
| 10 | + expect(readScalarField({ metadata: { type: 'core' } }, 'type')).toBe('core') |
| 11 | + }) |
| 12 | + |
| 13 | + it('prefers metadata over a top-level value when both are present', () => { |
| 14 | + expect( |
| 15 | + readScalarField({ type: 'top', metadata: { type: 'nested' } }, 'type'), |
| 16 | + ).toBe('nested') |
| 17 | + }) |
| 18 | + |
| 19 | + it('falls back to top-level when metadata exists but lacks the key (partial migration)', () => { |
| 20 | + expect( |
| 21 | + readScalarField( |
| 22 | + { type: 'top', metadata: { framework: 'react' } }, |
| 23 | + 'type', |
| 24 | + ), |
| 25 | + ).toBe('top') |
| 26 | + }) |
| 27 | + |
| 28 | + it('falls back to top-level when the metadata value is not a string', () => { |
| 29 | + expect( |
| 30 | + readScalarField({ type: 'top', metadata: { type: 123 } }, 'type'), |
| 31 | + ).toBe('top') |
| 32 | + }) |
| 33 | + |
| 34 | + it('ignores a metadata array and uses the top-level value', () => { |
| 35 | + expect(readScalarField({ type: 'top', metadata: ['type'] }, 'type')).toBe( |
| 36 | + 'top', |
| 37 | + ) |
| 38 | + }) |
| 39 | + |
| 40 | + it('ignores a metadata string and uses the top-level value', () => { |
| 41 | + expect(readScalarField({ type: 'top', metadata: 'nope' }, 'type')).toBe( |
| 42 | + 'top', |
| 43 | + ) |
| 44 | + }) |
| 45 | + |
| 46 | + it('returns undefined when the key is absent in both shapes', () => { |
| 47 | + expect(readScalarField({ name: 'x' }, 'type')).toBeUndefined() |
| 48 | + }) |
| 49 | + |
| 50 | + it('returns undefined when a non-string top-level value has no metadata fallback', () => { |
| 51 | + expect(readScalarField({ type: 123 }, 'type')).toBeUndefined() |
| 52 | + }) |
| 53 | + |
| 54 | + it('returns undefined for null frontmatter', () => { |
| 55 | + expect(readScalarField(null, 'type')).toBeUndefined() |
| 56 | + }) |
| 57 | + |
| 58 | + it('returns an empty-string metadata value as-is', () => { |
| 59 | + expect(readScalarField({ metadata: { type: '' } }, 'type')).toBe('') |
| 60 | + }) |
| 61 | +}) |
0 commit comments