|
| 1 | +import { beforeAll, describe, expect, it } from 'bun:test'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import { readSpaceDirectory } from '../../src/plugins/markdown/read-space'; |
| 4 | +import { createValidator } from '../../src/schema/schema'; |
| 5 | +import { makePluginContext } from '../helpers/context'; |
| 6 | + |
| 7 | +const TEST_SCHEMA_PATH = join(import.meta.dir, '../fixtures/test-schema.json'); |
| 8 | +const VALID_DIR = join(import.meta.dir, '../fixtures/date-coercion/valid'); |
| 9 | + |
| 10 | +const validateNode = createValidator(TEST_SCHEMA_PATH); |
| 11 | + |
| 12 | +describe('date coercion and format validation', () => { |
| 13 | + describe('readSpaceDirectory with unquoted YAML date', () => { |
| 14 | + let nodes: Awaited<ReturnType<typeof readSpaceDirectory>>['nodes']; |
| 15 | + |
| 16 | + beforeAll(async () => { |
| 17 | + ({ nodes } = await readSpaceDirectory(makePluginContext(VALID_DIR, TEST_SCHEMA_PATH))); |
| 18 | + }); |
| 19 | + |
| 20 | + it('produces one node', () => { |
| 21 | + expect(nodes).toHaveLength(1); |
| 22 | + }); |
| 23 | + |
| 24 | + it('coerces Date object to ISO date string', () => { |
| 25 | + expect(nodes[0]?.schemaData.date).toBe('2026-03-31'); |
| 26 | + expect(nodes[0]?.schemaData.date).toBeTypeOf('string'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('coerced date passes schema validation', () => { |
| 30 | + expect(validateNode(nodes[0]?.schemaData)).toBe(true); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('format: "date" schema validation', () => { |
| 35 | + it('accepts a valid ISO date string', () => { |
| 36 | + expect(validateNode({ type: 'note', title: 'Test', date: '2026-03-31' })).toBe(true); |
| 37 | + }); |
| 38 | + |
| 39 | + it('rejects a non-date string', () => { |
| 40 | + expect(validateNode({ type: 'note', title: 'Test', date: 'not-a-date' })).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it('rejects a datetime string for a date field', () => { |
| 44 | + expect(validateNode({ type: 'note', title: 'Test', date: '2026-03-31T00:00:00Z' })).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it('rejects a Date object (unconverted)', () => { |
| 48 | + expect(validateNode({ type: 'note', title: 'Test', date: new Date('2026-03-31') })).toBe(false); |
| 49 | + }); |
| 50 | + }); |
| 51 | +}); |
0 commit comments